【问题标题】:cucumber AmbiguousStepDefinitionsException Java黄瓜 AmbiguousStepDefinitionsException Java
【发布时间】:2019-12-29 11:04:44
【问题描述】:

在以下情况下,我收到了 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)

有什么想法吗?谢谢!

【问题讨论】:

  • 此模式^the (.*?) filter is clicked$ 将匹配两种方案。您也许可以使第一个模式不那么通用the (\w+) filter is clicked

标签: java regex automation cucumber


【解决方案1】:

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

如果您希望匹配the &lt;priceOrder&gt; filter is clicked 的第一个场景并且&lt;priceOrder&gt; 可以例如升序或降序,您可以匹配1+ 个单词字符(\w+),仅匹配小写字符([a-z]+) 或使用替代(ascending|descending)使其具体化。

例如保留捕获组:

the (\w+) filter is clicked

【讨论】:

    【解决方案2】:

    你需要修改小黄瓜步骤和对应的步骤定义

    1)

    From:  When the <priceOrder> filter is clicked
    To:    When the "<priceOrder>" filter is clicked
    

    步骤定义为:

    @When("^the \"([^\"]*)\" filter is clicked$")
    public void theFilterIsClicked(String arg0)  {
        // Write code here that turns the phrase above into concrete actions
    }
    

    2)

    From: Then prices are ordered by <priceOrder>
    To:   Then prices are ordered by "<priceOrder>"
    

    步骤定义为:

    @Then("^prices are ordered by \"([^\"]*)\"$")
    public void pricesAreOrderedBy(String arg0)  {
        // Write code here that turns the phrase above into concrete actions
    }
    

    3)

    From:  When the <star> stars hotel filter is clicked
    To:    When the "<star>" stars hotel filter is clicked
    

    步骤定义为:

    @When("^the \"([^\"]*)\" stars hotel filter is clicked$")
    public void theStarsHotelFilterIsClicked(String arg0) {
        // Write code here that turns the phrase above into concrete actions
    }
    

    4)

    From: Then all hotels are <star> stars
    To:   Then all hotels are "<star>" stars
    

    步骤定义为:

    @Then("^all hotels are \"([^\"]*)\" stars$")
    public void allHotelsAreStars(String arg0)  {
        // Write code here that turns the phrase above into concrete actions
    }
    

    【讨论】:

      猜你喜欢
      • 2019-12-28
      • 2016-07-13
      • 2017-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多