Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
319 views
in Technique[技术] by (71.8m points)

java - 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I made a very silly mistake. Look at the data table in my Examples, I forgot to "close" the table with another pipe |.

So it should look like this:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...