How to get attribute value of an element in webdriverio using typescript with wait syntax?

Subj.

Additional Information:

The following code examples will not compile:

let id = (await browser.element(selector)).getAttribute('id');

      

TSError: β¨― Unable to compile TypeScript ...: Property 'getAttribute' does not exist on type "RawResult". (2339)

let id = (await browser.element(selector).getAttribute('id'));

      

TSError: β¨― Unable to compile TypeScript ...: The type of the "expect" operand must either be a valid promise or must not contain a callable "then" member. (1320)

+3


source to share


2 answers


I don't really know why, but the definitions webdriverio

declare an object that looks like a promise but is called Client .

In TypeScript, you can await

return functions Promise

. You can wrap the API webdriverio

with your own functions and promises:

import * as webdriverio from "webdriverio";

function getTitleAsync(url: string) {
    return new Promise((resolve, reject) => {
        const options = { desiredCapabilities: { browserName: "chrome" } };
        const client = webdriverio.remote(options);
        client
            .init()
            .url(url)
            .getTitle()
            .then(function (title) {
                resolve(title);
            })
            .end();
    });
}

      



Then you can wait for your function:

(async () => {
    const title = await getTitleAsync("https://duckduckgo.com/");
    console.log(title);
})();

      

0


source


It's actually pretty straightforward if your wdio is running synchronously - so no asynchronous / pending ones are required:

it('get attribute', () => {
    browser.url('')
    let classes = browser.element('html').getAttribute('class')
    console.warn('ATRRIBUTE CLASS IS:', classes)
})

      

Your variable classes

will be a simple string object.



Just make sure you set sync: true property in config:

// Per default WebdriverIO commands getting executed in a synchronous way using
// the wdio-sync package. If you still want to run your tests in an async way
// using promises you can set the sync command to false.
sync: true,

      

0


source







All Articles