【问题标题】:How to make CONDITIONAL STATEMENTS using SELECTORS on PLAYWRIGHT?如何在 PLAYWRIGHT 上使用选择器制作条件语句?
【发布时间】:2021-11-11 08:24:27
【问题描述】:

我需要一些帮助才能使用 Playwright 测试制作条件语句。
我有一个给定的选择器,比如说一个按钮,我需要写一个这样的条件语句:

if (selector is not present/visible)

    do nothing and proceed with the *next lines of code*

else  

    await page.click(*selector*)

*next lines of code*

有没有什么有效的方法来做这些事情?我在 Playwright 文档中没有找到任何内容。

【问题讨论】:

    标签: javascript typescript testing automated-tests playwright


    【解决方案1】:

    它可以在 JavaScript 中实现,类似于使用 page.$(selector) 的伪代码,如果指定的元素不在 DOM 中,则返回 null。 (注意:注意条件中括号的正确用法!)

    我还将您最初的if(falsy)...else 条件反转为单个if(truthy),因为您只想在选择器存在时才执行命令,else 分支没有太大意义:

    if ((await page.$('#elem-id')) !== null) {
      await page.click('#elem-id')
    }
    // code goes on with *next lines of code*
    

    【讨论】:

      猜你喜欢
      • 2014-11-20
      • 2013-10-04
      • 1970-01-01
      • 2016-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-17
      • 2010-12-11
      相关资源
      最近更新 更多