Friday, October 19, 2018

Selenium Firefox - Test logging into two windows

I have a test case where I open first window and login. Next, I open a second window (not a tab) and I expect that I don't have to login. However, what I'm trying to understand is if the caching of the user credentials is in the browser session or not. I assume it is since I don't have to log back in (from a different window) when I'm already logged in another window that's still open.

I looked at this article, took some pointers and got the new tab to work.
https://www.testingexcellence.com/open-new-tab-browser-using-selenium-webdriver-java/

However, still working on the new window. Here's my code.

@Test
public void testNoLoginOnNewWindow() {

    WebDriver driver2;

    // Browser opened already (from @Before), login to application
    JavelinApplication.login(driver1, url, username, password);

    // Assert user's homepage displays

    // Open new browser, navigate to application
    driver2 = new FirefoxDriver(new FirefoxProfile());
    driver2.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    driver2.navigate().to(url);

    // Assert user's homepage displays without login credentials asked

    driver2.quit();
}

This isn't quite working as expected yet. So, I read this article which didn't really help much:
https://stackoverflow.com/questions/17325629/how-to-open-a-new-window-on-a-browser-using-selenium-webdriver-for-python

But, it got me thinking. So, I re-read this article to leverage the code and open a new window from the existing open browser (similar to File -> New Window).
https://www.testingexcellence.com/open-new-tab-browser-using-selenium-webdriver-java/

Here's my code which actually worked as expected according to my test case.

@Test
public void testNoLoginOnNewWindow() {

    // Browser opened already (from @Before), login to application
    JavelinApplication.login(driver, url, username, password);

    // Assert user's homepage displays

    // Open new browser, navigate to application
    driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "n");
    ArrayList<String> windows = new ArrayList<String> (driver.getWindowHandles());
    driver.switchTo().window(windows.get(1));
    driver.navigate().to(url);

    // Assert user's homepage displays without login credentials asked
}

Yay, hooray!

Here's my reasoning between the different code used.

On a Linux env, I already have one Firefox window (#1) opened with My Application homepage displayed. When I open a new Firefox window (#2) from the OS, I must enter the username/password credentials. I enter the username/password and see My Application homepage displayed. When I open a new Firefox window (#3) from one of the existing Firefox windows (#1 or #2), I am not asked for login credentials and see My Application homepage displayed.

On a Windows env, I already have one Firefox window (#1) opened with my Application homepage displayed. When I open a new Firefox window (#2) from the OS (using Windows Start Menu -> Run -> Type firefox), I see My Application homepage displayed in window and am not asked for user credentials.

My assumption is that the different behavior is due to the Operating System. I am not sure and would need to do further research and testing, but this explanation is sufficient for now since my goal with the test case was reached. Cheers!

No comments:

Post a Comment