There is a delay in updating articles on this site, if you want to see articles about Python + Appium, please follow me over to Testhome. Testerhome.com/topics/2780…

From APP Android end automation test beginner’s notes, write wrong place we a lot of advice oh.

Currently, there is a sliding operation in many apps, but these elements can not be located separately, most of them are an array or list. Here are several methods to make the elements slide to the desired position and then stop.

Scroll () method

Webdriver in Appium provides the scroll() method to scroll a page, which only applies to two elements already displayed on the screen, from one element to the other. This method cannot be used if the element does not exist on the current screen or is occluded.

Method introduction:

scroll(self, start_el, stop_el, duration=None) : parameters: -start_el - start element to scroll to - stop_el- element to scroll to - stop_el to scroll from element stop_el to element start_el-durationCopy the code

Upper chestnut: Use the scroll() method to slide from “2008” to “2012”, that is, to slide 2012 up to 2008.

The specific code is as follows:

def scroll() :	
	stop_el = self.driver.find_element_by_xpath("//android.widget.TextView[@text='2008']")
	start_el = self.driver.find_element_by_xpath("//android.widget.TextView[@text='2012']")
	self.driver.scroll(start_el, stop_el)
Copy the code

Note: The above method does not allow you to slide from 2008 to 2013 because 2013 is not displayed on the screen and is obscured.

UiScrollable(

UiScrollable is a subclass of UiCollection. UiScrollable is a subclass of UiCollection. UiScrollable is a subclass of UiCollection.

Related concepts:

  • Step size: time spent from one point to another. The shorter the step, the faster the roll, and the longer the step, the slower the roll.
  • Sweep times: The number of times scrolling is triggered.

Upper scroll: Scroll from “2008” to “2012” using the previous scroll() method.

def test() :self.driver.find_element_by_android_uiautomator(
            'new UiScrollable (new UiSelector (). The scrollable (true)). ScrollIntoView (new UiSelector (), text (" 2012 ")). ScrollToEnd (10, 5)')

# text("2012") :Represents the position to slide# scrollToEnd(10,5) : Scroll to the bottom of the list up to 10 times by step (rate) 5.
Copy the code

Note: this method will slide directly to the bottom or top of the list. It is difficult to judge in the middle. If the list is long, it is recommended not to use it. Is there any great god in the comments section who has a good way to solve this limitation, thanks!

Refer to the article: blog.csdn.net/yiwaChen/ar…

Swipe (

There is already a wrapper class that publishes the swipe() method at juejin.cn/post/691558…

Again using the image in method 1 as an example, the current year is 2008 and you need to swipe or click 2020.

Use cycle implementation, the specific implementation method is:

  • Write a loop and make a judgment in the loop
  • If the element is found, a click is performed
  • If no element is found, slide up the list
  • If you slide up to the bottom of the list and still do not find the data, determine whether the current is at the bottom of the list
    • If you are currently at the bottom of the list, slide down the list
	def swipe() :
            No element found by default
            is_find = False
            # Default order lookup, i.e. slide up
            is_sequential_search = True
            while is_find is not True:
            try:
                self.driver.find_element_by_xpath("//android.widget.TextView[@text='1995']").click()
                is_find = True    # Found, exit the loop
            except NoSuchElementException:
                if is_sequential_search:    # order search
                    # This method calls the encapsulated slide up class
                    self.slide.swipe_up(2500)
                    Get the text value of the year at the bottom of the screen list
                    years = self.driver.find_elements_by_class_name("android.widget.TextView")
                    years_text = years[9].text
                    Check whether the value of text obtained is equal to 2021
                    if years_text == "2021":    # to the end of the list, but not found
                        is_sequential_search = False    # Convert to reverse lookup, i.e., slide
                else:
                    # This method calls the encapsulated scroll down class
                    self.slide.swipe_down_search(2500)
Copy the code

Swipe () is different from swipe(), which allows you to swipe as far as you want, whereas Scroll is swipe based on the distance between two elements on the page.