How to deal with windows authentication popups

auth-required-basic

 

If you dont like Bugs Bunny, maybe you should not continue reading.

 

What is the first thing that I do every morning at work?

Check the results of the automation scripts executed in Jenkins over night.

I want to know how many scripts passed or failed because of the work done in the previous day.

Most days, some scripts pass and some fail.

But recently, most scripts failed.

This was very unusual so I looked into it right away.

All failures followed the same pattern: the scripts failed on opening the site in the browser.

 

Bugs-Bunny-Looking-Shocked

 

 

A sample script looks like this:

@Test
public void canBrowseThroughPages() {

  HomePage homePage = new HomePage(driver);
  homePage.open();

  ResultsPage resultsPage = homePage.search(keyword);

  assertTrue(resultsPage.resultCount() > 0);
 
  resultsPage = resultsPage.selectNextPage();

  assertTrue(resultsPage.isCurrentPage(2));
  assertTrue(resultsPage.resultCount() > 0);

}

 

The error happened in all scripts on the second line:
homePage.open();

The error info was

java.lang.object.RuntimeException:
home page didnt load correctly:
expected url = http://www.testsite.com
actual url =

 

I looked at the code of the open() method:

public class HomePage {

  private WebDriver driver;
  private WebDriverWait wait;

  private String url = "http://www.testsite.com";

  public HomePage(WebDriver driver) {
    this.driver = driver;
    wait = new WebDriverWait(this.driver, 30);
  }

  public void open()

    try {
      this.driver.get(url);
    }
    catch (Exception ex) {
     throw new RuntimeException("home page cannot be opened!");
    }

    try {
     wait.until(urlContains(url));
    }
    catch (Exception ex) {
     throw new RuntimeException("home page didnt load correctly - " +
             "expected url = " + url +
             " - " +
             "actual url = " + this.driver.getCurrentUrl());
    }
  }

 

I could not find anything wrong in the method.

It tries to open the url.
If it cannot, it throws an exception.

If the url is opened, it tries to verify the page url.
If it cannot, it throws an exception.

 
So, I looked again at the exception info:

java.lang.object.RuntimeException:
home page didnt load correctly;
expected url = http://www.testsite.com
actual url =

 

The actual url is empty which means that the site was not loaded.

But, why is the second exception thrown instead of the first one?

If driver.get(url) does not work, why doesn’t it generate an exception?

 

bugs bunny puzzled

Strange, isn’t it?

 

Next, I execute the script locally in Eclipse and it works.

So, the script works locally but has an error when executed in Jenkins.

 

What is the issue?

I look at the Jenkins execution details and get the name of the Jenkins slave where the script ran.

I start a remote connection to that server, open Chrome and load the site url in it.

And surprise!

A windows authentication popup is displayed!!!

That’s why the second exception was thrown instead of the first.

When executing driver.get(url), before the url is launched in the browser, the windows authentication popup is displayed.

driver.get() passes and the code continues with the url validation.

Since the authentication popup is still displayed, the page is not loaded and the url validation fails.

So the authentication popup is the culprit.

 

How can I disable the windows authentication popup?

Resetting the chrome browser settings does not help.

I can try creating the chrome driver with options and adding the site url to the chrome authentication whitelist.

This works manually.

When added to the code, the authentication popup is still there:

ChromeOptions options = new ChromeOptions();
options.addArguments("auth-server-whitelist='www.testsite.com'");
ChromeDriver driver = new ChromeDriver(options);

 
What else can I do?

Nothing goes right ……………….

So …………..

 

nothing goes right

 

Lets go left 🙂

 

I can include the account’s username and password in the url:

public class HomePage {

  private WebDriver driver;
  private WebDriverWait wait;

  private String url = "http://www.testsite.com";

  public HomePage(WebDriver driver) {
    this.driver = driver;
    wait = new WebDriverWait(this.driver, 30);
  }

  public void open() {

    try {
      this.driver.get(urlWithAccount(url));
    }
    catch (Exception ex) {
      throw new RuntimeException("home page cannot be opened");
    }

    try {
      wait.until(ExpectedConditions.urlContains(url);
    }
    catch (Exception ex) {
      throw new RuntimeException("home page didnt load correctly - " +
                                 "expected url = " + url +
                                 " - " +
                                 "actual url = " +                                                             this.driver.getCurrentUrl());
    }

  }

  private urlWithAccount(String url) {
     String username = System.getProperty("username");
     String password = System.getProperty("password");

     String newUrl = url.replace("http://",
                                 "http://" +
                                 username +
                                 ":" +
                                 password +
                                 "@");

     return newUrl;
  }

 

With the new method, the url would changes from

http://www.testsite.com

to

http://username:password@www.testsite.com

 

Rerunning the Jenkins scripts again showed that the problem is resolved.

 

thats all folks

Advertisements

One Comment Add yours

  1. This post is indeed very informative about managing authentication popups. However, I want to automate the PKI login dialog that comes in Firefox and asks for a security PIN. Is it possible using the method underlying in this post.

    Like

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s