【问题标题】:TestCafe, Selector withAttribute works with string but not with regular expressionTestCafe, Selector withAttribute 适用于字符串但不适用于正则表达式
【发布时间】:2021-01-28 09:57:47
【问题描述】:

当我尝试使用下面的选择器切换到 TestCafe 中的 iframe 时:

const iframeCase = Selector('iframe').withAttribute('title', 'SR-1234');

我成功切换到 iframe。但是由于标题中的数字每次测试运行都会改变。我需要添加一个正则表达式,就像我在这里做的那样:

const iframeCase = Selector('iframe').withAttribute('title', /SR\-[0-9]{0,6}/); 

但是使用这个正则表达式变体,我收到以下错误: 与指定选择器匹配的元素不可见。

这是我要选择的 iframe:

<iframe name="PegaGadget7Ifr" id="PegaGadget7Ifr" border="0" frameborder="0"style="" title="SR-1234" 
data-hammerhead-focused="true"></iframe>

有谁知道我做错了什么?我首先认为我的正则表达式是错误的。但其他组合如:

  /SR/ 
  /SR\-[0-9][0-9][0-9][0-9][0-9]/ 
  /SR[-0-9][0-9][0-9][0-9][0-9]/ 

也都失败了。不幸的是,我无法共享正在测试的应用程序,因为它在封闭的公司环境中运行。

testcafe: 1.10.1
Chrome 87.0.4280.141 / Windows 10.  
node: v14.15.4

【问题讨论】:

    标签: regex testing automated-tests e2e-testing testcafe


    【解决方案1】:

    试试,

    await t.switchToIframe(
        Selector("iframe[title^='SR']"),
        { timeout: 60000 }));
    

    【讨论】:

    • 嗨 Vysakmohan,不幸的是这没有帮助:(
    • 检查控制台中的 document.querySelector("#PegaGadget7Ifr") 是否返回您的 iframe
    • document.querySelector("#PegaGadget7Ifr") 正在我的控制台中恢复 iframe。但是每次测试运行时,id 中的数字都会改变。所以我真的不能用这个ID。
    • 试试这个选择器:Selector("iframe[title^='SR']");
    • 不幸的是,选择器不起作用。我确实更新了我的问题,因为我有一些新的发现。
    猜你喜欢
    • 1970-01-01
    • 2016-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多