Skip to content
Advertisement

JS WebDriverIO how to check if tag h1 is present on a page

I tried doing like this:

await browser.url(https://webdriver.io/');
expect(browser).toHaveText('h1')

so tried:

await browser.url(https://webdriver.io/');
await browser.findElement('xpath', 'h1')

But it doesn’t help

Advertisement

Answer

this is how i solved the issue:

const url = 'https://webdriver.io/';
const crossFetch = require('cross-fetch');
const links = [];

describe('test', async () => {
    beforeEach(async function () {
        const result = await crossFetch(`${url}/wp-json/wp/v2/pages/?per_page=100`);
        const pages = await result.json();
        pages.forEach(page => links.push(page.link));
    });

    it('Check Headers', async () => {
        const errorsUrls = [];

        for (const url of links) {
            await browser.url(url);
            const classNameAndText = await $('h3');

            if (typeof classNameAndText.error !== 'undefined') {
                errorsUrls.push(url);
            }
        }

        if (errorsUrls.length > 0) {
            throw new Error( `
                Header Not Found:
                ${errorsUrls.join('rn')}
            `);
        }
    })
});
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement