如何从 WebDriverIO 中的网站获取 h1 标签

How to get the h1 tag from a website in WebDriverIO

我正在使用 WebDriverIO 进行 UI 测试。我正在尝试获取此 h1 标签:

这是我的尝试:

 it('should get the header of the intro text', () =>{
        const h1 = $('#yui_3_17_2_1_1617935280900_2042');
        expect(h1).toHaveValue("Confidence AI Application: machine learning models provide a systemized approach to assess cash flow risk and opportunity.");
    });

测试失败:

[chrome 89.0.4389.114 linux #0-1] ✖ should get the header of the intro text
[chrome 89.0.4389.114 linux #0-1]
[chrome 89.0.4389.114 linux #0-1] 1 passing (14.2s)
[chrome 89.0.4389.114 linux #0-1] 1 failing
[chrome 89.0.4389.114 linux #0-1]
[chrome 89.0.4389.114 linux #0-1] 1) should get the header of the intro text
[chrome 89.0.4389.114 linux #0-1] Expect $(`yui_3_17_2_1_1617935280900_2042`) to have property value

Expected: "Confidence AI Application: machine learning models provide a systemized approach to assess cash flow risk and opportunity."
Received: undefined
[chrome 89.0.4389.114 linux #0-1] Error: Expect $(`yui_3_17_2_1_1617935280900_2042`) to have property value

我该如何解决?

这是因为您使用的是 toHaveValue 方法而不是 toHaveText

toHaveValue - 检查 输入元素是否具有特定值 .

toHaveText - 检查 元素是否有特定文本 .

所以,您的代码应该是:

 it('should get the header of the intro text', () =>{
   const h1 = $('#yui_3_17_2_1_1617935280900_2042');

   expect(h1).toHaveText("Confidence AI Application: machine learning models provide a systemized approach to assess cash flow risk and opportunity.");
 });

注:

I'm not sure about selectors, because it looks like they're generated on a fly. So, please make sure, that they're correct and always the same.

这对我有用

it("should get the header of the intro text", () => {
  const h1 = $("#yui_3_17_2_1_1617935280900_2042");
  expect(h1.getText()).to.eq("Confidence AI Application: machine learning models provide a systemized approach to assess cash flow risk and opportunity.");
});