class TestWxMicro:
def setup(self):
desire_caps = {
“platformName”: ‘android’,
“deviceName”: ‘192.168.216.103:5555’,
“appPackage”: ‘com.tencent.mm’,
“appActivity”: ‘com.tencent.mm.ui.LauncherUI’,
“noReset”: ‘true’,
‘unicodeKeyBoard’: ‘true’,
‘resetKeyBoard’: ‘true’,
“ChromedriverExecutable”: “D:/Study/Automation_Tester_Guide/Lesson6_appium/chromedriver”,
“chromeOptions”: {‘androidProcess’: ‘com.tencent.mm:appbrand0’},
“adbPort”:5038
}
self.driver = webdriver.Remote(“http://127.0.0.1:4723/wd/hub”, desire_caps)
self.driver.implicitly_wait(30)
self.driver.find_element(By.XPATH,"//*[@text='通讯录']")
self.driver.implicitly_wait(10)
def teardown(self):
pass
def test_search(self):
#原生自动化测试
size = self.driver.get_window_size()
width = size.get("width")
height = size.get("height")
self.driver.swipe(int(width*0.5),int(height*0.2),int(width*0.5),int(height*0.9),2000)
self.driver.find_element(By.CLASS_NAME,'android.widget.EditText').click()
self.driver.find_element(By.XPATH,"//*[@text='取消']")
self.driver.find_element(By.CLASS_NAME, 'android.widget.EditText').send_keys("雪球")
self.driver.find_element(By.CLASS_NAME, 'android.widget.Button')
self.driver.find_element(By.CLASS_NAME, 'android.widget.Button').click()
self.driver.find_element(By.XPATH, "//*[@text='自选']")
print(self.driver.contexts)
#进入webview
self.driver.switch_to.context("WEBVIEW_xweb")
self.driver.implicitly_wait(10)
self.find_top_window()
#css定位
self.driver.find_element(By.CSS_SELECTOR,"[src*=stock_add]").click()
#等待新窗口
WebDriverWait(self.driver,30).until(lambda x: len(self.driver.window_handles) > 2)
self.find_top_window()
self.driver.find_element(By.CSS_SELECTOR, "._input").click()
#输入
self.driver.switch_to.context("NATIVE_APP")
ActionChains(self.driver).send_keys("alibaba").perform()
#点击
self.driver.switch_to.context("WEBVIEW_xweb")
self.driver.find_element(By.CSS_SELECTOR, ".stock_item")
self.driver.find_element(By.CSS_SELECTOR, ".stock_item").click()
def find_top_window(self,driver=None):
for window in self.driver.window_handles:
print(window)
if ":VISIBLE" in self.driver.title:
print("find")
print(self.driver.title)
else:
self.driver.switch_to.window(window)
这是完整的代码,想问下1 self.driver.find_element(By.XPATH,“//*[@text=‘通讯录’]”)
这种代码里有好几处,这个有什么作用,又不点击,也没有做其他操作;2 find_top_window(self,driver=None):
for window in self.driver.window_handles:
print(window)
if “:VISIBLE” in self.driver.title:
print(“find”)
print(self.driver.title)
else:
self.driver.switch_to.window(window) 这段,不应该是找visible窗口,然后切换到visible吗,为啥self.driver.switch_to.window(window) 写到else里面了,看不明白,能具体解释下吗