首页 > 解决方案 > 黄瓜 AmbiguousStepDefinitionsException Java

问题描述

在以下情况下,我得到了一个 AmbiguousStepDefinitionsException,我不明白如何解决这个问题。请帮忙!

设想

  Scenario Outline: Testing the price ordering filter
    When the <priceOrder> filter is clicked
    Then prices are ordered by <priceOrder>
    Examples:
    |  priceOrder |
    |  ascending  |
    |  descending |

  Scenario Outline: Testing the stars hotel filter
    When the <star> stars hotel filter is clicked
    Then all hotels are <star> stars
    Examples:
    |  star  |
    |    1   |
    |    2   |
    |    3   |
    |    4   |
    |    5   |

步骤文件

    @When("^the (.*?) filter is clicked$")
    public void thePriceOrderFilterIsClicked(String priceOrder) {
        hotelPage.activatePriceFilter(priceOrder);
    }

    @When("^the (\\d+) stars hotel filter is clicked$")
    public void theStarStarsHotelFilterIsClicked(int star) {
        hotelPage.activateStarsFilter(String.valueOf(star));
    }

    @Then("^all hotels are (\\d+) stars$")
    public void allHotelsAreStarStars(int star) throws InterruptedException {
        hotelPage.checkHotelStars(String.valueOf(star));
    }

错误

cucumber.runtime.AmbiguousStepDefinitionsException: ✽.When the 5 stars hotel filter is clicked(hotelSearches.feature:16) matches more than one step definition:
  ^the (.*?) filter is clicked$ in HotelSearchesSteps.thePriceOrderFilterIsClicked(String)
  ^the (\d+) stars hotel filter is clicked$ in HotelSearchesSteps.theStarStarsHotelFilterIsClicked(int)

任何想法?谢谢!

标签: javaregexautomationcucumber

解决方案


您使用This pattern ^the (.*?) filter is clicked$匹配的模式,直到第一次出现将匹配两个场景的过滤器。

如果您希望匹配第一个场景the <priceOrder> filter is clicked并且<priceOrder>可以例如升序或降序,您可以匹配 1+ word characters (\w+),仅小写字符([a-z]+)或使用交替(ascending|descending)来使其具体化。

例如保留捕获组:

the (\w+) filter is clicked

推荐阅读