gpt4 book ai didi

java - 收到错误 - java.lang.ClassCastException : org. openqa.selenium.chrome.ChromeDriver 无法转换为 com.initialization.DriverInitialization

转载 作者:行者123 更新时间:2023-12-02 12:42:35 32 4
gpt4 key购买 nike

我正在尝试自动化 Web 应用程序以进行多个浏览器测试。我正在尝试集成一些 Spring 功能,但发现了一些困难。这是我的代码:

在 DriverInitialize.java 上:

package com.initialization;

import org.openqa.selenium.WebDriver;

public class DriverInitialization {

private WebDriver webDriver;

public void setWebDriver(WebDriver webDriver) {
this.webDriver = webDriver;
}

public WebDriver getWebDriver() {
return webDriver;
}
}

在 Start.java 上

package com.initialization;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

public class Start {
public static Properties properties;

public static void loadProperties() {
properties = new Properties();

try {

properties.load(new FileReader("src/com/properties/driver.properties"));
properties.load(new FileReader("src/com/properties/driverpath.properties"));

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static Properties getAllProperties() {
return properties;
}

public static String getProperty(String property) {
return properties.getProperty(property);
}
}

在 BrowserTest.java 上:

package com.tests;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import com.initialization.DriverInitialization;
import com.initialization.Start;

public class BrowserTest {
private ApplicationContext context;
private DriverInitialization driverInit;
@Parameters("browser")
@BeforeClass
public void beforeClass(String browser) {
System.out.println("Browser : " + browser);
Start.loadProperties();
System.setProperty(Start.getProperty("driver." + browser), Start.getProperty("driverpath." + browser));

System.out.println(Start.getAllProperties());

context = new ClassPathXmlApplicationContext("com/driversetup/driversetup.xml");
driverInit = (DriverInitialization) context.getBean(browser);
}

@Test
public void test() {
System.out.println(driverInit.getWebDriver());
}

@AfterClass
public void AfterClass() {
System.out.println("After Class ....");
}

}

在 driversetup.xml 上:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">

<bean id="chrome" class="org.openqa.selenium.chrome.ChromeDriver" lazy-init="true"/>
<bean id="firefox" class="org.openqa.selenium.firefox.FirefoxDriver" lazy-init="true" />
<bean id="ie" class="org.openqa.selenium.ie.InternetExplorerDriver" lazy-init="true" />

</beans>

在startup.xml上:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
<test name="ChromeTest">
<parameter name="browser" value="chrome" />
<classes>
<class name="com.tests.BrowserTest" />
</classes>
</test>
</suite>

在 driverpath.properties 上:

driverpath.chrome=drivers/chromedriver.exe
driverpath.firefox=drivers/geckodriver.exe
driverpath.ie=drivers/IEDriverServer.exe

在 driver.properties 上:

driver.chrome=webdriver.chrome.driver
driver.firefox=webdriver.gecko.driver
driver.ie=webdriver.ie.driver

当我从startup.xml 执行此命令时,我在 TestNG 报告上收到错误:

java.lang.ClassCastException: org.openqa.selenium.chrome.ChromeDriver cannot be cast to com.initialization.DriverInitialization
at com.tests.BrowserTest.beforeClass(BrowserTest.java:26)
... Removed 25 stack frames

最佳答案

您无法从驱动程序类转换为DriverInitialization

你必须使用你的setter方法:

 driverInit = new DriverInitialization();
driverInit.setWebDriver(context.getBean(browser));

关于java - 收到错误 - java.lang.ClassCastException : org. openqa.selenium.chrome.ChromeDriver 无法转换为 com.initialization.DriverInitialization,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44880579/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com