1、Pge object 封装:youdao_page.py 存放于 Page 文件夹。
from poium import Page, NewPageElement
class YoudaoPage(Page):
search_box = NewPageElement(id_ = "com.youdao.dict:id/search_input_box")
search_button = NewPageElement(class_name = "android.widget.ImageView")
search_result =NewPageElement(id_ = "com.youdao.dict:id/tv_word")
2、APP 启动模块封装:my_test.py 存放于 common 文件夹。
import unittest,time
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import sys
from os.path import dirname, abspath
BASE_PATH = dirname(dirname(abspath(file)))
sys.path.append(BASE_PATH)
caps = {}
caps["deviceName"] = "HWJSN-HW"
caps["automationName"] = "appium"
caps["platformVersion"] = "10"
caps["platformName"] = "Android"
caps["appPackage"] = "com.youdao.dict"
caps["appActivity"] = ".activity.MainActivity"
caps["ensureWebviewsHavePages"] = True
class MyTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
cls.driver.implicitly_wait(10)
try:
loc = ("xpath", "//*[@text='始终允许']")
el = WebDriverWait(cls.driver, 5, 0.5).until(EC.presence_of_element_located(loc))
TouchAction(cls.driver).tap(el).perform()
except:
pass
@classmethod
def tearDownClass(cls):
cls.driver.quit()
if name=="main":
unittest.main()
3、测试用列封装:test_word_search.py 存放于 test_case 文件夹:
import unittest
import sys
from os.path import dirname, abspath
BASE_PATH = dirname(dirname(abspath(file)))
sys.path.append(BASE_PATH)
from common.my_test import MyTest
from page.youdao_page import YoudaoPage
class TestYoudaoSearch(MyTest):
def test_search_clear(self):
"""定义搜索关键字:clear"""
page = YoudaoPage(self.driver)
page.search_box.click()
page.search_box = "clear"
page.search_button.click()
print(page.search_result.text)
self.assertEqual("clear", page.search_result.text)
if name=="main":
unittest.main()
4、运行测试封装:run_tests.py 存放于 test_report 文件夹。
import unittest
import time
import yagmail
from HTMLTestRunner import HTMLTestRunner
def send_mail(report):
yag = yagmail.SMTP(user = "%%%%@163.com",
password = "XESWYPMRUGLMIOYY",
host = "smtp.163.com")
subject = "主题:自动化测试报告"
contents = "请查看邮件"
yag.send("%%%%@163.com",subject, contents, report)
print("email has send out")
if name == "main":
test_dir = './test_case'
suit = unittest.defaultTestLoader.discover(test_dir, pattern='test_*.py')
now_time = time.strftime("%Y-%m-%d %H_%M_%S")
html_report = now_time + "youdao.html"
with open(html_report,"wb") as fp:
runner = HTMLTestRunner(stream=fp, title="youdao",
description="android",
verbosity=2)
runner.run(suit)
fp.close()
4、以上所有文件夹都存放于 appium project 文件夹内,每个文件夹也放置了 名为init.py 的空文件。
但运行测试回报如题所示的错误。请问是什么原因造成的,怎么解决?