很抱歉我问了个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
}注意:我看过其他类似的问题,如:related question #1
我确实在回答这个问题时看到了一线希望:Possible answer
$('.myModal').isPresent().then(function(inDom) {
return inDom; // returns bool
};我正在使用Atom JavaScript编辑器,与TestCafe和ES6结合使用,我更愿意说远离像getElementById这样的代码,因为这不是利用JavaScript中的CSS定位器的最好方法(性能和稳定),特别是如果我们必须在站点的其他部分重用类似的定位器来进行样式设计和其他测试自动化。
发布于 2017-11-09 19:07:40
可以使用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
}https://stackoverflow.com/questions/47208410
复制相似问题