简体   繁体   中英

How to use java switch statements in cucumber step definitions?

I have a feature file and corresponding step defs and both work as expected. Now, in the same project structure, I created a second feature file. Even before creating the step defs for the new file, cucumber told me that a matching step def exists for one of the steps in the new file, of course, the reason being that the step is worded same way as a step in the first feature, so cucumber refused to auto-generate a step def for that step, on the premise that an implementation already exists. The two steps are sharing the same regex (capture group) but the regex represents different URL parameters

My problem now is how to reuse that existing step def. I read that it's possible to use a java switch statement so that the step def can be used to represent the two feature steps but the author did not elaborate on how this can be achieved.

Feature file 1:

Scenario: Open shipping page
    When I select a course
    And I navigate to the shipping app
    Then the "shipping page" should open

Step Def:

@Then("^the \"([^\"]*)\" should open$") . //matching step def
  public void verifyShippingPage(String shippingPage) {
  shipping.verifyShippingPage(shippingPage);  //method call

  }

Feature file 2:

Scenario: Open planet page
    When I select a course
    And I navigate to the planet app
    Then the "planet page" should open   //step that wants to reuse the step def above

The shipping page and the planet page have different URLs, but share the same regex, \\"([^\\"]*)\\" . Can a switch statement be used to alter between the URLs or is there any way of achieving this task?

Yes, you could use a switch statement. , for instance like this:

@Then("^the \"([^\"]*)\" should open$")
public void verifyOnExpectedPage(String expectedPage) {
    switch (expectedPage) {
       case "shipping page":
          verifyShippingPage();
          break;  
       case "otherpage":  
          verifyOtherPage();
          break;
  }

I would recommend two things: 1. Use a default for when this step is used with a page this is not defined in the switch statement 2. In order to make it more visible which options are available, use capture groups like this (shipping page|other page) instead of a regex. In that case it will only match pages that are part of the capture group, making in visual in your feature file when you are using a page that is not (yet) defined.

So it would be more like this:

@Then("^the (shipping page|other page) should open$")
public void verifyOnExpectedPage(String expectedPage) {
    switch (expectedPage) {
       case "shipping page":
          verifyShippingPage();
          break;  
       case "otherpage":  
          verifyOtherPage();
          break;
       default:
           System.out.println("Unexpected page type");  
  }

Finally, another option would be to declare individual step definitions per page, like so:

@Then("^the shipping page should open$")
public void verifyShippingPage() {
     verifyShippingPage();
}

@Then("^the other page should open$")
public void verifyOtherPage() {
     verifyOtherPage();
}

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