@Test方法无法在TestNG类中注入多个类

时间:2019-05-13 12:35:12

标签: java selenium testng

我用TestNG类创建了一个Maven项目。在TestNG.xml中,我指定了套件名称。我使用了多个浏览器Chrome和Firefox来并行运行。就设置类和一个以上的类而言,它可以正常工作,但是当我在多个包含@Test注释的类时,将出现注入错误并给出错误。

我将提供我尝试过的代码

Setup.java

    if (browser.equals("Firefox")) {
          /*the path of the gecko driver is set*/
          System.setProperty("firefoxpath");
          drfirefox=DesiredCapabilities.firefox();
          drfirefox.setBrowserName("firefox");
          drfirefox.setPlatform(Platform.WINDOWS);
        } else {
          /*the path of the chrome driver is set*/
          System.setProperty("chrome path");
          drchrome=DesiredCapabilities.chrome();
          drchrome.setBrowserName("chrome");
          drchrome.setPlatform(Platform.WINDOWS);
        }
logintest_valid.java 
@Test
public static void valid_logintest ()throws MalformedURLException, InterruptedException {

 }
@Test
 public static void valid_test ()throws MalformedURLException, InterruptedException {   

    }

我收到以下错误消息:

  

无法使用[class org.openqa.selenium.remote.DesiredCapabilities]注入@Test注释方法[valid_test]。

期望同时运行两个测试用例valid_logintest和valid_test

2 个答案:

答案 0 :(得分:0)

很可能您的项目中某处有一个函数,如下所示:

@Test
public void sometest(DesiredCapabilities caps) {  
}

这不是对TestNG test methods进行参数化的正确方法,您应该从DesiredCapabilities注释的方法中删除此@Test自变量

如果要将外部参数传递给以@Test注释的方法,则应使用@Parameters annotation

答案 1 :(得分:0)

我认为@Test注释应使用非静态方法。