A developer tip, in the form of a gif, in your inbox each week.
Subscribe: umaar.com/dev-tips
Or search: 'chrome dev tips'
>120 tips posted so far! 🎊
Over 1 billion downloads per week
Projects most ⭐starred⭐ on github are mainly JS
JavaScript has elegant syntax
const odds = evens.map(v => v + 1)
[a, , b] = [1,2,3];
// a = 1
// b = 3
browser.getTitle(function(title) {
console.log(title)
})
browser.getTitle().then(function(title) {
console.log(title)
})
title = await browser.getTitle()
const selector = `.header`
findElement(`body ${selector}`)
# In your terminal
npm i selenium-webdriver
const webdriver = require('selenium-webdriver');
// no need to memorise this 😧
const browser = new webdriver
.Builder()
.usingServer()
.withCapabilities({'browserName': 'chrome' })
.build();
browser.get('http://en.wikipedia.org/wiki/Wiki');
const links = await browser.findElements(
webdriver.By.css('[href^="/wiki/"]')
)
console.log(links.length)
node Wiki.js
# > 396
Getting to Philosophy on Wikipedia
Clicking on the first lowercase link in the main text of a Wikipedia article, and then repeating the process for subsequent articles, usually eventually gets one to the Philosophy article.
const url = 'wikipedia.org/wiki/Special:Random';
browser.get(url).then(clickFirstLink)
function clickFirstLink() {
const link = '#mw-content-text > p a[title]';
browser.click(link);
}
if (linkText === 'Philosophy') {
// We made it!
} else {
link.click().then(clickFirstLink);
}
The 'hello world' of browser automation
Official JS bindings
browser.get('https://www.google.com');
browser
.findElement(webdriver.By.name('q'))
.sendKeys('selenium conf 2016');
browser
.findElement(webdriver.By.name('btnG'))
.click();
const selector = webdriver.By.css(
'[href="http://seleniumconf.co.uk/"]'
);
const link = webdriver
.until
.elementLocated(selector);
browser.wait(link, 2000).click();
console.log('Title: ', await browser.getTitle());
//Instead of:
browser.getTitle()
.then(title => console.log(title));
$ node --harmony-async-await search.js
# Title: SeleniumConf UK 2016 - Home page
browser.get()
var client = webdriverio.remote(options);
client
.init()
.url('https://google.com')
.setValue('*[name="q"]','selenium conf 2016')
.click('*[name="btnG"]')
.click('[href="http://seleniumconf.co.uk/"]')
.getTitle().then(console.log)
.end()
end()
it('performs a google search', function() {
const linkSelector = '[href="http://seleniumconf.co.uk/"]';
browser.navigateTo('https://google.com');
browser.type('[name="q"]', 'selenium conf 2016')
browser.waitForElementExist(linkSelector);
browser.click(linkSelector);
console.log('Title is: ', browser.getPageTitle());
});
import { ClientFunction } from 'testcafe';
const getTitle = ClientFunction(() => document.title);
fixture `Google Search for Selenium Conf 2016`
.page `https://www.google.com`;
const text = 'selenium conf 2016';
const link = '[href="http://seleniumconf.co.uk/"]';
await t.typeText('[name="q"]', 'selenium conf 2016')
await t.click('[name="btnK"]');
await t.click('[href="http://seleniumconf.co.uk/"]');
console.log('Title: ', await getTitle());
getTitle
Source code as documentation
github.com/SeleniumHQ/selenium/blob/master/javascript/node/selenium-webdriver/lib/webdriver.js
.sleep() / .pause()
🙅sleeps
with waitFor
Encourage best practices
Split your tests up based on priority
Run p1
tests before a deploy
Run p3
tests overnight
Take time to learn async with callbacks, promises and await
async/await
is still async code
node --inspect google-search-example.js
Twitter: @umaar
umar.hansa@gmail.com for feedback or ideas