I'm helping a friend of mine with one of his first tests in jUnit. We had a problem recently and I have no idea what can be the cause of that.
We're executing the test which pops up the Facebook login window on our page, then fills up the inputs for login, it's going to close the window and refresh the browser. After logging in we want to continue testing the functionalities for logged in users.
The thing is... After executing click()
function on the element that submits and closes the window, test does not follow and acts like it restarts the work, but after a while stops.
To specify the issue better... here's the code sample:
@Before
public void setUp() throws Exception
{
driver = new FirefoxDriver();
baseUrl = "https://www.example.com/";
}
@Test
public void OrderWithLoginFbCash() throws Exception
{
driver.get(baseUrl);
// remembers the parent Windowhandle
String parentHandle = driver.getWindowHandle();
// Opens Facebook login window
WebElement FbLogin = (new WebDriverWait(driver , 20)).until(
ExpectedConditions.presenceOfElementLocated(By.id("fblogin"))
);
FbLogin.click();
for (String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
// ... filling up the inputs ...
WebElement FbLoginFacebook = (new WebDriverWait(driver, 20)).until(
ExpectedConditions.presenceOfElementLocated(By.id("u_0_0"))
);
// everything's still fine here
FbLoginFacebook.click(); // logs in, closes the window and reloads the page
// !!! Test won't continue here for some reason. !!!
System.out.println("Did the logging in stuff"); // Won't be printed
}
I'm not a tester myself, so it's hard for me to say why it's stopping there. I thought it might be because the window it's set for, gets closed on click()
of the element, but then I can't think of any walk-around for that problem. Any help would be appreciated. Thank you.
So after trying a bit more we managed to successfully run the tests. What caused the issue was apparently the FbLoginFacebook.click()
, which actually closed the currently active window, so the test couldn't manage to continue.
In case anyone is interested how we solved the issue... FbLoginFacebook
element was triggering the submit event on form which on success closed the opened window (with Facebook's login). I have no idea why it works, but after we submitted the form manually (by selecting form element and executing submit()
method on it) it worked perfectly. We could then return back to parent window by driver.switchTo().window(previouslySavedParentWindowHandle)
and everything works like a charm.
// before changing of window we saved the
// String parentHandle = driver.getWindowHandle();
WebElement FbLoginFacebook = (new WebDriverWait(driver, 20)).until(
ExpectedConditions.presenceOfElementLocated(By.id("login_form"))
);
FbLoginFacebook.submit();
driver.switchTo().window(parentHandle);
Anyways, I would appreciate if someone can explain why this was happening and how can we avoid it in future (or how can we handle clicks like the one in the question above). I can then add this to this answer. Thank you.