You can automate browser tests using Selenium WebDriver for Javascript. Selenium offers you plenty of built in methods and classes to automate your web-browser workflow. It can be really useful for automating your tests using Javascript Binding.
In this tutorial, we are going to see how to install selenium webdriver for NodeJS and automate selenium tests. Let’s first start by installation the WebDriver. After that we’ll execute simple javascript test.
You can check out the Video Tutorial.
Installation
In order to Install selenium webdriver for javascript, you need to install NodeJS. First make sure your operating system has the NodeJS and NPM installed. You can check out video of NodeJS Installation.
After setting NodeJS on your system you can run the following code in your terminal.
npm install selenium-webdriver
This installation will install selenium webdriver for Javascript binding.
Now you can easily run the selenium tests using Javascript.
Check this video for simple test created using Selenium WebDriver for Javascript Binding.
Let’s take a look at simple test that gets the page title.
require('selenium-webdriver'); var driver = new webdriver.Builder().build(); driver.get('http://www.google.com'); driver.getTitle().then(function(title) { console.log('Page title is: ' + title); }); driver.quit();
Save this file with name “demo.js”.
And now open terminal and type the code below.
node demo.js
This should run your test using Selenium WebDriver.
And the output of the text will be “Google”.
If you prefer Video Instructions, check out the video below.
You can do many other tests using Selenium JavaScript. You may want to check out the documentation of Selenium for more guidance on methods and examples.
If you want to find an element on page, you can check out the following Video to see how to do just that.
NodeJS has plenty of modules that can be helpful for browser automation and other tests. Check out the videos above for more tests in Selenium Javascript.