使用Testcafe绕过弹出窗口的条件测试

时间:2017-08-17 13:42:39

标签: testcafe

我正在使用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...}

最后,我正在执行另一个.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。

1 个答案:

答案 0 :(得分:2)

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');
});