2014-07-08 55 views
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(); 
}); 

回答

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" 
相關問題