mardi 6 août 2019

Maintaining repository of elements in Python unittest with Appium

At the moment I'm performing mobile testing in Appium with Python. I'm wondering if it's possible to maintain a repository for elements.

For example maybe have all elements in a separate class and access them from other classes. What I need is to prevent mentioning the locators of the page elements again and again. I know there is Page-Object model, but I'm not familiar with the concept.

Here is some of the code I have.

from adb.client import Client as AdbClient
import HtmlTestRunner
import xmlrunner
import yaml
import datetime
import os, sys
import glob
import unittest
from appium import webdriver
from time import sleep
from appium.webdriver.common.touch_action import TouchAction

PLATFORM_VERSION = '8.1.0'


class Q_suite1_01(unittest.TestCase):

@classmethod
def setUpClass(cls):
    desired_caps = {}
    desired_caps['platformName'] = 'Android'
    desired_caps['platformVersion'] = '8.1.0'
    desired_caps['deviceName'] = 'Samsung Galaxy J7 Max'

    devices = AdbClient(host= "127.0.0.1", port= 5037).devices()
    for device in devices:
        desired_caps['udid'] = device.serial

    desired_caps['appPackage'] = 'com.quallogi'
    desired_caps['appActivity'] = 'com.quallogi.MainActivity'
    url = "http://localhost:{}/wd/hub".format(4723)
    cls.driver = webdriver.Remote(url, desired_caps)



def signin_incorrect_credentials(self):
    phone_num = self.driver.find_element_by_class_name('android.widget.EditText')
    phone_num.clear()
    phone_num.send_keys('+' +str(data['SignIn_incorrect_ph_pw']['phoneNum']))

    pw = self.driver.find_elements_by_class_name('android.widget.EditText')[1]
    pw.clear()
    pw.send_keys(str(data['SignIn_incorrect_ph_pw']['password']))

    login_bttn = self.driver.find_element_by_xpath('//*[contains(@text,"Login") and contains(@class, "android.widget.TextView")]')
    login_bttn.click()

    sleep(3)
    error_msg = self.driver.find_element_by_xpath('//*[contains(@text,"Username or password is incorrect") and contains(@class, "android.widget.TextView")]').is_displayed()
    if error_msg is True:
        print 'Error prompts for incorrect Password.'
    else:
        return


def signin(self):
    phone_num = self.driver.find_element_by_class_name('android.widget.EditText')
    phone_num.clear()
    phone_num.send_keys('+' +str(data['SignIn_valid']['phoneNum']))

    pw = self.driver.find_elements_by_class_name('android.widget.EditText')[1]
    pw.clear()
    pw.send_keys(str(data['SignIn_valid']['password']))

    login_bttn = self.driver.find_element_by_xpath('//*[contains(@text,"Login") and contains(@class, "android.widget.TextView")]')
    login_bttn.click()

    sleep(3)
    getting_started = self.driver.find_element_by_xpath('//*[contains(@text,"Getting started") and contains(@class, "android.widget.TextView")]').is_displayed()
    if getting_started is True:
        print 'Successful Sign In.'
    else:
        return




def testcase_E_Signin_incorrect_phonePW(self):
    self.signin_incorrect_credentials()

def testcase_J_Signin(self):
    self.signin()




@classmethod
def tearDownClass(cls):
    cls.driver.quit()





# class Q_suite1_02(unittest.TestCase):
#   ...
   # Another class as the previous



if __name__ == '__main__':
data = yaml.load(open(os.path.join(sys.path[0],'Q_signin_signup.yaml'), 'r'))

result = []

# Test Suite 01
suite1= unittest.TestLoader().loadTestsFromTestCase(Q_suite1_01)
result.append(HtmlTestRunner.HTMLTestRunner(output='./HTML Reports/' + str(datetime.date.today())).run(suite1))
print(result)

# # Test Suite 02
# suite2= unittest.TestLoader().loadTestsFromTestCase(Q_suite1_02)
# result.append(HtmlTestRunner.HTMLTestRunner(output='./HTML Reports/' + str(datetime.date.today())).run(suite2))
# print(result)

Aucun commentaire:

Enregistrer un commentaire