DEV Community

Cover image for Automated UI Testing with Appium and Specflow
Rquaicoo
Rquaicoo

Posted on • Edited on

Automated UI Testing with Appium and Specflow

1. Introduction to Automated UI testing

Automated UI tests can repeatedly execute scenarios with high precision, ensuring consistency across different versions of an application. They can run quickly and frequently reducing the need for manual UI testing. It can be executed as part of the CI/CD pipeline thus catching bugs early in the development cycle. They could be designed to cover a broad range of test scenarios and configurations which will be difficult to achieve manually.

2. Introduction to Appium and SpecFlow

Appium: is an open-source tool for automating mobile applications. It allows for the testing of native, hybrid, and mobile web apps. Appium uses the WebDriver Protocol based on http and thus supports multiple programming languages, including Java, JavaScript, and C#. But we use C# here.

SpecFlow: is a .NET-based framework that supports Behavior-Driven Development (BDD). It allows for writing human-readable test scenarios in Gherkin language, making it easier to create tests that non-technical stakeholders understand. The testing scenarios can serve as documentation for the application since they cover all the use cases. It can integrate with various testing frameworks and tools like Appium for UI automation.

3. Combining Appium and Specflow

Purpose of Integration:

  • Using Appium with Specflow allows you to leverage the readability of Specflow’s Gherkin syntax while utilizing Appium’s powerful mobile automation capabilities.

  • This combination supports writing BDD-style tests for mobile applications, enhancing collaboration and communication between developers, testers, and business stakeholders.

Typical Workflow:

  • Define Scenarios: Write high-level scenarios using Gherkin syntax in feature files.

  • Implement Step Definitions: Map the Gherkin steps to code that interacts with the mobile application using Appium.
    Run Tests: Execute the tests on real devices or emulators to validate the application’s behavior.

4. Setting Up the Environment

Appium Setup:

Installing Appium
Run the command below to install Appium in the command line

npm install -g appium
Enter fullscreen mode Exit fullscreen mode


bash
Run the command below to verify the installation of Appium

apppium –version
Enter fullscreen mode Exit fullscreen mode

Extra Configuration

  • Make sure the following user environment variables are set correctly.

ANDROID_HOME = C:\Users\<user>\AppData\Local\Android\Sdk

JAVA_HOME = C:\Users\<user>\Tools\jdk-22.0.2

  • Set up Android emulators or connect physical devices. You can manage emulators through Android Studio.
  • Platform Tools: Ensure that platform-tools are in your system PATH for ADB (Android Debug Bridge) commands.

Setting Up iOS Testing:

  • Xcode: Install Xcode from the Mac App Store. Ensure you have the Xcode Command Line Tools installed.
  • Simulators: Configure iOS simulators using Xcode’s device manager.
  • WebDriverAgent: Appium uses WebDriverAgent for iOS automation. You may need to set up WebDriverAgent with Xcode, following Appium’s iOS setup documentation.

5. Writing and Organizing Feature Files

  • Gherkin Syntax:
    • Feature: Describes the functionality or feature being tested.
    • Scenario: A specific test case within the feature.
    • Given, When, Then: Steps that define the initial context, actions, and expected outcomes. Example Gherkin syntax:
Feature: User Login

  Scenario: Successful login with valid credentials
    Given the user is on the login page
    When the user enters valid credentials
    And the user submits the login form
    Then the user should be redirected to the homepage
Enter fullscreen mode Exit fullscreen mode
  • Feature File Structure:
    • File Extension: Feature files typically have a .feature extension.
    • Location: Place feature files in a dedicated directory, often named Features, within your test project.

6. Setting up Specflow with Appium

  • Install the Specflow Extension from here. An alternative is to use the Visual Studio marketplace to install the extension.
  • Create a new Specflow test project.
  • Select 'Create' and choose the xUnit Test library.
  • Congratulations. You have been able to set up a minimal specflow project.
  • In using Specflow with Appium, ensure you have the flowing folder structure:
|_Specflow_Project
    |_Drivers
    |_Pages
    |_StepDefinitions
    |_Features
Enter fullscreen mode Exit fullscreen mode

Create project in Visual Studio

Select the xUnit test framwork

7. Creating Feature Files

Structure and Syntax of Gherkin
Gherkin is the language used to write feature files in Specflow. It is designed to be readable by both technical and non-technical stakeholders. Here’s an overview of its structure and syntax:

  • Feature:
    • Describes a functionality or feature of the application.
    • Provides a high-level description of what the feature is about.

Example:

Feature: User Login
  As a user
  I want to be able to log in to my account
  So that I can access personalized features
Enter fullscreen mode Exit fullscreen mode
  • Scenario:
    • Represents a specific example or test case for the feature.
    • Consists of a sequence of steps that describe the initial context, actions, and expected outcomes.

Example:

Scenario: Successful login with valid credentials
  Given the user is on the login page
  When the user enters valid credentials
  And the user submits the login form
  Then the user should be redirected to the homepage
Enter fullscreen mode Exit fullscreen mode
  • Steps:
    • Given: Describes the initial context or setup.
    • When: Describes the action or event.
    • Then: Describes the expected outcome or result.
    • And / But: Used to add more conditions or actions to the Given, When, or Then steps.

Example:

Given the user is on the login page
When the user enters valid credentials
And the user submits the login form
Then the user should be redirected to the homepage
Enter fullscreen mode Exit fullscreen mode

** Writing Scenarios and Steps

  • Writing Scenarios:
    • Start with a clear title for the scenario that describes what it tests.
    • Use Given to set up the initial state of the application.
    • Use When to specify the actions taken by the user.
    • Use Then to define the expected outcomes or results.

Example Scenario for a login feature:

Scenario: Unsuccessful login with invalid credentials
  Given the user is on the login page
  When the user enters invalid credentials
  And the user submits the login form
  Then an error message should be displayed
Enter fullscreen mode Exit fullscreen mode
  • Best Practices:
    • Keep Scenarios Simple: Each scenario should test a single behavior or outcome.
    • Use Descriptive Names: Scenarios and steps should be named clearly to convey their purpose.
    • Avoid Hard-Coding Data: Use placeholders or variables for dynamic data, making tests more flexible and reusable.
    • Organize Feature Files: Group related scenarios into feature files to maintain a clear structure and organization.

Feature File Example
Here’s an example of a complete feature file for a user login feature:

Feature: User Login

  As a registered user
  I want to log in to my account
  So that I can access my dashboard

  Scenario: Successful login with valid credentials
    Given the user is on the login page
    When the user enters valid credentials
    And the user submits the login form
    Then the user should be redirected to the dashboard

  Scenario: Unsuccessful login with invalid credentials
    Given the user is on the login page
    When the user enters invalid credentials
    And the user submits the login form
    Then an error message should be displayed

  Scenario: Login attempt with empty fields
    Given the user is on the login page
    When the user leaves the username and password fields empty
    And the user submits the login form
    Then a validation error should be shown
Enter fullscreen mode Exit fullscreen mode

Creating well-structured and descriptive feature files is crucial for effective BDD and test automation. It ensures that tests are clear, maintainable, and easy to understand by all stakeholders.

Mapping Gherkin Steps to Code

  • Creating Step Definition Files:
    • Step definitions in Specflow are written in code and map the steps described in the Gherkin scenarios to executable actions.
    • Typically, you create a separate class for each feature or set of related features.
  • Step Definition Syntax:
    • Use attributes to define the mapping between Gherkin steps and the methods in your code.
    • [Given], [When], [Then], [And], and [But] attributes are used to associate the methods with specific steps in your scenarios.

Excample:

[Binding]
public class LoginSteps
{
    private readonly IWebDriver _driver;

    public LoginSteps()
    {
        // initialize driver
    }

    [Given(@"the user is on the login page")]
    public void GivenTheUserIsOnTheLoginPage()
    {
        _driver.Navigate().GoToUrl("http://example.com/login");
    }

    [When(@"the user enters valid credentials")]
    public void WhenTheUserEntersValidCredentials()
    {
        _driver.FindElement(By.Id("username")).SendKeys("user");
        _driver.FindElement(By.Id("password")).SendKeys("password");
    }

    [When(@"the user submits the login form")]
    public void WhenTheUserSubmitsTheLoginForm()
    {
        _driver.FindElement(By.Id("loginButton")).Click();
    }

    [Then(@"the user should be redirected to the homepage")]
    public void ThenTheUserShouldBeRedirectedToTheHomepage()
    {
        Assert.AreEqual("http://example.com/home", _driver.Url);
    }
}
Enter fullscreen mode Exit fullscreen mode

Writing Step Definitions

  • Mapping Gherkin Steps to Code
    • Creating Step Definition Files:
    • Step definitions in Specflow are written in code and map the steps described in the Gherkin scenarios to executable actions.
    • Typically, you create a separate class for each feature or set of related features.
  • Step Definition Syntax:
    • Use attributes to define the mapping between Gherkin steps and the methods in your code.
    • [Given], [When], [Then], [And], and [But] attributes are used to associate the methods with specific steps in your scenarios. Example step definition in C#:
[Binding]
public class LoginSteps
{
    private readonly IWebDriver _driver;

    public LoginSteps()
    {
        _driver = new ChromeDriver();
    }

    [Given(@"the user is on the login page")]
    public void GivenTheUserIsOnTheLoginPage()
    {
        _driver.Navigate().GoToUrl("http://example.com/login");
    }

    [When(@"the user enters valid credentials")]
    public void WhenTheUserEntersValidCredentials()
    {
        _driver.FindElement(By.Id("username")).SendKeys("user");
        _driver.FindElement(By.Id("password")).SendKeys("password");
    }

    [When(@"the user submits the login form")]
    public void WhenTheUserSubmitsTheLoginForm()
    {
        _driver.FindElement(By.Id("loginButton")).Click();
    }

    [Then(@"the user should be redirected to the homepage")]
    public void ThenTheUserShouldBeRedirectedToTheHomepage()
    {
        Assert.AreEqual("http://example.com/home", _driver.Url);
    }

    [AfterScenario]
    public void AfterScenario()
    {
        _driver.Quit();
    }
}
Enter fullscreen mode Exit fullscreen mode

Handling Complex Interactions with Appium

  • Interacting with Mobile Elements:
    • Use Appium’s methods to interact with mobile elements. For example, FindElementById, FindElementByXPath, etc.
    • Ensure that you handle different types of elements (e.g., buttons, text fields) appropriately.

Example with Appium:

[When(@"the user enters valid credentials on mobile")]
public void WhenTheUserEntersValidCredentialsOnMobile()
{
    var usernameField = _driver.FindElementById("com.example:id/username");
    var passwordField = _driver.FindElementById("com.example:id/password");
    var loginButton = _driver.FindElementById("com.example:id/loginButton");

    usernameField.SendKeys("user");
    passwordField.SendKeys("password");
    loginButton.Click();
}
Enter fullscreen mode Exit fullscreen mode
  • Handling Dynamic Elements:
    • For dynamic elements (e.g., elements that change based on user actions), use strategies to locate and interact with them, such as waiting for elements to be present.

Example:

[When(@"the user waits for the homepage to load")]
public void WhenTheUserWaitsForTheHomepageToLoad()
{
    var wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(10));
    wait.Until(driver => driver.FindElement(By.Id("homepage")).Displayed);
}
Enter fullscreen mode Exit fullscreen mode

8. Managing Test Data and Context

  • Test Data Management:
    • Use parameterized steps to manage test data efficiently. Pass data from your feature files to your step definitions.

Example:

Scenario: Login with different credentials
  Given the user is on the login page
  When the user enters username "user1" and password "password1"
  Then the user should be redirected to the homepage
Enter fullscreen mode Exit fullscreen mode

Corresponding step definition:

[When(@"the user enters username ""(.*)"" and password ""(.*)""")]
public void WhenTheUserEntersUsernameAndPassword(string username, string password)
{
    _driver.FindElement(By.Id("username")).SendKeys(username);
    _driver.FindElement(By.Id("password")).SendKeys(password);
}
Enter fullscreen mode Exit fullscreen mode

Executing Tests

  • Running Specflow Tests:
    • Build your solution to compile the Specflow feature files and step definitions.
    • Open the Test Explorer in Visual Studio to see and run your tests. You can run all tests or individual tests from here.
  • Executing Tests with Appium:
    • Start Appium Server: Ensure the Appium server is running before executing tests. You can start it via the Appium Desktop application or from the command line:
appium
Enter fullscreen mode Exit fullscreen mode

Starting with the following command allows you to use the appium inspector on the web.

appium –allow-cors
Enter fullscreen mode Exit fullscreen mode

Run Tests:
- Execute your Specflow tests as you normally would. The tests will use the Appium server to interact with the mobile application.

Monitoring Test Execution

  • Reviewing Test Results:

    • After running your tests, check the test results in your test runner. Test results typically include information about passed, failed, and skipped tests.
    • In Visual Studio:
      • The Test Explorer will show detailed results, including stack traces for failed tests.
    • In Command Line:
      • Test runners often provide a summary of results, including detailed logs for failed tests.
  • Logs and Debugging:

    • Appium Logs:
      • Appium logs can be very useful for debugging issues. They provide detailed information about the interactions between Appium and the mobile application.
    • Test Logs:
      • Include logging in your step definitions to capture the test execution details. This can help diagnose issues and understand test failures.

9. Using Appium Inspector

What is Appium Inspector?
Appium Inspector is a graphical user interface tool provided by Appium to help you inspect and interact with the UI elements of your mobile applications. It allows you to explore the structure of your application, identify element locators, and generate code snippets for interacting with these elements. It is a valuable tool for debugging and developing automated tests.

Features of Appium Inspector

  • Element Inspection:

    • Inspect the UI elements of your mobile application to view their properties and hierarchy.
    • Identify element locators (e.g., IDs, XPaths) that are used in your test automation code.
  • Interaction with Elements:

    • Interact with the application elements directly from the inspector. Perform actions like clicking buttons, entering text, and swiping.
  • Code Generation:

    • Generate code snippets in different programming languages (e.g., Java, Python, C#) based on the interactions you perform in the inspector.
    • This feature helps you quickly translate UI interactions into test automation code.
  • Session Recording:

    • Record and replay your interactions with the application to help you understand and replicate issues.
  • Debugging:

    • Use the inspector to debug issues by examining element properties and verifying that your locators and interactions work as expected.

Getting Started with Appium Inspector

  • Installation:

    • Appium Desktop:
    • Appium Inspector is part of the Appium Desktop application. Download and install Appium Desktop from the Appium website.
    • Starting Appium Desktop:
    • Launch Appium Desktop and start the Appium server. Ensure that your mobile device or emulator is connected and configured properly.
  • Setting Up a Session:

    • Open Appium Inspector:
    • Click on the "Start Session" button in Appium Desktop to open the Appium Inspector.
    • Configure Desired Capabilities:
    • Enter the desired capabilities for your mobile application in the "Desired Capabilities" section. These capabilities include details such as platform name, device name, app path, and automation engine.
    • Example capabilities:
      {
        "platformName": "Android",
        "deviceName": "Android Emulator",
        "app": "path/to/your/app.apk",
        "automationName": "UiAutomator2"
      }
    
    • Start the Session:
    • Click the "Start Session" button to initiate the session and connect to your mobile application. The Appium Inspector will launch your application and display its UI hierarchy.
  • Using the Inspector:

    • Inspect Elements:
    • Use the element inspector tool (often represented by a magnifying glass icon) to hover over and select elements in your application. The inspector will display details about the selected element, such as its ID, XPath, and attributes.
    • Perform Actions:
    • Interact with elements by performing actions like clicking, tapping, and entering text directly from the inspector. This helps you test and verify interactions.
    • Generate Code Snippets:
    • Generate code snippets for your interactions by using the code generation feature. You can select the programming language and copy the generated code to use in your test automation scripts.
  • Troubleshooting:

    • Inspect Element Properties:
    • Use the inspector to check the properties of elements if you encounter issues with locating or interacting with them.
    • Verify Locators:
    • Ensure that the locators you use in your test scripts match the properties shown in the inspector.
    • Debugging:
    • Record your session and replay interactions to diagnose issues or understand behavior.

Example Workflow

  • Launch Appium Desktop and Start the Server:

    • Open Appium Desktop and click the "Start Server" button.
  • Configure and Start a New Session:

    • Enter the desired capabilities for your mobile application and click "Start Session."
  • Inspect and Interact:

    • Use the inspector to examine the UI elements of your application. Click on elements to see their properties and generate code snippets.
  • Perform Actions:

    • Test interactions with elements and verify that they work as expected.
  • Use Generated Code:

    • Incorporate the generated code snippets into your automated test scripts.

10. Workflow Summary

  • Setup and Configuration:

    • Install Required Tools:
    • Appium: Download and install Appium Desktop or set up Appium via command line.
    • Specflow: Install Specflow and related dependencies (e.g., SpecFlow.NUnit).
    • Appium WebDriver: Install the Appium WebDriver package for your language (e.g., C#).
    • Configure Appium:
    • Create Desired Capabilities: Define capabilities such as platform name, device name, and app path in your configuration files or directly in code.
  • Write Feature Files:

    • Create Feature Files:
    • Write Scenarios: Use Gherkin syntax to describe the feature and scenarios in a readable format.
  • Implement Step Definitions:

    • Write Step Definitions:
    • Create Step Definition Classes: Map Gherkin steps to code using attributes like [Given], [When], and [Then] to implement the logic for each step.
  • Run and Debug Tests:

    • Execute Tests:
    • Start Appium Server: Ensure that the Appium server is running before executing tests.
    • Run Tests: Use your test runner or IDE to execute tests and monitor their execution.
    • Monitor Execution:
    • Review Results: Check the results for passes, fails, and errors. Utilize tools like the Appium Inspector for debugging.
  • Manage Test Environments:

    • Configure Test Environments:
    • Define Capabilities: Adjust capabilities for different environments (e.g., staging, production) using configuration files or environment variables.
    • Integrate with CI/CD:
    • Set Up CI/CD Pipelines: Integrate your tests into CI/CD pipelines to automate test execution on code changes.

Top comments (1)

Collapse
 
anshul_dashputre_9bd8a03b profile image
Anshul Dashputre

Great overview of automating mobile app testing with Appium & Specflow! The focus on readability with Gherkin and the step-by-step guide are helpful. I’m excited to try this out for my next project.