Below is the code which is showing integration of Appium with Cucumber and Testng for Android app. Please have a look.
Maven Dependencies:
<dependencies> <!-- Cucumber dependencies --> <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> <!-- Appium --> <dependency> <groupId>io.appium</groupId> <artifactId>java-client</artifactId> <version>8.5.1</version> </dependency> <!-- TestNG --> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.10.1</version> <scope>test</scope> </dependency> <!-- WebDriver --> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.22.0</version> </dependency> </dependencies> <build> <plugins> <!-- Maven Surefire Plugin to run tests --> <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 Scenario: Valid Login Given App is launched When I enter username "admin" and password "admin123" Then I should see the homepage
Create Step Definitions
src/test/java/stepDefinitions/LoginSteps.java
package stepDefinitions; import io.appium.java_client.android.AndroidDriver; import io.cucumber.java.en.*; import org.openqa.selenium.remote.DesiredCapabilities; import java.net.URL; public class LoginSteps { AndroidDriver driver; @Given("App is launched") public void app_is_launched() throws Exception { DesiredCapabilities caps = new DesiredCapabilities(); caps.setCapability("deviceName", "Android Emulator"); caps.setCapability("platformName", "Android"); caps.setCapability("appPackage", "com.example"); caps.setCapability("appActivity", "com.example.MainActivity"); caps.setCapability("automationName", "UiAutomator2"); driver = new AndroidDriver(new URL("http://localhost:4723/wd/hub"), caps); } @When("I enter username {string} and password {string}") public void enter_credentials(String user, String pass) { // use driver.findElement() to locate and input values } @Then("I should see the homepage") public void verify_homepage() { // assertion logic here } }
Create Test Runner (Using TestNG)
src/test/java/runners/TestRunner.java
src/test/java/runners/TestRunner.java
package runners; import io.cucumber.testng.AbstractTestNGCucumberTests; import io.cucumber.testng.CucumberOptions; @CucumberOptions( features = "src/test/resources/features", glue = {"stepDefinitions"}, plugin = {"pretty", "html:target/cucumber-report.html"}, monochrome = true ) public class TestRunner extends AbstractTestNGCucumberTests { }
testng.xml
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"> <suite name="Cucumber-TestNG Suite"> <test name="Cucumber Tests"> <classes> <class name="runners.TestRunner"/> </classes> </test> </suite>
Run via Maven:
mvn clean test