I am automating an android app and have capabilities method in setup class file and returning driver instance from that method.
I have one class file for one of my pages where I am calling that capabilities method and using driver instance to locating elements, although I am not able to use that driver instance in other class file. what will be best approach to do that?
This should be done with OOP.
You should define a main class for creating a driver instance and define creating a driver instance in it. Then inherit other classes from the main class.
Assuming we call it MainClass
. Then we should automate two sections of our app like Register
and Login
. It should implement like this: (Python)
MainClass.py
from appium import webdriver
class MainClass:
def __init__(self):
self.driver_instance = None
def open_application(self, udid=None):
caps = {
'platformName': 'Android', 'deviceName': Galaxy A7, 'automationName': 'UiAutomator2',
'skipServerInstallation': True, 'appActivity': 'YOUR_APP_START_ACTIVITY', 'noReset': no_reset,
'udid': udid, 'newCommandTimeout': 1200, 'autoGrantPermissions': True,
'appPackage': 'YOUR_APP_PACKAGE_NAME'}
driver = webdriver.Remote(str(appium_server), caps)
self.driver_instance = driver
return driver
MyApplication.py
from MainClass import MainClass
class MyApplication(MainClass)
def __init__(self):
super().__init__()
def open_my_application(udid=None):
self.open_application()
Authenticate.py
from MainClass import MainClass
class Authenticate(MainClass)
def __init__(self):
super().__init__()
def login(self, username, password):
self.driver_instance.find_element_by_xpath("//input[1]").set_text(username)
self.driver_instance.find_element_by_xpath("//input[2]").set_text(password)
self.driver_instance.find_element_by_xpath("//button[text='Submit']").click()
def register(self, username, password, name, phone):
self.driver_instance.find_element_by_xpath("//input[1]").set_text(username)
self.driver_instance.find_element_by_xpath("//input[2]").set_text(password)
self.driver_instance.find_element_by_xpath("//input[3]").set_text(name)
self.driver_instance.find_element_by_xpath("//input[4]").set_text(phone)
Test.py
import MyApplication
import Authenticate
MyApplication().open_my_application(udid="5c1425bd54c88")
Authenticate().login('user12', 'user12-password')
1- As you can see in the above example, firstly we create MainClass
and include creating driver instance keyword which we called open_application()
here, it will create the instance and store driver instance variables as self.driver_instance
variable.
2- Then we create another class named MyApplication
that inherits from MainClass
. Then we define a function called open_my_application()
that will call the open_application()
from MainClass
as instance (class object). So the self.driver_instance
will store as MainClass
's variable and you can create any class inherited from MainClass
as much as you want.
3- We create a class named Authenticate
to do login and register.
4- Test.py is a sample test file finally.
Hope this helps.