Selenium + JavaScript = 😕🤔😳
by Željko Filipin
I gave a talk based on this post at Testival Meetup #27, JavaScript Zagreb Meetup #33 and Infinum JS Talks #8. It’s about Selenium packages 📦 in JavaScript.
Packages
As with all things JavaScript, there are plenty of choices. After some research, I have picked three packages:
- WebDriverJs, the official Selenium bindings, also called selenium-webdriver,
- WebdriverIO (named WebdriverJS until 2014),
- Nightwatch.js.
My initial research ended quickly for WebdriverIO and Nightwatch.js because they had Java ☕️ dependency. Some time later, after both the tools (and their documentation) and my understanding of the JavaScript ecosystem improved, I have figured out that they can be used without Java. 😓
Numbers
According to JSDOW (The JavaScript download index), WebDriverJs is far ahead in the number of downloads, but WebdriverIO is growing faster, if I have read the charts correctly.
Stack
What is needed for a test automation project that will not fail?
Browser | Chrome, Firefox |
Browser driver | ChromeDriver, geckodriver |
Language | JavaScript/Node.js |
Selenium bindings | WebDriverJs, WebdriverIO, Nightwatch.js |
Nicer API on top of Selenium (optional) | WebdriverIO, Nightwatch.js |
Assertion library | Assert (ships with Node.js) |
Testing framework | Mocha |
Page object pattern implementation (optional) | WebdriverIO, Nightwatch.js |
Configuration | Node-config, WebdriverIO, Nightwatch.js |
Simple code examples
The initial investigation of the tools consisted of a simple example:
- open a browser
- go to a page
- check that a link is present
- close the browser
The code is hosted at mediawiki-selenium-nodejs repository.
WebDriverJs
Stack
Browser | Chrome |
Browser driver | ChromeDriver |
Language | JavaScript/Node.js |
Selenium bindings | WebDriverJs |
Nicer API on top of Selenium | N/A |
Assertion library | Assert |
Testing framework | Mocha |
Page object pattern implementation | N/A |
Configuration | N/A |
Code
webdriverjs/main_page.js
WebdriverIO
Stack
Browser | Chrome |
Browser driver | ChromeDriver |
Language | JavaScript/Node.js |
Selenium bindings | WebdriverIO |
Nicer API on top of Selenium | WebdriverIO |
Assertion library | Assert |
Testing framework | Mocha |
Page object pattern implementation | N/A |
Configuration | N/A |
Code
webdriverio/stack/assert.js
Nightwatch.js
Stack
Browser | Chrome |
Browser driver | ChromeDriver |
Language | JavaScript/Node.js |
Selenium bindings | Nightwatch.js |
Nicer API on top of Selenium | Nightwatch.js |
Assertion library | Nightwatch.js |
Testing framework | Nightwatch.js |
Page object pattern implementation | N/A |
Configuration | N/A |
Code
nightwatch/main_page.js
Realistic code examples
After the initial investigation with a simple test, all three tools looked fine to me. So far, I liked WebdriverIO API. WebDriverJs was a bit verbose for my taste. Nightwatch.js API looked strange to me.
The second round included a more realistic set of tests.
- wiki page
- create
- edit
- check history
- user
- create
- log in
- change preferences
The entire code is available in the GitHub repositories. I don’t want this blog post to be too long, so I will include only the code for creating a wiki page.
WebDriverJs
The code is at mediawiki-webdriverjs repository.
Stack
Browser | Chrome |
Browser driver | ChromeDriver |
Language | JavaScript/Node.js |
Selenium bindings | WebDriverJs |
Nicer API on top of Selenium | N/A |
Assertion library | Assert |
Testing framework | Mocha |
Page object pattern implementation | N/A |
Configuration | Node-config |
Code
config/default.js
test/helper.js
test/page.js
WebdriverIO
The code is at mediawiki-webdriverio repository.
Stack
Browser | Chrome |
Browser driver | ChromeDriver |
Language | JavaScript/Node.js |
Selenium bindings | WebdriverIO |
Nicer API on top of Selenium | WebdriverIO |
Assertion library | Assert |
Testing framework | Mocha |
Page object pattern implementation | WebdriverIO |
Configuration | WebdriverIO |
Code
wdio.conf.js
specs/page.js
pageobjects/page.js
pageobjects/edit.page.js
Conclusion
I have spent some time with both WebDriverJs and WebdriverIO. I have liked WebDriverJs for it’s simplicity. I have liked WebdriverIO for it’s API and features. I have decided that spending time on investigating Nightwatch.js would not be a good investment.
tags: code - event - featured - javascript - organizer - selenium - testing - speaker - testival