gpt4 book ai didi

java - Selenium Webdriver 在打开的 Firefox 中失败

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

我正在学习如何使用 Maven for Selenium Webdriver 创建一个新项目。我创建了一个 pom.xml 文件和一个包含测试的基本测试文件。

它们在这里:

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>CleanSubmission</groupId>
<artifactId>CleanSubmission</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>CleanSubmissions</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>com.github.yev</groupId>
<artifactId>screenshot</artifactId>
<version>0.2</version>
</dependency>
</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.13</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.13</version>
</dependency>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-testng</artifactId>
<version>2.13</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>selenium-maven-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>xvfb</id>
<phase>test-compile</phase>
<goals>
<goal>xvfb</goal>
</goals>
<configuration>
<displayPropertiesFile>2.3</displayPropertiesFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>

和测试文件:

import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class ClearSubmissionTest {
public WebDriver driver;
String baseUrlAdmin = "http://www.google.pl/";

@Before
public void setUp() throws Exception {
WebDriver driver = new FirefoxDriver();
}

@Test
public void Testno1() throws Exception {
driver.get(baseUrlAdmin);
Thread.sleep(5000);
}

@After
public void tearDown() throws Exception {
driver.close();
}
}

但是总有错误java.lang.NullPointerException: null at COS2Clean.CleanSubmissions.ClearSubmissionTest.Testno1(ClearSubmissionTest.java:21) 指向 driver.get(baseUrlAdmin); 行。我一直在寻找答案,包括更新 Selenium、选择 Firefox Webdriver 作为二进制文件,但所有方法都失败了。

当我调试时,我可以看到问题出在 org.apache.maven.plugin.MojoFailureException 而不是我的代码,因此 pom.xml 文件中的某些内容必须丢失 - 对于现在我不知道还会有什么问题......

最佳答案

使用以下代码更新您的 CleanSubmissionTest.java 文件。

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class ClearSubmissionTest{
public WebDriver driver;
String baseUrlAdmin = "http://www.google.pl/";

@BeforeTest
public void setUp() throws Exception {
driver = new FirefoxDriver();
}

@Test
public void Testno1() throws Exception {
driver.get(baseUrlAdmin);
Thread.sleep(5000);
}

@AfterTest
public void tearDown() throws Exception {
driver.close();
}
}

如果您仍然遇到任何问题,请使用以下代码更新您的依赖项和插件并尝试

<dependencies>

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.10</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>com.github.yev</groupId>
<artifactId>screenshot</artifactId>
<version>0.2</version>
</dependency>
</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.13</version>
</plugin>
</plugins>
</pluginManagement>
</build>

我在我的机器上测试了它,它工作正常,请告诉我它是否适合您

关于java - Selenium Webdriver 在打开的 Firefox 中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43117944/

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