I'm also automating app for both Android and iOS using Appium and Python and I've had same problem, apps are same but of course locators are different.
In order to reuse same method but with different locators I've came up with this solution, example:
class Header(Page):
def __init__(self, driver):
super(Header, self).__init__(driver)
self.os = str(self.driver.desired_capabilities['platformName']).lower()
# Android
login_button_android = (MobileBy.ID, 'com.matchbook.client:id/loginButton')
#iOS
login_button_ios = (MobileBy.ACCESSIBILITY_ID, 'LOGIN')
def open_login_page()
self.driver.find_element(*getattr(self, 'login_button_' + self.os)).click()
When you now call open_login_page
from outside class it will know which locator to use (Android or iOS) as we retrieved OS with
self.os =str(self.driver.desired_capabilities['platformName']).lower()
in the __init__
method.
On execution 'login_button_' + self.os
will become either 'login_button_android'
or 'login_button_ios'
.
This way you only need 1 method for any number of OS versions, the only difference is in the name of locator variable, they need to have same name except the suffix at the end which should be _android
or _ios
.