Swipe Gesture

To perform swiping from left to right , right to left , bottom to top and top to bottom first we required to find coordinates of (x and y axis) of the device.

By using get_window_size() method we can find the device height and width. Then we can create starting and ending X , Y coordinates and swipe the screen of the device.

We need to turn on the pointer location in device settings to find the x and y coordinates of a device.

Go to settings > Tap build number 7 time > click on Developer options > turn on "Pointer location" toggle .

Syntax :


# Step 1 : Find the device width and height
deviceSize = driver.get_window_size()

# Step 2 : Find the x,y coordinate to swipe from Bottom to Top
startx = screenWidth/2
endx = screenWidth/2
starty = screenHeight*8/9
endy = screenHeight/9

# Step 3 : Create TouchAction class object
actions = TouchAction(driver)

# Step 4 : Call long press method along with move_to method
actions.long_press(None,startx,starty).move_to(None,endx,endy).release().perform()

Example: In the below example we are launching the app on an Android device and click on the tab activity and then perform the swiping from Left to Right and Right to Left.

  • Launch the App on an Android device.
  • Click on the tap activity
  • Find the device width and height
  • Create X , Y coordinates to swipe the screen from Left to Right and Right to Left
  • Create TouchAction class object
  • Using the TouchAction object, call the long_press() method and move_to() method
  • Wait for 2 seconds
  • Close the App

SwipeGesture.py

import time
from appium import  webdriver
from appium.webdriver.common.touch_action import TouchAction
from selenium.common.exceptions import ElementNotVisibleException, ElementNotSelectableException, NoSuchElementException
from selenium.webdriver.support.wait import WebDriverWait
from appium.webdriver.common.appiumby import AppiumBy

# Step 1 : Create "Desired Capabilities"
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['automationName'] = 'UiAutomator2'
desired_caps['platformVersion'] = '10'
desired_caps['deviceName'] = 'Pixel3XL'
desired_caps['app'] = ('/Skill2Lead/Appium_Demo_App/Android/Android_Appium_Demo.apk')
desired_caps['appPackage'] = 'com.skill2lead.appiumdemo'
desired_caps['appActivity'] = 'com.skill2lead.appiumdemo.MainActivity'

# Step 2 : Create "Driver object"
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)

# Step 3 : Create "WebDriver object"
wait = WebDriverWait(driver, 25, poll_frequency=1,
                     ignored_exceptions=[ElementNotVisibleException, ElementNotSelectableException,
                                         NoSuchElementException])

# Step 4 : Click on the tap activity
ele = wait.until(lambda  x: x.find_element(AppiumBy.ANDROID_UIAUTOMATOR,'text("TAB ACTIVITY")'))
ele.click()

# Step 5 : Find the device width and height
deviceSize = driver.get_window_size()
print("Device Width and Height : ",deviceSize)
screenWidth = deviceSize['width']
screenHeight = deviceSize['height']

# Step 6 : Find the x,y coordinate to swipe
# *********** Left to Right ************* #
startx = screenWidth*8/9
endx = screenWidth/9
starty = screenHeight/2
endy = screenHeight/2

# *********** Right to Left ************* #
startx2 = screenWidth/9
endx2 = screenWidth*8/9
starty2 = screenHeight/2
endy2 = screenHeight/2


actions = TouchAction(driver)

# Step 7 : perform the action swiping from left to Right
actions.long_press(None,startx,starty).move_to(None,endx,endy).release().perform()

# Step 8 : perform the action swiping from Right to Left
actions.long_press(None,startx2,starty2).move_to(None,endx2,endy2).release().perform()

time.sleep(2)
driver.quit()


Example: In the below example we are launching the app on an Android device and click on the tab activity and then perform the swiping from Bottom to Top and Top to Bottom.

  • Launch the App on an Android device.
  • Click on the ScrollView button
  • Find the device width and height
  • Create X , Y coordinates to swipe the screen from Bottom to Top and Top to Bottom
  • Create TouchAction class object
  • Using the TouchAction object, call the long_press() method and move_to() method
  • Wait for 2 seconds
  • Close the App

SwipeGesture.py

import time
from appium import  webdriver
from appium.webdriver.common.touch_action import TouchAction
from selenium.common.exceptions import ElementNotVisibleException, ElementNotSelectableException, NoSuchElementException
from selenium.webdriver.support.wait import WebDriverWait
from appium.webdriver.common.appiumby import AppiumBy

# Step 1 : Create "Desired Capabilities"
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['automationName'] = 'UiAutomator2'
desired_caps['platformVersion'] = '10'
desired_caps['deviceName'] = 'Pixel3XL'
desired_caps['app'] = ('/Skill2Lead/Appium_Demo_App/Android/Android_Appium_Demo.apk')
desired_caps['appPackage'] = 'com.skill2lead.appiumdemo'
desired_caps['appActivity'] = 'com.skill2lead.appiumdemo.MainActivity'

# Step 2 : Create "Driver object"
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)

# Step 3 : Create "WebDriver object"
wait = WebDriverWait(driver, 25, poll_frequency=1,
                     ignored_exceptions=[ElementNotVisibleException, ElementNotSelectableException,
                                         NoSuchElementException])

# Step 4 : Click on the ScrollView button
ele = wait.until(lambda  x: x.find_element(AppiumBy.ANDROID_UIAUTOMATOR,'text("ScrollView")'))
ele.click()

# Step 5 : Find the device width and height
deviceSize = driver.get_window_size()
print("Device Width and Height : ",deviceSize)
screenWidth = deviceSize['width']
screenHeight = deviceSize['height']

# Step 6 : Find the x,y coordinate to swipe
# *********** Swiping from Bottom to Top ************* #
startx = screenWidth/2
endx = screenWidth/2
starty = screenHeight*8/9
endy = screenHeight/9


# *********** Swiping from Top to Bottom ************* #
startx2 = screenWidth/2
endx2 = screenWidth/2
starty2 = screenHeight*2/9
endy2 = screenHeight*8/9

actions = TouchAction(driver)
actions.long_press(None,startx,starty).move_to(None,endx,endy).release().perform()
time.sleep(2)
actions.long_press(None,startx2,starty2).move_to(None,endx2,endy2).release().perform()

time.sleep(2)
driver.quit()