0

I am writing some cucumber tests using webdriverJS. I am trying to use an after hook to close the browser window after each scenario. The problem is, the window will close but not re-open. The error I get is that it cannot "find" a window. Any help or insight will be greatly appreciated.

Here is my .feature file

Background
Given I go to the website "..."

Scenario: One
When I click() on "..."
When I getText() of the title "..."
Then the title should be "..."

Scenario: Two
When I click() on "..."
When I getText() of the title "..."
Then the title should be "..."

Here is my hooks.js file

var ID = null;

module.exports = function(){
this.After( function (err, next){
    client

    .getCurrentTabId( function(err, tabID){ 
        ID = tabID;
        expect(err).to.be.null;
        next() })

    .close( ID, function(err){
        console.log('-------------CLOSE-------------');
        next(); });
    });
};

Here are the first few lines of the .js file

   client = webdriverjs.remote({ desiredCapabilities: {browserName: 'safari'},   logLevel:   
            'verbose'});

module.exports = function()
{
  client.init();

this.Given(/^I go to the website "([^"]*)"$/, function (url, next){
    client
    .url(url)
    console.log("BACKGROUND STATEMENT");
    next();
});
Artjom B.
  • 61,146
  • 24
  • 125
  • 222

1 Answers1

0

It seems that we were able to fix it. What is interesting is that the lines in the before hook cannot be done in the function for the background statement.

Here is the .js file (The hooks and step definitions are in the same file, but they should be able to be separate)

webdriverjs = require('webdriverjs');

var sharedSteps = module.exports = function(){

this.Before(function(done) {
    console.log('TestCase Enter >>>');
    client      = webdriverjs.remote({ desiredCapabilities: {browserName: 'firefox'}, logLevel:   
                   'silent'}),
    client.init(done); });

this.After(function(done) {
    console.log('TestCase Exit >>>');
    client.close(done);})

this.Given(/^I go to the website "([^"]*)"$/, function (url, next) {
    console.log("BACKGROUND STATEMENT");        
    client
        .url(url)
        .call(next);});

this.When(/^I use getTitle\(\) to get title of this website$/, function (next) {
    client 
        .getTitle(function(err, title) {
            tmpResult = title;
            next(); });
 });

this.Then(/^the command should return "([^"]*)"$/, function (result, next) {
    next(); });

};

 module.exports = sharedSteps;

Here is the .feature file for your reference:

Feature: Hook Example

Scenario: Get title of website One
    When I go to the website "http://www.google.com"
    When I use getTitle() to get title of this website
    Then the command should return "Google"

Scenario: Get title of website Two
    When I go to the website "http://www.yahoo.com"
    When I use getTitle() to get title of this website
    Then the command should return "Yahoo"