028-86922220

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

Appium+Python实现自动化登录

#Appium+Python实现自动化测试

创新互联专注于尼勒克网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供尼勒克营销型网站建设,尼勒克网站制作、尼勒克网页设计、尼勒克网站官网定制、小程序定制开发服务,打造尼勒克网络公司原创品牌,更为您提供尼勒克网站排名全网营销落地服务。

Appium简介

Appium 安装方式

1:直接安装Appium客户端

Appium官网,点击downLoad即可

2:命令行本地安装
1:安装node
2:npm install -g appium
3:npm install -g appium-doctor
appium-doctor是检查你的本地环境是否正常的工具
注:如果权限问题请加上sudo,appium-doctor在新版appium已经移除,需要利用npm安装
sudo npm install -g appium-doctor,安装完成后输入appium-doctor检测环境是否正常
如果遇到缺少js文件错误,说明你的node版本太低,需要升级nodejs,升级的方式为
    1: sudo npm cache clean -f  清楚nodejs的cache
    2:sudo npm install -g n 管理npm的工具
    3:sudo n stable 升级node
    4: sudo npm install npm@latest -g 更新npm
    4:node -v查看node版本(最新为11.4.0)
再次运行appium-doctor查看本地环境配置是否正常,不正常修复即可

Appium+Python实现自动化登录

安装Python

安装方式网上很多,不在阐述,目前我用的是python3.7
推荐的ide为:Pycharm

编写自动化用例

from appium import webdriver
import time

class UiTest(object):

    def __init__(self):
        # 设备信息
        self.config = {
            "platformName": "Android",
            "platformVersion": "5.1.1",
            "deviceName": "Pixel XL",
            "automationName": "app",
            "app": '/Users/davidxiong/Desktop/wx.apk',
        }
        # 有多重模拟点击方式:
        # 1:坐标,
        # 2:id
        # 3:xpath
        # 4:name,这边我采用坐标和xpath,因为微信的元素id会动态改变

        # 手机号元素路径
        self.phone = '/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.View/android.widget.FrameLayout[2]/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.ScrollView/android.widget.LinearLayout/android.widget.LinearLayout[2]/android.widget.EditText'
        # 手机号点击登录元素路径
        self.phone_enter = '/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.View/android.widget.FrameLayout[2]/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.ScrollView/android.widget.LinearLayout/android.widget.Button[2]'
        # 密码路径
        self.pass_word = '/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.View/android.widget.FrameLayout[2]/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.ScrollView/android.widget.LinearLayout/android.widget.LinearLayout[2]/android.widget.EditText'
        # 密码确认路径
        self.pass_word_enter = '/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.View/android.widget.FrameLayout[2]/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.ScrollView/android.widget.LinearLayout/android.widget.Button[2]'
        # 关闭匹配框
        self.close_match = '/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.LinearLayout[2]/android.widget.LinearLayout/android.widget.Button[1]'
        # 个人中心
        self.mine_info = '//android.widget.FrameLayout[@content-desc="当前所在页面,与的聊天"]/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.View/android.widget.FrameLayout[1]/android.widget.FrameLayout/android.widget.FrameLayout/com.tencent.mm.ui.mogic.WxViewPager/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.ListView/android.widget.LinearLayout[1]/android.widget.RelativeLayout/android.widget.LinearLayout'
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', self.config)

        # editText输入

    def edit_input(self, et_id, content, xpath='', timer=-1):
        if xpath == '':
            pl = self.driver.find_element_by_id(et_id)
        else:
            pl = self.driver.find_element_by_xpath(xpath)

        if timer != -1:
            time.sleep(timer)
        pl.send_keys(content)

        # 点击事件

    def click(self, btn_id, xpath='', timer=-1):
        if timer != -1:
            time.sleep(timer)
        if xpath == '':
            self.driver.find_element_by_id(btn_id).click()
        else:
            self.driver.find_element_by_xpath(xpath).click()

    def run(self):
        time.sleep(2)
        # 点击登录
        self.driver.tap([(259, 1773)])
        # 输入手机号
        self.edit_input('ht', '你的账号', xpath=self.phone, timer=1)
        # 点击下一步,这边采用xpath
        self.click('akb', xpath=self.phone_enter)
        # 输入密码
        time.sleep(2)
        self.edit_input('ht', '密码', xpath=self.pass_word, timer=1)
        # 登录R
        self.click('akb', xpath=self.pass_word_enter)
        time.sleep(8)
        # 关闭提示匹配通讯录弹出框
        self.click('akb', xpath=self.close_match)
        # 等地同步数据
        time.sleep(30)
        # 点击我的,这边采用坐标
        self.driver.tap([(933, 1823)])
        # 查看我的个人信息
        self.click('akb', xpath=self.mine_info)

if __name__ == "__main__":
        UiTest().run()

本文名称:Appium+Python实现自动化登录
文章位置:http://www.tsicrk.com/article/jocojg.html

其他资讯

让你的专属顾问为你服务

1.3892s