Page Object Model with Appium, Cucumber and TestNG for IOS App

 

Below is the code for page object model (POM) with Appium, 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.15.0</version>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-testng</artifactId>
        <version>7.15.0</version>
    </dependency>

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

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

    <!-- Selenium -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.19.1</version>
    </dependency>
</dependencies>





Base Setup for Appium


// hooks/BaseTest.java
package hooks;

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

public class BaseTest {
    protected static IOSDriver driver;

    public void initializeApp() throws Exception {
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "iOS");
        caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "17.0");
        caps.setCapability(MobileCapabilityType.DEVICE_NAME, "iPhone 14 Pro");
        caps.setCapability(MobileCapabilityType.APP, "/path/to/your/app.app");
        caps.setCapability(IOSMobileCapabilityType.AUTO_ACCEPT_ALERTS, true);

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

    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}





Hooks for Cucumber


// hooks/Hooks.java
package hooks;

import io.cucumber.java.After;
import io.cucumber.java.Before;

public class Hooks extends BaseTest {

    @Before
    public void setUp() throws Exception {
        initializeApp();
    }

    @After
    public void cleanUp() {
        tearDown();
    }
}





Feature File


# resources/features/login.feature
Feature: Login

  Scenario: Successful login
    Given User is on Login screen
    When User enters "testuser" and "password123"
    Then User is logged in






Step Definition file:


// stepdefinitions/LoginSteps.java
package stepdefinitions;

import hooks.BaseTest;
import pages.LoginPage;
import io.cucumber.java.en.*;

public class LoginSteps extends BaseTest {
    LoginPage loginPage;

    @Given("User is on Login screen")
    public void userIsOnLoginScreen() {
        loginPage = new LoginPage(driver);
    }

    @When("User enters {string} and {string}")
    public void userEntersCredentials(String username, String password) {
        loginPage.login(username, password);
    }

    @Then("User is logged in")
    public void userIsLoggedIn() {
        // Add validation logic here
    }
}






Page Class:


// pages/LoginPage.java
package pages;

import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class LoginPage {
    IOSDriver driver;

    public LoginPage(IOSDriver driver) {
        this.driver = driver;
        PageFactory.initElements(new AppiumFieldDecorator(driver), this);
    }

    @FindBy(accessibility = "username")
    WebElement usernameField;

    @FindBy(accessibility = "password")
    WebElement passwordField;

    @FindBy(accessibility = "loginButton")
    WebElement loginButton;

    public void login(String username, String password) {
        usernameField.sendKeys(username);
        passwordField.sendKeys(password);
        loginButton.click();
    }
}





Test runner:


// testrunner/TestRunner.java
package testrunner;

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

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





Testng.xml

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





Run Tests


mvn clean test

No comments:

Post a Comment