Appium is an open-source automation tool designed specifically for mobile applications. It supports native, hybrid, and mobile web applications on both iOS and Android platforms. One of the main benefits of using Appium is its ability to drive tests on real devices, emulators, and simulators, thus providing a flexible testing solution.
Integrating Appium with frameworks like TestNG and JUnit offers several advantages:
To set up Appium with TestNG, follow these steps:
Add Dependencies:
First, add the necessary dependencies in your pom.xml
file if you are using Maven:
<dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.1.0</version> </dependency> <dependency> <groupId>io.appium</groupId> <artifactId>java-client</artifactId> <version>8.0.0</version> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.4.0</version> <scope>test</scope> </dependency> </dependencies>
Create Appium Test Class: Now, create a new Java class for your tests:
import io.appium.java_client.AppiumDriver; import io.appium.java_client.MobileElement; import io.appium.java_client.android.AndroidDriver; import org.openqa.selenium.remote.DesiredCapabilities; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import java.net.MalformedURLException; import java.net.URL; public class AppiumTestNGIntegration { private AppiumDriver<MobileElement> driver; @BeforeClass public void setUp() throws MalformedURLException { DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability("platformName", "Android"); capabilities.setCapability("deviceName", "Emulator"); capabilities.setCapability("app", "<path-to-your-app>.apk"); driver = new AndroidDriver<MobileElement>(new URL("http://localhost:4723/wd/hub"), capabilities); } @Test public void exampleTest() { MobileElement element = driver.findElementById("element_id"); element.click(); // Add your assertions here } @AfterClass public void tearDown() { if (driver != null) { driver.quit(); } } }
Run Your Tests: You can run your tests using TestNG in your IDE or through the command line to see the results.
To integrate Appium with JUnit, the steps are quite similar:
Add Dependencies:
Again, you need to add relevant dependencies in your pom.xml
:
<dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.1.0</version> </dependency> <dependency> <groupId>io.appium</groupId> <artifactId>java-client</artifactId> <version>8.0.0</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> </dependencies>
Create JUnit Test Class: Next, create a new Java class for your tests:
import io.appium.java_client.AppiumDriver; import io.appium.java_client.MobileElement; import io.appium.java_client.android.AndroidDriver; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.remote.DesiredCapabilities; import java.net.MalformedURLException; import java.net.URL; public class AppiumJUnitIntegration { private AppiumDriver<MobileElement> driver; @Before public void setUp() throws MalformedURLException { DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability("platformName", "Android"); capabilities.setCapability("deviceName", "Emulator"); capabilities.setCapability("app", "<path-to-your-app>.apk"); driver = new AndroidDriver<MobileElement>(new URL("http://localhost:4723/wd/hub"), capabilities); } @Test public void exampleTest() { MobileElement element = driver.findElementById("element_id"); element.click(); // Add your assertions here } @After public void tearDown() { if (driver != null) { driver.quit(); } } }
Run Your Tests: Execute your JUnit tests using your IDE or build tools like Maven or Gradle.
Integrating Appium with TestNG and JUnit can significantly streamline your mobile application testing process. Each framework has its benefits, so choosing between TestNG and JUnit depends on your team's preferences and requirements.
30/09/2024 | Mobile Testing
18/09/2024 | Mobile Testing
30/09/2024 | Mobile Testing
18/09/2024 | Mobile Testing
21/09/2024 | Mobile Testing
21/09/2024 | Mobile Testing
21/09/2024 | Mobile Testing
21/09/2024 | Mobile Testing
21/09/2024 | Mobile Testing