Saturday, January 9, 2016

Selenium style automation script with plain JavaScript in Chrome

It is hard to beat Selenium for heavy browser scripting - it has a lot of features, support and infrastructure. But if you only need it occasionally, everything already exists in Chrome.

For example:

function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

async function demo($x) {
    $x('//a[@title="Click here first"]')[0].click();
    await sleep(2000);
    $x('//button[text()=\"Click here then\"]')[0].click();
}

demo($x);


It even have advantages over Selenium: you can omit initialisation, like logins and captchas and script only functional parts. Perfect for boring repetitive tasks and adhoc testing.

No comments:

Post a Comment