@FindBy annotation usage of locating one radio from a list of buttons

650 views Asked by At

I have a problem of locating one radio button from a list of buttons using @FindBy annotation.

I have a title option web elements with Mr Ms .. Dr as a radio button since they are located in the same div then the they have almost the same xpath or id apart from ending indexer, and I dont want to have 6 of @FindBy for every corresponding option of title

the solution that I'm looking for is when I use cucumber feature file with a step

Given Im 'Mr' Given Im 'Ms'

it will go to the page object and @FindBy(............) WebElement title .....

title.click //annotation will click the corresponding title

1

There are 1 answers

0
Kudin On

If you want to make a dynamic @FindBy then it's not possible, you can have dynamic By locator, or a String element in that By locator, but what you provide to the FindBy annotation must be immutable. So you should know what you are looking for exactly if you are using FindBy

It's hard to tell without a page source code example how to get to the radio button itself, but using xpath it can easily be achieved as most probably your radio buttons have their names or even ids set up, as a last resort you can search for a text within element. e.g:

"//input[@id='btnId']"   (in case of present id I would suggest using css selector "input#btnId")
"//label[contains(text(), 'Mr')]/input"