How to do UI Mapping in Appium

I just started using Appium recently for mobile app testing and have a question related to UI mapping.

I would like to assign frequently used elements in my app to variables at the beginning of my test script so I can just use the variable without typing out the locator each time I need to use the element in my testing.
e.g.
//Elements are not available yet
IOSElement settingButton = driver.findElement(By.name(“settings”));
IOSElement doneButton = driver.findElement(By.name(“Done”));

//Elements are now available
settingButton.click();
doneButton.click();

However, to my knowledge, the moment it is declared the findElement method will try to find the element and stop the test if it is not found.
The elements I would like to map might not be available at the time of declaration.

So my question is, how do I map the elements?

Thanks in advance.

1 Like

Anyone got any tips?

You should write objects that represent each screen. Inside the screen object, only find for elements that exist in that screen.

So for your example, you would have a ‘settings’ element on a screen object you’ve instantiated. You click that. You transition to another screen (and instantiate it) that has a ‘done’ button.

Modeling your code after real life in an objective way would be much better than a jumble of elements at the top of a test script that keep growing until they are unmanageable (which is very quick, I might add).

look here: My I keep an Element instance and reuse it after some operations?

1 Like

@wreed @Aleksei

Thanks a ton! I rewrote my tests with PageFactory and PageObject pattern and the element declaration reside in their respective pages now.