gpt4 book ai didi

testng - 如何在 Maven 中从 pom.xml 调用 testng.xml 文件

转载 作者:行者123 更新时间:2023-12-04 03:12:45 27 4
gpt4 key购买 nike

我正在使用 Selenium WebDriver、Eclipse、TestNG 和 Surefire 插件。我无法从 pom.xml 运行 testng.xml 文件。当我使用 运行 pom.xml 时mvn 测试 它直接运行中的文件源代码/测试/java .

我的 pom.xml 代码是

<groupId>angel</groupId>
<artifactId>Angel</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Angel</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties>

<dependencies>
<!-- Java API to access the Client Driver Protocols -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.21.0</version>
</dependency>
<!-- JExcel API is a java library which provides the ability to read, write, and modify Microsoft Excel spreadsheets.-->
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.10</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
<!-- Java API for manipulate the Microsoft Excel Sheets. -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-contrib</artifactId>
<version>3.5-beta5</version>
</dependency>
<!-- Java Mail API used to send Mails. --> <dependency> <groupId>javax.mail</groupId>
<artifactId>mail</artifactId> <version>1.4.3</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.3.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugin</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration> </plugin>

</plugins>
</project>

请帮我。

我的项目结构是
 project
--src/main/java
--src/main/resources
--src/test/java
|
--my testng class file with @Test method.
--src/test/resources
|
--testng.xml
--maven dependencies
--jre system library
--src
|
--main
--test
--target
pom.xml

-- => 文件夹名称
|-- => 子文件夹名称

我的 testng.xml 文件是...
  <?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite1" verbose="1" >

<test name="samplePage Test" >
<classes>
<class name="TestScripts.SamplePage" >
<methods>
<include name = "SamplePageTest_Execution"/>
</methods>
</class>
</classes>
</test>
<test name="newSamplePage Test" >
<classes>
<class name="TestScripts.NewSamplePage" >
<methods>
<include name = "NewSamplePage_Execution02"/>
</methods>
</class>
</classes>
</test>
</suite>

我只是想通过 testng.xml 文件从 pom.xml 调用 SamplePage_Execution 方法。

我的 SamplePage_Execution 方法是
  public class sample{
@Test
public static void SamplePageTest_Execution() throws Exception{

String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
boolean testStatus = true;
driver = new FirefoxDriver();
driver.get("http://www.google.com");

WebElement searchField = driver.findElement(By.name("q"));

searchField.sendKeys(new String [] {"selenium2.0"});

searchField.submit();

driver.close();
}}

最佳答案

我在 pom.xml 中的以下代码运行良好:

<profiles>
<profile>
<id>selenium-tests</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>

使用以下命令运行测试:
mvn clean test -U -Pselenium-tests

要么,
mvn clean test

关于testng - 如何在 Maven 中从 pom.xml 调用 testng.xml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10533151/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com