Search code examples
javamavenseleniumselenium-chromedriverselenium-iedriver

How to run tests without passing local relative path in Action class for the IE/Chrome WebDriver and run using only maven dependencies in pom file


I want to avoid giving the relative path or absolute path for IE/Chrome driver in this below method. Rather I want to call the IE/Chrome driver from pom file as maven dependencies. Instead of using the below local path I want to pass the pom dependency path to call the driver. Could you please anyone guide me on this.

public static void openBrowser(String data) throws Exception{

        if(data.equals("IE")){              
            File file = new File("C:\\Automation\\external jars\\IE Related\\IEDriverServer.exe");
            System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
            driver = new InternetExplorerDriver();
            driver.manage().window().maximize();
            }
        else (data.equals("Chrome")){
            File file = new File("C:\\Automation\\external jars\\Drivers\\ChromeDriver\\chromedriver.exe");
            System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
            driver=new ChromeDriver();
            driver.manage().window().maximize();
            }       

}

Solution

  • First you copy your dependency by unpacking the artifact with its inner executable driver file somewhere into your project Here a sample you need in your pom(profile or default build):

     <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.10</version>
    
        <executions>
            <execution>
                <id>unpack</id>
                <phase>process-resources</phase>
                <goals>
                    <goal>unpack</goal>
                </goals>
                <configuration>
                    <artifactItems>
                        <artifactItem>
                           <groupId>org.seleniumhq.selenium</groupId> 
                           <artifactId>selenium-ie-driver</artifactId>
                           <version>3.0.1</version>
                        </artifactItem>
                    </artifactItems>
              </configuration>
          </execution>
       </executions>
    </plugin>
    

    After this you should find the file in the folder ${project.build.directory}/dependency or configure the plugin to unpack it to a place you want.

    From this place you can, if you need , copy the file wherever you want. If you want it relative to your project you can use the maven vars like "${project.build.directory}" or even source directory.

                    <!-- copy driver file -->
                    <plugin>
                        <artifactId>maven-resources-plugin</artifactId>
                        <version>3.0.1</version>
                        <executions>
                            <execution>
                                <id>copy-resources</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>copy-resources</goal>
                                </goals>
                                <configuration>
                                    <outputDirectory>${project.build.directory}/driverDir</outputDirectory>
                                    <resources>
                                        <resource>
                                            <include>driver.file</include>
                                            <directory>${project.build.directory}/dependency</directory>
                                        </resource>
                                    </resources>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
    

    This file you can provide to selenium.Lets assume you put it into "src/main/resources" which is on classpath you should be able to find this file in Java by

    MyClass.class.getResource("/driver.file");