Write and run test scripts in Selenium with JavaScript
Creating First Test
- Go to VS Code.
- Open Selenium Demo Project in VS Code
- Check if package.json file is available
- Check if node_modules has all the npm packages.
- Create a new directory or folder in Selenium Demo Project. Name is as tests.
- We will keep all our test files in this directory or folder
- 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
- We can run our test using command in the terminal node tests/firstTest.js
- 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
- Writing our first Test Script
- Running our First Test Script