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

 


Below is the code for page object model (POM) with Appium, Cucumber and Junit for IOS app. Please have a look.


Feature File (Login.feature)

Feature: Login to iOS App

  Scenario: Valid login
    Given the app is launched
    When I enter username "testuser" and password "password123"
    And I tap on the login button
    Then I should see the home screen



Step Definition Class (LoginSteps.java)


package stepdefinitions;

import io.cucumber.java.en.*;
import org.junit.Assert;
import pages.LoginPage;
import utils.DriverFactory;

public class LoginSteps {

    LoginPage loginPage = new LoginPage(DriverFactory.getDriver());

    @Given("the app is launched")
    public void the_app_is_launched() {
        // Driver is initialized in Hooks
    }

    @When("I enter username {string} and password {string}")
    public void enter_credentials(String username, String password) {
        loginPage.enterUsername(username);
        loginPage.enterPassword(password);
    }

    @When("I tap on the login button")
    public void tap_login() {
        loginPage.tapLogin();
    }

    @Then("I should see the home screen")
    public void verify_home_screen() {
        // Example assertion
        Assert.assertTrue(true); // Replace with actual element verification
    }
}





Page Class (LoginPage.java)


package pages;

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

public class LoginPage {

    IOSDriver driver;

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

    @iOSXCUITFindBy(accessibility = "usernameField")
    private WebElement usernameField;

    @iOSXCUITFindBy(accessibility = "passwordField")
    private WebElement passwordField;

    @iOSXCUITFindBy(accessibility = "loginButton")
    private WebElement loginButton;

    public void enterUsername(String username) {
        usernameField.sendKeys(username);
    }

    public void enterPassword(String password) {
        passwordField.sendKeys(password);
    }

    public void tapLogin() {
        loginButton.click();
    }
}





DriverFactory Class (DriverFactory.java)


package utils;

import io.appium.java_client.ios.IOSDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.net.MalformedURLException;
import java.net.URL;

public class DriverFactory {

    private static IOSDriver driver;

    public static IOSDriver getDriver() {
        return driver;
    }

    public static void initializeDriver() throws MalformedURLException {
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability("platformName", "iOS");
        caps.setCapability("deviceName", "iPhone Simulator");
        caps.setCapability("platformVersion", "16.0");
        caps.setCapability("app", "/path/to/your/app.app");
        caps.setCapability("automationName", "XCUITest");

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

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






Hooks for Setup and Teardown (Hooks.java)


package utils;

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

import java.net.MalformedURLException;

public class Hooks {

    @Before
    public void setup() throws MalformedURLException {
        DriverFactory.initializeDriver();
    }

    @After
    public void teardown() {
        DriverFactory.quitDriver();
    }
}





JUnit Runner Class (TestRunner.java)


package runner;

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

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

No comments:

Post a Comment