I have a PageObject startPage
where I have a login and a logout method. The login method works fine and is executed in the @BeforeScenario
:
@BeforeScenario
public void login() {
// {..} Declaration of baseUrl,user,password...
homeVM.setDefaultBaseUrl(baseUrl);
homeVM.open();
homeVM.login(user, password);
}
and login(user,password)
in class homeVM
is like:
typeInto(find(By.id(getUserFieldId())), user);
typeInto(find(By.id(getPasswordFieldId())), password);
findBy(getLoginButtonXPath()).then().click();
so nothing special, this works all fine.
Then I switch through several PageObjects
in different test steps without a problem. When code reaches the @AfterScenario
which looks like:
@AfterScenario
public void logout() {
homeVM.logoff();
}
and class homeVM
with method logoff()
looks like:
WebElement btnLogout = getDriver().findElement(By.xpath("//a [contains(@class,'lnkLogout')]"));
btnLogout.click();
But this isn't working (nothing happens, no exception, no click.. just nothing). Then I tried to log some information about getDriver()
with:
System.out.println("WindowHandles:"+getDriver().getWindowHandles().size());
System.out.println("Title: "+getDriver().getTitle());
and both values are just empty (""). So it seems that getDriver()
is just empty (not even null, so I don't get a NullPointerException
). Why is it so? I tried to check getDriver()
for the last PageObject
I used in my test but there I get all the information I need, just getDriver()
in the @AfterScenario
is empty. Any idea or solution what to do next or why this is happening? I'm using chromeDriver
.
EDIT:
Okay, I recognized something unexpected:
I have an assertThat(<something>)
method in my last step and this step is actually producing an assignment failure (because the behaviour is not implemented yet)... and if I comment this assertThat()
out, the @AfterScenario
and its logout is executed correctly. So the webDriver
gets "emptied" if the test fails? Is this on purpose?
EDIT2:
If I catch the AssertionErrorException
Exception the test runs fine again but of course the test will be marked as "Test Passed". So it really has something to do that if the exception is thrown the current webDriver
gets emptied. But this seems to be wrong...