6

我正在使用 testcafe 在电子商务页面中运行一些测试,但是一个随机弹出窗口正在破坏测试。当它出现在窗口中时,Testcafe 无法单击下一个选择器并继续进行测试,然后失败。

目前,我正在使用 .js 文件来保存选择器,例如:

    import { Selector } from 'testcafe';

    export default class Checkout {
        constructor () {
            //address
            this.addressName = Selector('input#CC-checkoutCepAddressBook-sfirstname');
            this.addressLastname = Selector('input#CC-checkoutCepAddressBook-slastname');

//Rest of selectors...
}

然后,我将它们导入另一个 .js 并声明测试,如函数:

import { ClientFunction } from 'testcafe';
import { Selector } from 'testcafe';
import Fixture from '../../../DesktopModel/Chrome/fixture.js';
import Home from '../../../DesktopModel/Chrome/home.js';
import Cart from '../../../DesktopModel/Chrome/cart.js';
...
const fixtureUrlBase = new Fixture();
const home = new Home();
const pdp = new Pdp();
const cart = new Cart();
...

export async function checkoutLoggedBoleto(t) {

await t
    .click(pdp.addToCartBtn)
    .click(home.finishOrderBtn)
    .click(cart.finishOrderBtn)

    //Rest of the test actions...}

最后,我正在执行 another.js,在其中使用test命令声明测试:

test
    .before(async t => {
        await login(t);
    })

('Desktop - User Login + Checkout with Invoice', async t => {

    // Function Login  => Search => PDP => Checkout with Invoice 
    await checkoutLoggedBoleto(t);
});

由于它是随机事件(它发生在不同的时刻,例如有时在产品页面中,有时在结帐页面中),可以使用一些条件测试绕过此弹出窗口,例如弹出窗口“x”出现在屏幕,单击“关闭弹出窗口”并继续测试,否则继续测试。

我在testcafe Test API中搜索并没有找到这样的功能。

我正在使用 testcafe 0.17.0。

4

1 回答 1

3

TestCafe 没有为此提供 API。为了处理您的情况,您可以检查弹出窗口是否出现在每个操作之前。或者,为了使您的代码更清晰,您可以通过以下方式包装 TestCafe API 操作:

import { t, Selector } from 'testcafe';

const closePopupBtn = Selector('.close-popup');

async function checkPopup () {
    if(await closePopupBtn.exists)
        await t.click(closePopupBtn);
}

const tc = {
    click: async selector => {
        await checkPopup();
        await t.click(selector);
    }
}

test('my test', async () => {
    await tc.click('.btn1');
    await tc.click('.btn2');
});
于 2017-08-17T14:06:29.573 回答