Neeraj Singh
Neeraj Singh

Reputation: 1

How to handle authentication pop-up window in Selenium?

Please tell me the code for set user name and password for Authentication popup window

System.setProperty("webdriver.chrome.driver","Driver//chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("http://admin3-dev.ca.primus");
WebDriverWait wait = new WebDriverWait(driver, 10);      


Alert alert = wait.until(ExpectedConditions.alertIsPresent());     
alert.authenticateUsing(new UserAndPassword("nsingh","Primus01"));

The user name and password is not passed in authentication popup on above code.Please tell me how to click on login button also.

Upvotes: 0

Views: 5796

Answers (5)

Shubham Jain
Shubham Jain

Reputation: 17553

You can directly pass the username and password in URL itself as below:-

driver.get("http://UserName:[email protected]");

Refer Below link:-

http://www.seleniumeasy.com/selenium-tutorials/how-to-handle-authentication-popup-in-selenium-webdriver

Hope it will help you :)

Upvotes: 2

Neeraj Singh
Neeraj Singh

Reputation: 1

It works in chrome, still looking for firefox browser.

System.setProperty("webdriver.chrome.driver","Driver//chromedriver.exe");
        driver=new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        driver.get("http://admin3-dev.magma.ca");


       Robot rb  =new Robot();
       rb.keyPress(KeyEvent.VK_P);
       rb.keyRelease(KeyEvent.VK_P);
       rb.keyPress(KeyEvent.VK_C);
       rb.keyRelease(KeyEvent.VK_C);
       rb.keyPress(KeyEvent.VK_H);
       rb.keyRelease(KeyEvent.VK_H);
       rb.keyPress(KeyEvent.VK_A);
       rb.keyRelease(KeyEvent.VK_A);
       rb.keyPress(KeyEvent.VK_L);
       rb.keyRelease(KeyEvent.VK_L);
       rb.keyPress(KeyEvent.VK_L);
       rb.keyRelease(KeyEvent.VK_L);  
       rb.keyPress(KeyEvent.VK_A);
       rb.keyRelease(KeyEvent.VK_A);

       rb.keyPress(KeyEvent.VK_TAB);
       rb.keyRelease(KeyEvent.VK_TAB);

       rb.keyPress(KeyEvent.VK_CAPS_LOCK);
       rb.keyRelease(KeyEvent.VK_CAPS_LOCK);
       rb.keyPress(KeyEvent.VK_A);
       rb.keyRelease(KeyEvent.VK_A);

       rb.keyPress(KeyEvent.VK_CAPS_LOCK);
       rb.keyRelease(KeyEvent.VK_CAPS_LOCK);
       rb.keyPress(KeyEvent.VK_M);
       rb.keyRelease(KeyEvent.VK_M);
       rb.keyPress(KeyEvent.VK_M);
       rb.keyRelease(KeyEvent.VK_M);
       rb.keyPress(KeyEvent.VK_U);
       rb.keyRelease(KeyEvent.VK_U);
       rb.keyPress(KeyEvent.VK_L);
       rb.keyRelease(KeyEvent.VK_L);
       rb.keyPress(KeyEvent.VK_U);
       rb.keyRelease(KeyEvent.VK_U);
       rb.keyPress(KeyEvent.VK_1);
       rb.keyRelease(KeyEvent.VK_1);
       rb.keyPress(KeyEvent.VK_2);
       rb.keyRelease(KeyEvent.VK_2);

       rb.keyPress(KeyEvent.VK_TAB);
       rb.keyRelease(KeyEvent.VK_TAB);
       rb.keyPress(KeyEvent.VK_ENTER);`enter code here`
       rb.keyRelease(KeyEvent.VK_ENTER);

Upvotes: 0

Anon
Anon

Reputation: 339

There are different solutions for different browsers but few programmatic solutions work for all browsers.

  • Some WebDrivers (Chrome, Firefox) can use user:pass@site syntax in the URL. Some will accept this syntax after registry hacks but will suffer stability problems (IE).
  • Some WebDrivers let you set a preferences in their profile (Firefox)
  • Some WebDrivers let you set modify their behaviour with extensions (Chrome)
  • Some WebDrivers let you interact with the authentication dialog via alert.authenticateUsing (only IE at the time of writing)
  • Some browser's WebDrivers cannot do any of the above at the time of writing (Edge)

Short of using a third-party GUI automation framework, something that should work in all browsers is to set up a proxy that can add a Basic Authentication header or arbitrary headers (such as BrowserMob Proxy, Fiddler, mitmproxy) and set the browser to proxy through it.

Also see How to perform Basic Authentication for FirefoxDriver, ChromeDriver and IEdriver in Selenium WebDriver? and answers linked from it.

Upvotes: 1

Nethra P
Nethra P

Reputation: 1

I was facing Authentication Proxy pop-up issue in my project. So I tried below solution and it is working fine. When we run Script from Selenium Web driver on Security Environment following Setup needs to be done to handle Authentication Proxy.

First you need to know below details,

  • network.proxy.autoconfig_url (Example: http://example.com/abc.pac)
  • network.proxy.http (Example: abc-proxy.com)
  • network.proxy.http_port (Example: 8080)

private static WebDriver initFirefoxDriver(String appURL)  
{

    System.out.println("Launching Firefox browser..");

    FirefoxProfile firefoxProfile = new FirefoxProfile();
    firefoxProfile.setPreference("network.proxy.type", 1);
    firefoxProfile.setPreference("network.proxy.autoconfig_url", "http://example.com/abc.pac");
    firefoxProfile.setPreference("network.proxy.http", " abc-proxy.com");
    firefoxProfile.setPreference("network.proxy.http_port", 8080);


    WebDriver driver = new FirefoxDriver(firefoxProfile);
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    driver.manage().window().maximize();
    driver.navigate().to(appURL);
    //driver.get(appURL); 
    return driver;
}

Upvotes: 0

Walid El Ajmi
Walid El Ajmi

Reputation: 33

i think that you must use Robot you can add this class

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);
}

}

and in your code change this

Alert alert = wait.until(ExpectedConditions.alertIsPresent());     
alert.authenticateUsing(new UserAndPassword("nsingh","Primus01"));

by this

driver.switchTo().alert();
SmartRobot robot = new SmartRobot();
robot.type(USERNAME);
robot.keyPress(KeyEvent.VK_TAB);
robot.type(PASSWORD);
robot.keyPress(KeyEvent.VK_ENTER);

Upvotes: 1

Related Questions