简体   繁体   English

咕unt声,黄瓜,量角器测试意外失败

[英]Grunt, Cucumber, Protractor test failing unexpectedly

I have a feature file like so: 我有一个像这样的功能文件:

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

My steps.js file is as follows: 我的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);
    });

};

I also have a config like like this: 我也有这样的配置:

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'
    }
};

On the actual page, the checkbox and button both have the specified ID attributes. 在实际页面上,复选框和按钮都具有指定的ID属性。

When I run the test, the first two steps pass, but the last step fails with the reason: 当我运行测试时,前两个步骤通过了,但是最后一个步骤失败,原因如下:

ElementNotVisibleError: element not visible ElementNotVisibleError:元素不可见

However, I see Chrome launch and the checkbox and button are both visible on the page! 但是,我看到Chrome启动,并且复选框和按钮在页面上都可见! the worst part is, when I wrote the test and ran it for the first time, all the steps passed. 最糟糕的是,当我编写测试并首次运行它时,所有步骤都通过了。 Now, it fails every time and nothing has actually changed. 现在,它每次都会失败,并且实际上没有任何更改。

Any ideas? 有任何想法吗?

Edit 编辑

I've tried wrapping the callback invocations in then() as suggested. 我已经尝试按照建议在then()包装回调调用。 This has changed the output slightly but has not fixed the issue. 这稍微改变了输出,但没有解决问题。 Now, the second step fails and the last step is skipped. 现在,第二步失败,最后一步被跳过。 Previously, the second step passed, and the last step failed. 以前,第二步通过了,最后一步失败了。

I've also tried making the window fullscreen as suggested with browser.driver.manage().window().maximize(); 我还尝试按照browser.driver.manage().window().maximize();建议将窗口全屏显示browser.driver.manage().window().maximize();

I've also tried adding a longer timeout with this.setDefaultTimeout(10000); 我也尝试过使用this.setDefaultTimeout(10000);添加更长的超时时间this.setDefaultTimeout(10000); but this appears not to have made any difference. 但这似乎没有任何区别。

As requested, here is the stacktrace in the console output: 根据要求,这是控制台输出中的stacktrace:

Running "protractor:all" (protractor) task Using the selenium server at http://localhost:4444/wd/hub [launcher] Running 1 instances of WebDriver 运行http:// localhost:4444 / wd / hub上的硒服务器,运行“ protractor:all”(量角器)任务[启动器]运行1个WebDriver实例

Scenario: Step 1 场景:第1步
Given I go to "test-process/#/step-1" 鉴于我去“测试过程/#/步骤1”
When I have checked the checkbox 当我选中复选框时

  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) 

Then I am allowed to proceed to the next step 然后,我可以继续下一步

Failing scenarios: fakepath\\test\\e2e\\features\\test.feature:3 # Scenario: Acknowledge warnings 失败方案:fakepath \\ test \\ e2e \\ features \\ test.feature:3#方案:确认警告

1 scenario (1 failed) 3 steps (1 failed, 1 skipped, 1 passed) 0m02.814s [launcher] 0 instance(s) of WebDriver still running [launcher] chrome #1 failed 1 test(s) [launcher] overall: 1 failed spec(s) [launcher] Process exited with error code 1 1个场景(1个失败)3个步骤(1个失败,1个跳过,1个通过)0m02.814s [启动] WebDriver的0个实例仍在运行[启动] chrome#1失败1个测试[启动]总体上: 1个失败的规格[启动程序]进程退出,错误代码为1

Test failed but keep the grunt process alive. 测试失败,但保持正常运行状态。

Done, without errors. 完成,没有错误。

It's crazy because if I visit the page manually, the element is there with the expected ID. 太疯狂了,因为如果我手动访问该页面,则该元素在那里就会有预期的ID。 When the test runs and the browser launches, I can see the element there on the page before the browser closes again! 当测试运行并且浏览器启动时,在浏览器再次关闭之前,我可以在页面上看到该元素!

If it would specify exactly which element it thinks is not visible it would help 如果它确切指定它认为不可见的元素,将有所帮助

I have found that if you do not wrap the callbacks in your steps, that things do not seem to stay in sequence. 我发现,如果您没有在步骤中包装回调,那么事情似乎就不会保持按顺序进行。 I would try re-writing your steps as below: 我会尝试重新编写您的步骤,如下所示:

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

This way the callback does not get executed until after protractor has done what it needs to do. 这样,直到量角器完成所需的操作后,回调才会执行。

Usually the following things help to tackle "Element not visible" errors: 通常,以下操作有助于解决“元素不可见”错误:

  • move to the element and then click: 移动到该元素 ,然后单击:

     browser.actions().mouseMove(checkbox).click().perform(); 
  • scroll into view of the element and then click: 滚动到该元素的视图 ,然后单击:

     browser.executeScript("arguments[0].scrollIntoView();", checkbox.getWebElement()); checkbox.click(); 
  • click via JavaScript (see also WebDriver click() vs JavaScript click() ): 通过JavaScript点击(另请参见WebDriver click()与JavaScript click() ):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM