Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Cucumber.js to describe browser behavior as readable scenarios, and Selenium WebDriver to control the browser that executes them. Cucumber explicitly says it is not itself a browser automation tool; the two work together when JavaScript step definitions translate each scenario step into awaited WebDriver commands and assertions.

This tutorial sets up a local Chrome test, runs it with Cucumber, waits for an observable page result, and closes the browser even when a step fails. Selenium’s current JavaScript API requires Node.js 22 or later.

How Cucumber.js and Selenium fit together

Cucumber-JS is the Node.js implementation of Cucumber, installed from the @cucumber/cucumber package. It reads scenarios written in Gherkin, matches each Given, When, and Then to JavaScript step definitions, and reports whether the steps pass.

Selenium’s selenium-webdriver package supplies the browser-control layer. Your step definitions use its WebDriver API to navigate, find elements, interact with the page, and inspect results. The browser-specific driver communicates with the browser; Selenium Manager can handle driver installation in the documented JavaScript quick-start path, though it cannot guarantee that every machine or CI environment has all the required network and browser conditions.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Elebase USB to USB C Adapter for iPhone 18 Pro Max,USBC Car Charger Adapter
  • Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
  • Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
  • Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
  • Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
  • 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.

The division is useful: keep user-facing behavior in the feature file and browser mechanics in JavaScript. Assert what a user can observe, such as a result heading or visible message, rather than coupling the scenario to internal implementation details.

What you need before setup

  • Node.js 22 or later, as required by the current Selenium JavaScript API.
  • npm, which is included with standard Node.js installations, or Yarn.
  • A browser available in the environment. The example below uses Chrome.
  • A project directory in which you can install development dependencies.

Requirements and CLI or configuration details can change between releases. Check the package documentation for the versions you install rather than assuming an example for Cucumber’s main branch applies to every published version.

Install Cucumber.js and Selenium WebDriver

From the project directory, initialize a package if you do not already have a package.json, then install both packages as development dependencies:

npm init -y
npm install --save-dev @cucumber/cucumber selenium-webdriver

Cucumber’s installation documentation recommends adding @cucumber/cucumber as a development dependency. Selenium documents npm install selenium-webdriver for its JavaScript binding.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #2
Anker USB-C Hub, 5-in-1 USB Hub for Laptops, 4K HDMI Multiport Adapter
  • 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
  • 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
  • Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
  • 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
  • What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.

Create a feature scenario

Create features/search.feature. This example uses a public search page and checks an observable outcome; replace the URL and selectors with those of an application you own or are authorized to test.

Feature: Search

  Scenario: A visitor searches for a term
    Given I am on the search page
    When I search for "Cucumber"
    Then I should see results for "Cucumber"

Feature files describe behavior in business-readable language. They do not start the browser or perform the actions themselves; matching JavaScript steps do that work.

Implement asynchronous browser steps

Create features/step_definitions/search.js:

const { Given, When, Then } = require('@cucumber/cucumber');
const { Builder, Browser, By, until } = require('selenium-webdriver');
const assert = require('node:assert/strict');

Given('I am on the search page', async function () {
  this.driver = await new Builder().forBrowser(Browser.CHROME).build();
  await this.driver.get('https://www.google.com/');
});

When('I search for {string}', async function (term) {
  const input = await this.driver.wait(
    until.elementLocated(By.name('q')),
    10000,
    'Search field was not found'
  );
  await input.sendKeys(term);
  await input.submit();
});

Then('I should see results for {string}', async function (term) {
  await this.driver.wait(
    until.titleContains(term),
    10000,
    'Page title did not contain the search term'
  );
  const title = await this.driver.getTitle();
  assert.ok(title.includes(term), `Expected title to include ${term}; got ${title}`);
});

The step functions are asynchronous because WebDriver operations return promises. Await navigation, element lookup, input, waits, and reads; otherwise a step may finish before the browser operation does. The explicit wait checks the condition that matters rather than treating a completed navigation or click as proof that dynamic content is ready.

This example uses a public site as a simple illustration, not a guarantee that its markup or behavior will remain unchanged. For a durable test, point the scenario at your own test environment and use selectors and expected results maintained by your team.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3
Sale
Anker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter
  • Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
  • Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
  • Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
  • Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
  • What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.

Run the scenario and ensure the browser closes

Configure Cucumber’s After hook so it closes each created session, including after a failed assertion. Create features/support/hooks.js:

const { After } = require('@cucumber/cucumber');

After(async function () {
  if (this.driver) {
    await this.driver.quit();
    this.driver = undefined;
  }
});

Run the feature from the project directory with:

npx cucumber-js

Cucumber discovers feature files and support code under the conventional features directory. If your project changes that layout or adds CLI configuration, check the installed version’s documentation for the appropriate options.

Use Cucumber World and hooks for scenario state

Cucumber provides a World instance for each scenario. In the example, assigning the driver to this.driver makes it available to the scenario’s later steps and teardown. Use regular function expressions when a hook or step needs the World as this; arrow functions bind their own lexical this and cannot access the World that way.

For larger suites, separate responsibilities deliberately:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #4
Sale
UGREEN USB to USB C Adapter Combo 4-Pack, 10Gbps USB C Converter Space Gray
  • Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
  • Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
  • Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
  • Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
  • Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft
  • Use a Before hook when every scenario needs common setup, such as creating a browser session.
  • Keep scenario-specific actions and assertions in named steps so the feature remains understandable.
  • Use an After hook to quit the session and release resources regardless of pass or failure.
  • Store per-scenario state on World rather than in shared module-level variables, which can leak state across scenarios.

Hooks are documented in the Cucumber-JS hooks guide.

Choose local, remote, and browser execution deliberately

Local browser

For a small development setup, new Builder().forBrowser(Browser.CHROME).build() requests a local Chrome session. Selenium’s JavaScript quick start uses this Builder pattern and demonstrates Selenium Manager in its documented startup path. Browser availability, permissions, and network access can still affect startup.

Browser selection

Selenium’s Builder lets the test choose a browser. Select the browser or browsers your product needs to support; there is no universal browser choice that is fastest or most reliable for every project. Cucumber’s browser guide also discusses selecting browsers for automation.

Remote server or Selenium Grid

If browser sessions run on a Selenium Grid or another standalone remote server, configure the WebDriver client to use that server instead of launching a local browser. Selenium documents the SELENIUM_REMOTE_URL environment variable and the usingServer() Builder method. Remote execution adds server configuration and maintenance; it is useful when the browser environment is intentionally hosted elsewhere, not a prerequisite for this local example.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value
Anker USB C Hub, 5-in-1 USBC to HDMI Splitter with 4K Display
  • 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
  • Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
  • Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
  • HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
  • What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Wait for the page state your test needs

Modern pages may render or update after navigation. A click returning successfully does not necessarily mean the next assertion can already pass. Wait for a meaningful condition, such as a specific element becoming visible, a title changing, or a result container appearing. Selenium provides wait conditions such as until.elementLocated and until.titleContains; Cucumber’s Selenium example likewise waits for an expected title condition.

Keep waits bounded and tied to the next observable state. A fixed delay can waste time when the page is fast and still fail when it is slower than expected. If a wait times out, inspect whether the expected condition is correct, whether the locator still matches, and whether the application actually reached that state.

Troubleshooting common failures

Package or runtime errors

  • Module not found: run the installation command from the directory containing the intended package.json, and confirm both packages appear in the project’s dependencies.
  • Unsupported Node.js version: use Node.js 22 or later for the current Selenium JavaScript API, then rerun the test.
  • cucumber-js is not found: run it through npx cucumber-js from the project directory after installing @cucumber/cucumber.

Browser startup or driver errors

  • Chrome cannot start: confirm Chrome is installed and usable by the account running the test; in CI, check the environment’s browser and display requirements.
  • Driver setup fails: Selenium Manager supports automatic driver installation in the documented quick-start route, but network restrictions, browser setup, or environment-specific constraints can still block it. Check connectivity and the installed browser, then consult Selenium’s setup documentation.
  • Wrong browser is selected: confirm the Builder’s browser choice and any SELENIUM_BROWSER environment setting.
  • Remote connection fails: verify the remote Selenium server URL and that the server is reachable; use SELENIUM_REMOTE_URL or Builder’s usingServer() as appropriate.

Step synchronization or assertion failures

  • Element not found: verify the locator against the current page and wait for the element to be located before interacting.
  • Timeout after an action: wait for the outcome of the action, not merely its completion. Check whether a redirect, asynchronous render, or changed selector explains the missing condition.
  • Browser remains open after failure: ensure the After hook is in the discovered support-code path and calls quit() for the driver created by that scenario.
  • World value is undefined: use a regular function for any step or hook that accesses this; arrow functions do not receive Cucumber’s World as their this.

Or skip the browser setup

If your goal is to capture a page rather than exercise it through a browser test, ScreenshotNeo is a website screenshot API and MCP server. One GET request returns a PNG, JPEG, WebP, or PDF. For example, this cURL request captures a page as WebP:

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

See the ScreenshotNeo API documentation for request options. Cookie banners and consent overlays, newsletter popups, and chat widgets are removed before capture; bot checks, blank pages, timeouts, failed loads, and cache hits are not billed. Its MCP server exposes screenshot and page-information tools for AI agents. The free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 screenshots.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Sign up for ScreenshotNeo’s free plan to get 1,000 screenshots a month with no card.

Further reading

Frequently Asked Questions

Can Cucumber.js automate a browser by itself?

No. Cucumber maps readable scenarios to step definitions; a browser automation library such as Selenium performs the browser actions.

Can I run the same Cucumber scenarios against more than one browser?

Yes. Select a browser through Selenium’s Builder and run the scenarios in the browser environments your project supports.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.