How python implementation elements waiting

First, why should the elements waiting for?

  In the UI automation process, the influence of the elements appear by the network environment, equipment performance and other elements. Therefore, the elements to load and run scripts to the element of time inconsistency, will complain: the element can not be located.

  For example, a simple case: the actual UI test automation, click on a login control interface need to start a new activity, or need to load the bomb box, or request the network to load data after successfully refreshed the page, then need to wait for some time, the new interface appeared to UI to continue operating, otherwise data is still loaded ing, the script has begun to implement a new interface code for operation, the script error.

Second, the role of the elements waiting

  1. Set elements wait, enhance the robustness of the script, to improve the efficiency;
  2. Nature is to address timing mismatch: the script executes, script execution speed and loading speed is not necessarily the same page elements

Third, the type of elements waiting

  1. Forced to wait: Wait die, set a fixed latency - sleep (2) # forced to wait 5s, time to import the package (import time)
  2. Recessive wait: for the whole of the elements of the set wait time - driver.implicitly_wait (5)
  3. Dominant Wait: to wait for a certain set of elements - WebDriverWait (driver, timeout, poll_frequency = 0.5, ignored_exception = None), to be introduced into the package (from selenium.webdriver.support.ui import WebDriverWait)

Fourth, how to set the elements waiting

  Application Example 1. Forced waiting:

from appium import webdriver
import time
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.1.1'
desired_caps['deviceName'] = '127.0.0.1:62001'
desired_caps['packageName'] = 'com.cnblogs.android'
desired_caps['packagActivity'] = 'com.cnblogs.android.SplashActivity'
desired_caps['unicodeKeyboard'] = True
desired_caps['resetKeyboard'] = True
Driver = webdriver.Remote ( ' http://127.0.0.1:8888/wd/hub ' , desired_caps)
 # forced to wait 5s, regardless of whether or not to wait for the elements appear, we will have to wait 5S 
the time.sleep (5 )
driver.find_element_by_id('com.cnblogs.android:id/TabSearch').click()
time.sleep(2)
driver.find_element_by_id('com.cnblogs.android:id/txtSearch').send_keys('方缘')
driver.find_element_by_id('com.cnblogs.android:id/search_btn').click()
driver.quit()

  2. implicit wait:

from appium import webdriver
import time
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.1.1'
desired_caps['deviceName'] = '127.0.0.1:62001'
desired_caps['packageName'] = 'com.cnblogs.android'
desired_caps['packagActivity'] = 'com.cnblogs.android.SplashActivity'
desired_caps['unicodeKeyboard'] = True
desired_caps['resetKeyboard'] = True
Driver = webdriver.Remote ( ' http://127.0.0.1:8888/wd/hub ' , desired_caps)
 # recessive (wait all the elements), the longest 3S 
driver.implicitly_wait (. 3 )
driver.find_element_by_id('com.cnblogs.android:id/TabSearch').click()
time.sleep(2)
driver.find_element_by_id('com.cnblogs.android:id/txtSearch').send_keys('方缘')
driver.find_element_by_id('com.cnblogs.android:id/search_btn').click()
driver.quit()

  3. dominant waiting:

from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import time
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.1.1'
desired_caps['deviceName'] = '127.0.0.1:62001'
desired_caps['packageName'] = 'com.cnblogs.android'
desired_caps['packagActivity'] = 'com.cnblogs.android.SplashActivity'
desired_caps['unicodeKeyboard'] = True
desired_caps['resetKeyboard'] = True
Driver = webdriver.Remote ( ' http://127.0.0.1:8888/wd/hub ' , desired_caps)
 # dominant (wait a specific element appears) 
# the lambda provided a method of dynamically creating a run-time function. 
WebDriverWait (Driver,. 3) .until ( the lambda X: x.find_element_by_id ( ' com.cnblogs.android:id/TabSearch ' .)) The Click ()
driver.find_element_by_id('com.cnblogs.android:id/TabSearch').click()
time.sleep(2)
driver.find_element_by_id('com.cnblogs.android:id/txtSearch').send_keys('方缘')
driver.find_element_by_id('com.cnblogs.android:id/search_btn').click()
driver.quit()

 

Guess you like

Origin www.cnblogs.com/huainanhai/p/11831277.html