appium获取toast和操作webview实例

一、toast获取方式:

testerhome上看到很多测试同学分享了toast的获取方式,其中大部分是java版本的,但也有个python版本的:https://testerhome.com/topics/11799

Appium获取toast实际使用了谷歌改造的Uiautomator2测试框架原理,具体改了哪些东西我还木有细看。

前言:

Appium1.5以后版本(server版本)才支持toast定位,并且’automationName’必须设置为’Uiautomator2’。

因为appium底层使用的uiautomator去执行脚本命令,所以在appium1.5版本后其实在它内部加了uiautomator2进行执行脚本,它内部就实现了获取toast的方式。

目前appium1.7Android版本支持:EspressoUiautomator2UiautomatorSelendroid四种驱动模式,后边两个不推荐用了,Espresso这个是最新支持的beta阶段,Uiautomator2是最稳定的,这对搞Espresso的人来说可是好消息。

iOS我还木有调通,这里我讲下Android

环境准备:

1. appium server 1.7版本 下载地址:https://github.com/appium/appium-desktop/releases/tag/v1.5.0

下载appium-desktop-Setup-1.5.0-ia32.exe  并启动服务

2. 安装appium-uiautomator2-driver

使用npm命令安装,npm如何安装自己百度。

npm install appium-uiautomator2-driver

mac本需要自主下载两个apk(appium-uiautomator2-server-v0.1.8.apkappium-uiautomator2-server-debug-androidTest.apk)

需要放在本机 path usr/local/lib/node_modules/appium/node_modules/appium-uiautomator2-driver/uiautomator/目录下

下载地址:https://github.com/appium/appium-uiautomator2-server/releases

3. appium-python-client 只要大于1.0版本以上就行

代码示例:

需要自己有一个可以测试toast的包,这里有一个例子https://github.com/Ericsongyl/AndroidToastUtil

或者直接apkhttps://u3947882.ctfile.com/fs/3947882-246409278将应用安装到手机上。

# coding=utf-8

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.common.by import By

from appium import webdriver

import unittest

import time

class ToastDemo(unittest.TestCase):

    def setUp(self):

        desired_caps={}

        desired_caps['platformName'] = 'Android'

        desired_caps['platformVersion'] = '4.2'

        #desired_caps['unicodeKeyboard'] = 'true'

        desired_caps['deviceName'] = 'Android Emulator'

        desired_caps['automationName']='Uiautomator2'  #这里要声明uiautomator2

        desired_caps['udid'] = '98LFBNP22EYL'

        desired_caps['appPackage'] = 'com.nicksong.toastutil'

        desired_caps['appActivity'] = '.MainActivity'

        self.driver=webdriver.Remote("http://localhost:4723/wd/hub",desired_caps)

    def tearDown(self):

        self.driver.quit()

    def test1(self):

        time.sleep(5)

        self.driver.find_element_by_id("com.nicksong.toastutil:id/bt_default_toast").click()

        toast_loc=("xpath",".//*[contains(@text,'默认')]")

       e1=WebDriverWait(self.driver,20,0.1).until(EC.presence_of_element_located(toast_loc)) #获取toast位置

        print e1.text

参考网址:https://www.cnblogs.com/shangren/p/8191879.html

https://www.cnblogs.com/caoj/p/7813273.html

https://testerhome.com/topics/11799

 

二、Appium webview操作示例

前言:

很早之前有写过通过chromedriver驱动webview的文章:https://blog.csdn.net/jack_chen3/article/details/50281581

Webview测试包:https://u3947882.pipipan.com/fs/3947882-246409183

这里记录一下实例:

class MyTestCase(unittest.TestCase):

    def setUp(self):

        desired_caps = {}

        desired_caps['platformName'] = 'Android'

        desired_caps['platformVersion'] = '4.2'

        #desired_caps['unicodeKeyboard'] = 'true'

        desired_caps['deviceName'] = 'Android Emulator'

        desired_caps['chromeOptions']={'androidProcess': 'com.example.jack.webviewtest'} #需要添加该配置选项

        desired_caps['appPackage'] = 'com.example.jack.webviewtest'

        desired_caps['appActivity'] = '.MainActivity'

        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

        time.sleep(5)

    def tearDown(self):

        self.driver.quit()

 

    def Demo01(self):

        time.sleep(2)

        s=self.driver.contexts  #获取当前存在的上下文对象,可用于切换

        for i in s:

            print i

        print self.driver.current_context

        time.sleep(4)

        self.driver.switch_to.context("WEBVIEW_com.example.jack.webviewtest")  #切换为webview上下文

        time.sleep(5)

        print self.driver.page_source  #打印当前webview的界面

        bb=self.driver.find_element_by_xpath("//*[@id='word']")

        bb.click()

        bb.send_keys("test")

        time.sleep(3)

查看webview界面元素:

打开电脑上的chrome浏览器,输入地址chrome://inspect/,操作手机进入到webview页,电脑可以看到这个页面,然后点击inspect,就可以看到类似手机的界面,当操作电脑网页,手机网页会有相应反应。

找到xpath定位后,直接copy xpath就可以定位到该控件。

PS:需要注意的是电脑网络需要翻墙,因为没有翻墙情况时,打开inspect时会显示为空白情况。

 

 

 

 

猜你喜欢

转载自blog.csdn.net/jack_chen3/article/details/80028937
今日推荐