appium自动化基础总结

---恢复内容开始---

断断续续学了2周appium自动化,把经常用到基础写出来吧!方便日后便于查看。

appium解决中文无法输入和,启动app自动清除数据的问题。在前置代码里面加上这3句就可以解决。

desired_caps = {

    "unicodeKeyboard": "True",  # 使用unicode输入法
"resetKeyboard": "True", # 重置输入法到初始状态
  
"noReset": "True", # 解决启动app自动清除数据的问题,加这句让app启动不清除原有数据
}

安装apk到手机


driver.install_app("D:\\download\\38193ab216f29fdc9243bd8c39b65f95.apk") #后面跟apk路径

移除app
driver.remove_app(‘com.ibox.calculators‘) #参数填包名
判断app是否安装在手机上,参数填包名
result = driver.is_app_installed(‘com.ibox.calculators‘)

获取当前页面的文档结构,用来做断言
print(driver.page_source)

断言元素是否存在页面中
if ‘更多‘ in driver.page_source:
print(‘成功,存在‘)
else:
print(‘错误,不存在‘)

隐式等待
driver.implicitly_wait(30)


获取包名和appActivity

1,先打开获取包名的app
2,adb shell
3, dumpsys activity | grep mFocusedActivity 获取到包名和
appActivity

获取屏幕大小
size = driver.get_window_size()


定位方法截图:
appium自动化基础总结

appium自动化基础总结


后续补充的代码
# todo 实现appium1.9版本自动登录美逛的功能
from appium import webdriver
import time


class OpenMg():
    ‘‘‘自动登录美逛app‘‘‘

    def __init__(self):
        ‘‘‘打开APP及初始化工作‘‘‘
        desired_caps = {
            "platformName": "Android",
            "platformVersion": "5.1",
            "deviceName": "127.0.0.1:62001",
            "appPackage": "com.xk.span.zutuan",
            "appActivity": ".module.main.ui.activity.MainActivity",
            # "appWaitActivity": ".module.main.ui.activity.MainActivity",
            "unicodeKeyboard": "True",  # todo 屏蔽键盘,解决appium不能输入中文的问题
            "resetKeyboard": "True",
             "noReset":"True"     # todo 启动app时不要清除app里的原有的数据
        }
        self.driver = webdriver.Remote(http://127.0.0.1:4723/wd/hub, desired_caps)
        self.driver.implicitly_wait(30)

    def slide_pafe(self):
        ‘‘‘实现界面滑动的功能‘‘‘

        # todo swipe参数说明,(x轴起始位置,y轴起始,x轴结束,y轴结束,最后一个参数是滑动速度,可不填)
        ‘‘‘
        # todo 从下往上滑,起始y1比结束坐标y2大,差距越大,滑动距离越大,
        # TODO 从上往下滑,起始y1比结束坐标y2小,差距越大,滑动距离越大
        # todo 从左往右滑,起始x1比结束坐标x2大,差距越大,滑动距离越大
        # TODO 从右往左滑,起始x1比结束坐标x2小,差距越大,滑动距离越大
        ‘‘‘
        # 滑动首页图片进入首页
        for i in range(4):
            self.driver.swipe(972, 386, 108, 386, 500)
        self.driver.implicitly_wait(30)
        self.driver.find_element_by_class_name(android.widget.ImageView).click()
        self.driver.implicitly_wait(30)
        self.driver.find_element_by_id(com.xk.span.zutuan:id/image_canclePlay).click()
        self.driver.implicitly_wait(30)

    def register_page(self):
        ‘‘‘由注册切换到输入账号界面‘‘‘
        self.driver.find_element_by_id(com.xk.span.zutuan:id/tv_tab_txt4).click()
        self.driver.find_element_by_id(com.xk.span.zutuan:id/stv_user_un_login).click()
        self.driver.find_element_by_id(com.xk.span.zutuan:id/stv_login_mobile).click()

    def input_account(self, phone, password):
        ‘‘‘输入账号密码到首页界面‘‘‘
        self.phone = phone
        self.driver.find_element_by_id(com.xk.span.zutuan:id/et_mobile).send_keys(phone)
        self.driver.find_element_by_id(com.xk.span.zutuan:id/password).click()
        self.driver.find_element_by_id(com.xk.span.zutuan:id/et_password).send_keys(password)
        self.driver.find_element_by_xpath(//*[@text="登录"]).click()
        time.sleep(0.3)

    def switch(self):
        ‘‘‘首页切换到个人中心‘‘‘
        self.driver.find_element_by_xpath(//*[@text="个人中心"]).click()
        time.sleep(0.3)

    def finally_result(self):
        ‘‘‘断言结果是否正确,账号是否在当前元素界面‘‘‘
        if self.phone in self.driver.page_source:
            print(账号正确,登录成功)
        else:
            print(账号错误,登录失败)

    def click_exit(self):
        ‘‘‘点击退出,清理登录信息‘‘‘
        self.driver.implicitly_wait(30)
        self.driver.find_element_by_id(com.xk.span.zutuan:id/iv_user_logout).click()
        self.driver.implicitly_wait(30)
        self.driver.find_element_by_xpath(//*[@text="确定"]).click()
        time.sleep(1)

    def closed(self):
        ‘‘‘关闭驱动对象‘‘‘
        time.sleep(1)
        self.driver.quit()


def run1():
    ‘‘‘根据操作顺序,调用方法执行‘‘‘
    mg = OpenMg()
    mg.slide_pafe()
    mg.register_page()
    mg.input_account(1582, mg123456)
    mg.switch()
    mg.finally_result()
    mg.click_exit()
    mg.closed()


class SearchGoods(OpenMg):
    ‘‘‘搜索商品‘‘‘

    def input_goods(self, content):
        ‘‘‘操作搜索框及输入商品搜索‘‘‘
        self.content = content
        self.driver.find_elements_by_class_name(android.widget.TextView)[0].click()
        self.driver.implicitly_wait(30)
        self.driver.find_element_by_xpath(//*[@text="搜索关键词或粘贴淘宝标题"]).send_keys(self.content)
        time.sleep(0.5)
        self.driver.find_element_by_id(com.xk.span.zutuan:id/tv_search_submit).click()
        # self.driver.swipe(108, 1800, 108, 200, 500)
        # self.driver.swipe(108, 200, 108, 800, 500)
        time.sleep(1)

    def finally_result(self):
        ‘‘‘断言结果是否正确,账号是否在当前元素界面‘‘‘
        if self.content in self.driver.page_source:
            print(搜索商品正确,测试成功)
        else:
            print(搜索商品错误,测试失败)
        self.driver.implicitly_wait(30)

        # super().closed()

    def home_pege(self):
        ‘‘‘搜索界面返回到首页‘‘‘
        self.driver.find_element_by_id(com.xk.span.zutuan:id/iv_back).click()
        self.driver.find_element_by_id(com.xk.span.zutuan:id/iv_back).click()


def run2():
    ‘‘‘根据操作顺序调用方法执行‘‘‘
    sg = SearchGoods()
    sg.slide_pafe()
    sg.register_page()
    sg.input_account(158, mg123456)
    sg.input_goods(短袖)
    sg.finally_result()
    sg.home_pege()
    sg.switch()
    sg.click_exit()
    sg.closed()


class Filter_Goods(SearchGoods):
    ‘‘‘筛选商品‘‘‘

    def filter(self,price1,price2):
        ‘‘‘对筛选框的操作‘‘‘
        self.driver.implicitly_wait(30)
        self.driver.find_element_by_xpath(//*[contains(@text,"筛选")]).click()
        self.driver.find_element_by_xpath(//*[contains(@text,"最低价")]).send_keys(price1)
        self.driver.find_element_by_xpath(//*[contains(@text,"最高价")]).send_keys(price2)
        self.driver.implicitly_wait(30)
        self.driver.find_element_by_id(com.xk.span.zutuan:id/ok).click()


def run3():
    fg = Filter_Goods()
    fg.slide_pafe()
    fg.register_page()
    fg.input_account(1582, mg123456)
    fg.input_goods(书包)
    fg.filter(30,100)
    fg.finally_result()
    fg.home_pege()
    fg.switch()
    fg.click_exit()
    fg.closed()


def main():
    run1()
    run2()
    run3()


if __name__ == __main__:
    main()

 

appium自动化基础总结

上一篇:【知识强化】第四章 网络层 4.8 移动IP


下一篇:Vue - 仓库、axios前后台交互及跨域问题