简体   繁体   English

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

[英]Getting the Error - java.lang.ClassCastException: org.openqa.selenium.chrome.ChromeDriver cannot be cast to com.initialization.DriverInitialization

I am trying to automate a web application for multiple browser testing. 我正在尝试自动化Web应用程序以进行多个浏览器测试。 I am trying to integrate some spring features but finding a bit of dificulties. 我正在尝试整合一些春季功能,但发现了一些困难。 Here is my code : 这是我的代码:

On DriverInitialize.java : 在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;
    }
}

On Start.java 在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);
    }
}

On BrowserTest.java : 在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 ....");
    }

}

On driversetup.xml : 在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>

On startup.xml : 在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>

On driverpath.properties : 在driverpath.properties上:

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

On driver.properties : 在driver.properties上:

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

When I execute this from startup.xml I get the error on TestNG report : 当我从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

You can not cast from a Driver Class to DriverInitialization . 您不能从驱动程序类DriverInitializationDriverInitialization

You have to use your setter method: 您必须使用setter方法:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.ClassCastException:org.openqa.selenium.By $ ById无法转换为org.openqa.selenium.WebElement - java.lang.ClassCastException: org.openqa.selenium.By$ById cannot be cast to org.openqa.selenium.WebElement java.lang.ClassCastException: com.sun.proxy.$Proxy8 不能转换为 org.openqa.selenium.internal.WrapsDriver - java.lang.ClassCastException: com.sun.proxy.$Proxy8 cannot be cast to org.openqa.selenium.internal.WrapsDriver java.lang.ClassCastException: 类 org.openqa.selenium.By$ByXPath 不能转换为类 org.openqa.selenium.WebElement - java.lang.ClassCastException: class org.openqa.selenium.By$ByXPath cannot be cast to class org.openqa.selenium.WebElement java.lang.ClassCastException: org.openqa.selenium.firefox.FirefoxDriver cannot be cast to org.openqa.selenium.interactions.HasTouchScreen - java.lang.ClassCastException: org.openqa.selenium.firefox.FirefoxDriver cannot be cast to org.openqa.selenium.interactions.HasTouchScreen Java 12 无法访问 org.openqa.selenium.chrome.ChromeDriver 类型 - The type org.openqa.selenium.chrome.ChromeDriver is not accessible with Java 12 线程“ main”中的异常java.lang.ClassCastException:java.util.HashMap无法转换为org.openqa.selenium.WebElement - Exception in thread “main” java.lang.ClassCastException: java.util.HashMap cannot be cast to org.openqa.selenium.WebElement java.lang.ClassCastException:无法转换为 org.openqa.selenium.WebElement 使用 executeScript() 从 shadowHost 返回 shadowRoot - java.lang.ClassCastException: cannot be cast to org.openqa.selenium.WebElement using executeScript() to return shadowRoot from shadowHost 线程“主”中的异常java.lang.ClassCastException:无法将java.util.ArrayList强制转换为org.openqa.selenium.WebElement - Exception in thread “main” java.lang.ClassCastException: java.util.ArrayList cannot be cast to org.openqa.selenium.WebElement java.lang.ClassCastException:通过Selenium执行测试时,无法将java.base / java.lang.String强制转换为org.openqa.selenium.WebElement - java.lang.ClassCastException: java.base/java.lang.String cannot be cast to org.openqa.selenium.WebElement when executing test through Selenium java.lang.ClassCastException:org.bson.types.ObjectId无法转换为com.mongodb.DBObject - java.lang.ClassCastException: org.bson.types.ObjectId cannot be cast to com.mongodb.DBObject
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM