How to Integrate Cucumber with Appium for IOS App

  

Prerequisites

  • Java JDK (11+)

  • Maven

  • Xcode (for iOS app development)

  • Appium Server (desktop or CLI)

  • Node.js (Appium requires it)

  • iOS device/simulator (configured)

  • Appium Inspector (for locating elements)






Create Maven Project and ad below dependencies:


<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.ios.cucumber</groupId>
  <artifactId>ios-cucumber-appium</artifactId>
  <version>1.0-SNAPSHOT</version>

  <dependencies>
    <!-- Appium Java Client -->
    <dependency>
      <groupId>io.appium</groupId>
      <artifactId>java-client</artifactId>
      <version>9.0.0</version>
    </dependency>

    <!-- Cucumber -->
    <dependency>
      <groupId>io.cucumber</groupId>
      <artifactId>cucumber-java</artifactId>
      <version>7.11.2</version>
    </dependency>
    <dependency>
      <groupId>io.cucumber</groupId>
      <artifactId>cucumber-junit</artifactId>
      <version>7.11.2</version>
      <scope>test</scope>
    </dependency>

    <!-- JUnit -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13.2</version>
      <scope>test</scope>
    </dependency>

    <!-- Gson (optional for JSON parsing) -->
    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.10</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <!-- Compiler Plugin -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <source>11</source>
          <target>11</target>
        </configuration>
      </plugin>

      <!-- Surefire for Test Execution -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
          <includes>
            <include>**/TestRunner.java</include>
          </includes>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>





 Feature File (login.feature)

Feature: Login Feature

  Scenario: Successful login
    Given App is launched
    When I enter username and password
    Then I should be logged in successfully






Step Definitions (LoginSteps.java)


package stepDefinitions;

import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.MobileElement;
import io.cucumber.java.en.*;
import base.IOSDriverFactory;
import org.openqa.selenium.By;

public class LoginSteps {

    IOSDriver<MobileElement> driver;

    @Given("App is launched")
    public void app_is_launched() {
        driver = IOSDriverFactory.getIOSDriver();
    }

    @When("I enter username and password")
    public void i_enter_username_and_password() {
        driver.findElement(By.id("username")).sendKeys("testuser");
        driver.findElement(By.id("password")).sendKeys("password123");
        driver.findElement(By.id("loginButton")).click();
    }

    @Then("I should be logged in successfully")
    public void i_should_be_logged_in_successfully() {
        boolean isVisible = driver.findElement(By.id("homePage")).isDisplayed();
        assert isVisible;
    }
}





Driver Setup (IOSDriverFactory.java)


package base;

import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.URL;

public class IOSDriverFactory {

    private static IOSDriver driver;

    public static IOSDriver getIOSDriver() {
        try {
            DesiredCapabilities caps = new DesiredCapabilities();
            caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "iOS");
            caps.setCapability(MobileCapabilityType.DEVICE_NAME, "iPhone Simulator");
            caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "16.0");
            caps.setCapability(MobileCapabilityType.APP, "/path/to/your/app.app");
            caps.setCapability(MobileCapabilityType.AUTOMATION_NAME, "XCUITest");

            driver = new IOSDriver<>(new URL("http://localhost:4723/wd/hub"), caps);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return driver;
    }
}






Test Runner (TestRunner.java)

package runners;

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(
    features = "src/test/java/features",
    glue = "stepDefinitions",
    plugin = {"pretty", "html:target/cucumber-report.html"},
    monochrome = true
)
public class TestRunner {
}




Running the Tests

  • Start Appium Server.

  • Run the test using Maven:

mvn test





Important points:

  • Use Appium Inspector to inspect iOS elements and get their IDs.
  • You must have valid iOS signing and provisioning profiles if testing on a real device.
  • You can pass dynamic values to steps using {string} or DataTable.

No comments:

Post a Comment