title: Getting the Error - java.lang.ClassCastException: org.openqa.selenium.chrome.ChromeDriver cannot be cast to com.initialization.DriverInitialization
body: <p>I am trying to automate a web application for multiple browser testing. I am trying to integrate some spring features but finding a bit of dificulties. Here is my code :</p>
  
  <p>On DriverInitialize.java :</p>
  
  <pre><code>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;
      }
  }
  </code></pre>
  
  <p>On Start.java</p>
  
  <pre><code>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);
      }
  }
  </code></pre>
  
  <p>On BrowserTest.java :</p>
  
  <pre><code>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 ....");
      }
  
  }
  </code></pre>
  
  <p>On driversetup.xml :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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"&gt;
  
      &lt;bean id="chrome" class="org.openqa.selenium.chrome.ChromeDriver" lazy-init="true"/&gt;
      &lt;bean id="firefox" class="org.openqa.selenium.firefox.FirefoxDriver" lazy-init="true" /&gt;
      &lt;bean id="ie" class="org.openqa.selenium.ie.InternetExplorerDriver" lazy-init="true" /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>On startup.xml :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"&gt;
  &lt;suite name="Suite" parallel="none"&gt;
      &lt;test name="ChromeTest"&gt;
          &lt;parameter name="browser" value="chrome" /&gt;
          &lt;classes&gt;
              &lt;class name="com.tests.BrowserTest" /&gt;
          &lt;/classes&gt;
      &lt;/test&gt;
  &lt;/suite&gt;
  </code></pre>
  
  <p>On driverpath.properties :</p>
  
  <pre><code>driverpath.chrome=drivers/chromedriver.exe
  driverpath.firefox=drivers/geckodriver.exe
  driverpath.ie=drivers/IEDriverServer.exe
  </code></pre>
  
  <p>On driver.properties :</p>
  
  <pre><code>driver.chrome=webdriver.chrome.driver
  driver.firefox=webdriver.gecko.driver
  driver.ie=webdriver.ie.driver
  </code></pre>
  
  <p>When I execute this from startup.xml I get the error on TestNG report :</p>
  
  <pre><code>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
  </code></pre>