当前位置:网站首页>Appium automated test scroll and drag_ and_ Drop slides according to element position

Appium automated test scroll and drag_ and_ Drop slides according to element position

2022-07-06 17:49:00 Test Road King

background

We are operating APP When applied , Some need to slide from one element to another , At this time, we cannot determine the coordinates , therefore swipe According to the coordinate sliding mode, it cannot be used , Here's the picture : from Live class Slide up to Live open class Location
 Insert picture description here
At this time, we need to use other sliding methods , We thought we could slide according to the elements ,Appium The main methods of sliding according to elements are scroll and drag_and_drop

scroll Introduce

explain

Scroll from one element to another , It can only be the sliding between two elements .

Method details

def scroll(self: T, origin_el: WebElement, destination_el: WebElement, duration: Optional[int] = None) -> T:
        """Scrolls from one element to another Args: origin_el: the element from which to being scrolling destination_el: the element to scroll to duration: a duration after pressing originalEl and move the element to destinationEl. Default is 600 ms for W3C spec. Zero for MJSONWP. Usage: driver.scroll(el1, el2) Returns: Union['WebDriver', 'ActionHelpers']: Self instance """

        # XCUITest x W3C spec has no duration by default in server side
        if self.w3c and duration is None:
            duration = 600

        action = TouchAction(self)
        if duration is None:
            action.press(origin_el).move_to(destination_el).release().perform()
        else:
            action.press(origin_el).wait(duration).move_to(destination_el).release().perform()
        return self

Parameters :

  • origin_el - Start element to scroll
  • destination_el - End element to scroll to
  • duration - The duration of the , Unit millisecond , The default is 600 ms

Operation scenario

  • Enter Netease cloud homepage
  • Slide from live classroom broadcast to live open class

Key code implementation

#  Locate the classroom live broadcast element 
el1 = driver.find_element(AppiumBy.XPATH, "//*[@text=' Live class ']").click()

#  Locate the live open class element 
el2 = driver.find_element(AppiumBy.XPATH, "//*[@text=' Live open class ']").click()

#  Of board ⾏ Sliding operation 
driver.scroll(el1,el2)

explain

  • The operation process includes inertia , Need to add duration Parameters , The larger the parameter value is , The smaller the inertia .

drag_and_drop Introduce

explain

Slide from one element to another , The second element replaces the original screen position of the first element .

Method details

def drag_and_drop(self: T, origin_el: WebElement, destination_el: WebElement) -> T:
        """Drag the origin element to the destination element Args: origin_el: the element to drag destination_el: the element to drag to Returns: Union['WebDriver', 'ActionHelpers']: Self instance """
        action = TouchAction(self)
        action.long_press(origin_el).move_to(destination_el).release().perform()
        return self

Parameters :

  • origin_el - To slide the starting element of the page
  • destination_el - To slide the page to the end element

Operation scenario

  • Enter Netease cloud homepage
  • Slide from live classroom broadcast to live open class

Key code implementation

#  Locate the classroom live broadcast element 
el1 = driver.find_element(AppiumBy.XPATH, "//*[@text=' Live class ']").click()

#  Locate the live open class element 
el2 = driver.find_element(AppiumBy.XPATH, "//*[@text=' Live open class ']").click()

#  Of board ⾏ Sliding operation 
driver.drag_and_drop(el1,el2)

explain

  • Cannot set duration , No inertia

Slide and drag usage scene selection

Sliding and dragging are nothing more than considering whether it has “ inertia ”, And the parameters passed are “ Elements ” still “ coordinate ”.

  • scroll: Yes “ inertia ” , Pass in “ Elements ”, Can be set by duration Parameters to control the inertia
  • drag_and_drop: nothing “ inertia ” , Pass in “ Elements ”
  • swipe: Yes “ inertia ” , Pass in “ coordinate ”, Can be set by duration Parameters to control the inertia

explain : add to duration Parameters , The larger the parameter value is , The smaller the inertia

The above content is purely personal understanding , If there is any deficiency , Welcome to correct !

If you think the article is good , Welcome to WeChat official account. , The official account of WeChat is regularly pushing the relevant test technology articles.
 Personal micro signal

原网站

版权声明
本文为[Test Road King]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131306402164.html