首页 > 解决方案 > @Test 方法无法在 TestNG 类中注入多个类

问题描述

我创建了一个带有 TestNG 类的 Maven 项目。在 TestNG.xml 中,我给出了套件名称。我使用了多个浏览器 Chrome 和 Firefox 来并行运行。只需使用设置类和另外一个类,它就可以正常工作,但是当我包含多个带有@Test注释的类时,我会收到注入错误并给出错误。

我将提供我尝试过的代码

安装程序.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

标签: javaseleniumtestng

解决方案


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

@Test
public void sometest(DesiredCapabilities caps) {  
}

这不是参数化TestNG 测试方法的正确方法,您应该从使用@Test注释的方法中删除此DesiredCapabilities参数

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


推荐阅读