appium測試中验证toast的正确性

1)說明:appium使用uiaotomator底層的機制來分析抓取toast,並且把toast放到控件樹裡面,但本身並不屬於控件
2)capability參數設置android工作引擎,具體來說應該是appium要指定這個來驅動android客戶端設備,貌似android目前已有的版本都是這個工作引擎,以後就不知道了
automationName : uiautomator2
3)getPageSource是無法找到的,得用xpath

//*[@class=“class屬性值”]

//*[contain(@text,“text文本)”]

一、在原生中獲取toast文本

from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy

class TestToast:
    def setup(self):
        caps = {}
        caps["platformName"] = 'Android'
        caps["deviceName"] = '192.168.111.101:5555'
        caps["appPackage"] = 'com.example.android.apis'
        caps["appActivity"] = '.view.PopupMenu1'
        caps["automationName"] = 'uiautomator2'
        caps["noReset"] = 'true'
        caps["dontStopAppOnReset"] = 'true'
        caps["skipDeviceInitializati"] = 'true'
        caps["unicodeKeyboard"] = 'true'
        caps["resetKeyboard"] = 'true'

        self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", caps)
        self.driver.implicitly_wait(10)

    def teardown(self):
        # self.driver.back()
        self.driver.quit()

    def test_toast(self):
        """
        1.打開 API Demos -> Views -> Popup Menu
        2.點擊search
        3.獲取toast
        :return:
        """
        self.driver.find_element_by_xpath('//*[@text = "Make a Popup!"]').click()
        self.driver.find_element_by_id('android:id/title').click()
        print(self.driver.page_source)
        #複製打印信息中toast的class屬性並打印值
        #方法一
        # print(self.driver.find_element(MobileBy.XPATH, "//*[@class='android.widget.Toast']").text)
        #方法二
        print(self.driver.find_element(MobileBy.XPATH, "//*[contains(@text,'Clicked popup')]").text)

二、在H5頁面中獲取toast

方法一,打印當前頁面源碼,assert toast文本在源碼中,即斷言正確。
比如雪球開戶,點擊“A股開戶”—>輸入手機號和驗證碼—>點擊“立即開戶”,若沒有點擊獲取驗證碼而直接開戶就會toast提示“請獲取驗證碼”
代碼:

from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait


class TestAW:
    def setup(self):
        caps = {}
        caps["platformName"] = 'Android'
        caps["deviceName"] = '192.168.111.101:5555'
        caps["platformVersion"] = '6.0'
        caps["appPackage"] = 'com.xueqiu.android'
        caps["automationName"] = 'uiautomator2'
        caps["appActivity"] = '.common.MainActivity'
        caps["noReset"] = 'true'
        caps["dontStopAppOnReset"] = 'true'
        caps["skipDeviceInitializati"] = 'true'
        caps["unicodeKeyboard"] = 'true'
        caps["resetKeyboard"] = 'true'
        caps["chromedriverExecutable"] = 'C:\Program Files (x86)\Appium\chromedriver74.exe'

        self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", caps)
        self.driver.implicitly_wait(10)

    def teardown(self):
        # self.driver.quit()
        self.driver.back()

    def test_xueqiu_aw(self):
        """
        1.打開雪球 APP
        2.點擊“交易”
        3.點擊“A股開戶”
        4.在輸入用戶名和密碼
        5.點擊“立即開戶”
        6.退出應用
        :return:
        """

        # 2.點擊“交易”
        self.driver.find_element(MobileBy.XPATH,'//*[@text="交易"]').click()
        A_locator = (MobileBy.XPATH,'//*[@class="trade_home_agu_3ki"]/div[2]/h1')

        # 获取当前上下文
        print(self.driver.contexts)
        self.driver.switch_to.context(self.driver.contexts[1])
        WebDriverWait(self.driver ,10).until(expected_conditions.element_to_be_clickable(A_locator))

        # 3.點擊“A股開戶”
        self.driver.find_element(*A_locator).click()
        print(self.driver.window_handles)


        # 获取当前打开的窗口
        self.driver.switch_to.window(self.driver.window_handles[-1])
        phonenumber_locator = (MobileBy.ID,'phone-number')
        WebDriverWait(self.driver,60).until(expected_conditions.element_to_be_clickable(phonenumber_locator))

        # 4.在輸入用戶名和密碼,點擊“立即開戶”
        self.driver.find_element(*phonenumber_locator).send_keys('18218813163')
        self.driver.find_element(MobileBy.ID,'code').send_keys('123456')
        self.driver.find_element(MobileBy.XPATH,'/html/body/div/div/div[2]/div/div[2]/h1').click()

        # 获取toast文本
        print(self.driver.page_source)
        assert  "请获取验证码” in self.driver.page_source

方法二:只判断toast可见,返回true。
这里有个问题,就是不知道是操作失败还是成功导致的toast,总之我用原生方法获取不到文本信息只能判断它出现了,H5页面获取toast文本内容以后有空研究下吧。

        # 获取toast文本
        while True:
            toast_text = self.driver.find_element(By.XPATH,'//*[text()="请先获取验证码"]').is_displayed() 
            if toast_text:
                break
        assert  toast_text == True

你可能感兴趣的:(appium)