Integration of Appium with Cucumber and Testng for IOS App

 


Below is the code which is showing integration of Appium with Cucumber and Testng for IOS app. Please have a look.



Maven Dependencies:

<dependencies>
    <!-- Cucumber -->
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>7.16.1</version>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-testng</artifactId>
        <version>7.16.1</version>
    </dependency>

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

    <!-- TestNG -->
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.9.0</version>
        <scope>test</scope>
    </dependency>

    <!-- Logging -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>2.0.7</version>
    </dependency>
</dependencies>

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

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






Feature File (src/test/resources/features/login.feature)


Feature: Login Feature

  Scenario: Verify login with valid credentials
    Given I launch the iOS app
    When I enter valid credentials
    Then I should see the home screen





Step Definitions (stepdefinitions/LoginSteps.java)


package stepdefinitions;

import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.remote.IOSMobileCapabilityType;
import io.cucumber.java.en.*;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.net.URL;

public class LoginSteps {

    public static IOSDriver driver;

    @Given("I launch the iOS app")
    public void launchApp() throws Exception {
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability("platformName", "iOS");
        caps.setCapability("platformVersion", "16.4");
        caps.setCapability("deviceName", "iPhone 14");
        caps.setCapability("automationName", "XCUITest");
        caps.setCapability("app", "/path/to/your.app");

        driver = new IOSDriver(new URL("http://127.0.0.1:4723/wd/hub"), caps);
    }

    @When("I enter valid credentials")
    public void enterCredentials() {
        // Locate and interact with username and password fields
        // driver.findElement(By.id("username")).sendKeys("testuser");
        // driver.findElement(By.id("password")).sendKeys("password");
        // driver.findElement(By.id("loginButton")).click();
    }

    @Then("I should see the home screen")
    public void verifyHomeScreen() {
        // Assert that home screen element is displayed
        // Assert.assertTrue(driver.findElement(By.id("home")).isDisplayed());
        driver.quit();
    }
}






Cucumber TestNG Runner (runner/TestRunner.java)


package runner;

import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;

@CucumberOptions(
    features = "src/test/resources/features",
    glue = "stepdefinitions",
    plugin = {"pretty", "html:target/cucumber-reports.html"},
    monochrome = true
)
public class TestRunner extends AbstractTestNGCucumberTests {
}






Run Tests

mvn clean test





Testng.xml:

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="iOS Test Suite">
  <test name="Cucumber Tests">
    <classes>
      <class name="runner.TestRunner" />
    </classes>
  </test>
</suite>





Important Points:

  • Start Appium server before running.

  • WebDriverAgent must be built on your device once via Xcode.

  • Provide correct .app or .ipa path in DesiredCapabilities.

No comments:

Post a Comment