Appium test method - to determine whether there is pop ads

  • Method a: if use conditions, determines whether or pop off element
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()
  • Method Two: Use WebDriverWait + lambda expressions
    to determine whether there is pop ads
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()


  • Method 3: Use the methods provided in place of lambda appium Method
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()

  • Method four: a method using a function instead of 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")

Complete test code based 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")
Published 99 original articles · won praise 43 · views 160 000 +

Guess you like

Origin blog.csdn.net/mayanyun2013/article/details/104503320