android自动化appium封装案例

封装类:

from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.support.wait import WebDriverWait

class TestSeting():

    def __init__(self):
        desired_caps = {}  # 配置参数的字典
        desired_caps['platformName'] = 'Android'  # 平台名称,iOS、Android、 FirefoxOS,选择真机/模拟器对应的就好
        desired_caps['deviceName'] = '模拟器'  # 设备名称填写任意内容(使用的移动设备或模拟器的种类)
        # desired_caps['platformVersion'] = '5.1.1'  # 平台版本
        # desired_caps['appPackage'] = 'com.android.contacts'  # 要运行的Android应用程序的Java包
        # desired_caps['appActivity'] = '.activities.PeopleActivity'  # 要运行的Android应用程序的主入口
        desired_caps['platformVersion'] = '9'  # 平台版本
        desired_caps['appPackage'] = 'com.包名'  # 要运行的Android应用程序的Java包
        desired_caps['appActivity'] = '.Activity'  # 要运行的Android应用程序的主入口
        desired_caps['unicodeKeyboard'] = True  # 设置启动编码
        desired_caps['resetKeyboard'] = False  # 重置键盘
        desired_caps['noReset'] = 'True'                          #默认为False,当设置为true时不作为首次进入(在此会话之前,请勿重置应用程序状态)
        self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)
        # return self.driver

    def waitXpath(self, xpath):
        return WebDriverWait(self.driver, 5, 0.5).until(lambda x: x.find_element_by_xpath(xpath))

    def waitId(self, id):
            return WebDriverWait(self.driver, 5, 0.5).until(lambda x: x.find_element_by_id(id))

    def waitClassName(self, classname):
            return WebDriverWait(self.driver, 5, 0.5).until(lambda x: x.find_element_by_class_name(classname))

# 在当前类中的Terminal中执行:pytest -s 类名.py --html=./测试报告名.html
 

案例1:# 启动app,打开通知栏并人选一条数据进入详情页面

from text_homework.scripts import testSeting
import unittest
import time


class testDyhoa5(unittest.TestCase):

    def setUp(self):
        print("start开始")

    def tearDown(self):
        print("end结束")

    def test_001(self):
        driver = testSeting.TestSeting().driver
        time.sleep(5)
        driver.open_notifications()
        driver.find_element_by_id("android:id/status_bar_latest_event_content").click()


tes = testDyhoa5()
tes.test_001()
案例2:模拟器电话本新建用户 张三,要求必填信息为:姓名 电话 电子邮件 公司,保存后修改张三姓名改为李四

from text_homework.scripts import testSeting
import unittest
import time

class testDyhoa2(unittest.TestCase):

    def setUp(self):
        print("start开始")

    def tearDown(self):
        print("end结束")

    def test_001(self):
        driver = testSeting.TestSeting().driver
        print("start")
        driver.find_element_by_id("com.android.contacts:id/floating_action_button").click()
        driver.find_element_by_xpath("//*[contains(@text,'本地保存')]").click()
        driver.find_element_by_xpath("//*[contains(@text,'姓名')]").send_keys("张**")
        driver.find_element_by_xpath("//*[contains(@text,'姓名拼音')]").send_keys("yyy")
        driver.find_element_by_xpath("//*[contains(@text,'昵称')]").send_keys("**")
        driver.find_element_by_xpath("//*[contains(@text,'电话')]").send_keys("18810762963")
        time.sleep(2)
        driver.drag_and_drop(driver.find_element_by_xpath("//*[contains(@text,'电子邮件')]"),
        driver.find_element_by_xpath("//*[contains(@text,'张**')]"))  # 上下滑动
        time.sleep(2)
        driver.find_element_by_xpath("//*[contains(@text,'电子邮件')]").send_keys("@18810762963.com")
        driver.find_element_by_xpath("//*[contains(@text,'地址')]").send_keys("地址:北京天安门")
        driver.find_element_by_xpath("//*[contains(@text,'公司')]").send_keys("公司:故宫")
        testSeting.TouchAction(driver).tap(x=79, y=168).perform()
        driver.find_element_by_id("com.android.contacts:id/menu_edit").click()
        driver.find_element_by_xpath("//*[contains(@text,'张**')]").clear()
        driver.find_element_by_xpath("//*[contains(@text,'姓名')]").send_keys("韩**")
        testSeting.TouchAction(driver).tap(x=79, y=168).perform()

tes = testDyhoa2()
tes.test_001()
 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值