Today if you are looking to automate mobile applications, Appium is one of the widely used tool, you can find plenty of blogs on it but for Appium with C# there is limited documentation support.
Recently I worked for the client who is having application in .Net framework, so they want automation to be done in C# and suggested to use it. In this blog I will give you enough visibility around the process of iOS mobile automation using Appium and C#.
Reasons for Appium being so popular includes:
- Open source tool
- Framework for automating native, mobile web and hybrid applications
- Cross Platform, allowing same tests to run on multiple platforms (iOS and Android) using same API.
- Flexibility of writing code in different languages such as Java, C#, PHP, Ruby, Python.
- Supports automation on both physical devices and simulators (or emulators)
In order to follow along with this blog please download and install following prerequisites:
- Visual Studio (to write client code in C#)
(I have used XCODE 9.0 and Visual Studio Community Version 7.2.2)
- Install Appium-Desktop (I have used Version 1.3.0)
- A sample application to be automated. For this post, I will be using sample application which can be downloaded from from here.
Let’s start by creating a simple automation project in Visual Studio
Open Visual Studio
- Click on New Project, and choose NUnit Library Project template listed under .Net category
(Nunit is the unit testing framework for all .net languages, which is initially ported from JUnit)
- Click on Next and enter project name say, TestAppium and solution name say, Demo.
This will automatically create a project with default template as
The code snippet shown above has two interesting annotations:
- [TestFixture()] : It is the attribute which marks a class that contains test methods
- [Test()] : It is the attribute that can be used to mark a method inside TestFixture class as test
For using Appium in Visual Studio we need to add it through Nuget Package Manager. So,
- Go to Solution Explorer, Right Click on it and then click on Add NuGet Packages.
- Search for Appium.WebDriver and Click on Add Package.
This will automatically add the dependencies required by the Appium. You can view all those under References.
As Appium runs on top of WebDriver so, WebDriver and WebDriver.Support Packages are automatically added, Also Appium requires the JSON parsing for communicating with Server that’s why NewtonSoft.Json is added.
Writing the automation Script
For executing script on any desired device, we need to carry out following steps:
- Setting desired capabilities
- Creating the Appium driver instance
- Locating elements of UI
- Writing code for performing operations
Desired Capabilities are the set of key value pairs to instruct the Appium server about type of automation session to start. List of capabilities can be viewed here.
Appium use different properties of UI elements to identify such as:
- Accessibility ID
- Class Name
We can get locators using Appium inspector provided by Appium Desktop.
Below is the automation script. You can find the code from here
In the above code snippet, I used two more attributes of NUnit — [SetUp()] and [TearDown]. These attributes are self-explanatory so I will not be explaining them here. You can read more about them in NUnit documentation.
Few points about project structure:
- Driver instantiation is kept in setup phase as, it is required to be before any test execution
- Similarly, closing of driver is in TearDown phase
- Locators are maintained at separate class in Pages Folder, following the Page Object Model
- Application under test is placed under Resources folder.
For building the code, Go to Build and then Build All. If Build succeeds we will be able to run tests. Navigate as View and then select Test. On Right Hand side Unit Tests Explorer will come, from there you can select to run the test. Follow the below screenshots
If the Assert condition evaluates to true, then the test case will pass and Test Result will be shown as below:
I hope this blog will help to start your automation with Appium and C#. Please provide your feedback so that I can discuss in depth in my next blog.