How to pass parameters to the locator at runtime

I have a scenario in which one of the parameter value is changing as per the requirement.how to handle this case for andriod,iOS and selendriod.
Eg : public commonPage clickPinId(String pinNumber, int pinSize) {

driver.findElement(By.id(“ban.card.payanywhere:id/pin_”+pinNumber)).click();

return this;
}

The above example is for andriod.
In this the “pinNumber” variable in locator is changing,it can be 1,2,3,4…etc.
The above method works fine for Andriod but i want to have a common method for andriod,iOS and selendriod.i am not able to write such locators in @AndriodFindBy or @iOSFindBy

for iOS the locator is driver.findElement(By.xpath("//UIAButton[@label=‘0’]")).click();

please help me with this Case,bit urgent.Thanks in advance.

Hi Amita,

Firstly you can use ‘IF’ operator:

if(isAndroid){
driver.findElement(By.id(“ban.card.payanywhere:id/pin_”+pinNumber)).click()
}else {
driver.findElement(By.xpath("//UIAButton[@label=‘0’]")).click();
}

In way if you want have the same code you only can find element by text.
In this case try use methods:

driver.findElementByName(“text”);
driver.findElementByLinkText(“text”);

Hi Degard,
Thanks for the reply.
i am not able to find name or linktext for the element and the value is changing dynamically as per the requirement, as it cane be 123 or 344 …etc
regarding the if condition should i check which driver is instantiated,as i have lot of such changing locators,should i check the driver everytime while calling the method.

You can create static variable and initialise it when you create driver.
Then you can use it in all your project.

You mean you have pinNumber element in iOS ,Android, Android(API <17 … selendroid)

for android app its id is “ban.card.payanywhere:id/pin_”+“dynamicpinNumber”
for iOS app its xpath is //UIAButton[@label=‘0’]

In case u r only problem is dynamic id then u can use below xpath
driver.findElement(By.xpath("//*[starts-with(@id, ‘ban.card.payanywhere:id/pin_’)]")).click();

other wise u need to check driver.instanceOf since u r locators are different or one more method is

// Write this method in BasePage Class
import org.openqa.selenium.Platform;
public int platformCheck(){
Platform current = Platform.getCurrent();
if (current.is(Platform.WINDOWS)){ return 1;}
else if (current.is(Platform.MAC)){ return -1;}
}

public commonPage clickPinId(String pinNumber, int pinSize, int platform) {
if(platform > 0) { // iOS code goes here}
else {driver.findElement(By.id(“ban.card.payanywhere:id/pin_”+pinNumber)).click();}
return this;
}

In your method calling class where u call ‘clickPinId’ call it like this
CommonPageClassObj.clickPinId(“5757”, 4, int platform,platformCheck());

Hi All,

There is a way, instead of looking for platform(Android/IOS), you can direcly use type of driver object to make decision.

for example:

AppiumDriver driver=AndroidDriver(<>);
or 
AppiumDriver driver=IOSDriver(<>);

By loginButtonLocator=null;

if(driver instanceOf AndroidDriver){
   loginButtonLocator=MobileBy.AndroidUIAutomatior(<>);
}else{
   loginButtonLocator=MobileBy.AccesibilityId(<>);
}

driver.findElement(loginButtonLocator).click();

Thanks,
Priyank Shah

1 Like

Thanks everyone for the reply