Android Native和Hybrid两种架构采用Appium进行UI自动化

一、Native和Hybrid两种架构,整理一张图

Android Native和Hybrid两种架构采用Appium进行UI自动化

二、native与web view上下文切换简单代码示例

 1 import pytest,time
 2 from appium import webdriver
 3 from selenium.webdriver.common.by import By
 4 from selenium.webdriver.support import expected_conditions
 5 from selenium.webdriver.support.wait import WebDriverWait
 6 
 7 class TestDemo():
 8     def setup(self):
 9         caps = {}
10         caps["platformName"] = "Android"
11         caps["deviceName"] = "Android Emulator"
12         caps["appPackage"] = "com.xxxxx.android"
13         caps["appActivity"] = ".view.WelcomeActivityAlias"
14         caps["autoGrantPermissions"] = "true"
15         #Chromedriver可通过Chrome浏览器的inspect看到app自带的web view版本号,再去下载相应的driver
16         # caps["chromedriverExecutable"] = "Chromedriver的路径"
17 
18         self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
19         self.driver.implicitly_wait(20)
20         self.driver.find_element_by_id("com.xueqiu.android:id/tv_agree").click()
21 
22     def test_webview(self):
23         self.driver.find_element_by_xpath("//*[@text='男女']").click()
24         #打印出上下文信息
25         for i in range(3):
26             time.sleep(5)
27             print(self.driver.contexts)
28 
29         self.driver.find_element_by_accessibility_id("开户").click()
30         #切换到web view中
31         self.driver.switch_to.context(self.driver.contexts[1])
32         #等待元素展示完全再进行点击和输入内容
33         WebDriverWait(self.driver,15).until(expected_conditions.visibility_of_element_located((By.ID,"phone-number")))
34         self.driver.find_element_by_id("phone-number").send_keys("13577778881")
35 
36 
37     def teardown(self):
38         time.sleep(10)
39         self.driver.quit()