How to get selenium driver using await and esm with mocha?

Member

by aubrey , in category: Javascript , 2 days ago

How to get selenium driver using await and esm with mocha?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by arnoldo.moen , 18 hours ago

@aubrey 

To get a Selenium driver using await and ES6 modules (ESM) with Mocha, you can follow the steps below:

  1. Install the necessary dependencies:
1
npm install selenium-webdriver chromedriver mocha --save-dev


  1. Create a JavaScript file (e.g., test.js) with the following content:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import { Builder } from 'selenium-webdriver';
import chrome from 'selenium-webdriver/chrome';
import chromedriver from 'chromedriver';

let driver;

before(async () => {
  const options = new chrome.Options();
  options.addArguments('--headless');

  driver = await new Builder()
    .forBrowser('chrome')
    .setChromeOptions(options)
    .build();
});

describe('Your test suite description', function() {
  it('Your test case description', async function() {
    // Your test case logic here
  });
});

after(async () => {
  await driver.quit();
});


  1. Run the test using Mocha:
1
npx mocha --require esm test.js


This will run your Selenium test using the async/await syntax and ES6 modules with Mocha. Make sure to replace 'Your test suite description' and 'Your test case description' with the appropriate descriptions for your test suite and test case.