Login

Login
  • Register
  • VectorDesign blog

    Testing Automation and general Tech Geek stuff

    handle browser user authentication pop up

    Posted in java on 21 July 2015 by vectordesign

    One of the usual problems in testing is getting access to the test environment.

    Usually a test server is setup where a users authentication is required in order to access the application under test.

    In most  browsers (firefox,chrome, safari ect) this can be easily achieved by parsing the credentials in the url like so

    http://USERNAME:PASSWORD@www.testdomain.com
    

    Some browsers though don’t accept this for security reasons. one of these browsers is Internet Explorer. To handle this case we use JAVA ROBOTS

     

    public class SmartRobot extends Robot {
    
        public SmartRobot() throws AWTException
        {
            super();
        }
     public void pasteClipboard()
        {
            keyPress(KeyEvent.VK_CONTROL);
            keyPress(KeyEvent.VK_V);
            delay(50);
            keyRelease(KeyEvent.VK_V);
            keyRelease(KeyEvent.VK_CONTROL);
        }
    
        public void type(String text)
        {
            writeToClipboard(text);
            pasteClipboard();
        }
    
        private void writeToClipboard(String s)
        {
            Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
            Transferable transferable = new StringSelection(s);
            clipboard.setContents(transferable, null);
        }
    }
    
     try {
                    SmartRobot robot = new SmartRobot();
                    robot.delay(150);
                    robot.type(USERNAME);
                    robot.keyPress(KeyEvent.VK_TAB);
                    robot.type(SetUpVariables.orfeas_pass);
                    robot.keyPress(KeyEvent.VK_CONTROL);
                    robot.keyPress(KeyEvent.VK_V);
                    robot.delay(50);
                    robot.keyRelease(KeyEvent.VK_V);
                    robot.keyRelease(KeyEvent.VK_CONTROL);
                    robot.keyPress(KeyEvent.VK_ENTER);
                    robot.delay(250);
                } catch (Exception AWTException) {
                    System.out.println("Exception " + AWTException.getMessage());
                }
    
    

    Leave a Reply