Appium iOS native app automation

Few Appium Code:


Date range selection


driver.findElement(By.xpath("//XCUIElementTypePickerWheel[4]")).sendKeys("PM");
driver.findElement(By.xpath("//XCUIElementTypePickerWheel[3]")).sendKeys("07");
driver.findElement(By.xpath("//XCUIElementTypePickerWheel[2]")).sendKeys("12");
driver.findElement(By.xpath("//XCUIElementTypePickerWheel[1]")).sendKeys("Thu, Oct 17");


OR


HashMap param = new HashMap();
param.put("order", "next");
param.put("offset", 0.15);
param.put("element", driver.findElement(By.xpath("//XCUIElementTypePickerWheel[1]")));
driver.executeScript("mobile: selectPickerWheelValue", param);


----------------------------------------------------------------------------------------------------------------------------
ALERT
One option:
driver.switchTo().alert().accept();


Or


HashMap param = new HashMap();
param.put("action", "accept");
driver.executeScript("mobile:alert", param);


Multiple Options


HashMap param = new HashMap();
param.put("action", "getButtons");
List buttons = (List)driver.executeScript("mobile:alert", param);


for(String button:buttons) {
System.out.println(button);
if(button.equalsIgnoreCase("Don’t Allow")) {
param.put("action", "accept");
driver.executeScript("mobile:alert", param);
break;
}
}


----------------------------------------------------------------------------------------------------------------------------


SWITCHING BETWEEN APPS
Suitable for mobile device and not emulator
//SWITCHING APPS
// activate whatsapp
HashMap param = new HashMap();
param.put("bundleId", "net.whatsapp.WhatsApp");
driver.executeScript("mobile: launchApp", param);
//reactive integration app
param.put("bundleId", "com.facebook.wda.integrationApp");
driver.executeScript("mobile: activateApp", param);
//activate photoalbum
param.put("bundleId", "com.apple.mobileslideshow");
driver.executeScript("mobile: launchApp", param);


----------------------------------------------------------------------------------------------------------------------------
Get Bundle Id from apps


----------------------------------------------------------------------------------------------------------------------------
SLIDERS
WebElement slider = driver.findElement(By.className("XCUIElementTypeSlider"));
slider.sendKeys("0.8");


----------------------------------------------------------------------------------------------------------------------------
SWITCHES
WebElement switches = driver.findElement(By.className("XCUIElementTypeSwitch"));


if(switches.getAttribute("value").equals("1")) {
switches.click();
Assert.assertEquals("0", switches.getAttribute("value"));
}else {
switches.click();
System.out.println(switches.getAttribute("value"));
Assert.assertEquals("1", switches.getAttribute("value"));
}


----------------------------------------------------------------------------------------------------------------------------
TAPPING GESTURES


public static void tapByElement(WebElement element) {
new TouchAction(driver).tap(new TapOptions().withElement(ElementOption.element(element)))
.waitAction(WaitOptions.waitOptions(Duration.ofMillis(250))).perform();
}


public static void tapByCoordinates(int x, int y) {
new TouchAction(driver).tap(PointOption.point(x, y)).waitAction(WaitOptions.waitOptions(Duration.ofMillis(250)))
.perform();
}


SWIPING
 SCROLL


Get X co ordinates from appium inspector y co ordinates as well.


driver.findElement(MobileBy.AccessibilityId("Scrolling")).click();
WebElement tap = driver.findElement(MobileBy.AccessibilityId("TableView"));
tapByElement(tap);
Thread.sleep(3000L);
int i = 0;
while (!driver.findElement(MobileBy.AccessibilityId("35")).isDisplayed()) {
scrollOrSwipe(20, 650, 20, 379, 2);
i++;
}


public static void scrollOrSwipe(int x_start, int y_start, int x_end, int y_end, int duration) {


new TouchAction(driver).press(PointOption.point(x_start, y_start))
.waitAction(WaitOptions.waitOptions(Duration.ofSeconds(duration)))
.moveTo(PointOption.point(x_end, y_end)).release().perform();


}

 SINGLE TAP TWO FINGER THREE FINGERS TAP
 PARTICULAR AREA TAPPING
----------------------------------------------------------------------------------------------------------------------------
SWIPE LEFT


driver.findElement(MobileBy.AccessibilityId("Scrolling")).click();


driver.runAppInBackground(Duration.ofSeconds(-1));
int i = 0;


while (!driver.findElement(MobileBy.AccessibilityId("Settings")).isDisplayed()) {


System.out.println("Swipe count : " + i);


swipe(24, 172, 285, 172, 2);


i++;
}
driver.findElement(MobileBy.AccessibilityId("Settings")).click();
driver.findElement(MobileBy.AccessibilityId("Wi-Fi")).click();
WebElement switches = driver.findElement(By.xpath("//XCUIElementTypeSwitch"));


// Turn off the switch
if (switches.getAttribute("value").equals("1")) {


switches.click();
}

Comments

Popular posts from this blog

Inspect iOS app using Appium Inspector

How to Upload a file to S3 Bucket on AWS using JAVA

Android Native element functions / classes Appium