3

对不起,总的 N00b 问题,但这里是,

我正在编写一些测试自动化,而我正在自动化的网站有时会对页面上的某些元素有某些行为,而有时它对页面上的不同元素有不同的行为。它通常属于两种模式之一。将其称为模式 A 和模式 B。问题是模式 A 和模式 B 之间存在一些共享元素,因此它不仅仅是不同的元素。

但是,无论哪种情况,当模式 B 处于活动状态时,都有一个元素始终在屏幕上,而当模式 A 处于活动状态时,它不在屏幕上。此外,模式 A 的工作流程稍长一些,需要与之交互的一些额外控件。让我们将此元素称为“ProfileIdentifier”。我有一个定位器,我想编写一些逻辑代码来检查这个元素是否不存在,然后与额外的控件交互并执行更长的工作流程,然后是公共控件和常见的工作流程模式 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 定位器的最佳(高性能和稳定)方式,尤其是如果我们必须在网站的其他部分重复使用类似的定位器来进行样式设置和其他测试自动化。

4

1 回答 1

2

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

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
}
于 2017-11-09T19:07:40.100 回答