Saturday, August 6, 2016

Automating Mobile Web Gestures Using RSpec and Appium and Ruby's Touch Action library.



The following libraries are used:
ruby 2.3.0
rspec-core (3.4.0)
touch_action (1.3.3)
appium_lib (8.0.2)

The code below will automate the following mobile web gestures using Chrome on a real mobile device:

  tap
  double tap
  swipe left to right
  swipe right to left
  swipe up
  swipe down
  move/drag
  rotate
  pinch


 require 'rubygems'  
 require 'rspec/core'  
 require 'touch_action'  
 require 'appium_lib'  

 describe "Mobile Web Gestures" do  
 
      # install android sdk with platform-tools and build-tools  
      # run 'adb devices' to list deviceName  
      # start appium on a terminal  
      # on another terminal, run the following command once.  
      # bundle install   
      # to run the tests, use the ff:  
      # rspec mobile_web_gestures.rb --format documentation  

      before(:all) do  
           desired_caps = {  
                caps: {  
                     platformName: 'Android',  
                     versionNumber: '5.0.2',  
                     deviceName: '<device_id>',  
                     device: 'Android',  
                     browserName: 'Chrome'  
                }  
           }  
           @driver = Appium::Driver.new(desired_caps).start_driver  
           @driver.manage.timeouts.implicit_wait = 120  
           base_url = 'http://hammerjs.github.io'  
           @driver.navigate.to(base_url + '/')  
           sleep 2  
           @driver.execute_script("window.stop")  
           device = @driver.find_element(:css, 'div.device')  
           @driver.execute_script("arguments[0].scrollIntoView(true);", device)  
           @element = @driver.find_element(:id, 'hitarea')  
      end 
 
      after(:all) do  
           @driver.quit  
      end  

      it "performs mobile gestures" do  
           sleep 2  
           @element.touch_action(:tap)  
           sleep 2  
           @element.touch_action(:doubletap)  
           sleep 2  
           @element.touch_action(:flick, axis: "x", distance: 100, duration: 50)  
           sleep 2  
           @element.touch_action(:flick, axis: "y", distance: 100, duration: 500)  
           sleep 2  
           @element.touch_action(:move, xdist: 70, ydist: -50, duration: 500)  
           sleep 2  
           @element.touch_action(:rotate, {rotation: -75})  
           sleep 2  
           @element.touch_action(:rotate, {rotation: 0})  
           sleep 2  
           @element.touch_action(:pinch, r1: 50, r2: 100)  
      end  

 end  

See it in action here.

Thanks for visiting!

1 comment:

  1. I did above set up and tried touch command but it didn't help .....tap/swipe/actions did not give the error anymore , but also don't really performed any action.

    Do you have any idea,

    ReplyDelete