Appium测试方法-判断是否有弹窗广告

  • 方法一:使用if 条件,判断是否有弹窗关闭元素
sleep(20)
#判断是否有广告的关闭按钮:book_button_close,如有则点击关闭
if len(self.driver.find_elements_by_id('book_button_close')) >= 1:
self.driver.find_element_by_id('book_button_close').click()
  • 方法二:使用WebDriverWait+lambda表达式,
    判断是否有弹窗广告
from selenium.webdriver.support.wait import WebDriverWait
WebDriverWait(self.driver, 15).until(lambda x:len(self.driver.find_elements_by_id('book_button_close'))>=1)
self.driver.find_element_by_id('book_button_close').click()


  • 方法三:使用appium提供的方法代替lambda方法
WebDriverWait(self.driver, 15).until(
    expected_conditions.visibility_of_element_located((By.id,'book_button_close'))
         ) 
self.driver.find_element_by_id('book_button_close').click()

  • 方法四:使用函数代替lambda方法
        def loaded(driver)
            print(datetime.datetime.now())
            sleep(20)
            if len(self.driver.find_elements_by_id('book_button_close')) >= 1:
                self.driver.find_element_by_id('book_button_close').click()
                return True
            else:
                return False
        try:
            WebDriverWait(self.driver, 15).until(loaded)
        except:
            print("game center's home page has no ad")

完整的基于pytest的测试代码

 def test_start_game(self):
	self.test_start_agreement()
    self.driver.implicitly_wait(10)
        # 定位游戏手柄
    el2 = self.driver.find_element_by_xpath(
            "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.support.v4.widget.DrawerLayout/android.widget.FrameLayout/android.view.View/android.widget.LinearLayout/android.view.View/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.ImageView")
	el2.click()
    def loaded(driver)
      	print(datetime.datetime.now())
	    sleep(20)
        if len(self.driver.find_elements_by_id('book_button_close')) >= 1:
                self.driver.find_element_by_id('book_button_close').click()
                return True
            else:
                return False
	try:
       WebDriverWait(self.driver, 15).until(loaded)
    except:
       print("game center's home page has no ad")
发布了99 篇原创文章 · 获赞 43 · 访问量 16万+

猜你喜欢

转载自blog.csdn.net/mayanyun2013/article/details/104503320