Protractor 立即打开和关闭 chrome 浏览器,没有 运行 完整场景

Protractor opens and closes the chrome browser immediately without running the full scenario

我编写了一个场景来使用量角器测试应用程序。我的应用程序从非 angular 页面的登录页面开始,然后在登录后转到 angular 页面。

下面是我用来 运行:

的 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

从您问题中的代码风格来看,您似乎正在使用 Cucumber.js 作为您的测试运行器。那么在这种情况下,您应该能够在步骤定义中省略 callback 参数,而只需 return 一个承诺:

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 使用承诺的能力已记录 here

Protractor 基于 Selenium 构建。我强烈建议阅读整个 "Understanding the API" section 的 Selenium 文档,以便您了解 JavaScript 版本的 Selenium 如何使用和序列承诺。

您的代码现在无法正常工作的原因是,通过像您那样调用 callback(),您告诉 Cucumber.js 您的步骤已完成 before Protractor(和 Selenium)实际上已经执行了您想要的操作。当您 return 承诺时,Cucumber.js 会等到承诺得到解决或失败后再继续。