Wednesday, January 6, 2016

Appium Quick Reference


Appium Code Snippets

 

 

 

I am using following setup. Code snippets listed in the table below, were tested on following environment. 





Mac OS: OS X Yosemite
IOS: 8.3
Appium:1.4.13
Phone: iphone 4s 

If you have any useful code snippets which I can include in the following table, please let me
know.







Best Viewed in  Mozilla Firefox




Task to be done
Appium
Example
Find Element by XPATH
driver.findElement(By.xpath(XPATH));
driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIACollectionView[3]/UIACollectionCell[1]");
Find Element by Name
driver.findElement(By.name(NAME_STRING));
driver.findElement(By.name(“Contacts”));
Take Screenshot
((TakesScreenshot)driver1).getScreenshotAs(OutputType.FILE);
WebDriver driver1 = new Augmenter().augment(driver);
File file  = ((TakesScreenshot)driver1).getScreenshotAs(OutputType.FILE);
           try {
                FileUtils.copyFile(file, new File("Screenshot.jpg"));
           } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
           }

Verify Text
String txt = element.getText();

String txt = driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIAScrollView[1]/UIATableView[2]/UIATableCell[2]/UIAStaticText[1]")).getText();

Assert.assertEquals(txt, "Food & Drinks");

Single Tap or Single Click
element.click()
driver.findElement(By.xpath(SEARCH_BAR_XPATH)).click();


driver.tap(fingers, x, y, duration);

fingers - number of fingers/appendages to tap with
    x - x coordinate
    y - y coordinate
    duration - in millisconds

driver.tap(1,140,170,600);


driver.tap(fingers, x, y, duration)

fingers - number of fingers/appendages to tap with
    element - element to tap
    duration - how long between pressing down, and lifting fingers/appendages (in milliseconds) should be greater than 500ms
WebElement element = driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIAToolbar[1]/UIAButton[3]"));
driver.tap(1,element,600);

Double Tap or Double click
driver.executeScript("mobile: tap", tapD);
Map<String, Double> tapD = new HashMap<String,Double>();       
           tapD.put("tapCount", 2d);
           tapD.put("touchCount", 1d);
           tapD.put("duration", 0.0);
           tapD.put("x", 161d);
           tapD.put("y", 457d);
           driver.executeScript("mobile: tap", tapD);

Tap and Hold or Long Tap or Long Press
driver.tap
(1,element,3000);

WebElement element = driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIACollectionView[1]/UIACollectionCell[1]/UIATextView[1]"));
driver.tap(1,element,3000);

Tap and Drag
touchAction.
longPress()
.moveTo()
.release()
.perform()



WebElement dragPt1 = driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIACollectionView[3]/UIACollectionCell[10]"));
        WebElement dropPt2 = driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIACollectionView[3]/UIACollectionCell[1]"));
       
        TouchAction dragAndDrop = new TouchAction(driver);
        dragAndDrop.longPress(dragPt1,3000);
        dragAndDrop.moveTo(dropPt2).release();
        dragAndDrop.perform();

This code did not work
Type Text
Element.
sendKeys();
driver.findElement(By.xpath(SEARCH_BAR)).click();
driver.findElement(By.xpath(SEARCH_BAR)).sendKeys("Comp");
          

Clear Text
text_field.clear();

WebElement text_field = driver.findElement(By.xpath(SEARCH_BAR));
         text_field.sendKeys("Computer");
         text_field.clear();

Hide Keyboard
Hides the keyboard if it is showing.


 driver.hideKeyboard();

Change orientation
Roate()
driver.rotate(ScreenOrientation.LANDSCAPE);

Keep application in background
runAppInBackground(int seconds)
Runs the current app as a background app for the number of seconds requested.

 driver.runAppInBackground(10);

Find the center Point of an element/object

private Point getCenter(WebElement element)
{

  Point upperLeft = element.getLocation();
  Dimension dimensions = element.getSize();
   return new Point(upperLeft.getX() + dimensions.getWidth()/2, upperLeft.getY() + dimensions.getHeight()/2);
}

Swipe
swipe(startx, starty,endx,endy, duration)

    startx - starting x coordinate
    starty - starting y coordinate
    endx - ending x coordinate
    endy - ending y coordinate
    duration - amount of time in milliseconds for the entire swipe action to take

starty=endy, endx > startx ==> Left to right swipe
starty=endy, endx < startx  ==> Right to left swipe
startx=endx, endy > starty ==> Scroll down or up to down swipe
startx=endx, endy < starty ==> Scroll up or down to up swipe
 void swipe_horizontal() {
            //RIGHT to LEFT swipe
            //Find swipe start and end point from screen's with and height.
            //Find startx point which is at right side of screen.
            int startx = (int) (mWidth * 0.90);
            //Find endx point which is at left side of screen.
            int endx = (int) (mWidth * 0.10);
            //Find vertical point where you wants to swipe.
            //It is in middle of screen height.
            int starty = mHeight / 2;
            System.out.println("startx = " + startx + " ,endx = " + endx + " , starty = " + starty);
            //Swipe from Right to Left.
            driver.swipe(startx, starty, endx, starty, 600);
      }
     

      void swipe_vertical() {
            driver.swipe(mWidth/2, mHeight - mHeight/4, mWidth/2, mHeight/4, 600);
      }
Scroll To Text
Scroll to an element which contains the given text.

driver.scrollTo("Privacy Policy");
Pinch/ Zoom
zoom(element)
zoom(int x, int y)

pinch(int x, int y)
pinch(element)

Convenience method for "zooming in" on an element on the screen.

WebElement element = driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIAScrollView[1]/UIAScrollView[1]"));

 driver.pinch(element);
 driver.zoom(element);

Wait till element is visible or clickable

WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.elementToBeClickable(By.name("Privacy Policy")));

Get Screen size

Dimension size = driver.manage().window().getSize() ;
           System.out.print("height = " + size.height + "  width = " + size.width);
height = 480  width = 320

Scroll and search element


 WebElement element = driver.findElement(By.name("Privacy Policy"));
            while (!element.isDisplayed()) {
                   driver.swipe(width/2, height - height/4, width/2, height/4, 600);
                   element = driver.findElement(By.name("Privacy Policy"));
            }

Is element present?

public boolean isElementPresent(String p_name) {
     try {
           driver.findElement(By.xpath(p_name));
           return true;
     }
     catch (NoSuchElementException e) {
           System.out.println("Element not present\n");
           return false;
           }
     }

}
Swipe a table row. (E.g swipe a chat message so that delete option is visible)

WebElement address = driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIATableView[1]/UIATableCell[2]/UIAStaticText[2]"));

Point center = getCenter(address);

//perform swipe gesture
driver.swipe(center.getX(), center.getY(), center.getX() -90 , center.getY(), 600);
Close and remove app

driver.removeApp("com.abhijit.mytestapp");


1 comment: