最近在入手学 appium,发送一个批量执行 case 的好方法,分享下,希望大家勿喷。
test_001.py
from appium import webdriver
import unittest
import os
from selenium.webdriver.support.ui import WebDriverWait
PATH = lambda p: os.path.abspath(
os.path.join(os.path.dirname(__file__), p)
)
class test_001_cls(unittest.TestCase):
def setUp(self):
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '4.1'
desired_caps['deviceName'] = 'SCRIBEPRO'
desired_caps['appPackage'] = 'com.tencent.mm'
desired_caps['appActivity'] = '.ui.LauncherUI'
self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
def tearDown(self):
self.driver.close_app()
self.driver.quit()
def test_001(self):
# 智能等待
element = WebDriverWait(self.driver, 10).until(lambda x: x.find_element_by_name("通讯录"))
self.driver.find_element_by_name('通讯录').click()
test_002.py
.....................
runCase.py
.....................
def load_tests():
test_file_strings = glob.glob('test_*.py')
module_strings = [str[0:len(str)-3] for str in test_file_strings]
suites = [unittest.defaultTestLoader.loadTestsFromName(str) for str
in module_strings]
testSuite = unittest.TestSuite(suites)
return testSuite
if __name__ == '__main__':
unittest.TextTestRunner(verbosity=2).run(load_tests())
就这样,很简单也很实用,如果想运行指定的 case 只要改下 load_tests 即可。
另外贴下 api: https://docs.python.org/3.3/library/unittest.html#unittest.TestSuite.run
另外弱弱问下:
PATH = lambda p: os.path.abspath(
os.path.join(os.path.dirname(file), p)
)
返回此文件的绝对路径?
这段代码什么意思?我看到太多的例子都加了这段代码,我去掉了这段代码其实没有任何影响,但是打印出来的结果是: at 0x0227D2B8>,求解惑