我正在尝试使用Junit运行跨浏览器测试(从xml文件读取),我收到此错误:
**"java.lang.Exception: Method verifyTitle should have no parameters at org.junit.runners.model.FrameworkMethod.validatePublicVoidNoArg(FrameworkMethod.java:69) at org.junit.runners.ParentRunner.validatePublicVoidNoArgMethods(ParentRunner.java:131) at org.junit.runners.BlockJUnit4ClassRunner.validateTestMethods(BlockJUnit4ClassRun"**
XML代码:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1" parallel="tests" >
<test name="fireFoxTest" >
<parameter name="browser" value="fireFox"/>
<classes>
<class name="crossbrowser.VerifyTitle" />
</classes>
</test>
班级代码:
package testCases;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Parameters;
import utility.ConfigReader;
public class VerifyTitle {
WebDriver driver;
static ConfigReader config;
@Test
@Parameters("browser")
public void verifyTitle(String browserName) {
if (browserName.equalsIgnoreCase("chrome"))
{
config = new ConfigReader();
System.setProperty("Webdriver.chrome.driver", config.getChromePath());
System.out.println("========== Setup Ready =========");
}
可能是什么原因以及如何解决?
答案 0 :(得分:0)
问题是你混淆了JUnit和TestNG。如果您打算使用 TestNG ,那么:
删除
import org.junit.Test;
添加
import org.testng.annotations.Test;
请参阅完整示例here。
如果您打算使用 Junit ,那么
删除
import org.testng.annotations.Parameters;
添加
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
以这种方式注释类:
@RunWith(Parameterized.class)
public class VerifyTitle {
添加名为browserName
的成员变量,并从函数中删除参数。注释该变量而不是函数:
@Parameter(value = "chrome")
public String browserName;
@Test
public void verifyTitle() {
请参阅完整示例here
答案 1 :(得分:0)
i tried to fix the problem according the steps above but still i encountered failures.
this is what i did:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import utility.ConfigReader;
@RunWith(Parameterized.class)
public class VerifyTitle {
WebDriver driver;
static ConfigReader config;
@Parameter(value = "chrome")
public String browserName;
@Test
public void verifyTitle() {
if (browserName.equalsIgnoreCase("chrome"))
{
config = new ConfigReader();
System.setProperty("Webdriver.chrome.driver", config.getChromePath());
System.out.println("========== Setup Ready =========");
}
我看到的问题:
1. import org.junit.runners.Parameterized.Parameter; ----not recognized
2. java.lang.Exception: No public static parameters method on class testCases.VerifyTitle
at org.junit.runners.Parameterized.getParametersMethod(Parameterized.java:163)
at org.junit.runners.Parameterized.getParametersList(Parameterized.java:149)
at org.junit.runners.Parameterized.<init>(Parameterized.java:135)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
Am i missing something else?
Thanks