This is very useful tutorial for those who are learning appium as swiping and scrolling is commonly used for almost all mobile apps. So learning to automate it using Appium is going to help you in writing test code.Below is the code where I have written different functions for swiping from top to bottom, from bottom to top, from left to right, from right to left using Appium. We can all these methods in our test cases to perform required actions. plus I have created a video tutorial with detailed explanation.
/** /* Method to swipe screen from Bottom to Top (Vertical) Get the size of * screen. Find swipe start and end point from screen's width and height. * Find starty point which is at bottom side of screen. Find endy point * which is at top side of screen. Find horizontal point where you wants to * swipe. It is in middle of screen width. * Time duration should be in milliseconds */ public void bottomTopswipe(int timeduration) { size = driver.manage().window().getSize(); System.out.println(size); starty = (int) (size.height * 0.50); endy = (int) (size.height * 0.20); startx = size.width / 2; System.out.println("Start swipe operation"); driver.swipe(startx, starty, startx, endy, timeduration); } /** * * Method to swipe screen from Top to Bottom (Vertical) Get the size of * screen. Find swipe start and end point from screen's width and height. * Find starty point which is at bottom side of screen. Find endy point * which is at top side of screen. Find horizontal point where you wants to * swipe. It is in middle of screen width.
* Time duration should be in milliseconds
*/ public void topBottomswipe(int timeduration) { size = driver.manage().window().getSize(); System.out.println(size); starty = (int) (size.height * 0.50); endy = (int) (size.height * 0.20); startx = size.width / 2; System.out.println("Start swipe operation"); driver.swipe(startx, endy, startx, starty, timeduration); } /** * * Method to swipe screen from right to left (Horizontal) duration should be * in milliseconds Get the size of screen. Find swipe start and end point * from screen's width and height. Find startx point which is at right side * of screen. Find endx point which is at left side of screen. Find vertical * point where you wants to swipe. It is in middle of screen height.
* Time duration should be in milliseconds
*/ public void rightLeftSwipe(int timeduration) { size = driver.manage().window().getSize(); System.out.println(size); startx = (int) (size.width * 0.70); endx = (int) (size.width * 0.30); starty = size.height / 2; System.out.println("Start swipe operation"); driver.swipe(startx, starty, endx, starty, timeduration); } /** * * Method to swipe screen from left to right (Horizontal) duration should be * in milliseconds Get the size of screen. Find swipe start and end point * from screen's width and height. Find startx point which is at right side * of screen. Find endx point which is at left side of screen. Find vertical * point where you wants to swipe. It is in middle of screen height.
* Time duration should be in milliseconds
*/ public void leftRightSwipe(int timeduration) { // duration should be in milliseconds size = driver.manage().window().getSize(); System.out.println(size); startx = (int) (size.width * 0.70); endx = (int) (size.width * 0.30); starty = size.height / 2; System.out.println("Start swipe operation"); driver.swipe(endx, starty, startx, starty, timeduration); }
Thank you for wonderful tutorial.
ReplyDeleteNeed help from you. I tried the above code, but it showing error at driver.swipe()
"The method swipe(int, int, int, int, int) is undefined for the type AndroidDriver"
Can you please let me know the resolution
How to swipe horizontally as swipe method is removed. i am able to swipe vertically but horizontally i am facing some issues.
ReplyDeleteYes, these codes need to be updated as per latest release. Waiting for the same.
DeleteHi, i have used the below code to Swipe / Scroll and it is working perfectly.
ReplyDeleteCode to Swipe UP
public boolean swipeFromUpToBottom()
{
try {
JavascriptExecutor js = (JavascriptExecutor) driver;
HashMap scrollObject = new HashMap();
scrollObject.put("direction", "up");
js.executeScript("mobile: scroll", scrollObject);
System.out.println("Swipe up was Successfully done.");
}
catch (Exception e)
{
System.out.println("swipe up was not successfull");
}
return false;
}
Code to Swipe DOWN
public boolean swipeFromBottomToUp()
{
try {
JavascriptExecutor js = (JavascriptExecutor) driver;
HashMap scrollObject = new HashMap();
scrollObject.put("direction", "down");
js.executeScript("mobile: scroll", scrollObject);
System.out.println("Swipe down was Successfully done");
}
catch (Exception e)
{
System.out.println("swipe down was not successfull");
}
return false;
}
Code for carousel images swipe
public boolean swipeImages()
{
try {
WebElement pageIndicator = driver.findElement(page_indicator);
String pageString= pageIndicator.getAttribute("value");
int length = pageString.length();
String count_string= pageString.substring(length-2, length).trim();
int count = Integer.parseInt(count_string);
System.out.println("Number of Image available to Swipe: "+count);
for (int i=0; i<=count; i++){
JavascriptExecutor js = (JavascriptExecutor) driver;
HashMap scrollObject = new HashMap();
scrollObject.put("direction", "right");
js.executeScript("mobile: scroll", scrollObject);
}
System.out.println("Swipe Successfully");
}
catch (Exception e)
{
System.out.println("Image swipe was not successfull");
}
return false;
}
Thanks Vinita
DeleteThis comment has been removed by the author.
ReplyDeleteThank you on your manual. I've done and successful
ReplyDeletesubway surfers
error:- The method Swipe(int, int, int, int, int) is undefined for the type AndroidDriver
ReplyDeleteThis blog remains the best among all the blogs I have read still now; Great style of writing and way of content delivery. Kindly do share more updates.
ReplyDeleteIELTS Coaching in Anna Nagar
IELTS Coaching in Adyar
IELTS Coaching in Porur
IELTS Coaching in Chennai
Spoken English Classes in Chennai
Best Spoken English Classes in Chennai
Spoken English Class in Chennai
Great Blog!!! Was an interesting blog with a clear concept. And will surely help many to update them.
ReplyDeleteReactJS Training in Chennai
ReactJS Training
gst classes in chennai
ux design course in chennai
ReactJS course
Web Designing Course in Chennai
Ethical Hacking Course in Chennai
Tally Course in Chennai
Nice post, Thank you Cyber Security Training
ReplyDeleteThanks for such a great article here. I was searching for something like this for quite a long time and at last I've found it on your blog. It was definitely interesting for me to read about their market situation nowadays.
ReplyDeleteoracle training in chennai
oracle training in tambaram
oracle dba training in chennai
oracle dba training in tambaram
ccna training in chennai
ccna training in tambaram
seo training in chennai
seo training in tambaram
This blog remains the best among all the blogs I have read still now; Great style of writing and way of content delivery. Kindly do share more updates.
ReplyDeleteweb designing training in chennai
web designing training in omr
digital marketing training in chennai
digital marketing training in omr
rpa training in chennai
rpa training in omr
tally training in chennai
tally training in omr
Very informative blog and great job,
ReplyDeleteThanks to share with us ,
data science training in chennai
data science training in porur
android training in chennai
android training in porur
devops training in chennai
devops training in porur
artificial intelligence training in chennai
artificial intelligence training in porur
nice sharing thank you so much.well written i appreciate your work....
ReplyDeleteoracle training in chennai
oracle training in annanagar
oracle dba training in chennai
oracle dba training in annanagar
ccna training in chennai
ccna training in annanagar
seo training in chennai
seo training in annanagar
This is the information that have been looking for. Great insights & you have explained it really well. Thank you & looking forward for more of such valuable updates.
ReplyDeleteoracle training in chennai
oracle training in velachery
oracle dba training in chennai
oracle dba training in velachery
ccna training in chennai
ccna training in velachery
seo training in chennai
seo training in velachery
Great Blog!!! Was an interesting blog with a clear concept.
ReplyDeleteacte reviews
acte velachery reviews
acte tambaram reviews
acte anna nagar reviews
acte porur reviews
acte omr reviews
acte chennai reviews
acte student reviews
ReplyDeleteThis information is really awesome thanks for sharing most valuable information.
Workday Studio Training
Workday Studio Online Training
Workday Course
Workday Studio Online Training India
Workday Studio Online Training Hyderabad
Workday Studio Training India
Aivivu - đại lý vé máy bay, tham khảo
ReplyDeletevé máy bay đi Mỹ bao nhiêu tiền
vé mỹ về việt nam
vé máy bay đi Los Angeles bao nhiêu tiền
đăng ký bay từ canada về Việt Nam
student information system
ReplyDeletestudent admission management system
timetable management system
student assignment management system
size cannot be resolved to a variable
ReplyDeletei am getting error message for "size"
do we have any other option without creating method to scroll down for Android platform.
ReplyDelete