python+appium automated test - element waiting

This article is mainly used to sort out several element waiting methods in the process of executing automated testing ...

This article is mainly used to sort out several element waiting methods in the process of executing automated testing

  • implicity_wait()
  • sleep()
  • wait_activity()
  • Wait for an element to appear before executing the operation
  • WebDriverWait()
1, Implicit_ Wait () - the following case is microblog

Implicit wait: it belongs to global wait. It is not for a certain element, but for all elements of the current session (i.e. the life cycle of the current driver object). Therefore, it only needs to be set once when constructing the driver object.

If this command is not sent, the driver should default to implicitly waiting for 0s

Implicit waiting for appium official documents

self.driver.implicitly_wait(5) # waits 5 seconds

Implicit waiting is generally the same as the setting of starting the app. The storage location is shown in the following figure:

2, sleep()

Forced waiting: the sleep() method is provided by the time module of python, so you need to import: from time import sleep; After the sleep() method is executed, the sleep will be forced. The sleep time can be set in parentheses. The number in parentheses is in seconds, for example, sleep for 1 second: sleep(1). The parentheses can also be expressed with a decimal point, for example, sleep for 1.5 seconds: sleep(1.5)

from time import sleep # Forced sleep for 1 second sleep(1) # Forced sleep for 1.5 seconds sleep(1.5)
3, wait_activity() - the following case is QQ

wait_activity(): determine the activity of the page where the button needs to be executed appears, and then perform the operation on the page

#activity where the login button is located: com.tencent.mobileqq//.activity.LoginActivity # Set to wait for 100S, refresh every 2S, and wait for the activity page where the login button is located to appear, current_activity refers to the current activity page driver.wait_activity(".activity.LoginActivity",100,interval=2) # activity of the current page AC = driver.current_activity print(AC)

Note: how to get the activity of the current page: Enter: ADB shell dumpsys activities in the cmd command

The red box in the figure below indicates the activity of the current page. The figure below shows the microblog instance, so the activity of the current page is: com.sina.weibo/.SplashActivity

Judgment in code can be abbreviated as ". SplashActivity"

4, Wait for an element to appear before executing the operation - the following case is QQ

This method determines whether the button to be executed already exists in the page. If it exists, click it. If it does not exist, output the content

def check_LoginBtn(): """Click after the login button appears""" print("check_LoginBtn is running") try: LoginBtn = driver.find_element_by_id("com.tencent.mobileqq:id/btn_login") except NoSuchElementException: print("LoginBtn Element no Exist!") else: LoginBtn.click() check_LoginBtn) #Call function
5, WebDriverWait () - the following case is QQ

Display wait: WebDriverWait is the method provided by webdriver, and the until() and until provided by this class_ The not () method is then used to judge the exception according to the condition_ Conditions to wait. The until() method interrupts the wait when it returns to turn_ The not () method interrupts the wait when the return value is False. And implicity_ The difference between wait() and display wait() is to wait for the location of a single element. Check whether the element to be located is loaded every other period of time. If the element is not located within the time specified in the parameter, the location of the element fails and an exception is thrown.

1.Format: WebDriverWait(driver,timeout,poll_frequency=0.5,ignored_exceptions=None) driver: webdriver Drivers, such as( IE,FireFox,chrom) timeout: Timeout, in seconds by default poll_frequency=0.5: The interval of sleep time (step). The default is 0.5 Second, that is, the frequency of detecting whether the element exists ignored_exceptions=None: Exceptions after timeout are thrown by default:"NOSuchElementException",You can define ignored exception information
#lambda expression is equivalent to a function, which means that the click operation will be executed within 100 seconds until the id element is found, and WebDriverWait means driver # WebDriverWait(driver,100).until(lambda driver:driver.find_element_by_id("com.tencent.mobileqq:id/btn_login")) # driver.find_element_by_id("com.tencent.mobileqq:id/btn_login").click() WebDriverWait(driver,100).until(EC.presence_of_element_located((By.ID,"com.tencent.mobileqq:id/btn_login"))) driver.find_element_by_id("com.tencent.mobileqq:id/btn_login").click()
#QQ demo WebDriverWait() element waiting from appium import webdriver from time import sleep from selenium.common.exceptions import NoSuchElementException from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By desired_caps = {"platformName": "Android", "deviceName": "U4AIUKFAL7W4MJLR", "platforVersion": "9", "appPackage": "com.tencent.mobileqq", "appActivity": "com.tencent.mobileqq.activity.SplashActivity", "automationName": "UiAutomator2", "noRest": True } driver = webdriver.Remote("http://localhost:4723/wd/hub",desired_caps) #Login id: com.tencent.mobileqq:id/btn_login #Method 4: WebDriverWait #lambda expression is equivalent to a function, which means that the click operation will be executed within 100 seconds until the id element is found, and WebDriverWait means driver # WebDriverWait(driver,100).until(lambda driver:driver.find_element_by_id("com.tencent.mobileqq:id/btn_login")) # driver.find_element_by_id("com.tencent.mobileqq:id/btn_login").click() WebDriverWait(driver,100).until(EC.presence_of_element_located((By.ID,"com.tencent.mobileqq:id/btn_login"))) driver.find_element_by_id("com.tencent.mobileqq:id/btn_login").click() #Account element: class: android.widget.EditText #Password element: id: com.tencent.mobileqq:id/password def check_LoginText(): """Verify whether the account number input box exists""" print("check_LoginText is running") try: LoginText = driver.find_element_by_class_name("android.widget.EditText") except NoSuchElementException: print("LoginText Element no Exist!") else: LoginText.send_keys("111") driver.find_element_by_id("com.tencent.mobileqq:id/password").send_keys("222") def check_PwdText(): """Verify whether the password input box exists""" print("check_PwdText is running") try: PwdText = driver.find_element_by_id("com.tencent.mobileqq:id/password") except NoSuchElementException: print("PwdText Element no Exist!") else: PwdText.send_keys("222") check_LoginText() check_PwdText()

12 November 2021, 02:14 | Views: 2545

Add new comment

For adding a comment, please log in
or create account

0 comments