跳过正文
Background Image
  1. Posts/

Nightwatch 验证元素是否存在

·292 字·1 分钟· ·
沈显鹏
作者
沈显鹏
目录

元素常用验证方法
#

验证元素的值信息

andesFormSection
    .assert.containsText('@errorMessage', 'The email address is invalid.')

验证元素是否可用

andesFormSection
    .assert.attributeEquals('@continueBtn', 'disabled', 'true');

等待元素可用

andesFormSection
    .expect.element('@signInBtn').to.be.visible.before(5000);
# 或者
andesFormSection
    waitForElementVisible('signInBtn', 5000);

等待元素呈现

andesFormSection
    .expect.element('@signInBtn').to.be.present.before(5000);
# 或者
andesFormSection
    waitForElementPresent('signInBtn', 5000);

判断元素是否存在
#

用 Nightwatch 去判断一个 element 是否存在,如果存在执行如下操作,如果不存在做另外的操作。 这个在 Java 编写的自动化测试用例中可以用 try catch 可以解决,Nightwatch 试过不行。 另外看到 stackoverflow 上有通过判断 (result.status != -1),没有解决我的问题。

最后这样解决的,请看下面 tutorial.js

const tutorialCommands = {
  notShowTutorial: function() {
    const tutorialSection = this.section.tutorial;
    this.api.element('css selector', '.andes-dialog md-icon', function(result) {
      if (result.value && result.value.ELEMENT) {
        this.pause(2000);
        tutorialSection.click('@doNotShowBtn');
        this.pause(2000);
        tutorialSection.click('@closeBtn');
    } else {
      console.log('no tutorial exists');
    }
    });
  }
};

module.exports = {
  commands: [tutorialCommands],
  url: function() {
    return `https://shenxianpeng.github.io/`;
  },
  sections: {
    tutorial: {
      selector: '.andes-dialog',
      elements: {
        closeBtn: 'md-icon',
        doNotShowBtn: 'md-checkbox .md-container'
      }
    }
  }
};

注意:这里的元素不能通过 section 的方式引用,例如这样,怀疑这是 Nightwatch 的 bug。

tutorialSection.api.element('css selector', '@closeBtn', function(result) {

}

相关文章

Nightwatch 自动化测试中比较颜色
·290 字·1 分钟
本文介绍了如何在 Nightwatch.js 中处理颜色比较,包括获取元素的颜色值并与预期颜色进行对比的示例代码。
Nightwatch 获取接口返回数据
·270 字·1 分钟
本文介绍了如何在 Nightwatch.js 中通过接口自动生成和返回数据,并在测试用例中使用这些数据。
Nightwatch 异步操作
·213 字·1 分钟
本文介绍了如何在 Nightwatch.js 中处理异步操作,包括获取 License 数量并进行比较的示例代码。
Nightwatch 模拟键盘操作
·275 字·1 分钟
本文介绍了如何在 Nightwatch.js 中模拟键盘操作,包括输入文本和组合键操作的示例代码。
Nightwatchjs 中文参考手册
·302 字·1 分钟
Nightwatch.js 是一个基于 Node.js 的自动化测试框架,本文提供了 Nightwatch.js 的中文参考手册和示例代码。
Hexo 博客文章中插入图片
·141 字·1 分钟
在 Hexo 博客文章中插入图片的方法和技巧,帮助你更好地展示内容。