我有一个maven项目,其中我要从pom.xml文件运行testng.xml。
我的testng.xml文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="tests">
<test name="FirefoxTest">
<parameter name="browser" value="firefox"/>
<classes>
<class name="tests.PM_User_Test"/>
</classes>
</test> <!-- Test -->
<test name="ChromeTest">
<parameter name="browser" value="chrome"/>
<classes>
<class name="tests.PM_User_Test"/>
<class name="tests.PM_Extension_Test"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
下面是我的课:
package base;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.UnexpectedAlertBehaviour;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.ITestResult;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import utilities.ExcelReadAndWrite;
import utilities.Take_Screenshot;
public class ConfigClass
{
public WebDriver driver = null;
public static String excelPath = null;
public static ExcelReadAndWrite loginData;
public static ExcelReadAndWrite pmTests;
public static ExcelReadAndWrite snmTests;
public static ExcelReadAndWrite ipData;
@BeforeSuite
public void beforeSuite()
{
excelPath = "C://Users//mallikar//git//PM//PM//src//main//java//myTestData//TestData.xlsx";
loginData = new ExcelReadAndWrite("logindata", excelPath);
pmTests = new ExcelReadAndWrite("PMTestData", excelPath);
snmTests = new ExcelReadAndWrite("SNMTestData", excelPath);
ipData = new ExcelReadAndWrite("IP", excelPath);
}
@AfterSuite
public void afterSuite()
{
}
@Parameters({"browser"})
@BeforeClass
public void beforeClass(String browser)
{
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.ACCEPT);
if(browser.equalsIgnoreCase("firefox"))
{
driver = new FirefoxDriver(dc);
}
else if(browser.equalsIgnoreCase("chrome"))
{
driver = new ChromeDriver();
}
}
/*@Parameters("browser")
@BeforeClass
public void beforeClass()
{
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.ACCEPT);
driver = new FirefoxDriver(dc);
}*/
@AfterClass
public void afterClass()
{
driver.close();
}
@BeforeMethod
public void beforeMethod()
{
// driver.get("https://sqa.stackexchange.com/questions/36253/taking-screenshot-on-test-failure-selenium-webdriver-testng");
// System.out.println(driver);
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
@AfterMethod
public void afterMethod(ITestResult result) throws IOException
{
if(result.getStatus() == 2)
{
String methodName = result.getMethod().getMethodName();
new Take_Screenshot().get_Screenshot(driver, methodName);
}
}
}
But I am getting the "Parameter 'browser' is required by BeforeClass on method beforeClass but has not been marked @Optional or defined" error in maven project.
However when I run only testng.xml file it is absolutely working fine. The pro
当我从pom.xml文件运行testng.xml时出现问题。
下面是我定义了testng.xml的POM.xml文件的内容:
<profiles>
<profile>
<id>Regression</id>
<build>
<plugins>
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.2</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
我在Internet上经历了很多讨论,但是没有找到任何解决方案。请有人帮助我。
请尝试执行以下一项操作(这应该可以解决您的问题)
@Parameters
从包org.testng.annotations
导入run as TestNG test
,然后确保您编辑运行配置并提供JVM参数-Dbrowser=firefox
mvn clean test -p Regression
(这将确保配置文件Regression
被激活,并通过套件文件进行测试执行)