appium自动化测试(四)

一. 获取webview的html页面

方法一:

1. 获取webview中对应的html页面

谷歌浏览器中输入地址:chrome://inspect(第一次使用要翻墙)

前提:手机开启USB调试模式,并且用命令adb devices能够识别设备,app要打开webview页面

 

2. appium的日志中会显示当前系统的webdriver版本

3. 根据安卓系统的webview版本,去下载对应的chromedriver.exe

chromedriver下载地址:http://npm.taobao.org/mirrors/chromedriver

寻找webview版本39去对应的chromedriver.exe(上述网址中每个版本下都有一个notes.txt,这个文件列出了每个驱动对应的chrome版本),用下载好的chromedriver.exe取替换appium自带的chromedriver,路径是C:\Program Files (x86)\Appium\resources\app\node_modules\appium\node_modules\appium-chromedriver\chromedriver\win下的chromedriver.exe

4. 点击inpect

方法二:获取网页源码:driver.page_source 存成一个html页面再在浏览器里访问定位

方法二使用的前提也是要替换appium中的chromedriver.exe

思路一:保存后利用批处理调用chrome浏览器自动打开html

在D:\python_workshop\python6_appium\Common下新建一个get_pageSource_1.py,写入下列代码

from appium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from appium.webdriver.common.mobileby import MobileBy
import time, os

#由你主动来告诉appium server,我要操作哪个设备上的哪个app
#Desired Capabilities  键值对。键名已经定义好,不能随意更改
#操作对象 的信息准备
desires_caps = {}
#操作系统  -目标机
desires_caps["platformName"] = "Android"
#系统版本
desires_caps["platformVersion"] = "5.1.1"
#设备名字
desires_caps["deviceName"] = "Android Emulator"
#app信息
desires_caps["appPackage"] = "com.lemon.lemonban"
#首页
desires_caps["appActivity"] = "com.lemon.lemonban.WelcomeActivity"
#连接appium server,并告诉其要操作的对象
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',desires_caps)

WebDriverWait(driver, 30, 1).until(EC.visibility_of_element_located((MobileBy.ID, "com.lemon.lemonban:id/tv_cancel")))
driver.find_element_by_id("com.lemon.lemonban:id/tv_cancel").click()

WebDriverWait(driver, 30, 1).until(EC.visibility_of_element_located((MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("全程班")')))
driver.find_element_by_android_uiautomator('new UiSelector().text("全程班")').click()

WebDriverWait(driver, 60, 0.1).until(EC.visibility_of_element_located((MobileBy.CLASS_NAME, "android.webkit.WebView")))


contexts = driver.contexts
driver.switch_to.context(contexts[-1])

html = driver.page_source
with open(os.path.split(os.path.abspath(__file__))[0] + "/source.html", "w+", encoding="utf-8") as fs:
    fs.write(html)

time.sleep(5)

在桌面新建一个批处理文件get_pageSource_1.bat,写入如下内容

python D:\python_workshop\python6_appium\Common\get_pageSource_1.py
start chrome.exe D:\python_workshop\python6_appium\Common\source.html
cmd /k

双击bat文件,立即运行,运行后的效果

补充:

1. get_pageSource_1.py和创建的html文件是同级目录

2. bat文件的作用是自动执行get_pageSource_1.py,并且调用chrome浏览器自动打开保存的source.html

3. bat文件中python后面跟的是get_pageSource_1.py的全路径,start chrome.exe后面跟的是source.html的全路径,cmd /k的作用是让cmd窗口一直保持打开状态

思路二:保存后利用chromedriver驱动浏览器自动打开html

在D:\python_workshop\python6_appium\Common下新建一个get_pageSource_2.py

import appium
import selenium
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from appium.webdriver.common.mobileby import MobileBy
import time, os

#由你主动来告诉appium server,我要操作哪个设备上的哪个app
#Desired Capabilities  键值对。键名已经定义好,不能随意更改
#操作对象 的信息准备
desires_caps = {}
#操作系统  -目标机
desires_caps["platformName"] = "Android"
#系统版本
desires_caps["platformVersion"] = "5.1.1"
#设备名字
desires_caps["deviceName"] = "Android Emulator"
#app信息
desires_caps["appPackage"] = "com.lemon.lemonban"
#首页
desires_caps["appActivity"] = "com.lemon.lemonban.WelcomeActivity"
#连接appium server,并告诉其要操作的对象
driver_a = appium.webdriver.Remote('http://127.0.0.1:4723/wd/hub',desires_caps)

WebDriverWait(driver_a, 30, 1).until(EC.visibility_of_element_located((MobileBy.ID, "com.lemon.lemonban:id/tv_cancel")))
driver_a.find_element_by_id("com.lemon.lemonban:id/tv_cancel").click()

WebDriverWait(driver_a, 30, 1).until(EC.visibility_of_element_located((MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("全程班")')))
driver_a.find_element_by_android_uiautomator('new UiSelector().text("全程班")').click()

WebDriverWait(driver_a, 60, 0.1).until(EC.visibility_of_element_located((MobileBy.CLASS_NAME, "android.webkit.WebView")))

contexts = driver_a.contexts
driver_a.switch_to.context(contexts[-1])

html = driver_a.page_source
with open(os.path.split(os.path.abspath(__file__))[0] + "/source.html", "w+", encoding="utf-8") as fs:
    fs.write(html)

time.sleep(5)

driver_s = selenium.webdriver.Chrome()
driver_s.maximize_window()
driver_s.get(os.path.split(os.path.abspath(__file__))[0] + "/source.html")

在桌面新建一个批处理文件get_pageSource_2.bat,写入如下内容

python D:\python_workshop\python6_appium\Common\get_pageSource_2.py
cmd /k

双击bat文件,立即运行,运行后的效果

 

个人更倾向于第一种方法,其实两种方法在打开html文件耗时方面差不多

方法三:找开发人员获取网页

 

二. 定位webview内的元素

三. 实例

打开app,点击webview内的收藏

from appium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from appium.webdriver.common.mobileby import MobileBy
import time, os

#由你主动来告诉appium server,我要操作哪个设备上的哪个app
#Desired Capabilities  键值对。键名已经定义好,不能随意更改
#操作对象 的信息准备
desires_caps = {}
#操作系统  -目标机
desires_caps["platformName"] = "Android"
#系统版本
desires_caps["platformVersion"] = "5.1.1"
#设备名字
desires_caps["deviceName"] = "Android Emulator"
#app信息
desires_caps["appPackage"] = "com.lemon.lemonban"
#首页
desires_caps["appActivity"] = "com.lemon.lemonban.WelcomeActivity"
#连接appium server,并告诉其要操作的对象
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',desires_caps)

WebDriverWait(driver, 30, 1).until(EC.visibility_of_element_located((MobileBy.ID, "com.lemon.lemonban:id/tv_cancel")))
driver.find_element_by_id("com.lemon.lemonban:id/tv_cancel").click()

WebDriverWait(driver, 30, 1).until(EC.visibility_of_element_located((MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("全程班")')))
driver.find_element_by_android_uiautomator('new UiSelector().text("全程班")').click()

WebDriverWait(driver, 60, 0.1).until(EC.visibility_of_element_located((MobileBy.CLASS_NAME, "android.webkit.WebView")))

#获取所有的上下文并打印
contexts = driver.contexts
print(contexts)

#切换到webview
driver.switch_to.context(contexts[-1])

#收藏的定位表达式
collection_id = "js-bottom-fav"

#等待元素可见
WebDriverWait(driver, 30, 1).until(EC.visibility_of_element_located((MobileBy.ID, collection_id)))
#点击收藏(未登录会跳转到登录界面)
driver.find_element_by_id(collection_id).click()

#切换原生控件
driver.switch_to.context(None)

运行结果

D:\Program\python34\python.exe D:/python_workshop/python6_appium/Common/lemon_class.py
['NATIVE_APP', 'WEBVIEW_com.lemon.lemonban']

 

四. toast提示信息获取

要获取toast信息要满足以下两个要求:

1. appium版本1.6.3+才支持toast获取

而appium server 1.6.3没有可视化界面,解决方案:下载appium-desktop-Setup-1.4.1-ia32.exe

2. 代码中必须指定automationName为:UIAutomator2

3. UIAutomator2只支持安卓版本5.0+

夜神模拟器默认的安卓版本是4.4.2,可以在夜神多开器中创建并启动一个5.1.1的版本

4. 实例

注意:

1. desired_caps["automationName"] = "UiAutomator2"

2. 要求安装jdk 1.8 64位及以上,配置其环境变量JAVA_HOME和path

3. Android系统5.0以上

4. appium server版本1.6.3以上,由app server向设备上安装两个apk,会遇到超时等问题,如果一次运行失败,请关闭掉appium server重新再运行

5. 实际在运行时,发现判断元素可见的方法不能成功(0/3),而判断元素存在的方法是可以的(3/3)

from appium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from appium.webdriver.common.mobileby import MobileBy
from PageObjects.welcome_page import WelcomePage
from PageObjects.index_page import IndexPage
from PageObjects.login_page import LoginPage

#设备信息
desired_caps = {}
desired_caps["automationName"] = "UIAutomator2"
desired_caps["deviceName"] = "Android Emulator"
desired_caps["platformName"] = "Android"
desired_caps["platformVersion"] = "5.1.1"
desired_caps["appPackage"] = "com.xxzb.fenwoo"
desired_caps["appActivity"] = ".activity.addition.WelcomeActivity"
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)

#滑屏
WelcomePage(driver).swipe_page()
#点击登录
IndexPage(driver).click_login()
#输入手机号
LoginPage(driver).input_phoneNumber("13826741232")

part_str = '验证码已经发送到手机'
xpath_locator = "//*[contains(@text, \'%s\')]" %part_str

try:
    WebDriverWait(driver, 10, 0.1).until(EC.presence_of_element_located((MobileBy.XPATH, xpath_locator)))
except:
    #如果没找到,则打印
    print("toast错过了!")
else:
    text = driver.find_element_by_xpath(xpath_locator).text
    print(text)

运行结果:

验证码已经发送到手机号为138****1232,注意查收,验证码有效时间为60秒,验证码为7201

 

转载于:https://www.cnblogs.com/my_captain/p/9346646.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值