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

xilove0 2020-05-26

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

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

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

import pytest,time
from appium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

class TestDemo():
    def setup(self):
        caps = {}
        caps["platformName"] = "Android"
        caps["deviceName"] = "Android Emulator"
        caps["appPackage"] = "com.xxxxx.android"
        caps["appActivity"] = ".view.WelcomeActivityAlias"
        caps["autoGrantPermissions"] = "true"
        #Chromedriver可通过Chrome浏览器的inspect看到app自带的web view版本号,再去下载相应的driver
        # caps["chromedriverExecutable"] = "Chromedriver的路径"

        self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
        self.driver.implicitly_wait(20)
        self.driver.find_element_by_id("com.xueqiu.android:id/tv_agree").click()

    def test_webview(self):
        self.driver.find_element_by_xpath("//*[@text=‘男女‘]").click()
        #打印出上下文信息
        for i in range(3):
            time.sleep(5)
            print(self.driver.contexts)

        self.driver.find_element_by_accessibility_id("开户").click()
        #切换到web view中
        self.driver.switch_to.context(self.driver.contexts[1])
        #等待元素展示完全再进行点击和输入内容
        WebDriverWait(self.driver,15).until(expected_conditions.visibility_of_element_located((By.ID,"phone-number")))
        self.driver.find_element_by_id("phone-number").send_keys("13577778881")


    def teardown(self):
        time.sleep(10)
        self.driver.quit()

相关推荐