Skip to content

Instantly share code, notes, and snippets.

@elisemoe
Last active July 17, 2021 00:27
Show Gist options
  • Save elisemoe/69c2829a87d10b233a5b to your computer and use it in GitHub Desktop.
Save elisemoe/69c2829a87d10b233a5b to your computer and use it in GitHub Desktop.
Cucumber Step Definition - Useful Code and Comments

//scroll to the bottom of the page

((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight)");

Note: Do not upgrade Firefox browser version later than 40 - not compatible with latest Selenium Firefox Driver

Regex for a booking number: ^Ocean #BK+[0-9]{8}


Gherkin Keywords

* Feature
* Background
* Scenario
* Given
* When
* Then
* And
* But
* *
* Scenario Outline
* Examples

Gherkin

\d - stands for digit, or [0-9]

\w - stands for word character, specifically [A-Za-z0-9_]. Note: underscores and digits are included but not hyphens.

\s - stands for white space character, specifically [\t\r\n]. That means a space, a tab, or a line break.

\b - stands for workd boundary, which is a lot like \s but actually means the opposite of \w. Anything that is not a word character is a word boundary.

    • the plus modifier. Any number of times, at least once.
    • the star modifier. Any number of times including zero.

? - the question mark modifier makes the preceding character optional. It means zero or one times.

ex./ @Given("I have (\d+) cucumbers? in my basket) will match 'I have 1 cucumber in my basket' OR 'I have 256 cucumbers in my basket'

^ and $ are anchor characters


Feature Template

In order to

As a

I was

Each scenario must make sense and be able to be executed independently of any other scenario.

--

Cucumber

Test Results:

  • Failed - if a block of code raises an exception.
  • Pending - a step definition that's halfway through being implemented. Throws PendingException
  • Undefined - no step definition that matches a step
  • Skipped
  • Passed

Background

a background section in a feature file allows you to specify a set of steps that are common to every scenario in a file.

Data Tables

The table is technically a List of Lists of strings: List<List>

Scenario Outline

These are used for repetative scenarios that follow the same pattern with a different set of data

Cucumber Overrides

Hierarchy

1. The OS environment variable CUCMBER_OPTIONS

2. The Java system property cucmber.options

3. The Java resource bundle cucmber.properties with a cucmber.options property

* Overrides are provided in a variable (or property) called cucumber.options or CUCUMBER_OPTIONS. Cucumber searches for this 	variable in the above order
* Cucumber will not continue to look for other overrides once it has found one
* Except for the plugin argument, values found in the override replace any values already set.
* The plugin argument is the only one which Cucmber provides a default - if no plugin is specified, then Cucmber will always use the progress plugin to output to STDOUT.

the --glue option tells Cucumber which package(s) our glue code is in, and if there are step definitions in them (or hooks), they get loaded. Cucumber treats the packages you specify as a root from which to scan for glue code.

You can use the NOT operator (~) in @CucumberOptions to exclude a tag.

Selenium


notes

--dry-run switch tells Cucumber to parse the file without executing it. It will tell you if your Gherkin is valid. --strict command-line option will return an exit code of 1 (to indicate an error) if there are any undefined or pending steps. Useful in CI to detect half built features that have been checked in or when you've refactored your step definitions and some of your steps no longer match.

Reusing browsers is ok (reduces speed) as long as you clear all cookies and browser cache

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment