How to test that a substring, but not a superstring, is present using Cucumber web steps?

StackOverflow https://stackoverflow.com/questions/23600170

  •  20-07-2023
  •  | 
  •  

Frage

I understand that in Cucumber we can use regular expressions in the step definitions, but is there a way for me to use them within Gherkin?

The reason for this is so that I can use the cucumber websteps and keep my code as concise as possible.

Let's say I want to test whether a string is in one of these web pages:

Page 1

hey there

Page 2

hey there!

And I have this step:

Then I should see "hey there"

it passes for both pages. If I have this step:

Then I should see "hey there!"

then it only passes for the second page. But I want a step that passes only on the first page. Is there a way in Gherkin to tell it to not go to check if it goes to the end of the line, or do I have to suck it up and make my own step?

War es hilfreich?

Lösung

Answer for my own question:

You can't add reg ex's within Gherkin.

It sucks, but its the truth. BUT you can add a "not" :D

Here's the solution to searching for hey there in two instances when you'll have hey there! and hey there in two different files.

Here's one file:

hey there

Here's the second file:

hey there!

Here's one web step:

Scenario: See "hey there" in file 1
  Given that I am on "file 1"
  Then I should see "hey there"

When testing when you want to see "hey there" in file one, it returns true. Here's where it got complicated:

Scenario: Should not see "hey there" in file 2
  Given that I am on "file 2"
  Then I should not see "hey there"

Scenario for file 2 fails because it does see "hey there" in file two. To get around this:

Scenario: See not see "hey there!" in file 1
  Given that I am on "file 1"
  then I should not see "hey there!"

Scenario: Should see "hey there!" in file 2
  Given that I am on "file 2"
  Then I should see "hey there!"

Solution in Short: Just search for the longer form or, if your file permits it, search for other unique text. Small refactor saving ten seconds of work: fixed.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top