java - 如何运行测试而不在 IE/Chrome WebDriver 的 Action 类中传递本地相对路径,并仅使用 pom 文件中的 Maven 依赖项运行

标签 java maven selenium selenium-chromedriver selenium-iedriver

我想避免在下面的方法中给出 IE/Chrome 驱动程序的相对路径或绝对路径。相反,我想从 pom 文件调用 IE/Chrome 驱动程序作为 Maven 依赖项。我不想使用下面的本地路径,而是想传递 pom 依赖路径来调用驱动程序。请问有人可以指导我吗?

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();
            }       

}

最佳答案

首先,通过将 Artifact 及其内部可执行驱动程序文件解压到项目中的某个位置来复制依赖项 这是您的 pom(配置文件或默认构建)中需要的示例:

 <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>

之后,您应该在文件夹 ${project.build.directory}/dependency 中找到该文件,或者配置插件将其解压到您想要的位置。

如果需要,您可以从这里将文件复制到任何您想要的位置。如果您希望它与您的项目相关,您可以使用 Maven 变量,如“${project.build.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>

您可以将该文件提供给selenium。假设您将其放入类路径上的“src/main/resources”中,您应该能够通过以下方式在Java中找到该文件

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

关于java - 如何运行测试而不在 IE/Chrome WebDriver 的 Action 类中传递本地相对路径,并仅使用 pom 文件中的 Maven 依赖项运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47652877/

相关文章:

JavaMail : msg. setSubject() 在版本 1.4.7 中不接受第二个参数(编码),抛出编译时错误

java - JSTL forEach 不循环

java - Project Euler 5,我得到的答案只有一半,为什么?

PHPUnit 和 Selenium 示例 17.1

javascript - 如何迭代selenium中的每个文本字段?

java - 如何在 eclipse 中导入 Jdeveloper java 项目?

google-app-engine - datastore-connector-latest.jar的来源在哪里?我可以将其添加为 Maven 依赖项吗?

java - Maven 与 Cygwin - 错误 : JAVA_HOME is not defined correctly

java - 为什么我不能在 spring-boot 中使用 apache httpcomponents 对象,即使它列在 MVN 依赖项中?

JavaScript/Selenium - 滚动到 ul 内的 li