我在使用 selenium、TestNG 框架启动 chrome 驱动程序时遇到错误。尝试了多种方法,但测试用例失败并一直跳过它。这是我的第一个项目,所以我一无所知。错误日志
[RemoteTestNG] detected TestNG version 7.9.0
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.collectMethodsByGroup() took 8 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.sortMethods() took 14 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.collectMethodsByGroup() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.sortMethods() took 1 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.collectMethodsByGroup() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.sortMethods() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.collectMethodsByGroup() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.sortMethods() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.collectMethodsByGroup() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.sortMethods() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.collectMethodsByGroup() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.sortMethods() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.collectMethodsByGroup() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.sortMethods() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.collectMethodsByGroup() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.sortMethods() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.collectMethodsByGroup() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.sortMethods() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.collectMethodsByGroup() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.sortMethods() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.collectMethodsByGroup() took 1 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.sortMethods() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.collectMethodsByGroup() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.sortMethods() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.collectMethodsByGroup() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.sortMethods() took 1 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.collectMethodsByGroup() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.sortMethods() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.collectMethodsByGroup() took 1 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.sortMethods() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [TestNG] Running:
C:\Users\prabh\AppData\Local\Temp\testng-eclipse-705038558\testng-customsuite.xml
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] DynamicGraphHelper.createDynamicGraph() took 5 ms.
FAILED CONFIGURATION: @BeforeSuite commonfunctionalities.commonfunction.launchbrowser
org.testng.TestNGException:
Native Injection is NOT supported for @BeforeSuite annotated launchbrowser.
For more information on native dependency injection please refer to https://testng.org/doc/documentation-main.html#native-dependency-injection
at org.testng.internal.Parameters.checkParameterTypes(Parameters.java:445)
at org.testng.internal.Parameters.createParametersForMethod(Parameters.java:350)
at org.testng.internal.Parameters.createParameters(Parameters.java:702)
at org.testng.internal.Parameters.createConfigurationParameters(Parameters.java:166)
at org.testng.internal.invokers.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:306)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:382)
at org.testng.SuiteRunner.run(SuiteRunner.java:336)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:95)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1228)
at org.testng.TestNG.runSuites(TestNG.java:1134)
at org.testng.TestNG.run(TestNG.java:1101)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
SKIPPED: testcases.pending_leave_request.verifypendingleaverequest
org.testng.TestNGException:
Native Injection is NOT supported for @BeforeSuite annotated launchbrowser.
For more information on native dependency injection please refer to https://testng.org/doc/documentation-main.html#native-dependency-injection
at org.testng.internal.Parameters.checkParameterTypes(Parameters.java:445)
at org.testng.internal.Parameters.createParametersForMethod(Parameters.java:350)
at org.testng.internal.Parameters.createParameters(Parameters.java:702)
at org.testng.internal.Parameters.createConfigurationParameters(Parameters.java:166)
at org.testng.internal.invokers.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:306)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:382)
at org.testng.SuiteRunner.run(SuiteRunner.java:336)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:95)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1228)
at org.testng.TestNG.runSuites(TestNG.java:1134)
at org.testng.TestNG.run(TestNG.java:1101)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
===============================================
Default test
Tests run: 1, Failures: 0, Skips: 1
Configuration Failures: 1, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Passes: 0, Failures: 0, Skips: 1
Configuration Failures: 1, Skips: 1
===============================================
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [TestNG] Time taken by org.testng.reporters.jq.Main@6d07a63d: 48 ms
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [TestNG] Time taken by org.testng.reporters.FailedReporter@59cba5a: 12 ms
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [TestNG] Time taken by org.testng.reporters.XMLReporter@45a37759: 11 ms
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [TestNG] Time taken by org.testng.reporters.EmailableReporter2@31add175: 7 ms
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [TestNG] Time taken by org.testng.reporters.JUnitReportReporter@457c9034: 5 ms
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [TestNG] Time taken by org.testng.internal.ExitCodeListener@345f69f3: 1 ms
这里是常用功能程序 硒版本:4.11.0 测试NG版本:7.9.0 Eclipse 版本:: 2022-06 (4.24.0)
package commonfunctionalities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
public class commonfunction {
public static WebDriver driver=null;
public static Properties properties = null;
public void loadpropertyfile() throws IOException {
FileInputStream fileInputStream = new FileInputStream("C:\\Users\\prabh\\IdeaProjects\\OpenHrmProjects\\extras\\config.properties");
properties = new Properties();
properties.load(fileInputStream);
}
@SuppressWarnings("deprecation")
@BeforeSuite
public void launchbrowser(String driverlocation) throws IOException {
loadpropertyfile();
String browser = properties.getProperty("browser");
String url = properties.getProperty("url");
String Driverlocation = properties.getProperty("Driverlocation");
if (browser.equalsIgnoreCase("firefox")) {
System.setProperty("webdriver.gecko.driver",Driverlocation);
driver = new FirefoxDriver();
} else if (browser.equalsIgnoreCase("chrome")) {
System.setProperty("webdriver.chrome.driver",Driverlocation);
driver = new ChromeDriver();}
driver.manage().window().maximize();
driver.get(url);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@AfterSuite
public void teardown() {
driver.quit();
}
}
这是代码中出现问题的根本原因
@SuppressWarnings("deprecation")
@BeforeSuite
public void launchbrowser(String driverlocation) throws IOException { }
方法
launchbrowser()
有一个参数。
TestNG 在尝试调用带注释的
driverlocation
@BeforeSuite
时不知道如何传递参数
launchbrowser()
的值
您应该删除此方法参数。
如果您希望从套件文件中检索
driverlocation
的值,那么您可以执行如下操作:
import org.testng.ISuite;
import org.testng.Reporter;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import java.util.Optional;
public class SampleTestCase {
@BeforeSuite
@Parameters({"driver"})
public void beforeSuiteVariant1() {
ISuite suite = Reporter.getCurrentTestResult().getTestContext().getSuite();
Optional.ofNullable(suite.getParameter("driver"))
.ifPresent(it ->
System.err.println("Driver retrieved programmatically: " + it));
}
@BeforeSuite
@Parameters({"driver"})
public void beforeSuiteVariant2(String driver) {
System.err.println("Driver retrieved via mandatory parameter in suite file: " + driver);
}
@BeforeSuite
@Parameters({"driver"})
public void beforeSuiteVariant3(@org.testng.annotations.Optional String driver) {
System.err.println("Driver retrieved via optional parameter in suite file: " + driver);
}
@Test
public void testMethod() {}
}
现在,当您从 IDE 中运行上述测试类(不使用套件文件)时,您会注意到 TestNG 将在
beforeSuiteVariant2
上出错,因为此方法需要从套件文件中强制注入一个参数。
当您从 IDE 中运行测试时,TestNG IDE 插件(IntelliJ 和 Eclipse)将动态创建一个套件文件,该文件将不包含 driver
参数and thus
beforeSuiteVariant2` 将失败。
但是如果您创建一个如下所示的套件文件:
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="my_suite" verbose="2">
<parameter name="driver" value="chromedriver"/>
<test name="my_test">
<classes>
<class name="com.rationaleemotions.so.qn77995590.SampleTestCase"/>
</classes>
</test>
</suite>
您将看到如下所示的输出:
...
... TestNG 7.9.0 by Cédric Beust ([email protected])
...
Driver retrieved programmatically: chromedriver
Driver retrieved via mandatory parameter in suite file: chromedriver
Driver retrieved via optional parameter in suite file: chromedriver
PASSED: com.rationaleemotions.so.qn77995590.SampleTestCase.testMethod
===============================================
my_test
Tests run: 1, Failures: 0, Skips: 0
===============================================
要记住的要点:
@BeforeSuite
带注释的方法可以有一个参数,前提是您使用 TestNG 中的 @Parameters
注释。这样,TestNG 就知道它应该在 <parameters>
级别(不在 <suite>
级别内)读取套件文件的 <test>
标记,并使用该值作为参数传递给方法。@BeforeSuite
方法的参数定义默认行为,即您想告诉 TestNG,请使用套件文件中的参数值(如果存在)。如果它不存在,请不要向我抱怨,而是使用默认值,那么您应该使用beforeSuiteVariant1()
(或)beforeSuiteVariant3()
我认为您不再需要管理 selenium webdriver 位置。如果您使用的是最新的 selenium 4.x 系列(我认为今天最新发布的版本是
4.17.0
),那么 Selenium Manager 会自动为您执行此操作。您只需确保执行以下操作:
PATH
变量一部分的所有位置,并确保从这些位置删除所有 chromedriver|edgedriver|geckodriver
二进制文件。System.setProperty("webdriver.gecko.driver",Driverlocation);