简体   繁体   中英

How to make cucumber test steps run half steps only on Chrome and rest of the steps on different browsers?

My cucumber tests has multiple steps. For example a test like below:

Given I navigate to my setup application
And I published an article
When I open client website
Then I can see the published article

Step1 & Step2 are setup steps that need to run on a setup website and only on chrome once to create setup for scenario. And once it is done then step3 & Step4 needs to be check on different browsers/devices (IE,Chrome, Firefox, Safari, Mobile) on client website.

My framework is in Cucumber and JavaScript. How I can achieve this in cucumber that some steps of scenario run only on chrome once and rest of the steps should run on different devices and browsers ?

I believe i can't use Before and after hooks as setup steps differ for each test however, before and after hooks run for all the tests in the framework.Also, can't use background as again background will be background for all the tests in each feature file. But in my case , each test has different setup.

Why do you need your first two steps in this particular scenario to go through a browser. Surely you know that a published article is just a record in your datastore. Of course you probably have scenarios that show you can create the article using your web interface eg

Scenario: Create article
  Given I can create an article
  When I create an article
  Then I should have created an article

but once you have these you no longer need to go through your web interface each time you need an article creating. Instead you can shortcut the process either by

1) Calling directly the service that creates the article (preferred) 2) Writing directly to the database to create the appropriate article record

So your scenario would become something like

Scenario: View my published article
  Given I have published an article  # shortcuts and does not use a browser
  When I view my article             # uses a browser
  Then ...

Now your scenario only uses one browser and you problem has gone away. You can now use a variety of techniques to do ensure you can view articles in various browsers.

This pattern of shortcutting for Givens and only using the UI for When's has the added benefit of making your scenarios much faster. You can't use it easily for every Given, some things like 'Given I am logged in' need to go through the browser, but the vast majority of Givens can be shortcut.

Not sure if it is good idea, but this is what you want:

Scenario Outline: Multiple browsers
  Given I navigate to my setup application
  And I published an article
  When I open client website with "<browser>"
  Then I can see the published article with "<browser>"

Examples:
 | browser  |
 | IE       |
 | Chrome   |
 | Firefox  |
 | Safari   | 
 | Mobile   |

See https://cucumber.io/docs/guides/10-minute-tutorial/#using-variables-and-examples

In the step definition you must setup proper webdriver. Check https://github.com/bonigarcia/webdrivermanager

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM