mchd
mchd

Reputation: 3163

How to get the h1 tag from a website in WebDriverIO

I am using WebDriverIO to do UI testing. I am trying to get this h1 tag: enter image description here

This is my attempt:

 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.");
    });

The test fails:

[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

How can I fix it?

Upvotes: 0

Views: 444

Answers (2)

MkMan
MkMan

Reputation: 2191

This worked for me

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.");
});

Upvotes: 1

Yevhen Laichenkov
Yevhen Laichenkov

Reputation: 8672

It happens because you're using the toHaveValue method instead of the toHaveText.

toHaveValue - Checks if an input element has a certain value.

toHaveText - Checks if an element has a specific text.

So, your code should be:

 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.");
 });

Note:

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.

Upvotes: 3

Related Questions