1

代码的最后一步因 Assertion 错误而失败,因为实际值仍然是LoginPage,我猜是因为该步骤在浏览器实际重定向到HomePage之前完成。

我尝试使用browser.sleep(10000)and 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');
    });        
});
4

3 回答 3

2

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')
于 2017-08-28T14:11:27.123 回答
1

您的等待功能过于复杂。如果你使用 async/await,你的代码会更简单。在 promise 之外保存 url 的技巧是行不通的,在 promise 得到解决之前,您的变量将是未定义的。另外我不建议在实际项目中使用绝对 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`)
}
于 2017-08-28T19:01:56.463 回答
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');
    });        
});
于 2017-08-28T17:12:41.187 回答