"No element found using locator..." 使用 Cucumber 执行端到端测试时

"No element found using locator..." when performing e2e testing with cucumber

我在 Cucumber 的几个功能上遇到以下故障 "NoSuchElementError: No element found using locator: By(css selector, h1)"

我试图设置一个更大的超时时间,以便给 Cucumber 更多时间来查找元素,但它似乎不起作用

以下是测试的主要组成部分: cardTitle.feature:

@cardTitle-feature
Feature: See card title
  Display the card title

  @cardTitle-scenario
  Scenario: Card Page
    Given I am on the card page
    When I do nothing
    Then I should see the card title

app.steps.ts:

// Go to the card - Display the title
Given(/^I am on the card page$/, async () => {
    await page.navigateToCard();
});

When(/^I do nothing$/, () => {
});

Then(/^I should see the card title$/, async () => {
    expect(await page.getCardTitleText()).to.equal('Profile');
});

app.po.ts:

navigateToCard() {
        this.sleep(3000);
        return browser.get('/card');
    }

getCardTitleText() {
        this.sleep(3000);
        return element(by.css('h1')).getText();
    }

card.html:

<div class="profile-container">
  <!-- EXAMPLE TOP NAV -->

  <h1>Profile</h1>
...

我认为这可能发生,因为 "card" 在没有登录应用程序的情况下可能无法访问。如果这是问题所在,我如何执行登录到应用程序然后检查 "h1" 元素的测试? 谢谢!

写得好!感谢所有的细节,这有很大帮助。以下是我的想法:

  • 您不需要 await 页面对象中的睡眠吗?或者承诺将 sleep 链接到 get,如果您出于某种原因避免在页面对象中使用异步函数。
  • 鉴于您使用的是量角器,而且这大概是一个 angular 应用程序,我很惊讶您竟然需要睡觉。通常作为每个 browser.get 的一部分运行的内置 waitForAngular 应该等到页面完全加载,然后再继续
  • 您的 css 看起来很完美,应该可以正常工作。您可以使用 css.
  • 找到 html 标签和属性
  • 我假设 this.sleep 是您的页面对象基 class 中的一个方法?我有点期待 browser.sleep

顺便说一句,我已经从量角器中的 $$$ 别名中学到了很多东西,它们非常干净。你可以缩短

return element(by.css('h1')).getText();

刚好

return $('h1').getText();

如果你愿意。