代码中的最后一步因Assertion错误而失败,因为实际值仍为 LoginPage ,我猜是因为在浏览器实际重定向到 HomePage 之前完成了步骤。
我尝试使用browser.sleep(10000)
和browser.wait()
,但它们对我不起作用。处理这类问题的正确方法是什么?
import {browser, by, protractor} from 'protractor';
import { ClientPage } from '../pages/offerScreenPage';
import {CallbackStepDefinition, defineSupportCode} from 'cucumber';
import {By} from "selenium-webdriver";
import {Events} from "../pages/Event";
let chai = require('chai').use(require('chai-as-promised'));
let expect = chai.expect;
defineSupportCode(function ({ Given, When, Then}) {
let client: ClientPage = new ClientPage();
Given(/^User is in Login Page $/, async () => {
await expect(browser.getTitle()).to.eventually.equal('LoginPage');
});
When(/^User enters credentials$/, async () => {
await client.userId.sendKeys("abc123");
await client.password.sendKeys("passwod");
});
When(/^User clicks the submit button$/, async () => {
await client.submit.click();
});
Then(/^User is redirected to a new page$/, async () => {
await expect(browser.getTitle()).to.eventually.equal('HomePage');
});
});
答案 0 :(得分:1)
2个方法
1)等待新网址: http://www.protractortest.org/#/api?view=ProtractorExpectedConditions.prototype.urlContains
var EC = protractor.ExpectedConditions;
// Waits for the URL to contain 'foo'.
browser.wait(EC.urlContains('foo'), 5000);
2)等待仅存在于该页面上的一些独特元素:
http://www.protractortest.org/#/api?view=ProtractorExpectedConditions.prototype.visibilityOf
var EC = protractor.ExpectedConditions;
// Waits for the element with id 'abc' to be visible on the dom.
browser.wait(EC.visibilityOf($('#abc')), 5000);
可选择将第三个参数传递给browser.wait()以提供很好的错误消息:
browser.wait(EC.visibilityOf($('ololo')), 5000, 'Expected to be on Home page, but element ololo was not became visible in 5 seconds')
答案 1 :(得分:1)
您的等待功能过于复杂。 如果您使用async / await,您的代码可以更简单。除了承诺之外保存网址的技巧也不会起作用,你的变量将被定义,直到承诺得到解决。此外,我不建议在实际项目中使用绝对URL,如果您的环境URL将被更改,这将是痛苦的。将其存储为一些配置变量,并附加所需的路径。
检查一下:
async function waitForUrlToChangeTo(URL) {
let urlIsChangedTo = async () => (await browser.getCurrentUrl()) == URL
return browser.wait(urlIsChangedTo, 10000, `Expected URL to be changed to ${URL} in 10 seconds, but it wasn't`)
}
答案 2 :(得分:0)
import {browser, by, protractor} from 'protractor';
import { ClientPage } from '../pages/offerScreenPage';
import {CallbackStepDefinition, defineSupportCode} from 'cucumber';
import {By} from "selenium-webdriver";
import {Events} from "../pages/Event";
let chai = require('chai').use(require('chai-as-promised'));
let expect = chai.expect;
defineSupportCode(function ({ Given, When, Then}) {
let client: ClientPage = new ClientPage();
/**
* @name waitForUrlToChangeTo
* @description Wait until the URL changes to match a provided regex
* @param {RegExp} urlRegex wait until the URL changes to match this regex
* @returns {!webdriver.promise.Promise} Promise
*/
function waitForUrlToChangeTo(urlRegex) {
let currentUrl;
return browser.getCurrentUrl().then(function storeCurrentUrl(url) {
currentUrl = url;
}
).then(function waitForUrlToChangeTo() {
return browser.wait(function waitForUrlToChangeTo() {
return browser.getCurrentUrl().then(function compareCurrentUrl(url) {
return url == urlRegex;
});
});
}
);
}
Given(/^User is in Login Page $/, async () => {
await expect(browser.getTitle()).to.eventually.equal('LoginPage');
});
When(/^User enters credentials$/, async () => {
await client.userId.sendKeys("abc123");
await client.password.sendKeys("passwod");
});
When(/^User clicks the submit button$/, async () => {
await client.submit.click();
});
Then(/^User is redirected to a new page$/, async () => {
await waitForUrlToChangeTo("https://github.com/login")
await expect(browser.getTitle()).to.eventually.equal('HomePage');
});
});