【问题标题】:Test page title with NightwatchJS使用 NightwatchJS 测试页面标题
【发布时间】:2020-06-06 02:00:22
【问题描述】:

我需要检查一个页面的标题是否包含“主页”。

我尝试以下 browser.expect.element('title').text.to.contain('Homepage'); 但元素 title 总是返回为空。

任何其他元素都有效,但 title 似乎表现不同。如何检查子字符串?

示例

作品:

browser
  .url('https://stackoverflow.com')
  .waitForElementVisible('body', 2000)
  .assert.title('Stack Overflow - Where Developers Learn, Share, & Build Careers');

不起作用:

browser
  .url('https://stackoverflow.com')
  .waitForElementVisible('body', 2000)
  .expect.element('title').text.to.contain('Developers');

输出:Expected element <title> text to contain: "Developers" - expected "contain 'Developers'" but got: ""

然而这可行:browser.expect.element('h1').text.to.contain('Learn, Share, Build');

似乎只能测试可见元素,所以我不确定如何检查隐藏的内容。

【问题讨论】:

  • 发布网页的HTML
  • 您是否在等待页面加载?可能它试图在页面加载之前获取标题
  • 添加示例。

标签: nightwatch.js


【解决方案1】:

only interacts with visible elements.

这行得通:

   browser.getTitle(function(title) {
     this.assert.ok(title.includes("Homepage"));
   });

【讨论】:

    【解决方案2】:

    似乎在 nightwatch 中添加了标题方法

    使用断言,您可以检查整个标题 https://nightwatchjs.org/api/#assert-title browser.assert.title("Nightwatch.js");

    使用expect,你可以检查它的一部分 https://nightwatchjs.org/api/expect/#expect-title- browser.expect.title().to.contain('value'); startWithendWith 也可以这样使用,即使是正则表达式检查 browser.expect.title().to.match(/value/);

    【讨论】:

      【解决方案3】:

      我们创建了可以在应用程序中使用的通用命令 before() 将在断言失败之前等待超时设置

      exports.command = function waitForTitleContains(pageTitle) {
         this.expect
          .title().to.contain(pageTitle)
          .before(); //defaults to waitForConditionTimeout from nightwatch.conf
      };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-03
        • 2019-09-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-24
        相关资源
        最近更新 更多