详ç»å¦ä¸ï¼
ä¸é¢æ们å¼å§ç¬¬ä¸ä¸ªç®åçAndroid UIèªå¨åæµè¯
1.使ç¨adbå½ä»¤è¿æ¥çæºæ模æå¨
2.æå¼uiautomatorviewerå·¥å
·
3.使ç¨uiautomatorviewerå·¥å
·è·ååºç¨çå
ç´ è¿è¡å®ä½
4.ç®åä»ç»unittestæ¡æ¶ç使ç¨æ¹æ³
5.使ç¨Pythonç¼åç«å®èå¤åºç¨æ³¨å模åçèªå¨åæµè¯
1.使ç¨adbå½ä»¤è¿æ¥çæºæ模æå¨ï¼
ææºUSBè¿æ¥çµèï¼è¿å
¥å¼åè
模å¼ï¼
cmdå½ä»¤ï¼adb devices ï¼æ¥çææºæ¯å¦è¿æ¥
æ¾ç¤ºé误
è¿æ¯å 为adbç端å£è¢«å ç¨ï¼æ们éè¦æ¥çæ¯ä»ä¹åºç¨å ç¨äºè¿ä¸ªç«¯å£ï¼5037为adbé»è®¤ç«¯å£ï¼
cmdå½ä»¤ : netstat -aon|findstr â5037â
å¯ä»¥çå°å ç¨5037端å£å¯¹åºçç¨åºçPIDå·ä¸º8388;
cmdå½ä»¤ : tasklist|findstr â8388â
å¯ä»¥çåº8388对åºçç¨åºä¸ºkadb.exeï¼è¯´æ该ç¨åºæ£å¨ä½¿ç¨5037端å£;
è¿æ¶æ们éè¦å¨ä»»å¡ç®¡çå¨ä¸ç»ækadb.exeè¿ç¨ï¼æå¿«æ·é®âCtrl+Shift+Escâè°åºWindowsä»»å¡ç®¡çå¨ï¼æ¾å°âkadb.exeâï¼åå»ä¸æ¹çç»æè¿ç¨å³å¯ï¼
æ们å次è¿è¡cmdå½ä»¤ï¼adb devices
è¿ä¸æ¥æååæ们æè½è¿è¡sdkèªå¸¦çuiautomatorviewerï¼
æ们éè¦ç¨uiautomatorviewerå·¥å
·æ¥è·åå
ç´ ï¼ç¨äºå®ä½ã
cmdå½ä»¤ï¼uiautomatorviewerï¼æå¼uiautomatorviewerçé¢
æè
æ¾å°sdkç®å½ï¼sdk\toolsä¸æ¾å°uiautomatorviewer.batæ件åå»è¿è¡
2.æå¼uiautomatorviewerå·¥å
·
æ们å¯ä»¥æ ¹æ®textï¼resource-idï¼classçå
ç´ è¿è¡å®ä½
3.使ç¨uiautomatorviewerå·¥å
·è·ååºç¨çå
ç´ è¿è¡å®ä½
è¿éæ使ç¨pythonèªå¸¦çIDLEè¿è¡ç¼åæµè¯èæ¬ï¼æå¼pythonæ件æ¾å°IDLEï¼python GUIï¼åå»æå¼ï¼å¦å¾ï¼
4.ç®åä»ç»unittestæ¡æ¶ç使ç¨æ¹æ³
# -*- coding:utf-8 -*-
from uiautomator import device as d
import unittest
class Mytest(unittest.TestCase):
#åå§åå·¥ä½
def setUp(self):
print "--------------åå§åå·¥ä½"
#éåºæ¸
çå·¥ä½
def tearDown(self):
print "--------------éåºæ¸
çå·¥ä½"
#æµè¯ç¹å»ç«å®èå¤case
def test_login(self):
d(text="ç«å®èå¤").click()
print "--------------æµè¯1"
#æµè¯2
def test_z(self):
print "--------------æµè¯2" #è¿éä½ å¯ä»¥åä½ ç第äºä¸ªæµè¯ç¨ä¾ï¼
#æµè¯3
def test_w(self):
print "--------------æµè¯3" #è¿éä½ å¯ä»¥åä½ ç第ä¸ä¸ªæµè¯ç¨ä¾ããããããããããããã
if __name__ == '__main__':
unittest.main()
123456789101112131415161718192021222324252627282930123456789101112131415161718192021222324252627282930
ç»æå¦ä¸ï¼
Testing started at 21:14 â¦
âââââåå§åå·¥ä½
âââââæµè¯1
âââââéåºæ¸
çå·¥ä½
âââââåå§åå·¥ä½
âââââæµè¯3
âââââéåºæ¸
çå·¥ä½
âââââåå§åå·¥ä½
âââââæµè¯2
âââââéåºæ¸
çå·¥ä½
Process finished with exit code 0
ä»ç»æä¸æ们å¯ä»¥çåºunittestæ¡æ¶çè¿è¡æ¹å¼ä¸ºï¼
setUp æµè¯1 tearDown
setUp æµè¯2 tearDown
setUp æµè¯3 tearDown
5.使ç¨Pythonç¼åç«å®èå¤åºç¨æ³¨å模åçèªå¨åæµè¯
# -*- coding:utf-8 -*-
from uiautomator import device as d
import time
import unittest
class MyTestSuite(unittest.TestCase):
# åå§åå·¥ä½
def setUp(self):
print "--------------åå§åå·¥ä½"
# éåºæ¸
çå·¥ä½
def tearDown(self):
print "--------------éåºæ¸
çå·¥ä½"
#***************************æ¹æ³**************************************
# å¤ææ§ä»¶æ¯å¦åå¨ & text
def check_controls_exists(self, controls_text):
if d(text=controls_text).exists:
return 1
else:
return 0
# å¤ææé®æ¯å¦ç½®ç° & text & clickable
def check_controls_click_text(self, controls_text):
if d(text=controls_text).info.get("clickable") is True:
return 1
else:
return 0
#assertIn(a, b) a in b
def check_ainb(self,resourceid,b):
if d(resourceId=resourceid).info.get("text") in b:
return 1
else:
return 0
#***********************************************************
# 注å模å
def test_Aregister(self):
try:
time.sleep(2)
#ç«å®èå¤å¼å¯å
¨æ°æ¶ä»£
self.assertEqual(self.check_controls_click_text("注å"),1,u"ç«å®èå¤å¼å¯å
¨æ°æ¶ä»£")
# ç«å®èå¤å¼å¯å
¨æ°æ¶ä»£--ãç¹å»æ³¨åæé®è¿å
¥æ³¨åç«å®çé¢
d(text="注å").click()
time.sleep(3)
#注åç«å®çé¢
self.assertEqual(self.check_text("com.isentech.attendancet:id/regis_phone","请è¾å
¥ææºå·ç "),
1,u"注å页é¢-ã请è¾å
¥ææºå·ç ")
self.assertEqual(self.check_text("com.isentech.attendancet:id/regis_verifycode","请è¾å
¥éªè¯ç "),
1,u"注å页é¢-ã请è¾å
¥éªè¯ç ")
self.assertEqual(self.check_controls_click_text("è·åéªè¯ç "), 0,u"注å页é¢-ãè·åéªè¯ç ")
self.assertEqual(self.check_controls_click_text("ãä¸ç§ç±è®¯æå¡åè®®ã"), 1,u"注å页é¢-ããä¸ç§ç±è®¯æå¡åè®®ã")
self.assertEqual(self.check_controls_click_text("注å"), 0,u"注å页é¢-ã注å")
time.sleep(2)
#ãä¸ç§ç±è®¯æå¡åè®®ã
d(text="ãä¸ç§ç±è®¯æå¡åè®®ã").click()
time.sleep(2)
self.assertEqual(self.check_ainb("com.isentech.attendancet:id/title","æå¡åè®®"), 1,u"注å页é¢-ãæå¡åè®®")
time.sleep(1)
d(resourceId="com.isentech.attendancet:id/title_back").click()
time.sleep(1)
#ææºå·ä¸è¾å
¥æ¯å¦è½æ³¨å
d(text="注å").click()
time.sleep(3)
# ææºå·åªè¾å
¥1个æ°åæ¯å¦è½æ³¨å&åªè¾å
¥1个æ°åæ¯å¦è½è·åéªè¯ç
d(resourceId="com.isentech.attendancet:id/regis_phone").set_text("1")
self.assertEqual(self.check_controls_click_text("è·åéªè¯ç "), 0)
time.sleep(1)
d(text="注å").click()
time.sleep(1)
d(resourceId="com.isentech.attendancet:id/regis_phone").clear_text()
time.sleep(1)
#åªè¾å
¥5个æ°åæ¯å¦è½è·åéªè¯ç
d(resourceId="com.isentech.attendancet:id/regis_phone").set_text("11111")
self.assertEqual(self.check_controls_click_text("è·åéªè¯ç "), 0)
time.sleep(1)
d(resourceId="com.isentech.attendancet:id/regis_phone").clear_text()
time.sleep(1)
#åªè¾å
¥ææºå·æ¯å¦è½æ³¨å
d(resourceId="com.isentech.attendancet:id/regis_phone").set_text(phone_number)
self.assertEqual(self.check_controls_click_text("注å"), 0)
time.sleep(1)
d(text="注å").click()
time.sleep(1)
#è¾å
¥æ£ç¡®çéªè¯ç &è·åéªè¯ç æ¯å¦é«äº®
d(resourceId="com.isentech.attendancet:id/regis_verifycode").set_text("5648")
time.sleep(1)
self.assertEqual(self.check_controls_click_text("è·åéªè¯ç "), 1)
time.sleep(2)
#å¯ç åªè¾å
¥1个æ°åæ¯å¦è½æ³¨å&注åæé®æ¯å¦é«äº®
d(resourceId="com.isentech.attendancet:id/regis_pass").set_text("1")
d(resourceId="com.isentech.attendancet:id/regis_passAgain").set_text("1")
time.sleep(1)
self.assertEqual(self.check_controls_click_text("注å"), 0,u"å¯ç åªè¾å
¥1个æ°åæ¯å¦è½æ³¨å")
time.sleep(1)
d(resourceId="com.isentech.attendancet:id/regis_pass").clear_text()
d(resourceId="com.isentech.attendancet:id/regis_passAgain").clear_text()
time.sleep(1)
#è¾å
¥ä¸ç¸åçå¯ç æ¯å¦è½æ³¨å
d(resourceId="com.isentech.attendancet:id/regis_pass").set_text("123456")
d(resourceId="com.isentech.attendancet:id/regis_passAgain").set_text("12345")
time.sleep(1)
self.assertEqual(self.check_controls_click_text("注å"), 0,u"è¾å
¥ä¸ç¸åçå¯ç æ¯å¦è½æ³¨å")
time.sleep(1)
d(resourceId="com.isentech.attendancet:id/regis_pass").clear_text()
d(resourceId="com.isentech.attendancet:id/regis_passAgain").clear_text()
time.sleep(1)
#è¾å
¥æ£ç¡®çå¯ç æ¯å¦è½æ³¨å&æå·²åææ¯å¦æé©
d(resourceId="com.isentech.attendancet:id/regis_pass").set_text("123456")
d(resourceId="com.isentech.attendancet:id/regis_passAgain").set_text("123456")
time.sleep(1)
self.assertEqual(self.check_controls_click_resourceId("com.isentech.attendancet:id/regis_agree"), 1)
self.assertEqual(self.check_controls_click_text("注å"), 1)
time.sleep(2)
d(text="注å").click()
time.sleep(8)
except Exception, e:
print u"Error: 注å模åæé®é¢\n", e
def test_app():
test_unit = unittest.TestSuite()
test_unit.addTest(MyTestSuite("test_Aregister"))
if __name__ == "__main__":
# æµè¯app
unittest.main()
温馨提示:答案为网友推荐,仅供参考