execute script in appium

Some important points

  1. ExecuteScript is used to perform certain actions e.g. Selecting value form pickerwheel
  2. In XCUITest, you can use this method to perform scrolling, swiping etc
  3. In XCUITest, You can also use this method to pinch, doubleTap, touchAndHold, tap etc
  4. In XCUITest, you can also install, remove, terminate App etc
  5. In XCUITest, you can also use this method to enroll faceid and send matching or non matching face

Below hypothetical code demonstrates how you can perform actions in hybrid app using appium.

package org.softpost.ios.simulator;
import com.google.common.collect.ImmutableMap;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MultiTouchAction;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.remote.HideKeyboardStrategy;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.PointOption;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.net.URL;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Created by Sagar on 10-07-2016.
 */

public class ExecuteScriptTest {
    IOSDriver driver;
    @Test
    public void testContacts() throws Exception{
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability("platformName", "iOS");
        caps.setCapability("platformVersion", "11.0");
        caps.setCapability("deviceName", "iPhone 8");
        caps.setCapability("AutomationName" , "XCUITest");
        caps.setCapability("headless" , true);
        caps.setCapability("avd" , "44059DAD-FFCD-47EF-9B9A-0713AC9B6422");
        caps.setCapability("wdaLocalPort", 8100);
       // caps.setCapability("app", "/Users/admin/iosapps/softpost.app");


        caps.setCapability("bundleId", "com.apple.MobileAddressBook");

        try {
            driver = new IOSDriver(new URL("http://0.0.0.0:4723/wd/hub"), caps);
            driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

            WebElement e = driver.findElement(By.xpath("//XCUIElementTypeButton[@name="Add"]"));

            TouchAction action = new TouchAction(driver);
            action.tap( PointOption.point(e.getLocation())).perform();

            //e.click();
            driver.findElement(By.xpath("//XCUIElementTypeTextField[@name="First name"]")).sendKeys("Sagar");

            driver.findElement(By.xpath("//XCUIElementTypeButton[@name="Done"]")).click();
            Thread.sleep(5000);

            driver.findElement(By.xpath("//XCUIElementTypeButton[@name="Edit"]")).click();
            Thread.sleep(5000);

            Map<String, Object> params = new HashMap<>();
            params.put("direction", "down");
            driver.executeScript("mobile: scroll", params);

            Thread.sleep(5000);

            driver.findElement(By.xpath("//XCUIElementTypeStaticText[@value='add birthday']")).click();

            Thread.sleep(5000);

            WebElement picker = driver.findElement(By.xpath("//XCUIElementTypePickerWheel"));

            params = new HashMap();
            params.put("order", "next");
            params.put("offset", 0.15);
            params.put("element", picker);
            for (int i=0;i<10;i++) {
                driver.executeScript("mobile: selectPickerWheelValue", params);
            }

            Thread.sleep(2000);
        }catch (Exception ex){
            ex.printStackTrace();
            System.out.println(driver.getPageSource());
        }finally {
            driver.quit();
        }

    }

}

Complex problems, Simple Solutions