tagged: selenium

Javascript: Selenium setup in NodeJS with Firefox and ES6 modules

First install npm install selenium-webdriver. I'm on 4.0.0.
Then import Selenium with ES6 modules. We'll rename them since they're not built with ES6 modules in mind.

import * as webdriver from 'selenium-webdriver'
import * as firefox from 'selenium-webdriver/firefox'
let ff = firefox.default; 
let wd = webdriver.default

Then tell Selenium where your Firefox binary is and create the selenium instance.

let options = new ff.Options().setBinary('/usr/bin/firefox')
let browser = new wd.Builder().forBrowser('firefox').setFirefoxOptions(options).build()

Selenium will respond with promises. This is a little annoying so let's use an async function and use await.

async function test() {
  await browser.get("http://localhost:8080/")
  let next = await browser.findElement(wd.By.linkText("next"))
  await next.click()
  let pageSource = await browser.getPageSource()
  let hasPage = pageSource.indexOf("Page 2 of 2") != -1
  assert.equal(hasPage, true, "Checking if we're on page 2")

Finally we use the findElement method, once we've loaded a page, and then use the By class to find the link text. Then we click on it.
We then get the page source and do some string searching on that to use a normal nodejs assert statement.

javascript selenium nodejs

Using HtmlUnit, Selenium and Bootstrap

If you using Selenium's HtmlUnit driver on a website with Bootstrap, you must disable Javascript support.

HtmlUnitDriver driver = new HtmlUnitDriver(false);

Otherwise you'll find this exception

org.openqa.selenium.ElementNotVisibleException: You may only interact with visible elements

This is because HtmlUnit's Javascript support is patchy and doesn't seem to work well with Bootstrap's Javascript.
(I'm using HtmlUnit 2.12)
There may be a way around this to do with jQuery versions, I've read.
But if your website works without Javascript, then just testing without Javascript may be a solution.
Otherwise, PhantomJs seems to be another solution.

java bootstrap java-testing selenium

Page 1 of 1