App & Browser Testing Made Easy

Give your users a seamless experience by testing on 3000+ real devices and browsers. Don't compromise with emulators and simulators

Home Guide Running Appium Tests on iOS Simulator vs Real Devices

Running Appium Tests on iOS Simulator vs Real Devices

By Tom Collins, Community Contributor -

Appium is an open-source automation tool for running test scripts on native apps (like Android apps and iOS apps), mobile-web apps, hybrid apps and windows desktop platforms. It supports cross-browser architecture. Thus it supports writing test scripts on several platforms like iOS, Android and windows by using the same API associated with Web driver protocol.

Appium is language agnostic. It means you don’t have to depend upon a specific programming language. Instead, you can choose any language like Java, C#, Python, Kotlin, JavaScript, etc. It allows to run tests on real devices and also on both simulators and emulators.

You don’t need any recompilation for your app to automate it.

How does Appium work on iOS?

To understand this concept, all you need to know about the Client-Server Architecture of Appium.

  • Appium Client: It provides bindings in different languages and helps the clients to perform required actions on the devices.
  • Appium Server: It’s a Node.js based server which helps to communicate with iOS and Android platforms and create sessions to interact with them.
  • Communication between client and server:
  1. The Appium client (ex. Java/Python/C# etc.) sends a connection request to the appium server via JSON Wire protocol over HTTP.
  2. Then the server creates a session as per the request and connects to the automation framework like XCUITest.
  3. The framework communicates to the end device like a simulator/emulator/real device.
  4. After that, the end device will perform the task on your application under testing.
  5. After execution of the test cases, the end device sends a response to the appium server for the previous request.

Run Appium Tests on iOS Simulator

Set up the Environment

1. Download the XCode IDE. 

2. Start up an iOS simulator using XCode.

XCode provides a simulator for iPhone, iPad, and Apple watch also. 

Here, are few things to do for setting the simulator –

  • First, launch XCode.
  • Then launch the simulator. So,  go to Xcode> Open Developer Tool> Simulator 

3. Download the latest version of appium client.

4. Install a supported programming language’s client library for appium. If you are using Python, then you have to download the Appium Python Client because it supports appium’s WebDriver protocol. 

5. Use the following command to install it 

pip install Appium-Python-Client

Note:  Here, you can use other programming languages like Java, PHP, C#, Ruby, JavaScript, etc. They also support Appium’s WebDriver protocol.

6. (a) Now, start the appium server console by double clicking on the appium file. 

     (b) Then start the appium node server by clicking the launch button.

Run the Appium Test

Now, the environment set-up is completed for the Appium test. So, let’s get started with the testing!

1. First, download the source code of the targeted app for testing. It will be better if the source code language chosen is Swift

2. Now launch the XCode IDE and open the .xcodeproj file inside the Swift folder.

3. Then you need the .app file and bundle id to run the test script on the simulator. The .app file can be found from below path –

/Library/Developer/CoreSimulator/Devices/{{Device Code}}/data/Containers/Bundle/Application

4. After that, the appium test will search for the simulator’s details (Name, iOS version, ID). Thus we need to find out the xpath of different elements like the Search field. So, we will use Appium Inspector for this.

(a)Launch the appium inspector.

(b)Now it will search for the details to build the xpath for the search field.

(c)Xpath of Search field = “//UIASearchBar[@name=’Search’]”

5.  Then, install the Appium Desktop.

6. Next, run the following command to install the appium with npm.

npm install –g appium

7. Now install appium WebDriverAgent. It’s a WebDriver for iOS. You can find it from the following directory:

/Applications/Appium.app/Contents/Resources/app/node_modules/appium/node_modules/appium-webdriver

Then run the command to install it:

mkdir -p Resources/WebDriverAgent.bundle
./Scripts/bootstrap.sh –d

8. Then test your WebDriver with following desired capabilities-

DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "iOS"); capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "7.1");

capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "iPhone Simulator");

capabilities.setCapability(MobileCapabilityType.APP, APP_NAME);

Note: DesiredCapabilities is a class provided by Selenium. You have to use it in ‘Selenium Grid’. It helps you to set the properties like name, version, etc. of a browser.

  • First, you need to set a path for the browser_name _driver.exe file. Ex. chromedriver.exe
  • Next create an object of that browser’s DesiredCapabilities class and pass it to the WebDriver instance.

9. After that write the test script.

App location for the following code: Table_Search.py is the test script, created in this directory-

 $Directory_Of_My_Choice

It is based on the snippet below. Here is a sample Search.swift.app file inside the ‘TableSearchwithUISearchController/Swift’ folder within the above- mentioned directory named $Directoy_Of_My_Choice

There is an in-built path in this example but you can use a combination of os.path.abspath and os.path.dirname(__file__) to make it a path adjusted to your test file.

app = os.path.abspath(app)
self.driver = webdriver.Remote(
command_executor='http://127.0.0.1:4723/wd/hub',
desired_capabilities={
'app': app,
'platformName': 'iOS',
'platformVersion': '9.2',
'deviceName': 'iPhone 5s',
'bundleId':'com.example.apple-samplecode.Search-swift'
})
def test_search_field(self):
# Search for an Apple device and click on it to view the details and navigate back
# Find the search element and perform send keys action
search_element = self.driver.find_element_by_xpath("//UIASearchBar[@name='Search']")
search_element.send_keys("iPad")
sleep(2)
# Get the xpath of first element
first_element = self.driver.find_element_by_xpath("//UIAApplication[1]/UIAWindow[1]/UIATableView[1]/UIATableCell[1]/UIAStaticText[1]")
# Assert that the text matches
self.assertEqual('iPad', first_element.get_attribute('name'))
# Perform click action
first_element.click()
sleep(2)
# Click on search element
self.driver.find_element_by_name("Search").click()
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(TableSearchTest)
unittest.TextTestRunner(verbosity=2).run(suite)

Go to the location mentioned in the paragraph App location for the following code to find the app

app = os.path.abspath(app)
self.driver = webdriver.Remote(
command_executor='http://127.0.0.1:4723/wd/hub',
desired_capabilities={
'app': app,
'platformName': 'iOS',
'platformVersion': '9.2',
'deviceName': 'iPhone 5s',
'bundleId':'com.example.apple-samplecode.Search-swift'
})
def test_search_field(self):
# Search for an Apple device and click on it to view the details and navigate back
# Find the search element and perform send keys action
search_element = self.driver.find_element_by_xpath("//UIASearchBar[@name='Search']")
search_element.send_keys("iPad")
sleep(2)
# Get the xpath of first element
first_element = self.driver.find_element_by_xpath("//UIAApplication[1]/UIAWindow[1]/UIATableView[1]/UIATableCell[1]/UIAStaticText[1]")
# Assert that the text matches
self.assertEqual('iPad', first_element.get_attribute('name'))
# Perform click action
first_element.click()
sleep(2)
# Click on search element
self.driver.find_element_by_name("Search").click()
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(TableSearchTest)
unittest.TextTestRunner(verbosity=2).run(suite)

10. Finally, run the script (.py file) and check the result.

Running Appium tests on Real iOS Devices 

Set up the environment

A mac device (macOS version 10.11 or higher than 10) and an iOS device (iOS version 9.3 or higher) must be needed for this test. Then you have to install the following software in your macOS.

1. Install Homebrew for managing those missing packages which Apple can’t. Command for installing this: 

Bash brew install carthage

2. Install Carthage. It manages dependencies and includes binary frameworks for your project. So, run the Command:

brew install carthage

3. Appium server is written in Node.js language. So,install Node  for the Appium server and also install NPM. You can install the appium server directly from NPM. Command:

brew install node

4. Install Java and set up the environment for this.

5. Install Eclipse IDE for Java.

6. Install Maven. Command:

brew install maven

7. Install XCode 7 or higher. But it will be best if you are using XCode 11.

8. Install XCUITest driver.

9. Install TestNG.

Run the Appium test

1. Launch the Appium server on the desired iOS device.

2. Now, fetch the details of the iOS device. The details include device name, iOS version and bundle ID. These details are needed to set the desired appium capabilities and instantiation of the appium driver.  

In this example we are testing the Google Chrome app on a real iOS device. So, the bundle ID for Google chrome is com.google.Chrome.

Note: In this example XCUITest driver is used for iOS testing. So, it’s compulsory to set the automationName to XCUITest.

3. Then write the Appium test script. So, create a new project in Eclipse and create the required package and class libraries for this.

4. Finally, run the test script. It will make the Google Chrome app open on connected iOS devices.

Appium Test Script for Real iOS Device

import io.appium.java_client.ios.IOSDriver;
import java.net.MalformedURLException;
import java.net.URL;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.remote.DesiredCapabilities;

public class Edition041_iOS_Real_Device {
private IOSDriver driver;

@Before
public void setUp() throws MalformedURLException {
//Setting Desired Capabilities
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName", "iOS");
capabilities.setCapability("platformVersion", "12.4.7");
capabilities.setCapability("deviceName", "iPhone 7");
capabilities.setCapability("udid", "<your iPhone’s udid>");
capabilities.setCapability("bundleId", "com.google.Chrome");
capabilities.setCapability("xcodeOrgId", "<your org id>");
capabilities.setCapability("xcodeSigningId", "<your signing id>");
capabilities.setCapability("updatedWDABundleId", "com.google.chrome.ios");
@After
public void tearDown() {
if (driver != null) {
driver.quit();
}
}

@Test
public void testFindingAnElement() {
driver.findElementByAccessibilityId("Login Screen");
}
}

driver = new IOSDriver<>(new URL("http://localhost:4723/wd/hub"), capabilities);
}

Benefits of Appium Testing on BrowserStack App Automate

  • It offers cloud-based access to the latest and legacy iOS devices.
  • It saves time and helps to meet deadlines as fast as possible.
  • It enables you to test native and hybrid mobile applications using the Appium automation framework on thousands of real devices. 
  • Appium tests are best run on real mobile devices because monitoring apps in real user conditions leads to 100% accurate results.

Try Appium iOS Testing

Apple has a strong market demand nowadays. So, Appium testing is required for those top iOS devices. Many organizations choose Appium for iOS testing (both simulator and real device) because of its cross-browser support, covering major iOS versions, fast execution, and other additional features.

It is always recommended to use a real device cloud for iOS testing. One just needs to choose the desired device to test on and configure for automating tests accordingly.

Tags
Appium Automation Testing

Featured Articles

How to perform Parallel Test Execution in Appium?

How to Analyze Appium Logs

Curated for all your Testing Needs

Actionable Insights, Tips, & Tutorials delivered in your Inbox
By subscribing , you agree to our Privacy Policy.
thank you illustration

Thank you for Subscribing!

Expect a curated list of guides shortly.