WebDriver - how to pass object locator as parameter in data driven test?

4.7k views Asked by At

I'm wondering if it's possible to pass locator as parameter in data driven test? For example:

    //this is non-parameterized object RADIO_BUTTON locator

    WebElement radiobElm = driver.findElement(RADIO_BUTTON);
    radiobElm.click();

    vs. 

    //I'd like to pass locator "RADIO_BUTTON" as string (strRadioButton) from Excel sheet, so for each test iteration my script will click on different radio-buttons. Is it possible? 

    WebElement radiobElm = driver.findElement(strRadioButton);
    radioElm.click();
2

There are 2 answers

1
Saifur On BEST ANSWER

One of the many ways to accomplish this would be to use same method to find elements and read the locator from excel file.

 WebElement radiobElm = driver.findElement(By.xpath("Your xpath string from exel"));

Of course you have to do all of other works to read the correct cell from excel.

0
Subh On

Yes you can. You just have to get the data from the excel (using jexcelapi's getContents() method, for instance). Then, you can manipulate them accordingly as they come.

1- If it's an xpath, use this:

WebElement radiobElm = driver.findElement(By.xpath("Cell contents containing xpath of the webelement"));
radioElm.click();

2- If it's an id, use this:

WebElement radiobElm = driver.findElement(By.id("Cell contents containing id of the webelement"));
radioElm.click();

3- If it's an cssSelector, use this:

WebElement radiobElm = driver.findElement(By.cssSelector("Cell contents containing cssSelector of the webelement"));
radioElm.click();

Similar thing, can be applied for "className, name, linkText, partialLinkText, and tagName" also.