Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Introduction to Selenium | Browser Automation with Selenium
Introduction to QA Automation Testing

book
Introduction to Selenium

So far, we were writing Unit Tests to test the functionality of units of code, however, for testing broader features like the login and registration system, we can no longer employ unit testing, as it is no longer a single unit, rather, it is a combination of multiple units and components of the program working together to form a feature.

One way to test features like the login system is to manually use the website and verify the results; however that would be considered as manual testing. There are tools which help us automate the browsers and perform various operations like visiting the website, inputting data, and interacting with elements. These tools can be used along with a test framework like Jest or Mocha, to perform such tests.

The following video introduces one of such tools along with a tutorial on how to integrate it with our Node application.

Code

Note that it is not important to analyze and understand the code used in this chapter. Most of the functions and methods in this code will be explored in the upcoming chapters.

js
const { Builder, By, Key, until } = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');
const path = require('path');

// Helper function to wait for a specified number of milliseconds
const sleep = (milliseconds) => {
return new Promise(resolve => setTimeout(resolve, milliseconds));
};

(async () => {
let chromeDriverPath = path.resolve('path/to/webdriver.exe');

let service = new chrome.ServiceBuilder(chromeDriverPath);

let options = new chrome.Options();
options.addArguments('--disable-search-engine-choice-screen');
options.addArguments('--lang=en-GB');

let driver = await new Builder()
.forBrowser('chrome')
.setChromeService(service)
.setChromeOptions(options)
.build();

try {
await driver.get('https://www.google.com');

let btn_xpath = '/html/body/div[2]/div[2]/div[3]/span/div/div/div/div[3]/div[1]/button[2]'
await driver.wait(until.elementLocated(By.xpath(btn_xpath)), 5000);
let button = await driver.findElement(By.xpath(btn_xpath));
await button.click();

let searchBox = await driver.findElement(By.name('q'));
await searchBox.sendKeys('cats', Key.RETURN);

await driver.wait(until.elementLocated(By.linkText('Images')), 5000);

let imagesLink = await driver.findElement(By.linkText('Images'));
await imagesLink.click();

await sleep(10000);
} finally {
await driver.quit();
}
})();

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 1

Kysy tekoälyä

expand
ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

We use cookies to make your experience better!
some-alt