我已经编写了一个Spring Test集成设置来运行硒测试。无法使用spring实例化驱动程序对象
是Spring的新手。请让我知道哪里做错了。
我正在尝试调用硒驱动程序对象,以便我可以在测试类中使用它
//此类创建新的驱动程序对象
DriverFactory
@Configuration
public class DriverFactory {
public DriverFactory() {
}
@Bean
public WebDriver getDriver() {
ChromeDriverManager.chromedriver().setup();
return new ChromeDriver();
}
}
//此类用于执行组件扫描
TestCoreConfiguration
@Configuration
@ComponentScan(basePackages = { "com.cdm" })
public class TestCoreConfiguration {
public TestCoreConfiguration() {
}
}
//此类启动驱动程序
CoreBaseTest
@ContextConfiguration(
classes = {TestCoreConfiguration.class,DriverFactory.class}
)
public class CoreBaseTest extends AbstractTestNGSpringContextTests{
@Autowired(required = true )
public WebDriver cs;
public CoreBaseTest() {
System.out.println(cs +"&&&&&&&&&&&&&&");
cs.get("https://www.google.com");
}
}
应该调用驱动程序对象
//此类用于运行实际测试
SanityTest
public class SanityTest extends CoreBaseTest{
@Test
public void sanityTest() {
}
}
例外
Cannot instantiate class com.cdm.Login.SanityTest
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:30)
at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:423)
at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:336)
at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:125)
at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:190)
at org.testng.TestClass.getInstances(TestClass.java:95)
at org.testng.TestClass.initTestClassesAndInstances(TestClass.java:81)
at org.testng.TestClass.init(TestClass.java:73)
at org.testng.TestClass.<init>(TestClass.java:38)
at org.testng.TestRunner.initMethods(TestRunner.java:389)
at org.testng.TestRunner.init(TestRunner.java:271)
at org.testng.TestRunner.init(TestRunner.java:241)
at org.testng.TestRunner.<init>(TestRunner.java:192)
at org.testng.remote.support.RemoteTestNG6_12$1.newTestRunner(RemoteTestNG6_12.java:33)
at org.testng.remote.support.RemoteTestNG6_12$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG6_12.java:66)
at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:713)
at org.testng.SuiteRunner.init(SuiteRunner.java:260)
at org.testng.SuiteRunner.<init>(SuiteRunner.java:198)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1295)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1273)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1128)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:24)
... 25 more
Caused by: java.lang.NullPointerException
at com.cdm.core.CoreBaseTest.<init>(CoreBaseTest.java:23)
at com.cdm.Login.SanityTest.<init>(SanityTest.java:7)
... 30 more