In IOS, we have UIPickerWheel element which will have options to select, we will see how we can automate the picker wheel using appium, a sample picker wheel is shown in below screenshot
We will see how we ca select an option from the picker wheel
we can use sendkeys method to select an option, in sendkeys method, send the option to chose as a parameter.
Watch the demo here
Here is the folder structure of the project –
create a maven project as shown below screenshot
DatePickerWheel.java
Identify the picker wheel and use sendKeys() to send the string and select the option.
Even we can use hash map to scroll through the picker wheel to get all picker wheel options.
package iOSTest; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.poi.ss.formula.functions.Column; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import com.mobile.MobileTest.*; public class DatePickerWheel { public void setDateAndTime() { WebElement DatePickerListItem = MobileDriver.getDriver().findElement(By.xpath("//XCUIElementTypeStaticText[@name='Date Picker']")); DatePickerListItem.click(); WebElement DatePickerWheel1 = MobileDriver.getDriver().findElement(By.xpath("//XCUIElementTypeDatePicker")); List<WebElement> Columns = DatePickerWheel1.findElements(By.xpath("//XCUIElementTypePickerWheel")); JavascriptExecutor js = (JavascriptExecutor)MobileDriver.getDriver(); Map<String, Object> hp = new HashMap<String, Object>(); hp.put("order", "next"); hp.put("offset", 0.15); hp.put("element", Columns.get(0)); js.executeScript("mobile: selectPickerWheelValue", hp); System.out.println(Columns.get(0).getAttribute("value")); Columns.get(1).sendKeys("9"); Columns.get(2).sendKeys("30"); Columns.get(3).sendKeys("PM"); } }
AppTest.java
To invoke the application
package com.mobile.MobileTest; import java.net.MalformedURLException; import java.net.URL; import org.openqa.selenium.WebElement; import org.openqa.selenium.remote.DesiredCapabilities; import org.testng.Assert; import io.appium.java_client.AppiumDriver; import io.appium.java_client.android.AndroidDriver; import io.appium.java_client.ios.IOSDriver; public class AppTest { public static AppiumDriver<WebElement> driver; public static DesiredCapabilities cap; public static void iOS_LaunchApp() throws MalformedURLException { cap = new DesiredCapabilities(); cap.setCapability("platformName", "iOS"); cap.setCapability("deviceName", "iPhone 8 Plus"); cap.setCapability("platformVersion", "11.3"); cap.setCapability("bundleId", "com.example.apple-samplecode.UICatalog"); driver = new IOSDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), cap); Assert.assertNotNull(driver); MobileDriver.setWebDriver(driver); } public static void CloseApp() { driver.quit(); } }
MobileDriver.java
Get the driver instance
package com.mobile.MobileTest; import org.openqa.selenium.WebDriver; public class MobileDriver { private static WebDriver Driver; public static WebDriver getDriver() { return Driver; } static void setWebDriver(WebDriver driver) { Driver = driver; } }
TestApp.java
Actual testNG test to execute
package iOSTest; import java.net.MalformedURLException; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.testng.annotations.*; import com.mobile.MobileTest.*; public class TestApp { @Test public void Test() throws MalformedURLException, InterruptedException { AppTest.iOS_LaunchApp(); DatePickerWheel dateWheel = new DatePickerWheel(); dateWheel.setDateAndTime(); Thread.sleep(2000); } @AfterTest public void TearDown() { AppTest.CloseApp(); } }
testng.xml
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="Suite1" verbose="1" > <test name="TestApps"> <classes> <class name="iOSTest.TestApp" /> </classes> </test> </suite>
You can find the pom.xml from here
Start the appium server and run the testng.xml to chose the pickerwheel option
Note : If you are new to appium, you can follow this below starter post to work with appium server