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

Leave a Comment