#1.导包
import pytest
import appium
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
#新版本(appium和selenium客户端都可以是最新的)中支持的写法
from appium.options.common import AppiumOptions
import time
import os
#2.创建单元测试类
class Test_Login():
#3.初始化
def setup(self):
#1>定义一个option对象,封装连接设计设备和应用的desiredcapabities参数
options = AppiumOptions()
options.set_capability("platformName","Android")
options.set_capability("platformVersion","9")
options.set_capability("deviceName","Android Emulator")
options.set_capability("noReset",True)
options.set_capability("appPackage","com.tencent.mobileqq")
options.set_capability("appActivity","com.tencent.mobileqq.activity.LoginActivity")
#2>定义一个appium server对象driver(连接appium)
self.driver = webdriver.Remote(r"http://127.0.0.1:4723/wd/hub",options=options)
#加10s的隐式等待
self.driver.implicitly_wait(10)
#4.写具体的qq登录的业务脚本
def test_qq_login_01(self):
#1>点击登录按钮
self.driver.find_element(AppiumBy.ID,'com.tencent.mobileqq:id/btn_login').click()
time.sleep(2)
#2>定位qq号码文本框,并输入号码 (如果重复执行用例,记得清空文本框)
# self.driver.find_element(AppiumBy.XPATH,'//android.widget.EditText[@content-desc="请输入QQ号码或手机或邮箱"]').clear()
# self.driver.find_element(AppiumBy.XPATH,'//android.widget.EditText[@content-desc="请输入QQ号码或手机或邮箱"]').send_keys("1099278700")
self.driver.find_element(AppiumBy.ACCESSIBILITY_ID,'请输入QQ号码或手机或邮箱')
self.driver.find_element(AppiumBy.ACCESSIBILITY_ID, '请输入QQ号码或手机或邮 箱').send_keys("1099278700")
time.sleep(2)
#3>定位密码文本框,并输入密码(如果重复执行用例,记得清空文本框)
self.driver.find_element(AppiumBy. XPATH,"//android.widget.EditText[@content-desc='密码 安全']").clear()
self.driver.find_element(AppiumBy. XPATH,"//android.widget.EditText[@content-desc='密码 安全']").send_keys("123456")
time.sleep(2)
#4>点击登录按钮
self.driver.find_element(AppiumBy.ID,"com.tencent.mobileqq:id/login").click()
time.sleep(2)
5. #断言(断言要写在测试用例中,注意缩进)
expectValue='当前网络不稳定,登录失败。推荐使用常用设备或通过手机号登录。' actualValue=self.driver.find_element(AppiumBy.ID,'com.tencent.mobileqq:id/dialogText').get_attri bute("text")
assert expectValue == actualValue
6.资源释放
def teardown(self):
#关闭连接driver
self.driver.quit()
7.生成报告
if __name__ == '__main__':
# pytest.main()
#通过allure生成报告,最终在test文件夹中的index.html中;result是原始文件。
pytest.main(["--alluredir","./result"])
os.system("allure generate ./result -o ./test --clean")