运行Selenium案例时,我想在同一版本中使用firefox和chrome。我的testng.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Browser Compatibility Test Cases--firefox" thread-count="1" preserve-order="false">
<parameter name="browserType" value="firefox" />
<classes>
<class name="com.yeetrack.selenium.test.ParameterTest" />
</classes>
</test>
<test name="Browser Compatibility Test Cases--chrome" thread-count="1" preserve-order="false">
<parameter name="browserType" value="chrome" />
<classes>
<class name="com.yeetrack.selenium.test.ParameterTest" />
</classes>
</test>
</suite>
我的测试用例:
public class ParameterTest {
@Parameters("browserType")
@BeforeMethod
public void before(String browser)
{
System.out.println(browser);
}
@Test(dataProvider = "KeywordDataProvider", dataProviderClass = KeywordData.class)
public void test(String keyword)
{
System.out.println(keyword);
}
}
但我收到了一个错误:
before(com.yeetrack.selenium.test.ParameterTest) Time elapsed: 0.222 sec <<< FAILURE!
org.testng.TestNGException:
Parameter 'browserType' is required by @Configuration on method before
but has not been defined in src\test\resources\testng.xml
at org.testng.internal.Parameters.createParameters(Parameters.java:109)
at org.testng.internal.Parameters.createParameters(Parameters.java:264)
at org.testng.internal.Parameters.createConfigurationParameters(Parameters.java:69)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:135)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:427)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:607)
运行时,browserType = firefox,它通过了。但是当browserType = chrome时,它失败了。我不能同时使用@Parameters和DataProvider?当我将案例改为:
@Test //no DataProvider
public void test()
{
System.out.println("Hello world"));
}
它通过了!为什么? THX。
答案 0 :(得分:0)
这是OP的评论解决方案作为答案,为未来的访问者提供更清晰的信息。
在pom.xml中,原始海报添加了testng而不是surefire:
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
</dependency>
这不是surefire-testng:
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-testng</artifactId>
<version>2.14.1</version>
</dependency>