Javascript 如何组合多个this.expect语句

Javascript 如何组合多个this.expect语句,javascript,nightwatch.js,pageobjects,Javascript,Nightwatch.js,Pageobjects,我想创建一个方法,在执行下一个测试步骤之前验证所有登录页对象是否存在。下面是我目前拥有的,但我正在寻找一种更干净的方法来实现这一点。如有任何建议,我们将不胜感激 verifyLandingPageNavElements:function(){ this.expect.element('@NavLogo_img') .to.be.present.after(2000), this.expect.element('@BuySearch_nav') .to.be.pre

我想创建一个方法,在执行下一个测试步骤之前验证所有登录页对象是否存在。下面是我目前拥有的,但我正在寻找一种更干净的方法来实现这一点。如有任何建议,我们将不胜感激

verifyLandingPageNavElements:function(){
    this.expect.element('@NavLogo_img')
    .to.be.present.after(2000),
    this.expect.element('@BuySearch_nav')
    .to.be.present.after(2000),
    this.expect.element('@Sell_nav')
    .to.be.present.after(2000),
    this.expect.element('@Appraise_nav')
    .to.be.present.after(2000),
    this.expect.element('@Products_nav')
    .to.be.present.after(2000),
    this.expect.element('@LocateADealer_nav')
    .to.be.present.after(2000)
    this.expect.element('@Search_nav')
    .to.be.present.after(2000)
}, 

最好的方法是为此创建一个自定义命令

// ./lib/custom-commands/expectElementsToBePresent.js

exports.command = function(selectors, ms) {
  selectors.forEach(selector => this.expect.element(selector).to.be.present.after(ms));
  return this;
};

// Test case

client
  .url('https://some.url')
  .expectElementsToBePresent([
    '.some.selector',
    '.another.selector'
  ], 2000);
在我的示例中,通过设置配置文件的custom_commands_path属性,不要忘记将Nightwatch指向保存自定义命令的目录。/lib/custom commands

如果您想了解有关自定义命令的更多信息,请访问以下官方文档链接: