selenium-webdriver - Nightwatch 自定义断言导致测试执行停止,无论结果如何

标签 selenium-webdriver nightwatch.js

我正在尝试创建一个 nightwatchJS 自定义断言,它将检查文件是否存在。断言似乎触发了,但 nightwatch 在断言命令完成后立即退出。

我想我不会将控制权返回给 nightwatch api,但如果是这样的话,我该如何实现呢?

  // filename = doesFileExist.js

  exports.assertion = function(fname, msg) {
   var fs = require('fs');
  
   this.message = msg + 'FileExists: ' + fname ;     
   this.expected = true;                     
   this.pass = function(value) {             
       return value == this.expected;
        } ;

   this.value = function(result) { 
       return result;       
       };    
 
   this.command = function(callback) { 
      return fs.exists(fname, callback);
       };
};

测试用例(以nightwatch.json为例)是;

  this.checkForFile = function() {
    browser
      .verify.doesFileExist('nightwatch.json', 'test1')  
    return browser;
  };

最佳答案

我知道这是一个老问题,但我在编写自己的自定义断言时发现您需要在命令函数中返回“this”。回调将您的值发送到传递函数中使用的 this.value。

所以它看起来像这样

this.command = function(callback) { 
    fs.exists(fname, callback);
    return this;
};

关于selenium-webdriver - Nightwatch 自定义断言导致测试执行停止,无论结果如何,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30963234/

相关文章:

javascript - Nightwatch.js setValue 并执行

javascript - 如何按特定顺序运行 Nightwatch 测试?

javascript - e2e测试是否应将数据持久保存在真实数据库中?

java - 如何使用 QAF 查找元素

python - AttributeError : 'NoneType' object has no attribute 'to_capabilities' . 在 Appium 上运行 python 脚本时出现此错误

python - 在 Python Selenium 的 xpath 中使用变量

python - Selenium 失败,错误为 : Service geckodriver unexpectedly exited. 状态代码为:2

ruby - 在 ruby​​ selenium 中捕获超时事件

javascript - 如何覆盖 on_failure 屏幕截图的命名?

javascript - 有没有办法在nodejs中迭代和比较dom元素?