首页 > 解决方案 > Cucumber examples pulled through literally and not using the values - no such element: Unable to locate element:

问题描述

I have the following cucumber feature file:

Feature: When a user is not logged in they should be able to view and use the main menu navigation bar
 Scenario Outline: Navigate to the company site as a guest and interact with the main navigation bar
   Given I access "<url>" main landing page
   When I select the "<mainMenu>" tab
   Then the url should change
  
Examples:
|url                       |mainMenu
|https://www.website.com/  |live
|https://www.website.com/  |live games
|https://www.website.com/  |live sports

My Java code:

    @Given("^I access \"([^\"]*)\" main landing page$")
        public void i_access_something_main_landing_page(String url) throws Throwable , InterruptedException {
        getDriver().get(url);
    }

    @When("^I select the \"([^\"]*)\" tab$")
    public void i_select_the_something_tab(String mainmenu) throws Throwable {
        menuOptionWithoutSpace = mainmenu.toLowerCase().replaceAll("\\s","");
        driver.findElement(By.xpath("//a[@href='/"+ menuOptionWithoutSpace+ "']")).click();
        System.out.println("menuOptionWithoutSpace: " + menuOptionWithoutSpace);
    }

    @Then("^the url should change$")
    public void the_url_should_change() throws Throwable {
        String mainMenuUrl = driver.getCurrentUrl();
        Assert.assertTrue(mainMenuUrl.contains(menuOptionWithoutSpace));
    }

The test navigates to the correct url (meaning it gets the url from the feature file examples). However I get the following issue no such element: Unable to locate element: {"method":"xpath","selector":"//a[@href='/<mainmenu>']"} when it tries to access the mainMenu examples.

I have tried examples such as not using the " " and the \" I have also rewrote my scenario but I get the same error. I understand why it cant access <mainmenu> because there is no element with that name but I don't understand why it does not use the given values.

When I debug the value of <mainmenu> is <mainmenu> in IntelliJ

标签: javacucumber

解决方案


我犯了一个非常愚蠢的错误。看看我的数据表Examples,我忘了用另一个管道“关闭”表|

所以它应该是这样的:

Examples:
|url                       |mainMenu    |
|https://www.website.com/  |live        |
|https://www.website.com/  |live games  |
|https://www.website.com/  |live sports |

推荐阅读