结合pytest框架来实现app(qq)的自动化脚本

#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")

  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值