gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:55:15 25 4
gpt4 key购买 nike

我想避免在下面的方法中给出 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/

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