How to do swipe gestures using selendroid web driver? - java

I have tried with below codings for swiping a mobile pages.
While running the test case, the swipe action doesn't occurs and i am also not getting any error message.
3.How can I swipe on both side from left to right and vice-versa.
1. //Swipe Right to Left side of the Media Viewer First Page
WebElement firstPages = driver.findElement(By.id("media-list"));
TouchActions flick = new TouchActions(driver).flick(firstPages,-100,0,0);
flick.perform();
2. //perform swipe gesture
TouchActions swipe = new TouchActions(driver).flick(0, -20);
swipe.perform();

Here is my code to flick pages and it is working fine for me
for (int i = 0; i <= 3; i++) {
Thread.sleep(5000);
WebElement pages = driver.findElement(By.id("action_bar_overlay_layout"));
TouchActions flick = new TouchActions(driver).flick(pages, 500, 0, 0);
flick.perform();
}
You can flick in both sides by just changing the sign of the x-parameter whose value is 500 here.

Related

How to mouse hover using java through selenium-webdriver and Java

While trying to automate the portal http://demo.nopcommerce.com/, am not able to select mouse hover over "Electornics" menu and select "Camera & Photo" sub menu.
Used the below script for the same.
WebElement electronic_Pdts = driver.findElement(By.xpath("//*[#class='title']//*[#title='Show products in category Electronics']"));
action.moveToElement(electronic_Pdts).build().perform();
driver.findElement(By.xpath("//*[#src='http://demo.nopcommerce.com/images/thumbs/0000006_camera-photo_450.jpeg']")).click();
To Mouse Hover over Electornics menu and select Camera & Photo you can use the following code block :
driver.get("http://demo.nopcommerce.com/");
Actions act = new Actions(driver);
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement electronics = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//li/a[#href='/electronics']")));
act.moveToElement(electronics).perform();
WebElement camera_n_photo = driver.findElement(By.xpath("//li/a[#href='/electronics']//following::ul/li/a"));
camera_n_photo.click();
System.out.println("Camera & photo Clicked.");

Gmail Sign In page - Unable to scroll down till the end

I am trying to automate the gmail sign up page.Once I enter all the mandatory details like user name, pass, DOB, email etc and click on Next Step button, I got a pop up message which needs to scroll down till the end and then Accept button will be enabled.
Below is the code which i am using and its scrolling em down but not enough down so that the "I Accept" button is enabled. I tried below code also to double click but its not scrolling till the end
Actions action = new Actions(driver); action.moveToElement(driver.findElement(By.id("tos-scroll-bu‌​tton"))).doubleClick‌​().build().perform()‌​;
Here is code//
driver.manage().window().maximize();
driver.get("https://accounts.google.com/SignUp?service=mail&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&ltmpl=default");
driver.findElement(By.xpath(".//*[#id='FirstName']")).sendKeys("Krishna");
driver.findElement(By.xpath(".//*[#id='LastName']")).sendKeys("Krishna");
driver.findElement(By.xpath(".//*[#id='GmailAddress']")).sendKeys("Krishna.Krishna1154");
driver.findElement(By.xpath(".//*[#id='Passwd']")).sendKeys("04Mar1989#");
driver.findElement(By.xpath(".//*[#id='PasswdAgain']")).sendKeys("04Mar1989#");
//Input the month
List<WebElement> month_dropdown = driver.findElements(By.xpath(".//*[#id='BirthMonth']/div"));
//iterate the list and get the expected month
Thread.sleep(3000);
for (WebElement month_ele:month_dropdown){
String expected_month = month_ele.getAttribute("innerHTML");
// Break the loop if match found
Thread.sleep(3000);
if(expected_month.equalsIgnoreCase("August")){
month_ele.click();
break;
}
driver.findElement(By.id("BirthMonth")).click();
driver.findElement(By.id(":3")).click();
driver.findElement(By.xpath(".//*[#id='BirthDay']")).sendKeys("14");
driver.findElement(By.xpath(".//*[#id='BirthYear']")).sendKeys("1988");
driver.findElement(By.id("Gender")).click();
driver.findElement(By.id(":e")).click();
driver.findElement(By.xpath(".//*[#id='RecoveryPhoneNumber']")).sendKeys("XXXXXXXX");
driver.findElement(By.xpath(".//*[#id='RecoveryEmailAddress']")).sendKeys("XXX#gmail.com");
driver.findElement(By.id("submitbutton")).click();
Thread.sleep(3000L);
driver.findElement(By.xpath("//*[#id='tos-scroll-button']/div/img")).click();
Try scrolling down until Accept button is in view :
WebElement element =driver.findElement(by);
JavascriptExecutor jse =(JavascriptExecutor)driver;
jse.executeScript("arguments[0].scrollIntoView(true);",element);

Unable to scroll down page on Android device

I am unable to scroll down in mobile page on an android device using appium. I have tried driver.scrollTo(element) , it is not scrolling down to the specified element.
I have tried using Actions class but I got an error as "Method has not been implemented yet"
I have tried another approach using Javascript but it didn't work either.
Someone give suggestion.
Here is the code:
public static void main(String[] args) throws IOException, InterruptedException {
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(CapabilityType.BROWSER_NAME, "Android"); //Name of mobile web browser to automate. Should be an empty string if automating an app instead.
cap.setCapability(CapabilityType.VERSION, "6.0.1");
cap.setCapability(CapabilityType.PLATFORM, "Mac");
cap.setCapability("platformName", "android");
cap.setCapability("platformVersion", "6.0.1");
cap.setCapability("deviceName", "4d00a89e4b2631e9");
cap.setCapability("app", "/Users/ds_nivedha/Downloads/flipkart.apk");
cap.setCapability("appPackage", "com.usablenet.mobile.walgreen"); //Replace with your app's package
cap.setCapability("appActivity", "com.usablenet.mobile.walgreen.AppStart"); //Replace with app's Activity
driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), cap);
driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
/* MobileElement skip= driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.support.v4.view.ViewPager[1]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[2]/android.widget.Button[1]"));
skip.click(); */
MobileElement skiparrow=
driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.support.v4.view.ViewPager[1]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[3]/android.widget.ImageButton[1]"));
skiparrow.click();
MobileElement skiparrowRefill=
driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.support.v4.view.ViewPager[1]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[2]/android.widget.ImageButton[1]"));
skiparrowRefill.click();
MobileElement skiparrowPaperless=
driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.support.v4.view.ViewPager[1]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[2]/android.widget.ImageButton[1]"));
skiparrowPaperless.click();
MobileElement done=
driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.support.v4.view.ViewPager[1]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[1]/android.widget.Button[1]"));
done.click();
MobileElement shopProducts=
driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.support.v7.widget.RecyclerView[1]/android.widget.RelativeLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[2]/android.widget.TextView[2]"));
shopProducts.click();
MobileElement personalCare=
driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.ScrollView[1]/android.widget.LinearLayout[1]/android.widget.GridView[1]/android.widget.TextView[2]"));
personalCare.click();
MobileElement hairCare=
driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.ListView[1]/android.widget.TextView[3]"));
hairCare.click();
MobileElement stylingProduct=
driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.ListView[1]/android.widget.TextView[3]"));
stylingProduct.click();
MobileElement faroukAddToCart=
driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.LinearLayout[1]/android.webkit.WebView[1]/android.webkit.WebView[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[3]/android.view.View[2]/android.view.View[1]/android.view.View[3]/android.view.View[1]/android.view.View[1]/android.view.View[4]/android.view.View[1]/android.view.View[1]/android.view.View[2]/android.widget.Button[1]"));
faroukAddToCart.click();
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//android.widget.Button[contains(#resource-id,'addToCart-cart-checkout')]")));
driver.findElement(By.xpath("//android.widget.Button[contains(#resource-id,'addToCart-cart-checkout')]")).click();
//driver.scrollTo("Proceed to checkout");
MobileElement proceedToCheckout=
driver.findElement(By.xpath("//android.widget.Button[contains(#resource-id,'proceedtocheckout')]"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", proceedToCheckout);
Thread.sleep(500);
/* Actions actions = new Actions(driver);
actions.moveToElement(proceedToCheckout);
actions.perform(); */
MobileElement checkout=
driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.LinearLayout[1]/android.webkit.WebView[1]/android.webkit.WebView[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[2]/android.view.View[2]/android.view.View[5]/android.view.View[2]/android.view.View[5]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.widget.Button[1]"));
checkout.click();
Use swipe method
Keep start x and end x value constant and change start y and end y values to scroll up or down
The scrollTo() method is deprecated now, you can check on the official site over here
Instead if it you can use swipe method to swipe vertically on mobile device and to check if your expected text is visible or not, in not then you can make more swipes, of course you will be using loop for doing so.
To know more about Swipe refer here
For Swipe method refer below example
public void swipingVertical() throws InterruptedException {
//Get the size of screen.
size = driver.manage().window().getSize();
System.out.println(size);
//Find swipe start and end point from screen's with and height.
//Find starty point which is at bottom side of screen.
int starty = (int) (size.height * 0.80);
//Find endy point which is at top side of screen.
int endy = (int) (size.height * 0.20);
//Find horizontal point where you wants to swipe. It is in middle of screen width.
int startx = size.width / 2;
System.out.println("starty = " + starty + " ,endy = " + endy + " , startx = " + startx);
//Swipe from Bottom to Top.
driver.swipe(startx, starty, startx, endy, 3000);
Thread.sleep(2000);
//Swipe from Top to Bottom.
driver.swipe(startx, endy, startx, starty, 3000);
Thread.sleep(2000);
}
Let me know if you face any more hurdle in this.

How to scroll down/up in a Web page without using JavascriptExecutor

Is there any other way to scroll up/down in a web page without using JavaScriptExecutor ?
You can try the PageUp and PageDown keys as an alternate:
Syntax: driver.findElement(By.xpath("xpath")).sendKeys(Keys.PAGE_DOWN)
u can scroll down by using:
driver.findElement(By.xpath("xpath")).sendKeys(Keys.PAGE_DOWN)
but this will scroll down only once.
but if u want to move to that elemnt, use the below code:
WebElement scroll = driver.findElement(By.cssSelector("ur selector"));//u can use By.xpath or By.id here
Actions actions = new Actions(driver);
actions.moveToElement(scroll);
actions.perform();
this will take u to the element by scrolling.
Please try below code as per your requirement:
//Scroll to element using Keys
Actions action = new Actions(dr);
action.sendKeys(Keys.PAGE_DOWN);
Thread.sleep(5000);
action.click(driver.findElement(By.partialLinkText("XYZ"))).perform();
//For scrolling on the page
for (int second = 0;; second++) {
if(second >=60){
break;
}
driver.executeScript("window.scrollBy(0,200)", "");
Thread.sleep(1000);
}
//Scroll bottom of the Page:
Actions actions = new Actions(driver);
actions.keyDown(Keys.CONTROL).sendKeys(Keys.END).perform();

How to swipe in mobile browser using selenium on java

I want to swipe in mobile web site using selenium on java.
I have tried the following code. But it doesn't help me out.
1. //Swipe Right to Left side of the Media Viewer First Page
WebElement firstPages = driver.findElement(By.id("media-list"));
TouchActions flick = new TouchActions(driver).flick(firstPages,-100,0,0);
flick.perform();
Result:- org.openqa.selenium.chrome.ChromeDriver cannot be cast to org.openqa.selenium.interactions.HasTouchScreen
2. //perform swipe gesture
TouchActions swipe = new TouchActions(driver).flick(0, -20);
swipe.perform();
3. JavascriptExecutor js = (JavascriptExecutor) driver;
HashMap<String, Double> swipeObject = new java.util.HashMap<String, Double>();
swipeObject.put("startX", 0.95);
swipeObject.put("startY", 0.5);
swipeObject.put("endX", 0.05);
swipeObject.put("endY", 0.5);
swipeObject.put("duration", 1.8);
js.executeScript("mobile: swipe", swipeObject);
}
Result: unknown error: swipe is not defined(..)
You could trigger an event via javascript eg. with jquery trigger
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("$('#media-list').trigger('swipe', {startX: 0.95})");

Categories

Resources