How to swipe in mobile browser using selenium


646
646 points
swipe in mobile browser using selenium
swipe in mobile browser using selenium

How to swipe in mobile browser using selenium

Nowdays mobile it’s a must , all websites are responsive and web features are always build in to provide native experience. Since you are here , I guess you wander how to swipe in mobile browser using selenium? How to use selenium swipe actions?

Well , you might already tried the solution from stackoverflow using getLocation()  and action class:

action.press(startX, startY).waitAction(2000).moveTo(endX, endY).release().perform();

Forget about it , at least I did forget about it as is not as reliable as I would like it to be. I did tried to find a solution allover the internet to basically be able to tell the browser to swipe that image/locator back and forward.

Last week one of my coleagues needed the same thing and she basically had a chat with one of our developers , and here you are reading the solution that the dev provided to us.

Swipe in mobile web

Since my current project is ruby-capybara based I will give you the solution in ruby but you’ll get how to do it in your current programing language.

The current solution is using myScroll handler from javascript. The only disatvantage I see into it , it might not work in browsers such as Internet Explorer version 6 or 7 , but who cares about that.

Given a property page : https://m.zoopla.co.uk/to-rent/details/46631845

In my case the image class that I have to swipe is ‘listing-details-photos-46631845’.

 def swipe_photos(direction = "next")
        listing_id = get_listing_id_from_page
        script = "myScroll['listing-details-photos-#{listing_id}'].#{direction}();"
        page.execute_script(script)
 end

Everytime you call the method it will scroll to the next image.

Of course you can scroll backwards

swipe_photos('prev')

You can check more technical details about the scroll handler here.

If you are wandering when to stop scrolling forward and backwards , well its simple you can get the total number of object that you have to scroll and wrap this method up in a for loop 🙂 .

When can you use this solution?

You can use it on desktop , you can use it on mobile web testing using user agents , and you can use it if you are running your mobile web tests using appium.

You CAN’T apply this solutiuon on native apps.

This is pretty much the same like scroll to element only is using a different handler.

Happy testing!

P.S. If you ddon’t know how to to excecute scrips in java you can reffer to this article.


Like it? Share with your friends!

646
646 points
Test engineer

0 Comments

Your email address will not be published. Required fields are marked *

9 + 4 =