Selenium with Javascript – First Test Script

Write and run test scripts in Selenium with JavaScript

Creating First Test

  1. Go to VS Code.
  2. Open Selenium Demo Project in VS Code
  3. Check if package.json file is available
  4. Check if node_modules has all the npm packages.
  5. Create a new directory or folder in Selenium Demo Project. Name is as tests.
  6. We will keep all our test files in this directory or folder
  7. Create a new file in tests folder. Name it as firstTest.js

We should have a little bit javascript knowledge.

Now, Let’s write our first test script. In this first test script, We will only see, How can we launch a Browser and How can we navigate to our applicate in the browser using Selenium with Javascript.

const {Builder, Browser} = require('selenium-webdriver');

(async function example() {

//launch the browser
  let driver = await new Builder().forBrowser(Browser.FIREFOX).build();

 //navigate to the application
    await driver.get('https://www.google.com/ncr');
  
//close the browser
    await driver.quit();
  
})();
const { Builder, Browser } = require("selenium-webdriver");


(async function example1() {

//launch the browser
let driver = await new Builder().forBrowser(Browser.FIREFOX).build();

//navigate to the application
await driver.get("https://qpdemoecommerce.com");


//close the browser
await driver.quit();

})();

Running Our First Test

  1. We can run our test using command in the terminal node tests/firstTest.js
  2. We can find a run button in vscode and use that to run our code, If the button is not visible, then we can install code runner extension, and we will find the button.

Summary

We have covered below topics in today’s session

  1. Writing our first Test Script
  2. Running our First Test Script
Admin

Working Professional with more than 12 Years of Experience in Software Testing with Automation and Manual Testing Knowledge.

Recent Posts

Objects, Classes and Constructor in Javascript

Objects, Classes and Constructor in Javascript Classes and Objects A class in any programming language…

55 years ago

OOPs in Javascript

We will start from object oriented programming basics like What are different concepts in Object…

55 years ago

Why we can not over ride Constructor

Why we can not over ride a Constructor This is a very important question, that,…

55 years ago

Javascript Tutorial – Arrow Function in Javascript

What is Arrow Function In the last session, We had discussed about Anonymous Function. Anonymous…

55 years ago

Javascript Tutorial – Anonymous Function in Javascript

What is Anonymous Function Anonymous Function is a type of function which has no name…

55 years ago

Javascript Tutorial – Map, Filter and Reduce

What is Map, Reduce and Filter Map Filter and Reduce are higher order methods for…

55 years ago