Grunt,Cucumber,Protractor测试意外失败

时间:2016-01-22 10:53:03

标签: javascript cucumber protractor karma-runner

我有一个像这样的功能文件:

Feature: Test feature

Scenario: Step 1
    Given I go to "test-process/#/step-1"
    When I have checked the checkbox
    Then the button is enabled

我的steps.js文件如下:

'use strict';

var chai = require('chai'),
    chaiAsPromised = require('chai-as-promised');

chai.use(chaiAsPromised);

module.exports = function () {

    this.Given(/^I go to "([^"]*)"$/, function (path, callback) {
        browser.get(path);

        callback();
    });

    this.When(/^When I have checked the checkbox$/, function (callback) {
        var checkbox = element(by.id('check'));
        checkbox.click();

        callback();
    });

    this.Then(/^the button is enabled$/, function (callback) {
        var button = element(by.id('next'));

        chai.expect(button.isEnabled()).to.eventually.equal(true).and.notify(callback);
    });

};

我也有这样的配置:

exports.config = {
    framework: 'custom',
    frameworkPath: '../../../../node_modules/protractor-cucumber-framework',
    seleniumAddress: 'http://localhost:4444/wd/hub',
    specs: ['features/*.feature'],
    baseUrl: 'http://localhost:60625/',
    cucumberOpts: {
        require: 'steps/*.steps.js',
        format: 'pretty'
    }
};

在实际页面上,复选框和按钮都具有指定的ID属性。

当我运行测试时,前两个步骤通过,但最后一步失败的原因是:

  

ElementNotVisibleError:元素不可见

但是,我看到Chrome启动,页面上也会显示复选框和按钮!最糟糕的是,当我第一次编写测试并运行它时,所有步骤都通过了。现在,它每次都失败,实际上没有任何改变。

有什么想法吗?

修改

我已按照建议尝试在then()中包装回调调用。这略微改变了输出,但没有解决问题。现在,第二步失败,跳过最后一步。以前,第二步过去了,最后一步失败了。

我也尝试按照browser.driver.manage().window().maximize();

的建议使窗口全屏显示

我还尝试使用this.setDefaultTimeout(10000);添加更长的超时时间,但这似乎没有任何区别。

根据要求,这是控制台输出中的堆栈跟踪:

  

跑步"量角器:全部" (量角器)任务   在http://localhost:4444/wd/hub使用selenium服务器   [launcher]运行WebDriver的1个实例

     

场景:第1步      鉴于我进入" test-process /#/ step-1"
     当我选中复选框时

 ElementNotVisibleError: element not visible
    (Session info: chrome=47.0.2526.111)
    (Driver info: chromedriver=2.20.353145 
    (343b531d31eeb933ec778dbcf7081628a1396067),platform=Windows NT 6.1 SP1 x86_64) > (WARNING: The server did not provide any stacktrace information)
 Command duration or timeout: 15 milliseconds
 Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:59:12'
 System info: host: 'BDTUK79DW', ip: '172.23.83.166', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_71'
 Driver info: org.openqa.selenium.chrome.ChromeDriver
 Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, chrome={userDataDir=C:\Users\DanW\AppData\Local\Temp\scoped_dir14556_23663}, takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=47.0.2526.111, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
 Session ID: 8591f8db6eea372305d94c1aea52eca0
      at new bot.Error (fakepath\node_modules\grunt-protractor-runner\node_modules\selenium-webdriver\lib\atoms\error.js:108:18)
      at Object.bot.response.checkResponse (fakepath\node_modules\grunt-protractor-runner\node_modules\selenium-webdriver\lib\atoms\response.js:109:9)
      at fakepath\node_modules\grunt-protractor-runner\node_modules\selenium-webdriver\lib\webdriver\webdriver.js:379:20
      at [object Object].promise.Promise.goog.defineClass.invokeCallback_ (fakepath/node_modules/grunt-protractor-runner/node_modules/selenium-webdriver/lib/goog/../webdriver/promise.js:1337:14)
      at [object Object].promise.ControlFlow.goog.defineClass.goog.defineClass.abort_.error.executeNext_.execute_ (fakepath/node_modules/grunt-protractor-runner/node_modules/selenium-webdriver/lib/goog/../webdriver/promise.js:2776:14)
      at [object Object].promise.ControlFlow.goog.defineClass.goog.defineClass.abort_.error.executeNext_ (fakepath/node_modules/grunt-protractor-runner/node_modules/selenium-webdriver/lib/goog/../webdriver/promise.js:2758:21)
      at goog.async.run.processWorkQueue (fakepath\node_modules\grunt-protractor-runner\node_modules\selenium-webdriver\lib\goog\async\run.js:124:15)
      at process._tickCallback (node.js:405:9)
  Error
      at [object Object].ElementArrayFinder.applyAction_ (fakepath\node_modules\grunt-protractor-runner\node_modules\protractor\lib\element.js:382:21)
      at [object Object].ElementArrayFinder.(anonymous function) [as click] (fakepath\node_modules\grunt-protractor-runner\node_modules\protractor\lib\element.js:78:17)
      at [object Object].ElementFinder.(anonymous function) [as click] (fakepath\node_modules\grunt-protractor-runner\node_modules\protractor\lib\element.js:711:7)
      at World.<anonymous> (fakepath\Static\test\e2e\steps\test.steps.js:21:18)
     

然后我被允许继续下一步

     

失败的情况:   fakepath \ test \ e2e \ features \ test.feature:3#Scenario:Acknowledge warnings

     

1个场景(1个失败)   3个步骤(1个失败,1个跳过,1个过去)   0m02.814s   [launcher] WebDriver的0个实例仍在运行   [启动器] chrome#1失败1次测试   [发射器]整体:1个失败的规格   [launcher]进程已退出,错误代码为1

     

测试失败,但保持grunt进程活着。

     

完成,没有错误。

这很疯狂,因为如果我手动访问该页面,该元素就会带有预期的ID。当测试运行并且浏览器启动时,我可以在浏览器再次关闭之前在页面上看到元素!

如果它确切地指定了它认为哪个元素不可见,那么它将有所帮助

2 个答案:

答案 0 :(得分:2)

我发现如果你没有在你的步骤中包装回调,那么事情似乎并没有按顺序排列。我会尝试重写你的步骤如下:

this.Given(/^I go to "([^"]*)"$/, function (path, callback) {
    browser.get(path).then(function(){callback();});
});

this.When(/^When I have checked the checkbox$/, function (callback) {
    var checkbox = element(by.id('check'));
    checkbox.click().then(function(){callback();});
});

this.Then(/^the button is enabled$/, function (callback) {
    var button = element(by.id('next'));
      chai.expect(button.isEnabled()).to.eventually.equal(true)
         .and.notify(callback);
});

这样,在量角器完成它需要做的事情之后才会执行回调。

答案 1 :(得分:2)

通常以下事项有助于解决&#34;元素不可见&#34;错误:

  • 移至元素,然后点击:

    browser.actions().mouseMove(checkbox).click().perform();
    
  • 滚动到该元素的视图,然后点击:

    browser.executeScript("arguments[0].scrollIntoView();", checkbox.getWebElement());
    checkbox.click();
    
  • 点击JavaScript(另请参阅WebDriver click() vs JavaScript click()):

    browser.executeScript("arguments[0].click();", checkbox.getWebElement());