Selenium getDriver () empty for @AfterScenario (after AssertionError)
I have PageObject startPage
where I have login and logout method. The login method works fine and is executed in @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
as follows:
typeInto(find(By.id(getUserFieldId())), user);
typeInto(find(By.id(getPasswordFieldId())), password);
findBy(getLoginButtonXPath()).then().click();
so nothing special, it all works fine. Then I switch multiple PageObjects
at different stages of the test with no problem. When the code reaches @AfterScenario
that looks like this:
@AfterScenario
public void logout() {
homeVM.logoff();
}
and class homeVM
using the method logoff()
looks like this:
WebElement btnLogout = getDriver().findElement(By.xpath("//a [contains(@class,'lnkLogout')]"));
btnLogout.click();
But that doesn't work (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());
(""). , , getDriver()
( null, NullPointerException
). ? getDriver()
PageObject
, , , getDriver()
@AfterScenario
. , ? chromeDriver
.
EDIT:
Ok, I found out something unexpected: I have a method assertThat(<something>)
in the last step, and this step actually causes the assignment to fail (since the behavior hasn't been implemented yet) ... and if I comment assertThat()
out this out, @AfterScenario
and it exits system is running correctly. So webDriver
gets "emptied" if the test fails? Is it on purpose?
EDIT2:
If I catch the AssertionErrorException
Exception, the test will succeed fine again, but of course the test will be marked as "Test Passed". So it really should do something, if an exception is thrown, the current one webDriver
gets emptied. But that doesn't seem right ...
source to share
As I learned from John Smart, as soon as Serenity detects an error during testing, the test switches to dry mode, so web driver calls are no longer needed. I had to find another way to log out. Since my chrono engraver runs all scripts by default in the same session and in the browser, I had to manually log out after each script. But by setting
System.setProperty("restart.browser.each.scenario", "true");
managed to restart the browser and clear the session after each script. This worked for me, so I no longer need @AfterScenario
c logoff();
.
source to share
Overcoming the problem within the watuccher watir system
filename = DateTime.now.strftime ("% Y-% m-% d -% Hh_% Mm_% Ss")
@ browser.driver.save_screenshot ("# {filename} .png")
Note: filename is the filename of the screenshot
you can pass the location of the screenshot file just like this
@ browser.driver.save_screenshot ("/ Screenshots / # {filename} .png")
source to share