【问题标题】:Conditional test to bypass pop up with Testcafe使用 Testcafe 绕过弹出窗口的条件测试
【发布时间】:2018-01-25 23:14:22
【问题描述】:

我正在使用 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”出现在屏幕上,点击“关闭弹出窗口”并继续测试,否则继续测试。

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

我正在使用 testcafe 0.17.0。

【问题讨论】:

    标签: testing automated-tests conditional-statements e2e-testing testcafe


    【解决方案1】:

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

    【讨论】:

    • 用一些示例代码更新了问题。尝试了你的建议,但没有奏效。我相信我做错了:)
    • 布鲁诺,你的页面是公开的吗?如果您与我分享其网址,我将能够尝试创建一个工作示例
    • AlexanderMos 谢谢,但我更喜欢自己尝试实现。我会采纳你的答案并与我的同事分享,然后再试一次。
    • 我试过你的代码,但是 VS 返回 TypeError: tc.hover is not a function。 await tc 39 | //.hover(home.firstPromoImg) > 40 | .hover(home.logoLnk) 41 | await tc 42 | .typeText(home.search, product.productName) 43 | .click(home.firstProductImg)
    • 如果你使用这种方式,你就不能使用链。而不是await tc.hover('el1').hover('el2'),你必须写await tc.hover('el1'); await tc.hover('el2')
    猜你喜欢
    • 2014-04-28
    • 1970-01-01
    • 2019-03-25
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-27
    相关资源
    最近更新 更多