【问题标题】:Nightwatch.js - Getting title of element and compare it laterNightwatch.js - 获取元素的标题并稍后进行比较
【发布时间】:2017-06-14 12:30:24
【问题描述】:

我正在使用 Nightwatch.js 进行 E2E 测试。

在测试中,应该点击表格列表中的特定元素来选择一篇文章。然后文章被加载。

现在我想通过比较标题来检查是否加载了正确的文章。

如何获取所选元素的文本?在我的代码中,只是单击了该元素。在第二部分,应该比较这个标题。在我下面的代码中,它只是硬编码:

module.exports = {
  'article': function(browser) {
    browser
      .click('#list tbody tr:first-child .selectable')
      .waitForElementVisible('#article-wrapper', 10000)

    browser.expect.element('h1').text.to.equal('specific title')

    browser.end()
  }
}

【问题讨论】:

    标签: javascript testing selenium-webdriver nightwatch.js e2e-testing


    【解决方案1】:

    如果我很好理解你的问题,我认为你需要使用getText 方法。我不知道您的 HTML 代码的结构,但是这个最小的工作示例肯定会对您有所帮助:

    HTML (index.html)

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>Nightwatch</title>
    </head>
    <body>
      <h1>Foo</h1>
      <table>
        <tr>
          <th>Header 1</th>
          <th>Header 2</th>
        </tr>
        <tr>
          <td>Foo</td>
          <td>Bar</td>
        </tr>
      </table>
    </body>
    </html>
    

    JavaScript (title.js)

    module.exports = {
      'Title': function (browser) {
        browser
          .url('http://localhost:8000/index.html') // Change this if needed
          .waitForElementPresent('body', 1000);
    
        browser.getText('table tr:last-child td:first-child', function (title) {
          browser.expect.element('h1').text.to.equal(title.value);
        });
    
        browser.end();
      }
    };
    

    命令

    nightwatch -t tests/title.js
    

    【讨论】:

    • 我觉得你有点误会了:首先我需要获取表格元素的文本。然后 nightwatch 正在点击它,这将路由到另一个页面。此页面的标题应与之前从表格中提取的文本进行比较。
    • 这是我理解的,只是我以为你在使用 AJAX。但是,从我的角度来看,getText 的这个基本示例仍然足以解决您的问题。什么阻碍了你?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-30
    • 1970-01-01
    • 1970-01-01
    • 2015-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多