logo logo

Selenium JavaScript Automation Testing Tutorial For Beginners

Selenium JavaScript Automation Testing Tutorial For Beginners

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.

📍 If you are looking for an all-in-one test automation framework for web & mobile that supports JavaScript – there is also an easy open source solution provided by TestProject. The free platform recently introduced its OpenSDK which opens a whole new chapter for supporting developers as well as testers in their work. By using OpenSDK you can easily develop your native Selenium & Appium tests (or plug in any of your existing tests), while benefiting from built-in FREE dashboards, local test reports & automated modern CI flows.

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.


  • 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));



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.


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  


7 9 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?

  • Avatar
    SridharK August 18, 2020, 4:43 pm

    Hi All
    There is a requirement in our project. I should start automating the test cases/scenarios using Selenium webdriver and JavaScript. I searched over the web. Not getting much useful information.
    Can any one share the documentation around Selenium with JavaScript. with which one can learn and get to advanced level.
    Thanks in advance.

  • Avatar
    Jugad Singh October 3, 2020, 5:13 am

    Thanks for this helpful page, but I had to create an account to point out that there are serious errors in your code

    Line 1:
    Buy -> By
    selenum -> selenium
    no need to import util

    Line 7:
    Builder -> Builder()

    Line 11:
    By.name(“q”).sendKeys -> (By.name(“q”)).sendKeys

    Please fix the code, unless its intended as a debugging exercise for the readers. 😄

  • Avatar
    Randall Ward April 4, 2022, 3:59 pm

    While it’s minor you should change the step where you type in ‘selenium–web driver’ to ‘selenium-wbdriver’ as novices could stumble on this part and not find the next steps as the search result will be something completely different.

Leave a Reply

popup image

Become a master for Selenium testing

Getting started with web testing? We’ve got you covered with a FREE ready-to-go test automation platform that’s already bundled up with Selenium to simplifying and enhancing your experience.
Get Started