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



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

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

我的步骤.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 属性。

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

元素

不可见错误:元素不可见

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

有什么想法吗?

编辑

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

我还尝试按照browser.driver.manage().window().maximize();的建议使窗口全屏显示

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

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

运行"量角器

:全部"(量角器)任务 在 http://localhost:4444/wd/hub 使用硒服务器 [启动器]运行 1 个网络驱动程序实例

方案:步骤 1
鉴于我转到"测试过程/#/步骤-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:UsersDanWAppDataLocalTempscoped_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 (fakepathnode_modulesgrunt-protractor-runnernode_modulesselenium-webdriverlibatomserror.js:108:18)
      at Object.bot.response.checkResponse (fakepathnode_modulesgrunt-protractor-runnernode_modulesselenium-webdriverlibatomsresponse.js:109:9)
      at fakepathnode_modulesgrunt-protractor-runnernode_modulesselenium-webdriverlibwebdriverwebdriver.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 (fakepathnode_modulesgrunt-protractor-runnernode_modulesselenium-webdriverlibgoogasyncrun.js:124:15)
      at process._tickCallback (node.js:405:9)
  Error
      at [object Object].ElementArrayFinder.applyAction_ (fakepathnode_modulesgrunt-protractor-runnernode_modulesprotractorlibelement.js:382:21)
      at [object Object].ElementArrayFinder.(anonymous function) [as click] (fakepathnode_modulesgrunt-protractor-runnernode_modulesprotractorlibelement.js:78:17)
      at [object Object].ElementFinder.(anonymous function) [as click] (fakepathnode_modulesgrunt-protractor-runnernode_modulesprotractorlibelement.js:711:7)
      at World.<anonymous> (fakepathStaticteste2estepstest.steps.js:21:18)

然后我可以继续下一步

失败场景: 假路径\测试\e2e\功能\测试。功能:3 # 场景:确认警告

1 个

方案(1 个失败) 3 个步骤(1 个失败、1 个跳过、1 个通过) 0分钟02.814秒 [启动器] 0 个 Web 驱动程序实例仍在运行 [启动器] 铬 #1 未通过 1 次测试 [启动器] 总体:1 个失败的规格 [启动器]进程退出,错误代码为 1

测试失败,但保持咕噜声进程处于活动状态。

完成,没有错误。

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

如果它能准确指定它认为不可见的元素,这将有所帮助

我发现,如果你不把回调包装在你的步骤中,事情似乎不会按顺序排列。我会尝试重写您的步骤,如下所示:

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

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

通常,以下事项有助于解决"元素不可见"错误:

  • 移动到该元素,然后单击:

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

最新更新