3

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...

spcial
  • 1,529
  • 18
  • 40

3 Answers3

2

Once Serenity (or Thucydides in this case) spots a test failure (e.g. from an assertion error), the test is switched to "dry-run" mode, as it considers that subsequent steps are compromised and may result in unnecessary (and slow) web driver calls.

John Smart
  • 969
  • 1
  • 5
  • 5
  • Thank you for your answer! But what should I do if I need web driver calls for `@AfterScenario` methods (e.g. performing a logout because I always want to start my scenarios with a login)? – spcial Jul 18 '15 at 10:29
1

As I found out from John Smart that once Serenity spots a test failure the test is switched to "dry-run" mode, so no web driver calls are possible anymore I had to find another way to perform a logout. As my chromedriver runs by default all scenarios in the same session and browser I had to perform a manual logout after every scenario. But by setting

System.setProperty("restart.browser.each.scenario", "true");

it was possible to restart the browser and clean the session after every scenario. This worked for me so I don't need the @AfterScenario with logoff(); anymore.

spcial
  • 1,529
  • 18
  • 40
0

overcoming the issue in cucumber watir framework

filename = DateTime.now.strftime("%Y-%m-%d--%Hh_%Mm_%Ss")

@browser.driver.save_screenshot ("#{filename}.png")

Note: filename is the name of the screenshot file

you can pass the location of the screenshot file as well like this

@browser.driver.save_screenshot ("/Screenshots/#{filename}.png")

Sam
  • 1
  • 1