0

I am using Java, Selenium Webdriver and Cucumber for my automation.In my feature file I have below statement :

And add a room '<x>'

enter image description here

The XML code for selecting rooms is shown in below image :

enter image description here

And the xpath is :

//div[@class='room-holder col-xs-6']/div[@class='group-of-rooms col-xs-24']/div/div[@class='list-group-item'][2]

How can I pass the parameter in above xpath in order to select the room?

I have tried using

@FindBy(xpath = "//div[@class='group-of-rooms col-xs-24']/div/div[@class='list-group-item']['+x+']")
    public WebElement RoomSelection;

And below method

public void addRoom(String x) {
        utils.waitForElements(RoomSelection, KnowsTestContext.timeout, KnowsTestContext.driver);
        RoomSelection.click();
    }

It always selects the first option. Is there any way to pass parameters defined in my step definition ?

2

As far as I can see you use Page Object way. This means you cannot parameterize the xpath for the element (parameters of annotations cannot be changed in runtime in Java). I would suggest you different approach. You define the list of RoomSelection

@FindBy(xpath = "//div[@class='group-of-rooms col-xs-24']/div/div[@class='list-group-item']")
public List<WebElement> roomSelection;

Then in your step definition you can do something like this:

public void addRoom(String x) {
    utils.waitForElements(roomSelection, KnowsTestContext.timeout, KnowsTestContext.driver);
    roomSelection.get(Integer.valueOf(x) - 1).click();
}

Here I add - 1 because I assume you start the numbering from 1 in your test data, however list index starts from 0.

You should also take care of different corner or exceptional cases. Fir example if no elements would be returned and list will left empty, you will get exception thrown.

1

Here is the solution for index variable:

Let's say, you have found 5 elements with same locator and you would like to perform action on each element by providing index number (Here, variable is used for index as "i")

for(int i=1; i<=5; i++)
{
    string xPathWithVariable = "(//div[@class='className'])" + "[" + i + "]";
    driver.FindElement(By.XPath(xPathWithVariable)).Click();
}

It takes XPath as below
(//div[@class='className'])[1]
(//div[@class='className'])[2]
(//div[@class='className'])[3]
(//div[@class='className'])[4]
(//div[@class='className'])[5]

I have verified and it's working fine.

0

I am not sure if I understood your question properly. I think you want the xpath for a specific element when the xpath returns a list of element, similar to extracting an element in an array by passing the index.

In this case, you are almost there but the problem you are facing is due to the preference of operator [x] against //.

Since // has lower preference, you need to wrap the expression in brackets and then pass the index. Below id the syntax.

(//div[@class='room-holder col-xs-6']//div[@class='list-group-item'])[2]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.