1. Setting Up Your Project
Ensure you have the necessary dependencies in yourpom.xml
(if you're using Maven):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<dependencies> <!-- Appium dependencies --> <dependency> <groupId>io.appium</groupId> <artifactId>java-client</artifactId> <version>8.2.0</version> </dependency> <!-- TestNG dependencies --> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.7.0</version> <scope>test</scope> </dependency> </dependencies> |
2. Writing Your Test Class
Create a test class that defines your Appium tests. Each test method can represent a different test case. Here’s a simple example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
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 AppiumParallelTest { private AppiumDriver<MobileElement> driver; @BeforeClass public void setUp() throws MalformedURLException { DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability("platformName", "Android"); capabilities.setCapability("deviceName", "Android Emulator"); capabilities.setCapability("app", "/path/to/your/app.apk"); driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); } @Test public void testOne() { // Your test logic here } @Test public void testTwo() { // Your test logic here } @AfterClass public void tearDown() { if (driver != null) { driver.quit(); } } } |
3. Configuring TestNG XML for Parallel Execution
Create atestng.xml
file to configure TestNG to run tests in parallel. This file allows you to define how many threads to use and specify the test classes.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"> <suite name="Suite" parallel="tests" thread-count="2"> <test name="Test1"> <classes> <class name="your.package.AppiumParallelTest"/> </classes> </test> <test name="Test2"> <classes> <class name="your.package.AppiumParallelTest"/> </classes> </test> </suite> |
parallel="tests"
specifies that tests should run in parallel.thread-count="2"
specifies the number of threads to use for parallel execution.
Need testing? – Try RobotQA and Start Testing on Real Devices. Start Free Trial
4. Running Your Tests
You can run your tests using the TestNG command-line interface or through an IDE that supports TestNG. To run from the command line, use:
1 |
mvn test -Dsurefire.suiteXmlFiles=testng.xml |
testng.xml
configuration file for running the tests.
Tips for Parallel Execution
- Ensure Appium server is ready: Make sure you have Appium servers running on different ports for each parallel execution instance.
- Unique Device Configurations: If you are running tests on multiple devices/emulators, ensure each test configuration points to a unique device.
- Thread Safety: Ensure your test code is thread-safe, especially if you have shared resources.