Testing - SE-TINF22B6/CookHub GitHub Wiki

Selenium Webdriver

Selenium is a browser automation library. Most often used for testing web-applications, Selenium may be used for any task that requires automating interaction with the browser.

Official Website: https://www.selenium.dev/documentation/webdriver/

Instructions: https://www.npmjs.com/package/selenium-webdriver


1 ⇨ Setup with npm

Install the Selenium Webdriver via npm in the path:
C:\Users<user>\IdeaProjects\CookHub>

npm install selenium-webdriver

2 ⇨ Additional Requirements: 2.1 | 2.2

You will need to download additional components to work with each of the major browsers. The drivers for Chrome, Firefox, and Microsoft's IE and Edge web browsers are all standalone executables that should be placed on your system path.


2.1 ⇨ Installation with WebDriver Manager (recommended)

The WebDriver-Manager is an open source tool which automatically downloads the newest version of WebDriver for the required browser. To install go to the path: C:\Users<user>\IdeaProjects\CookHub>

npm install --save-dev webdriver-manager

Install newest chrome WebDriver:

npx webdriver-manager update --chrome=true

Install newest firefox WebDriver:

npx webdriver-manager update --gecko=true


2.2 ⇨ Installation manually

Browser Component
Chrome chromedriver(.exe)
Internet Explorer IEDriverServer.exe
Edge MicrosoftWebDriver.msi
Firefox geckodriver(.exe)

3 ⇨ Usage

3.1 ⇨ Example

const { Builder, Browser, By, Key, until } = require('selenium-webdriver')

;(async function example() {
  let driver = await new Builder().forBrowser(Browser.FIREFOX).build()
  try {
    await driver.get('https://www.google.com/ncr')
    await driver.findElement(By.name('q')).sendKeys('webdriver', Key.RETURN)
    await driver.wait(until.titleIs('webdriver - Google Search'), 1000)
  } finally {
    await driver.quit()
  }
})()

3.2 ⇨ Using the Builder API

The Builder class is your one-stop shop for configuring new WebDriver instances. Rather than clutter your code with branches for the various browsers, the builder lets you set all options in one flow. When you call Builder#build(), all options irrelevant to the selected browser are dropped:

const webdriver = require('selenium-webdriver')
const chrome = require('selenium-webdriver/chrome')
const firefox = require('selenium-webdriver/firefox')

let driver = new webdriver.Builder()
  .forBrowser(webdriver.Browser.FIREFOX)
  .setChromeOptions(/* ... */)
  .setFirefoxOptions(/* ... */)
  .build()
⚠️ **GitHub.com Fallback** ⚠️