Need help regarding terminating session for each scenario using nightwatch cucumber

326 views
Skip to first unread message

Gaurav Rajput

unread,
Mar 8, 2017, 1:58:53 AM3/8/17
to NightwatchJs
Hi All,

I have developed a framework using nightwatch along with cucumber. I have used gherkin language to write test cases. 

As of now, i have to write this.end() for each scenario's last step. But i want to get functionality whenever a test scenario gets completed this.end() should be called. I have tried after hook but did not get success.

Can someone help me on this.

Regards,
Gaurav

Eric Mumford

unread,
Mar 8, 2017, 10:22:36 PM3/8/17
to NightwatchJs
I am having trouble imagining why you need to close the browser object out after each test scenario, but if that is what you need, you might consider writing a custom command that does some helpful things you find yourself repeating in your tests and have the custom command finish with the end() call.

Gaurav Rajput

unread,
Mar 8, 2017, 10:33:07 PM3/8/17
to NightwatchJs
Hey Eric,

Thanks for the reply.

I need to have it as we want to run scenarios in parallel mode so we do not want to depend on any previous scenarios. Lets say one scenario is i log in to facebook and then check profile picture is available or not. second test would be log in to facebook and check whether i can edit profile so both are independent scenario  and want to close session which is right now i am doing with api command this.end().

Now i have problem lets say last step definition of a test scenario can be middle step definition of some other scenario. To understand better give you below example..

Scenario: Test edit button is clickable
When I log in to facebook
And i click on profile button 
Then Edit should be visible

Scenario: Test edit button is clickable
When I log in to facebook
And i click on profile button 
Then Edit should be visible
When I click on edit button 
Then i should be able to see fields to edit

So in above scenarios, if i put this.end() or browser.end() or api.end() in the step "Then Edit should be visible" then next scenario terminated abruptly before execution of last 2 steps. So i want to have some functionality which should be run after each scenario. I think we have similar sort of functionality in testNg where we have annotation @BeforeTest and @AfterTest.

I am not getting how to use same concept in nightwatchJs just wondering if someone can provide me code snippet.

Regard,
Gaurav

Eric Mumford

unread,
Mar 8, 2017, 10:47:17 PM3/8/17
to nightw...@googlegroups.com
Gaurav,

You wrote, "So in above scenarios, if i put this.end() or browser.end() or api.end() in the step "Then Edit should be visible" then next scenario terminated abruptly before execution of last 2 steps."

I am not following what you are saying. Nightwatch is very clean about how it handles client.end() and client.navigate(). 

It may help if you posted actual code that is not behaving the way you think it should. 

Gaurav Rajput

unread,
Mar 8, 2017, 10:58:48 PM3/8/17
to NightwatchJs, eric.m...@gmail.com
Hi Eric,

Below is the code for one step definition

  this.Then(/^I should see the list of employees left in year (\d+)$/, function (year) {
    searchPage = this.page.search();
    searchPage.verifySearchedEmployee();
    searchPage.verifySearchResults();
    this.end();
  });

Now if i would like to use above step definition in any other scenario like below

Scenario: Edit left employee data in year 2016
When log in to application
And select employee from the year 2016 who are left
Then I should see the list of employees left in year 2016
When I click on first employee who left in 2016
Then i should see details of employee

So if i run above scenario, it terminates at step definition Then I should see the list of employees left in year 2016. It will not execute others steps. 

So how to handle that situation 

Eric Mumford

unread,
Mar 9, 2017, 5:48:13 AM3/9/17
to nightw...@googlegroups.com
If you were to write your test using the Nightwatch test runner, Nightwatch would manage the synchronous control of your commands. Meaning for example, if you were to encapsulate your test logic using custom commands, the equivalent code to run your logic using Nightwatch might look something like:

browser
.navigate()
.searchForEmployee(empName)
.waitForElementVisible('@empSearchResults')
.verifyContainsText('@empSearchResults',empName)
.api.end();

Because Nightwatch tests are chained functions themselves, the built-in test runner ensures the instructions are run sequentially.

In your case, it seems you have written your own cucumber/gherkin thing so you have to manage your own execution. You are using a promise (.then) for a portion of your test but not the callback which contains a definition of searchPage, two function calls to searchPage, and a call to the end() function. My guess is that this.end() is being called before the other two functions verifySearchedEmployee() and verify SearchResults() return with results because you are not using promises or callbacks for those.

--
You received this message because you are subscribed to a topic in the Google Groups "NightwatchJs" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/nightwatchjs/JSbQjn2tCyU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to nightwatchjs+unsubscribe@googlegroups.com.
To post to this group, send email to nightw...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nightwatchjs/d0fe447b-8878-4277-b6cc-6f2a08ce6fd9%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages