Coding approach in Appium

Hi,

I am new to appium learning and trying to write simple code for test automation. One query i have is that when I try to, say, automate the dialing of a number from phone, i can either use layouts like this:

driver=new AppiumDriver(new URL(“http://127.0.0.1:4723/wd/hub”),capabilites);
WebElement linearLayout=driver.findElement(By.id(“com.android.contacts:id/dialpad”));
linearLayout.findElement(By.id(“com.android.contacts:id/nine”)).click();

or I can simply call find element function from AppiumDriver objects like this:

driver=new AppiumDriver(new URL(“http://127.0.0.1:4723/wd/hub”),capabilities); dr.findElement(By.id(“com.android.dialer:id/one”)).click();

My question is that which approach is better in terms of coding best practices? How does use of elements like layout make a difference while writing tests like these?

Exposing the underlying uiautomator code limits your tests to Android only. That may be your goal, but it makes the code less readable. I prefer your second example. I might be tempted to create constants such as DIALER_ONE to replace the id to make it even more readable.

Actually, he is not exposing uiautomator code. He simply named his WebElement linearLayout and then searched its child elements. He could have named it Dialpad as well. Creating WebElements and giving them simple names makes the code more readable, because you don’t have to write the findElementBy… part each time you interact with a given element.

dr.findElement(By.id("com.android.dialer:id/one")).click();

becomes

One.click();

if you previously created a “One” WebElement.