• Appium模拟器新app安装后首次进入的滑动操作


    1-10 driver和滑动函数封装结合

    手机app全新安装,首次进入app,一般会显示一个可以左右滑动的,展示新功能的宣传页面。

    在安卓手机的设计,原点位于左上角处,水平方向x 垂直方向为y

    appium获取当前手机显示的宽和高

    driver.get_window_size()['width']
    driver.get_window_size()['height']

    使用python编程控制模拟手指滑动的函数如下

    # coding=utf-8
    # 模拟移动端首次安装手指滑动操作
    
    from appium import webdriver
    
    
    def get_driver():
        capabilities = {
            "platformName": "Android",
            "platformVersion": "6.0.1",
            # "deviceName": "bd619e047cf3",
            "deviceName": "127.0.0.1:7555",
            "app": "D:\Android\open_mooc_701_.apk",
            # "appPackage":新版Appium1.19.1无需手动配置
            # "appActivity":新版Appium1.19.1无需手动配置
            "appPackage":"cn.com.open.mooc",
            "appWaitActivity": "com.imooc.component.imoocmain.splash.GuideActivity"
    
        }
        driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", capabilities)
        return driver
    
    
    # driver.swipe(x,y,x1,y1)
    # driver.swipe(500,400,50,400) #水平从右向左滑动
    # 获取屏幕像素的宽和高
    # size = driver.get_window_size()
    # width = size['width']
    # height = size['height']
    
    
    # 获取屏幕像素宽和高,方法封装
    def get_size():
        # driver = get_driver()
        size = driver.get_window_size()
        width = size['width']
        height = size['height']
        return width, height
    
    
    # 手指向左滑动,方法封装
    def swipe_left():
        x = get_size()[0] / 10 * 9
        y1 = get_size()[1] / 2
        x1 = get_size()[0] / 10
        # driver = get_driver()
        driver.swipe(x, y1, x1, y1)
    
    
    # 手指向右滑动,方法封装
    def swipe_right():
        x = get_size()[0] / 10
        y1 = get_size()[1] / 2
        x1 = get_size()[0] / 10 * 9
        # driver = get_driver()
        driver.swipe(x, y1, x1, y1)
    
    
    # 手指向上滑动,方法封装
    def swipe_up():
        x = get_size()[0] / 2
        y = get_size()[1] / 10 * 9
        y1 = get_size()[1] / 10
        x1 = get_size()[0] / 2
        # driver = get_driver()
        driver.swipe(x, y, x1, y1)
    
    
    # 手指向下滑动,方法封装
    def swipe_down():
        x = get_size()[0] / 2
        y = get_size()[1] / 10
        y1 = get_size()[1] / 10 * 9
        x1 = get_size()[0] / 2
        # driver = get_driver()
        driver.swipe(x, y, x1, y1)
    
    
    # 进一步封装,滑动的方法
    def swipe_on(direction):
        if direction == 'up':
            swipe_up()
        elif direction == 'down':
            swipe_down()
        elif direction == 'left':
            swipe_left()
        else:
            swipe_right()
    
    
    if __name__ == '__main__':
        driver = get_driver() #获取一个全局变量
        swipe_on('left')
        swipe_on('left')

    获取当前打开着的appActivity

    adb shell dumpsys window|findstr mCurrentFocus

    连接mumu模拟器

    adb connect 127.0.0.1:7555

    运行测试模拟器,发现可正确响应。

    1-12 id定位进行登录操作

    使用uiautomatorviewer.bat定位页面元素

    D:Androidandroid-sdk oolsuiautomatorviewer.bat

    在模拟新安装app从右向左滑动4次后,需要点击图片进入立即体验

    if __name__ == '__main__':
        driver = get_driver() #获取一个全局变量driver
        swipe_on('left')
        time.sleep(2)
        swipe_on('left')
        time.sleep(2)
        swipe_on('left')
        time.sleep(2)
        swipe_on('left')
        time.sleep(2)
    
    #点击图片进入“立即体验”
        driver.find_element_by_class_name('android.widget.ImageView').click()

     如果遇到强制升级更新,还需要注意验证点击升级操作

    time.sleep(2)
    #调用升级按钮
        driver.find_elements_by_id('cn.com.open.mooc:id/upgrade_layout').click()

     

    #点击未知源的安装按钮
        time.sleep(2)
        driver.find_elements_by_id('com.android.packageinstaller:id/ok_button').click()

    1-14 层级定位思想分析

    在虚拟机下载并安装最新版本8的慕课网app

     base_update_mooc.py

    首页——>账号

     find_element_by_android_uiautomator('new UiSelector().index(4)')

    1-15 层级定位和list定位结合

    可以通过find_elements_by_class_name方法返回一个元素的列表

    # -*- coding:utf-8 -*-
    # Author:Marlon Kang
    # 模拟手机已经更新app后,进入后的登录操作
    # 需要先手动点击,把初次进页面引导提示取消掉
    import time
    from appium import webdriver
    
    
    #mCurrentFocus = "Window{3c696c4u0cn.com.open.mooc / com.imooc.component.imoocmain.index.MCMainActivity}"
    def get_driver():
        capabilities = {
            "platformName": "Android",
            "platformVersion": "6.0.1",
            "deviceName": "127.0.0.1:7555",
            "appPackage":'cn.com.open.mooc',
            "appActivity": "com.imooc.component.imoocmain.index.MCMainActivity",
            "noReset": 'true',
        }
        driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", capabilities)
        return driver
    
    def login_by_node():
        element1 = driver.find_element_by_id('cn.com.open.mooc:id/tab_layout')
        #通过层级关系和index定位页面元素
        element2 = element1.find_element_by_android_uiautomator('new UiSelector().index(0)')
        element3 = element2.find_element_by_android_uiautomator('new UiSelector().index(4)')
        element3.click()
        #点击登录
        login_by_password()
        return print("login_by_node()点击账号")
    
    def login_by_index():
        element1 = driver.find_element_by_id('cn.com.open.mooc:id/tab_layout')
        #通过层级关系和index定位页面元素
        element2 = element1.find_element_by_android_uiautomator('new UiSelector().index(0)')
        elements = element2.find_elements_by_class_name('androidx.appcompat.app.ActionBar$Tab')
        #账号的索引4
        elements[4].click()
        # 点击登录
        login_by_password()
        return print("login_by_index()点击账号")
    
    
    def login_by_password():
        time.sleep(1)
        driver.find_element_by_id('cn.com.open.mooc:id/header_line').click()
        time.sleep(1)
        driver.find_element_by_id('cn.com.open.mooc:id/tvPassLogin').click()
        #填入信息
        driver.find_element_by_id('cn.com.open.mooc:id/accountEditChannel2').send_keys('15652236641')
        time.sleep(1)
        driver.find_element_by_id('cn.com.open.mooc:id/passwordEditChannel2').send_keys('KYH0403a')
        #点击登录
        time.sleep(1)
        driver.find_element_by_id('cn.com.open.mooc:id/login').click()
    
    def get_web_view():
        time.sleep(8)
        webview = driver.contexts
        print(webview)
    
    
    
    if __name__ == '__main__':
        driver = get_driver()
        time.sleep(3)
        #切换到“账号”底边选项
        login_by_node()

     Appium在底层是调用【UiAutomator2】

    .find_element_by_android_uiautomator('new UiSelector().index(0)')

    .find_element_by_android_uiautomator('new UiSelector().text("abcd")')

    .find_element_by_android_uiautomator('new UiSelector().resourceId(110111)')

    1-18 原生app和H5进行相互切换代码

     

  • 相关阅读:
    JavaScript打印99乘法表
    Python列表推导式玩法
    Python错误重试方法
    pandas + jupyter进行数据处理
    新手小白的爬虫神器-无代码高效爬取数据
    Adb连接模拟器出现版本错误
    Python发送多人邮件报错
    Django入门
    git clone 下载速度解决办法
    Python实现自动刷抖音
  • 原文地址:https://www.cnblogs.com/MarlonKang/p/14225603.html
Copyright © 2020-2023  润新知