如何通过Maven命令行向TestNG.xml传递参数(含Appium场景)
Hey there! Let's break this down into two clear parts: first, the general approach to passing parameters from Maven to TestNG.xml, and then the specific implementation for your Appium app path scenario.
General Method: Passing Parameters from Maven to TestNG.xml
Maven lets you pass system properties via the command line using -D flags, and TestNG can either reference these properties directly in its XML config or you can read them in your test code. Here's how it works:
1. Pass Parameters via Maven Command Line
Use the -D<parameter-name>=<parameter-value> syntax when running your Maven test command. For example:
mvn test -DappPath="/path/to/your/mobile-app.apk"
2. Reference Parameters in TestNG.xml (or Code)
You have two options here:
- Option 1: Reference directly in TestNG.xml
Define a<parameter>in your suite/test and use thesystem-propertyattribute to link it to the Maven-passed system property:<suite name="AppiumTestSuite"> <!-- Link the TestNG parameter to the Maven system property --> <parameter name="appPath" value="${appPath}" system-property="appPath"/> <test name="MobileAppTests"> <classes> <class name="com.yourcompany.tests.AppiumLoginTest"/> </classes> </test> </suite> - Option 2: Read directly in test code
Skip defining the parameter in TestNG.xml and fetch the system property directly in your Java code:String appPath = System.getProperty("appPath");
Specific Implementation: Pass App Path to Appium's Desired Capabilities
Now let's apply this to your Appium use case. There are two common ways to set up Desired Capabilities—pick the one that fits your test structure:
Scenario 1: Build Capabilities in Code (Most Common)
This is the straightforward approach: read the Maven-passed appPath property and set it directly in your Desired Capabilities:
import io.appium.java_client.remote.MobileCapabilityType; import org.openqa.selenium.remote.DesiredCapabilities; import org.testng.annotations.BeforeTest; public class AppiumBaseTest { private DesiredCapabilities caps; @BeforeTest public void setupCapabilities() { caps = new DesiredCapabilities(); // Fetch the app path from Maven's system property String appPath = System.getProperty("appPath"); caps.setCapability(MobileCapabilityType.APP, appPath); // Add other required capabilities caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android"); caps.setCapability(MobileCapabilityType.DEVICE_NAME, "Pixel_5_API_30"); } }
Run your tests with:
mvn test -DappPath="/Users/you/Documents/Apps/MyShoppingApp.apk"
Scenario 2: Use TestNG Parameter Injection
If you prefer to inject the parameter via TestNG's @Parameters annotation:
- First, define the parameter in your TestNG.xml (as shown in the general method above).
- Then inject it into your setup method and set it to Capabilities:
import io.appium.java_client.android.AndroidDriver; import io.appium.java_client.remote.MobileCapabilityType; import org.openqa.selenium.remote.DesiredCapabilities; import org.testng.annotations.BeforeTest; import org.testng.annotations.Parameters; import java.net.URL; public class AppiumLoginTest { private AndroidDriver driver; @BeforeTest @Parameters("appPath") public void setup(String appPath) throws Exception { DesiredCapabilities caps = new DesiredCapabilities(); caps.setCapability(MobileCapabilityType.APP, appPath); caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android"); caps.setCapability(MobileCapabilityType.DEVICE_NAME, "emulator-5554"); driver = new AndroidDriver(new URL("http://localhost:4723/wd/hub"), caps); } }
Run the same Maven command as before to pass the app path.
Quick Notes to Avoid Issues
- If your app path has spaces, wrap it in quotes:
-DappPath="/Users/you/My Mobile App.apk" - By default, Maven's Surefire plugin passes all
-Dsystem properties to TestNG. If you have a custom Surefire config in yourpom.xml, ensure you're not overriding this behavior. If needed, explicitly pass the parameter in Surefire's<argLine>:<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M7</version> <configuration> <argLine>-DappPath=${appPath}</argLine> <suiteXmlFiles> <suiteXmlFile>testng.xml</suiteXmlFile> </suiteXmlFiles> </configuration> </plugin> </plugins> </build>
内容的提问来源于stack exchange,提问作者Manju_Asthuthe

