如何编写一个if条件,而不用等待检查select器是否不存在Node.js和TestCafe

对于总共N00b的问题抱歉,但在这里,

我正在编写一些testing自动化,而我自动化的网站有时会在页面上有某些特定的行为,有时则会在页面上有不同的行为。 它通常落入两种模式之一。 把它称为模式A和模式B.事情是模式A和模式B之间有一些共享元素,所以它不是唯一不同的元素。

但是,在任何一种情况下,当模式B处于活动状态时,总是有一个元素在屏幕上,而当模式A处于活动状态时,该元素不在屏幕上。 此外,模式A的工作stream程稍长一些,需要一些额外的控件来进行交互。 让我们调用这个元素“ProfileIdentifier”。 我有一个定位器,我想编写一些逻辑代码,检查这个元素是否不存在,然后与额外的控件进行交互,然后执行较长的工作stream,然后是通用的控件和工作stream模式A和模式B.

总之,这样的事情:

import mypage from '../../page-objects/mypage'; //JS file with all the locators for that page. async function FillOutForm(t) { var profileIdentifier = t.getElement(mypage.profileIdentifierLocator); if (not(profileIdentifier.exists)) { // Do the extra workflow } // Do the common workflow } 

或者可能是这样的:

 import mypage from '../../page-objects/mypage'; //JS file with all the locators for that page. async function FillOutForm(t) { var profileIdentifier = t.getElement(mypage.profileIdentifierLocator); if (profileIdentifier.exists.notOk()) { // Do the extra workflow } // Do the common workflow } 

注:我已经看过其他类似的问题,如: 相关的问题#1

我在这个问题的答案中看到了一丝希望: 可能的答案

 $('.myModal').isPresent().then(function(inDom) { return inDom; // returns bool }; 

我正在使用Atom JavaScript编辑器,结合使用TestCafe和ES6,我宁愿说getElementById之类的代码,因为这不是在JavaScript中利用CSS定位器的最佳(性能和稳定性)方法,尤其是如果我们必须在我们网站的其他部分重复使用类似的定位器进行造型和其他testing自动化。

您可以使用existsasynchronous属性来检查元素是否存在。

 import mypage from '../../page-objects/mypage'; //JS file with all the locators for that page. async function FillOutForm(t) { if (!await mypage.profileIdentifierLocator.exists) { // Do the extra workflow } // Do the common workflow }