Appium自动化测试之IOS WebView和Safari

前言

不管是iOS平台的web view还是safari浏览器都需要安装及设置以下三点:

1、brew install --HEAD libimobiledevice

2、brew install ios-webkit-debug-proxy

3、settings(设置) > safari(浏览器) > advanced(高级) > web inspector(网页检查器) -> on(打开)

 

iOS WebView

 1 import time
 2 from appium import webdriver
 3 
 4 class Test_Demo():
 5     def setup(self):
 6         caps = {}
 7         caps["deviceName"] = "iPhone 11 Pro Max"
 8         caps["platformVersion"] = "13.5"
 9         caps["platformName"] = "ios"
10         caps["automationName"] = "xcuitest"
11         # caps["noReset"] = True
12         caps["app"] = "/Users/aaron/Desktop/UICatalog.app.zip"
13         caps["startIWDP"] = True
14 
15         self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
16         self.driver.implicitly_wait(20)
17 
18     def test_click(self):
19         self.driver.find_element_by_accessibility_id("Web View").click()
20         #每隔一秒打印一下上下文
21         for i in range(3):
22             print(self.driver.contexts)
23             time.sleep(1)
24         #切换webview里
25         self.driver.switch_to.content(self.driver.contexts[1])
26         #打印网页源文件
27         print(self.driver.page_source)
28         #保存图片到本地
29         self.driver.save_screenshot("main.png")
30         #点击bug按钮
31         self.driver.find_element_by_link_text("Buy").click()
32         time.sleep(10)
33         self.driver.save_screenshot("buy.png")
34 
35     def teardown(self):
36         time.sleep(20)
37         self.driver.quit()

如上我是在模拟器中运行的代码。

 

iOS Safari

 1 from appium import webdriver
 2 from selenium.webdriver.common.by import By
 3 from selenium.webdriver.support import expected_conditions
 4 from selenium.webdriver.support.wait import WebDriverWait
 5 import time
 6 
 7 class TestView():
 8     def setup(self):
 9         caps = {}
10         caps["deviceName"] = "iPhone 11 Pro Max"
11         caps["platformVersion"] = "13.5"
12         caps["platformName"] = "ios"
13         caps["automationName"] = "xcuitest"
14         caps["browserName"] = "Safari"
15         # 如果是真机需要加上如下四项
16         # caps["startIWDP"] = True
17         # caps["xcodeOrgId"] = "8278HWK4WG"
18         # caps["xcodeSigningId"] = "iPhone Developer"
19         # caps["udid"] = "2721277896a12c5170b349ef1a0c350c305d85ce"
20 
21         self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
22 
23     def test_webview(self):
24         url = "http://www.baidu.com"
25         self.driver.get(url=url)
26         WebDriverWait(self.driver,15).until(expected_conditions.visibility_of_element_located((By.NAME,"word")))
27         self.driver.find_element_by_name("word").send_keys("appium")
28 
29     def teardown(self):
30         time.sleep(20)
31         self.driver.quit()

 

posted @ 2020-06-01 23:55  韩凯1202  阅读(1539)  评论(0编辑  收藏  举报