0

我正在使用 webdriverJS 编写一些黄瓜测试。我正在尝试在每个场景之后使用后挂钩来关闭浏览器窗口。问题是,窗口将关闭但不会重新打开。我得到的错误是它无法“找到”一个窗口。任何帮助或见解将不胜感激。

这是我的 .feature 文件

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

这是我的 hooks.js 文件

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(); });
    });
};

这是 .js 文件的前几行

   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();
});
4

1 回答 1

0

看来我们能够修复它。有趣的是 before 钩子中的行不能在后台语句的函数中完成。

这是.js文件(钩子和步骤定义在同一个文件中,但它们应该能够分开)

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;

这是供您参考的 .feature 文件:

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"
于 2014-07-09T17:12:34.083 回答