logo logo

Selenium JavaScript Automation Testing Tutorial For Beginners

main post image

Users play a vital role in the success of any product, especially when it comes to user interface – we cannot compromise. We need to introduce functional automation testing to our product and make sure the product’s functionality is as expected and deliver a flawless experience to our end users. There are many tools to do so, Selenium being the most popular among them for web automation (Check out all the latest updates to Selenium 4). In this tutorial, we will focus on JavaScript with Selenium for web automation.

Table of Contents – Selenium JavaScript Automation Testing Tutorial For Beginners

  1. Intro to JavaScript and Selenium
  2. Prerequisites
  3. Setting up Node.js
  4. Installing a Package Manager and Adding a Path Variable
  5. Create and Run Automation Testing using Selenium JavaScript

Intro to JavaScript and Selenium?

  • JavaScript is a text-based programming language used both on the client-side and server-side, allowing you to transform static web pages into interactive ones.
  • WebDriver is a remote control interface that enables introspection and control of user agents. It provides a platform and language-neutral wire protocol as a way for out-of-process programs to remotely instruct the behavior of web browsers – WebDriver W3C Recommendation. The cross-platform behavior of web browsers is controlled by web driver. Different web browsers such as Chrome, Firefox, Edge, etc have their own implementation of WebDriver Standards. They all possess browser-specific functionalities using the standard APIs.
  • Selenium is an extra wrapper outside the WebDrivers in order to make the WebDriver implementation more smooth and easier for the supporting browsers. For example, if you have Chrome Driver installed on the machine with correct path, Selenium will automatically detect it. The same is the case with other browsers as well. It provides ease to the developers to run the same test script on multiple browser platforms.

Prerequisites

  • Node.js (with npm and node installed).
  • A package manager for handling open source JavaScript projects.
  • Any code editor (I will choose Visual Studio).
  • Any internet browser (I will be using Firefox in my case).

Setting up Node.js

Open the command prompt and type “node -v” (verifying Node.js is installed):

node -v

Then type “npm -v” (verifying npm is installed):

npm -v

If you are not getting these outputs, then you have to install node and npm manually. Click on the following link https://nodejs.org/en/download/ and it will take you to the Node.js official downloads page. Download the version according to the required system you are using. You will see the following page when you click the link given above.

node js installation

Installing a Package Manager and Adding a Path Variable

Now we are going to set up the package manager for our project which will be “selenium-web driver”. Just click on the link https://www.npmjs.com/. It will take you to the following page:

Package Manager

In the search bar type ‘selenium-web driver’ and hit enter. You will be taken to the next page where you will find selenium web drivers for different browsers:

selenium web drivers for different browsers

Click on Firefox (or whichever browser you would like to use). Then, you’ll see options for different kinds of operating systems you are working on. Simply select the one you are using:

Package Manager Operating Systems

Now we need to set up the path variable for our web driver. In order to do so, just unzip the file in your C directory inside the folder “seleniumWebdrivers” (you have to make a new folder) as seen below:

Selenium WebDriver

Copy the above path to your clipboard:

Selenium WebDriver

Then go to search and type “environmental variables”. You will see the following pop-up showing:

environmental variables

Click on it and you will have the following window opened:

environmental variables

Click on environmental variables and you will get to the following window:

environmental variables

Simply select the path and click on the edit button:

environmental variables

You will see this window appearing up:

environmental variables

Click on new and add the path you recently copied and click on the OK button.

Now, go to the folder in the C drive where you put the geckodriver.exe file and open it. You should see this type of output confirming that your path has been set correctly:

Setting the Path

Create and Run Automation Testing using Selenium JavaScript

Now that we’ve completed the dependencies part, it’s time to create a JavaScript project and write our first testing code. Go to any directory and create a new project by typing “npm init”:

npm init

Then, open the terminal in vs-code or in the command line and type “npm install selenium-webdriver” and hit enter. Open the package.json file, it should look like the following:

package.json file

Now, let’s create a new file with the name “index.js”. This is our time to start writing some code! 💪

Piece-wise code with explanation:

const {Builder,Buy,Key,util} = require("selenum-webdriver");

In the above Code we are simply pulling out functions from the node module.

async function example()

{

let driver = await new Builder.forBrowser("firefox").build();

Created the function with the name “example” and waiting for the browser to build and load properly.

await driver.get("http://google.com");

Trying to fetch http://google.com from the browser with our code.

await driver.findElement(By.name("q").sendKeys("Selenium",Key.RETURN));

Sending a search query by the name selenium.

Below is the complete code:

const {Builder,Buy,Key,util} = require("selenum-webdriver");

async function example()

{

let driver = await new Builder.forBrowser("firefox").build();

await driver.get("http://google.com");

await driver.findElement(By.name("q").sendKeys("Selenium",Key.RETURN));

}

example();

In order to run this, go to the terminal and type node index (name of the file in which you put the above code). You should be getting the desired output as below:

Selenium JavaScript Test

We’ve got our first Selenium JavaScript test up and running! 🌟
I invite you to dive even further to learn some Selenium with JavaScript best practices, and read more content on JavaScript and automation testing in this JavaScript category.

Avatar

About the author

Mujtaba Mehdi

Mujtaba is a Software Engineer from NUST. He was working with Telenor microfinance Bank before joining APIMatic. He’s been writing blogs & articles, conducting training & workshops related to Test Automation. He’s interested in hiking, football and likes to travel when he gets time.

Join TestProject Community

Get full access to the world's first cloud-based, open source friendly testing community. Enjoy TestProject's end-to-end test automation Platform, Forum, Blog and Docs - All for FREE.

Join Us Now  

Comments

7 3 comments
  • Avatar
    usman June 30, 2020, 10:25 am

    Thanks for the article. However, I am getting an error in the terminal as:
    Error: Cannot find module ‘selenum-webdriver’
    Can you please help?
    Thanks

    • Avatar
      usman July 2, 2020, 9:32 am

      I noticed it should not be “selenium-webdriver” instead of “selenum”??? (check the first line of given code “require(“selenum-webdriver”);”
      I used the word as “selenium-webdriver” but running index.js shows the following error:

      (node:21424) UnhandledPromiseRejectionWarning: TypeError: Builder.forBrowser is not a constructor

      Help is really appreciated.

Leave a Reply

popup image

Selenium for Teams

Sharing and distributing Selenium tests has never been so easy! With TestProject's FREE Selenium based platform, you can finally create awesome tests with the freedom to collaborate with your team effortlessly.
Sign Up Now right arrow
FacebookLinkedInTwitterEmail