Saturday, 23 January 2016

How to Enter User id and Password in Authentication window using Robot Class in selenium



We can use Robot class or AutoIT tool for enter user id and password in Authetication window using selenium


Using Robot Class
----------------


fd.get("https://www.google.co.in");
driver.findElement(By.name("btnK")).click()
       
        Thread.sleep(5000);
        Robot rb = new Robot();

        //Enter user name by ctrl-v
        StringSelection username = new StringSelection("username");
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(username, null);           
        rb.keyPress(KeyEvent.VK_CONTROL);
        rb.keyPress(KeyEvent.VK_V);
        rb.keyRelease(KeyEvent.VK_V);
        rb.keyRelease(KeyEvent.VK_CONTROL);

        //tab to password entry field
        rb.keyPress(KeyEvent.VK_TAB);
        rb.keyRelease(KeyEvent.VK_TAB);
        Thread.sleep(2000);

        //Enter password by ctrl-v
        StringSelection pwd = new StringSelection("password");
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(pwd, null);
        rb.keyPress(KeyEvent.VK_CONTROL);
        rb.keyPress(KeyEvent.VK_V);
        rb.keyRelease(KeyEvent.VK_V);
        rb.keyRelease(KeyEvent.VK_CONTROL);

        //press enter
        rb.keyPress(KeyEvent.VK_ENTER);
        rb.keyRelease(KeyEvent.VK_ENTER);

        //wait
        Thread.sleep(5000);
       

No comments:

Post a Comment