量角器会立即打开和关闭Chrome浏览器,而不会运行完整方案

时间:2015-07-01 12:23:10

标签: javascript angularjs selenium protractor

我写了一个场景来使用量角器测试应用程序。我的应用程序从登录页面开始,这是非角度页面,然后登录后移动到角度页面。

以下是我以前运行的javascript代码段:

var chai = require('chai');
var chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);
var expect = chai.expect;
var myStepDefinitionsWrapper = function () {
    this.Given(/^that I login with valid user credentials$/, function (callback) {
        console.log("I'm in before URL");
        browser.driver.get('http://localhost:8000/#');
        console.log("I'm in after URL");
        browser.driver.wait(function() {
            console.log("I'm in Wait");
            return browser.driver.isElementPresent(by.xpath("//input[contains(@placeholder,'Username')]"));
        },10000);
        browser.driver.findElement(by.xpath("//input[contains(@placeholder,'Username')]")).then(function(username) {
            console.log("I'm in Username");
            username.sendKeys("welby");
        });
        browser.driver.findElement(by.xpath("//input[contains(@type,'password')]")).then(function(password) {
            console.log("I'm in Password");
            password.sendKeys("asdf");
        });

        browser.driver.findElement(by.xpath("//button[@type='submit']")).click();
        console.log("I'm after click");
        callback();
    });

     this.When(/^I click perform button in Tasks window$/, function (callback) {
         browser.waitForAngular();
         element(by.xpath("//*[text()[contains(.,'Smith, Sally')]]/following::td[2]/button[text()='Perform']")).click();
         console.log("Clicked Perform");
         callback();
    });
}

输出:

"C:\Program Files (x86)\JetBrains\WebStorm 10.0.4\bin\runnerw.exe" "C:\Program Files (x86)\nodejs\node.exe" node_modules\protractor\lib\cli.js E2E\protractor-conf.js Using the selenium server at http://127.0.0.1:4444/wd/hub [launcher] Running 1 instances of WebDriver 

 - I'm in before URL  
 - I'm in after URL
 - I'm after click
 - Clicked Perform

1 scenario (1 passed)  3 steps (3 passed)

[launcher] 0 instance(s) of WebDriver still running [launcher] chrome #1 passed

Process finished with exit code 0

1 个答案:

答案 0 :(得分:2)

根据您问题中代码的样式判断,您似乎正在为测试运行员使用Cucumber.js。在这种情况下,您应该能够省略步骤定义的callback参数,只需返回一个承诺:

this.Given(/^that I login with valid user credentials$/, function () {

    // The rest of the code remains the same.

    return browser.driver.findElement(by.xpath("//button[@type='submit']")).click();
});

 this.When(/^I click perform button in Tasks window$/, function () {
     browser.waitForAngular();
     return element(by.xpath("//*[text()[contains(.,'Smith, Sally')]]/following::td[2]/button[text()='Perform']")).click();
});

Cucumber.js使用promises的能力记录在案here

量角器建立在Selenium上。我强烈建议您阅读Selenium文档的全部"Understanding the API" section,以便了解Selenium的JavaScript版本如何使用和序列承诺。

您的代码现在无法正常工作的原因是,通过调用callback()就像你一样,你告诉Cucumber.js你的步骤已经完成之前量角器(和Selenium)实际上执行了你想要的动作。当你返回一个承诺时,Cucumber.js会一直等到承诺得到解决或失败,然后再继续。