Pytest Appium: iOS/Android Setup BrowserStack and Simulators
Mobile applications are becoming more and more popular and their quality is key for success in the market. But how can you ensure that the application works correctly on different devices, operating systems, and versions? One of the possible solutions is to use automated tests, which verify the functionality and user interface of the application.
In this article, we will look at the tool Pytest Appium, which allows us to write and run automated tests for mobile applications using Python and Appium. We chose to use BrowserStack for automation testing based on the experience of other teams. We will also show how to use simulators locally and BrowserStack remotely to test our applications on various platforms, and we will show you how to use Pytest fixtures and markers to configure your Appium tests for iOS and Android. For more details, check out the Pytest documentation.
What are PyTest and Appium?
Before we dive into the Pytest Appium testing guide, let's clarify the main keywords:
Pytest: A Testing Framework for Pythonistas
Pytest is a testing framework that lets you write tests in Python with minimal fuss. Pytest has some cool features that make testing fun and easy, such as:
- Fixtures: These are functions that run before or after your tests to set up or clean up things like test data, resources, settings, etc. You can use fixtures to avoid repeating code and make your tests more reliable. You can also share fixtures across different tests or modules by using scopes.
- Markers: These are labels that you can attach to your tests or fixtures to add some extra information or behaviour. You can use markers to skip tests, mark them as expected failures, parametrize them with different values, etc. You can also use markers to group tests by category or tag.
Appium is an open-source tool that allows us to automate the testing of native, hybrid, and web applications on mobile devices. Appium works by communicating with the device through a WebDriver protocol, which is a standard interface for web automation.
Appium supports various platforms and languages, including iOS, Android, Python, Java, Ruby, etc. Appium also provides libraries and clients for different languages, such as Appium Python Client for Python.
What is Requested for the Setup
Please ensure you have installed the following prerequisites to prepare the testing environment:
Set Up Your Testing Environment
First of all, I'd recommend using PyCharm as an IDE. It is a powerful and user-friendly tool for Python development, which supports Pytest and Appium integration. But feel free to use any other IDE which supports Python and Pytest.
Start by registering for BrowserStack (you can try it for free), a cloud-based service that provides real devices and simulators for testing mobile applications. You can follow the browserstack tutorial to install all requirements. You will need to install Python 3, pip, Virtualenv, Appium Python Client, Pytest, Selenium, and BrowserStack SDK.
To use Appium with Python, we need to import the webdriver module from the Appium package and create an instance of the webdriver.Remote class with the desired capabilities and command executor parameters.
The desired capabilities are a set of key-value pairs that specify the device and application settings for the test session. The command executor is the URL of the Appium server that will handle the requests from the client.
We can use either a local or a remote Appium server depending on whether we want to test our application on a simulator or a real device.
More information on Appium can be found here.
Make sure that you have Appium installed and able to run. Now let's install the necessary drivers for iOS and Android:
appium driver install xcuitest
appium driver install uiautomator2
After you install the drivers run Appium server:
Now, download & install Appium inspector. This is a graphical tool that helps us to identify UI elements in the application and get their attributes.
We're gonna use Appium Inspector to identify UI elements in the app so we can use the IDs of elements in tests.
You have already made sure the Appium server is running. Now, make sure Appium Inspector is connected on the same HOST/PORT as Appium is running on (e.g. 127.0.0.1:4723).
Be careful about the port that Appium is using (127.0.0.1:4723). Make sure the port is empty (not in use) – if not, kill the process or use any other free port (e.g. 127.0.0.1:4725).
First, install the Appium drivers:
To run Appium Inspector, you need to specify capabilities to run simulators on Android/iOS:
Then, in Appium Inspector click on Start Session. You will see the simulator screen and the UI elements tree on the left side. You can select any element and see its attributes on the right side. You can use these attributes to localize the element in your tests.
iOS Appium Inspector
Android Appium Inspector
To use simulators, we need to have them installed on our machine and specify their settings in our desired capabilities file. We also need to start an Appium server locally on our machine using the appium command in the terminal.
Then, we need to make sure that conftest.py is settled for Local Simulator. In other words, running UI tests on a simulator takes a few steps to set up. We need a driver for running on Localhost with specific capabilities. Both iOS and Android are using different automation drivers – for iOS it's XCUITest and for Android UiAutomator2.
We can use Pytest fixtures to create and destroy our local driver for each test function. Here is an example of how we can do that:
The ios_local_driver/android_local_driver also creates and destroys the webdriver and Appium Service instance with the desired capabilities for a simulator/emulator. The yield statement returns the driver object to the test function and executes the teardown code after the test is finished.
We can use this fixture in our test functions by passing it as an argument. For example on iOS:
To run our tests on the iOS simulator, we can use the following:
Run the Appium server:
- Be careful about the port that Appium is using (127.0.0.1:4723)
- Make sure the port is empty (not in use) – if not, kill the process or use any other free port (e.g. 127.0.0.1:4725).
Run pytest to run tests:
pytest -s test_name.py
In order to run Android tests just change the ios_local_driver -> android_local_driver
We can also use BrowserStack to test our application remotely on real devices or simulators that are hosted in the cloud. BrowserStack provides us with a large variety of devices, operating systems and versions that we can choose from. This way, we can test our application on different scenarios and ensure its compatibility and performance.
To use BrowserStack, we need to have an account and an access key, which we can get from the user's profile. We also need to specify our device and application settings in our browserstack.yml file, which is a configuration file that BrowserStack uses to run our tests. You can follow BrowserStack's Getting Started and choose Pytest.
Then, we need to make sure that conftest.py is settled for Remote BrowserStack.
We can use Pytest fixtures and markers to create and destroy our remote driver for each test function. Here is an example of how we can do that:
The remote_driver fixture uses the session_capabilities parameter, which is provided by the BrowserStack SDK plugin for Pytest. This parameter contains the desired capabilities for the device and application settings that we have specified in our browserstack.yml file. The fixture also creates and destroys the webdriver instance with the remote URL of the BrowserStack server.
We can use this fixture in our test functions by passing it as an argument. For example:
To run our tests on BrowserStack, we need to use the browserstack-sdk command in the terminal:
Run the below command for Android/iOS:
browserstack-sdk pytest -s test_name.py
This will run our tests on the device or simulator that we have specified in our browserstack.yml file.
In this article, we have learned how to use Pytest Appium to write and run automated tests for mobile applications on both iOS and Android. We have also seen how to use simulators locally and BrowserStack remotely to test our applications on various platforms. We have also used Appium Inspector to identify UI elements in the application.
Pytest Appium is a powerful and flexible tool that can help us ensure the quality and functionality of our mobile applications. It can also save us time and resources by automating the testing process and providing us with different testing environments.
We hope you have enjoyed this article and found it useful. If you have any questions or feedback, please let us know via email or social media. Happy testing!