簡體   English   中英

量角器無需運行完整方案即可立即打開和關閉chrome瀏覽器

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

我編寫了一個方案,使用量角器測試應用程序。 我的應用程序從非角度頁面的登錄頁面開始,然后在登錄后進入角度頁面。

以下是我曾經運行過的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參數,而只需返回一個promise:

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的能力在此處記錄

量角器基於硒。 我強烈建議閱讀Selenium文檔的整個“了解API”部分 ,以便您了解JavaScript版本的Selenium如何使用和序列。

您的代碼現在無法正常工作的原因是,通過像您一樣調用callback() ,您告訴Cucumber.js Protractor(和Selenium)實際執行了所需的操作之前 ,您的步驟已完成。 當您返回承諾時,Cucumber.js會等到承諾被解決或失敗后再繼續。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM