Best Python code snippet using websmith_python
ddtTestSettings.py
Source:ddtTestSettings.py
1import unittest2from ddt import ddt,data,unpack3import uiautomator2 as u24import datetime5from config.config import *6from base.baseframe import BaseFrame7from base.watcherframe import WatcherFrame8from project.page.loginPage import *9# from data.get_data_login import GetData10@ddt #å°æµè¯ç±»æ·»å ddt修饰11class TestSettings(unittest.TestCase): # å建æµè¯ç±»12 @classmethod # ç±»æ¹æ³ï¼åªæ§è¡ä¸æ¬¡ï¼ä½å¿
é¡»è¦å 注解@classmethod,ä¸åååºå®ä¸ºsetUpClass13 def setUpClass(cls):14 # cls.d = u2.connect('192.168.199.168')15 # cls.d = u2.connect_usb('127.0.0.1:62025')16 cls.watcherframe = WatcherFrame(outdevice=TestDeviceID) #å®ä¾å17 cls.watcherframe.new_create_watcher()18 cls.watcherframe.new_create_watcher(outwatchername='OK',outconditiontextname='OK',outclicktextname='OK')19 cls.watcherframe.start_watcher()20 print('\n')21 pass22 @classmethod # ç±»æ¹æ³ï¼åªæ§è¡ä¸æ¬¡ï¼ä½å¿
é¡»è¦å 注解@classmethod,ä¸åååºå®ä¸ºtearDownClass23 def tearDownClass(cls):24 print('\n')25 cls.watcherframe.close_all_watchers()26 pass27 def setUp(self): # æ¯æ¡ç¨ä¾æ§è¡æµè¯ä¹åé½è¦æ§è¡æ¤æ¹æ³28 self.baseframe = BaseFrame(outdevice=TestDeviceID) #å®ä¾å29 print('\n')30 #pass31 def tearDown(self): # æ¯æ¡ç¨ä¾æ§è¡æµè¯ä¹åé½è¦æ§è¡æ¤æ¹æ³32 print('\n')33 pass34 #ç»å½çæµè¯æ¹æ³35 def defineclickandbackandlogout(self,outtestcasediscription=None,issettings=False,islogout=False,outclickelementtext=None):36 if outtestcasediscription==None:37 testcasediscription = 'æµè¯ç¨ä¾'38 else:39 testcasediscription = outtestcasediscription40 if outclickelementtext==None:41 clickelementtext = "My QR"42 else:43 clickelementtext = outclickelementtext44 if issettings:45 self.baseframe.findbytext_and_click("Settings")46 if islogout:47 self.baseframe.findbytext_and_click("Log Out")48 self.baseframe.findbytext_and_click("Log Out")49 self.baseframe.findbytext(outclickelementtext)50 else:51 self.baseframe.findbytext_and_click(clickelementtext)52 self.baseframe.delaytime(5)53 self.baseframe.clickback()54 self.baseframe.findbytext("Settings")55 print("%s.---æµè¯éè¿" %testcasediscription)56 @data(("ç¹å»Settingsè¿å
¥Settings页ï¼ç¹å»ChangeLoginPasswordåè¿åSettings",True,False,"Change Login Password"),57 ("ç¹å»ChangeTransactionPINåè¿åSettings", False, False, "Change Transaction PIN"),58 ("ç¹å»AboutQRindoåè¿åSettings", False, False, "About QRindo"),59 ("ç¹å»Helpåè¿åSettings", False, False, "Help"),60 ("ç¹å»Logoutç»åº",False,True,"Login"))61 @unpack62 def test_clickandbackandlogout(self,outtestcasediscription,issettings,islogout,outclickelementtext):63 self.defineclickandbackandlogout(outtestcasediscription,issettings,islogout,outclickelementtext)64# @staticmethod #æ ¹æ®ä¸åçåæ°çææµè¯ç¨ä¾65# def getTestFunc(outtestcasediscription,outphonenumberinput,66# outpwdinput,outprepagetext,67# outpretoastmessage,outexceptiontext):68# def func(self):69# self.definelogin(outtestcasediscription,outphonenumberinput,70# outpwdinput,outprepagetext,71# outpretoastmessage,outexceptiontext)72# return func73#74#75# @unittest.skip('test_020') # è·³è¿ç¨ä¾åå为âtest_01âçç¨ä¾ï¼è·³è¿çç¨ä¾çæ§è¡ç»ææ¾ç¤ºï¼æ¯76# def test_020(self):77# """78# æ£ç¡®çè´¦å·åå¯ç ï¼ç»å½æå79# """80# outadborder = None81# outphonenumberid = None82# outphonenumberinput = None83# outpwdid = None84# outpwdinput = None85# outloginbuttonid = None86# outprepagetext = 'QRindo-Merchant'87# outpretoastmessage = None88#89# self.definelogin(outprepagetext=outprepagetext)90# print("å·²ç»æ³¨ååæ·çæ£ç¡®çé±å
è´¦å·åå¯ç ï¼ç»å½æå.---æµè¯éè¿")91#92# # def generateTestCases():93# # arglists = [("é8æ08å¼å¤´çææºå·ç»å½ï¼toastæ示 'You have entered an invalid Indonesia number'",94# # '1122336666','123456',None,95# # 'You have entered an invalid Indonesia number',None),96# # ("没æ注ååæ·çæ£ç¡®çé±å
è´¦å·åå¯ç ï¼ç»å½æ示'Login account is not bound to the merchant(MC320)'",97# # '81122337788','a123456','Login account is not bound to the merchant(MC320)',98# # None,'OK'),99# # ('没æ注åçé±å
è´¦å·ç»å½ï¼æ示âIncorrect account number or login password Please try again(PP001)â',100# # '86754893987','a123456','Incorrect account number or login password. Please try again(PP001)',101# # None,'OK'),102# # ('æ£ç¡®çé±å
è´¦å·ï¼é误çå¯ç ï¼æ示âIncorrect account number or login password Please try again(PP001)â',103# # '833669911','123456','Incorrect account number or login password. Please try again(PP001)',104# # None,'OK'),105# # ("è´¦å·é¿åº¦è¾å
¥å°äº8ä½ï¼å¯ç ä½æ°çäº6ä½ï¼ç»å½æé®ç½®ç°ä¸å¯ç¨",106# # '1234567','123456','Log in',107# # None,'Log in'),108# # ("è´¦å·é¿åº¦è¾å
¥çäº8ä½ï¼å¯ç ä½æ°å°äº6ä½ï¼ç»å½æé®ç½®ç°ä¸å¯ç¨",109# # '12345678','12345','Log in',110# # None,'Log in'),111# # ("è´¦å·é¿åº¦è¾å
¥å¤§äº13ä½æ¶ï¼å¯ç ä½æ°çäº6ä½ï¼ç»å½æé®ç½®ç°ä¸å¯ç¨",112# # '12345678901234','123456','Log in',113# # None,'Log in'),114# # ("è´¦å·é¿åº¦è¾å
¥çäº13ä½æ¶ï¼å¯ç ä½æ°å¤§äº24ä½ï¼ç»å½æé®ç½®ç°ä¸å¯ç¨",115# # '1234567890123','1234567890123456789012345','Log in',116# # None,'Log in'),117# # ("å·²ç»æ³¨ååæ·çæ£ç¡®çé±å
è´¦å·åå¯ç ï¼ç»å½æå",118# # None,None,'QRindo-Merchant',119# # None,None),120# #121# # ]122# # for i in range(0,len(arglists)):123# # args = arglists[i]124# # setattr(TestLogin, 'test_func_%s_%s' % (i,args[0]),125# # TestLogin.getTestFunc(*args)) # éè¿setattrèªå¨ä¸ºTestCase类添å æåæ¹æ³ï¼æ¹æ³ä»¥âtest_func_âå¼å¤´126#127# def __generateTestCases():128# file_name = "D:/Users/Administrator/PycharmProjects/uiautomator2project/dataconfig/autologin.xls"129# sheet_id = 0130# datasheet = GetData(file_name,sheet_id) #å®ä¾å131# rows_count = datasheet.get_case_lines() #è·å表çè¡æ°132# for i in range(1, rows_count): # 循ç¯ï¼ä½å»æ第ä¸133# args = []134# args.append(datasheet.get_case_title_content(i))135# args.append(datasheet.get_account_input_content(i))136# args.append(datasheet.get_password_input_content(i))137# args.append(datasheet.get_pre_text_content(i))138# args.append(datasheet.get_pre_toast_content(i))139# args.append(datasheet.get_pre_back_text_content(i))140# setattr(TestLogin, 'test_func_%s_%s' % (datasheet.get_case_id_content(i),args[0]),141# TestLogin.getTestFunc(*args)) # éè¿setattrèªå¨ä¸ºTestCase类添å æåæ¹æ³ï¼æ¹æ³ä»¥âtest_func_âå¼å¤´142#143#144# __generateTestCases()145if __name__ == '__main__':146 print("hello world")...
baseframe.py
Source:baseframe.py
1# _*_ coding:utf-8 _*_2__author__ = 'bobby'3__date__ = '2018/10/9 15:55'4import os5import uiautomator2 as u26import time7from util.gettimestr import GetTimeStr #导å
¥è·åæ¶é´ä¸²å½æ°8class BaseFrame:9 def __init__(self,outdevice=None):10 if outdevice==None:11 # self.d = u2.connect('192.168.199.168')12 self.d = u2.connect_usb('810EBM32TZ4K')13 else:14 self.d = u2.connect_usb(outdevice)15 self.timeStr = GetTimeStr() # å®ä¾å16 def adbshell(self,order):17 d = self.d18 d.adb_shell(order)19 print('è¾å
¥shellå½ä»¤:',order,'ã\n')20 def startapp(self,packagename):21 d = self.d22 d.app_start(packagename)23 print('å¯å¨å
å为[%s]çåºç¨---------'% packagename)24 # print('设å¤ä¿¡æ¯ï¼', d.device_info)25 self.delaytime(3)26 def delaytime(self,dalaytime):27 dalaytime = int(dalaytime)28 time.sleep(dalaytime)29 print('çå¾
%dç§...'% dalaytime)30 def findbyresourceId(self,resourceId):31 d = self.d32 try:33 ele = d(resourceId=resourceId)34 eletext = self.geteleinfo_text(ele)35 if eletext == "":36 print("å®ä½å°resourceId为[%s]çæ§ä»¶ã" % resourceId)37 else:38 print("å®ä½å°text为[%s]çæ§ä»¶ã" % eletext)39 return ele40 except Exception as e:41 self.getScreenshotError()42 self.printredword()43 print("åºéåå ï¼",e)44 self.printnormalword()45 self.delaytime(3)46 def findbyresourceId_and_input(self,resourceId,inputtext):47 ele = self.findbyresourceId(resourceId)48 self.ele_input(ele, inputtext)49 def findbyresourceId_and_click(self,resourceId,outpretoastmessage=None):50 ele = self.findbyresourceId(resourceId)51 toastmessage = self.ele_click_and_return_toastmessage(ele,outpretoastmessage)52 return toastmessage53 def findbyresourceId_and_return_enabledstatus(self,resourceId):54 ele = self.findbyresourceId(resourceId)55 eleinfo_enabled = self.geteleinfo_enabled(ele)56 return eleinfo_enabled57 def findbyresourceId_and_return_text(self,resourceId):58 ele = self.findbyresourceId(resourceId)59 eleinfo_text = self.geteleinfo_text(ele)60 return eleinfo_text61 def findbyresourceId_and_return_selectedstatus(self,resourceId):62 ele = self.findbyresourceId(resourceId)63 eleinfo_selected= self.geteleinfo_selected(ele)64 return eleinfo_selected65 def findbytext(self,text):66 d = self.d67 try:68 ele = d(text=text)69 eletext = self.geteleinfo_text(ele)70 if eletext == "":71 print("没æå®ä½å°æ§ä»¶ã")72 else:73 print("å®ä½å°text为[%s]çæ§ä»¶ã" % eletext)74 return ele75 except Exception as e:76 self.getScreenshotError()77 self.printredword()78 print("åºéåå ï¼",e)79 self.printnormalword()80 self.delaytime(3)81 def findbytext_and_input(self,text, inputtext):82 ele = self.findbytext(text)83 self.ele_input(ele,inputtext)84 def findbytext_and_click(self,text,outpretoastmessage=None):85 ele = self.findbytext(text)86 toastmessage = self.ele_click_and_return_toastmessage(ele,outpretoastmessage)87 return toastmessage88 def findbytext_and_return_enabledstatus(self,text):89 ele = self.findbytext(text)90 eleinfo_enabled = self.geteleinfo_enabled(ele)91 return eleinfo_enabled92 def findbytext_and_return_text(self,text):93 ele = self.findbytext(text)94 eleinfo_text = self.geteleinfo_text(ele)95 return eleinfo_text96 def findbytext_and_return_selectedstatus(self,text):97 ele = self.findbytext(text)98 eleinfo_selected= self.geteleinfo_selected(ele)99 return eleinfo_selected100 def ele_input(self,ele,inputtext):101 ele.clear_text()102 ele.send_keys(inputtext)103 print("è¾å
¥:%sã" % inputtext)104 self.delaytime(1)105 def ele_click_and_return_toastmessage(self,ele,outpretoastmessage=None):106 ele.click()107 print("ç¹å»è¯¥æ§ä»¶ã")108 if outpretoastmessage==None:109 self.delaytime(3)110 return None111 else:112 toastmessage = self.getToast()113 print("pretoastmessage:",outpretoastmessage)114 return toastmessage115 def geteleinfo_enabled(self,ele):116 value = 'enabled'117 eleinfo_enabled = self.geteleinfo_value(ele,value)118 print('该æ§ä»¶çenabledå±æ§çå¼ä¸ºï¼',eleinfo_enabled)119 return eleinfo_enabled120 def geteleinfo_text(self,ele):121 value = 'text'122 eleinfo_text = self.geteleinfo_value(ele,value)123 if eleinfo_text !='':124 print('该æ§ä»¶çtextå±æ§çå¼ä¸ºï¼', eleinfo_text)125 return eleinfo_text126 def geteleinfo_selected(self,ele):127 value = 'selected'128 eleinfo_selected = self.geteleinfo_value(ele,value)129 if eleinfo_selected !='':130 print('该æ§ä»¶çselectedå±æ§çå¼ä¸ºï¼', eleinfo_selected)131 return eleinfo_selected132 def geteleinfo_value(self,ele,value):133 eleinfo = ele.info134 # print('eleinfo:',eleinfo)135 eleinfo_value = eleinfo[value]136 return eleinfo_value137 def getToast(self):138 toastmessage = self.d.toast.get_message(5.0, default="")139 print("toastmessage:",toastmessage)140 return toastmessage141 def getdeviceinfo(self):142 deviceinfo = self.d.device_info143 print("deviceinfo:",deviceinfo)144 return deviceinfo145 def clickback(self):146 self.d.press("back")147 self.delaytime(1)148 def createwatcher(self):149 d = self.d150 d.watcher('crash').when(text='å¾æ±æï¼âQRindo MCHâå·²åæ¢è¿è¡ã').click(text="ç¡®å®")151 d.watcher("crash").triggered152 print('d.watcher:',d.watcher)153 #æå°çº¢è²æå154 def printredword(self):155 print('\033[1;31;0m') #<!--1-é«äº®æ¾ç¤º 31-åæ¯è²çº¢è² 47-èæ¯è²ç½è²-->156 #æå°é»è®¤æå157 def printnormalword(self):158 print('\033[0m') # <!--éç¨ç»ç«¯é»è®¤è®¾ç½®ï¼å³åæ¶é¢è²è®¾ç½®-->159 #æå°ç»¿è²æå160 def printgreenword(self):161 print('\033[1;32;0m') # <!--1-é«äº®æ¾ç¤º 32-åæ¯è²ç»¿è² 40-èæ¯è²é»è²-->162 #è·åæ¶é´ä¸²163 def getTimeStr(self):164 tStr = self.timeStr.getTimeStr()165 return tStr166 #åºéæ¶ï¼è·å页é¢æªå¾167 def getScreenshotError(self):168 d = self.d169 self.printredword()170 print("è°ç¨æªåå¾çå½æ°")171 tStr = self.getTimeStr()172 # path = "../screenshots/screenpicture_%s.png"% tStr173 path = '%s/screenshots/screenpicture_%s.png'%(str(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))),tStr)174 self.printnormalword()175 d.screenshot(path)176 print("*****")177 print(path)178 print("*****")179 return path180 #æ£å¸¸ï¼è·å页é¢æªå¾181 def getScreenshotNormal(self):182 d = self.d183 print("è°ç¨æªåå¾çå½æ°")184 tStr = self.getTimeStr()185 # path = "../screenshots/screenpicture_%s.png"% tStr186 path = '%s/screenshots/screenpicture_%s.png' % (str(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))), tStr)187 d.screenshot(path)188 print("*****")189 print(path)190 print("*****")191 return path192if __name__ == "__main__":193 bf = BaseFrame()...
MainPage.py
Source:MainPage.py
1#!usr/bin/env python2#-*- coding:utf-8 -*-3from selenium.webdriver.common.by import By4from page_object.driver.AndroidClient import AndroidClient5from page_object.page.BasePage import BasePage6from page_object.page.MarketPage import MarketPage7from page_object.page.ProfilePage import ProfilePage8from page_object.page.SearchPage import SearchPage9from page_object.page.SelectedPage import SelectedPage10class MainPage(BasePage):11 _profile_button=(By.ID,"user_profile_icon")12 _search_button=(By.ID,"home_search")13 def gotoSelected(self):14 #è°ç¨å
¨å±çdriver对象使ç¨webdriver apiæ纵app15 zixuan=("èªé")16 self.findByText(zixuan)17 self.findByText(zixuan).click()18 return SelectedPage()19 def gotoMarket(self):20 market=("è¡æ
")21 self.findByText(market)22 self.findByText(market).click()23 return MarketPage()24 def gotoSearch(self):25 self.find(self._search_button).click()26 return SearchPage()27 def gotoProfile(self):28 self.find(MainPage._profile_button).click()...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!