In our previous post we introduced the steps to set up your Appium environment for iOS 9 devices. In this post we will write about how can you open the Settings app and do some changes in it. We will use Java as programming language, Eclipse for IDE and JUnit as runner.

Ok, so first step is to get the Java client of Selenium from www.seleniumhq.org/download/. Extract the downloaded *.zip file, create a new Java Project in Eclipse and add all extracted *.jar files to Build Path. Now, we can start to implement our first test case for iOS device where we will open the Settings app and disable the Location Service on iPhone. As we mentioned above, we will use JUnit as runner, but you can also choose TestNG or just create a standard Java program with the main() function.

Let’s create a before() method with annotation @Before where we will set up all of the necessary things for the desired test case.

private IOSDriver driver;

@Before
	public void before() throws MalformedURLException{
		DesiredCapabilities cap = new DesiredCapabilities();
		cap.setCapability("deviceName", "iPhone 6");
		cap.setCapability("platformName", "iOS");
		cap.setCapability("platformVersion", "9.0");
		cap.setCapability("app", "settings");

		driver = new IOSDriver(new URL("http://0.0.0.0:4723/wd/hub"), cap);
	}

We will use Selenium’s DesiredCapabilites to set up the variables. There are more variables that you can choose but the above mentioned (deviceName, platformName, platformVersion, app) are the necessary ones. The deviceName variable is intended to contain the device name which we will use for test. You can take a look on all of your available virtual devices by starting the Simulator and choose Hardware / Device / Manage Devices… The platformName and platformVersion variables speak for themselves, and contain the information about the mobile phone operating system and version. The app variable contains the desired application name that we want to test like the Settings app. We pass the DesiredCapabilities variable to the driver alongside with Appium’s URL.

Here is the code of the test for turning off Location Services on iOS mobile device:

	@Test
	public void testTurnOffAndOnLocationServices() throws InterruptedException{
		//Click on Privacy
		driver.findElement(By.name("Privacy")).click();
		
		//Get Location Services value and check it
		String locationServiceState = driver.findElement(By.name("Location Services")).getAttribute("value");
		Assert.assertTrue("Location Service is turned off but it should be turned ON.", locationServiceState.equals("On"));
		
		//Turn off Location Services
		driver.findElement(By.name("Location Services")).click();
		driver.findElement(By.xpath("//UIASwitch[@name = 'Location Services']")).click();
		driver.findElement(By.xpath("//UIAButton[@name = 'Turn Off']")).click();
		driver.navigate().back();
		
		//Get Location Services value and check it
		locationServiceState = driver.findElement(By.name("Location Services")).getAttribute("value");
		Assert.assertTrue("Location Service is turned on but it should be turned OFF.", locationServiceState.equals("Off"));
	}

The Location Services setting is located under the Privacies, so we need first to “tap” on Privacy menu item. We can do this simply by searching for the button by its name. The next is to get the current value of the Location Services setting. You can do this by starting Appium Inspector from the GUI, click on the item what you want to inspect, and you will get the list of attributes.

Appium
Appium - Location inspector

Next step is to tap on the Location Services, and the following screen will contain a toggle button. You can get a locator by launching Appium Inspector, selecting the desired item on the screenshot and click on Locator at the left bottom corner of the window. You will get a long xpath string but we can simplify it like this: //UIASwitch[@name = 'Location Services'].
Location service menu
Tapping on the toggle will result an “ActionSheet” where we need to tap on the “Turn Off” button. You can locate the element with the simplified locator //UIAButton[@name = 'Turn Off'] and tap on it.

Turn off location service

The last step is to assert that the Location Services is truly turned off. To do this we need to navigate back simply by calling driver.navigate().back(); and to check the value of the Location Services menu item again.

Conclusion

As you can see it’s pretty simple to handle a virtual iOS device through a test flow with Appium. You can do this on physical device too, with no problems. In the latest Xcode (7.0 and above) there is a newly implemented test framework for Apple devices, but the best thing about Appium is that you don’t need to learn a new programming language to write test cases.

Please share you thoughts and ideas below!
We are glad to hear your opinions!

Thanks for reading!

Similar Posts from the author: