WebdriverIO Selenium Grid Examples

The selenium grid WebdriverIO examples below can quickly be run on a free Gridlastic selenium grid.

The Gridlastic grid hostname, credentials and video url used in these code examples are presented after launching your Gridlastic selenium grid.

Get a free account and launch your Gridlastic selenium grid, then run this code locally and test your grid!

wdio.conf.ts

// STARTING WITH WEBDRIVERIO V9, GRID CREDENTIALS CANNOT BE PASSED IN VIA HOST URL
import { Buffer } from 'buffer';

const GRIDLASTIC_GRID_HOSTNAME = ''; \\ LIKE: subdomain-hub.gridlastic.com
const GRIDLASTIC_GRID_USERNAME = '';
const GRIDLASTIC_GRID_PASSWORD = '';
const GRIDLASTIC_GRID_HUB_PROXY = ''; // like: your_subdomain-hub.gridlastic.com:8001
global.GRIDLASTIC_GRID_VIDEO_URL = ''; // like: https://s3-us-west-1.amazonaws.com/023a15f2-534d-31e5-f8cc-7ceaf6355377/238a51b7-c526-ceb8-8ffd-1759b612a464/play.html?

const gridlastic_grid_credentials = `${GRIDLASTIC_GRID_USERNAME}:${GRIDLASTIC_GRID_PASSWORD}`;
const gridlastic_grid_encodedCredentials = Buffer.from(gridlastic_grid_credentials).toString('base64');

export const config: WebdriverIO.Config = {
    // ...
    logLevel: 'error',
    maxInstances: 10,
    headers: {
            Authorization: `Basic ${gridlastic_grid_encodedCredentials}`
        },
    protocol: 'https',
    hostname: GRIDLASTIC_GRID_HOSTNAME,
    port: 443,
    path: '/wd/hub',
    // ...
    capabilities: [{
        browserName: 'chrome', // can be chrome, MicrosoftEdge, firefox
        browserVersion: 'latest',
        platformName: 'WIN11', // can be WIN11, WIN10 or LINUX
        'gridlastic:options': {
            video: true // default is false
        },
        webSocketUrl: true,
        /*
        proxy: {
            proxyType: "manual",
            httpProxy: GRIDLASTIC_GRID_HUB_PROXY,
            ftpProxy: GRIDLASTIC_GRID_HUB_PROXY,
            sslProxy: GRIDLASTIC_GRID_HUB_PROXY,
            noProxy: "none"
              }
        */
    }],
     specs: [
         './test/specs/**/*.js'
     ],
     connectionRetryTimeout: 600000, // 10 minutes max to wait for grid nodes to spin up which can take several minutes.
     waitforTimeout: 30000,
     mochaOpts: {
             ui: 'bdd',
             timeout: 60000
         },
}

example.js

describe('WebdriverIO Example', () => {

    after(async () => {
        console.log('Video: ' + GRIDLASTIC_GRID_VIDEO_URL + driver.sessionId);
    })

    it('Check title', async () => {
        await browser.url('https://www.gridlastic.com/?demo')
        await browser.maximizeWindow()
        await expect(browser).toHaveTitle('Selenium grid')
        await browser.pause(10000); // pause for demo purposes
    })
/*
    it('Check that IP is the same as the selenium grid hub IP', async () => {
        await browser.url('https://www.whatismybrowser.com/')
        await browser.maximizeWindow()
        await browser.pause(5000); // pause for demo purposes
        await browser.scroll(0, 1600)
        await browser.pause(5000); // pause for demo purposes
    })
*/
})

bidi-geolocation.js

describe('BiDi geolocation example', () => {

    after(async () => {
        console.log('Video: ' + GRIDLASTIC_GRID_VIDEO_URL + driver.sessionId);
    })

    it('should find my emulated geo location', async () => {
        await browser.emulate('geolocation', { // Paris @48.8587895,2.2975715,16z
            latitude: 48.8587895,
            longitude: 2.2975715,
            accuracy: 300
        })
        await browser.url('https://www.google.com/maps')
        const btnMyLocation = await browser.$('#mylocation')
        await btnMyLocation.waitForClickable()
        await btnMyLocation.click()
        console.log(await browser.getUrl())
        await expect(browser).toHaveUrl(
            expect.stringContaining('@48.8587895,2.2975715,16z?')
        )
        await browser.maximizeWindow()
        await browser.pause(10000); // pause for demo purposes
    })
})

package.json

{
  "name": "selenium-grid-tests",
  "version": "1.0.0",
  "dependencies": {
    "assert": "2.1.0",
    "selenium-webdriver": "4.28.1"
  },
  "devDependencies": {
    "@wdio/cli": "^9.7.2",
    "@wdio/local-runner": "^9.7.2",
    "@wdio/mocha-framework": "^9.7.2",
    "@wdio/spec-reporter": "^9.6.3",
    "mocha": "11.0.1"
  },
  "scripts": {
    "wdio": "wdio run ./wdio.conf.ts"
  }
}