简体   繁体   English

Selenium Webdriver在打开的Firefox中失败

[英]Selenium Webdriver fails in open Firefox

I was learning how to create a new project using Maven for Selenium Webdriver. 我正在学习如何使用Maven for Selenium Webdriver创建一个新项目。 I created a pom.xml file and one basic test file containing the test. 我创建了一个pom.xml文件和一个包含测试的基本测试文件。

Here they are: 他们来了:

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>

and the Test File: 和测试文件:

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

But somehow there is always an error java.lang.NullPointerException: null at COS2Clean.CleanSubmissions.ClearSubmissionTest.Testno1(ClearSubmissionTest.java:21) which points to the line driver.get(baseUrlAdmin); 但是不知何故总是存在错误java.lang.NullPointerException: null at COS2Clean.CleanSubmissions.ClearSubmissionTest.Testno1(ClearSubmissionTest.java:21)该错误指向行driver.get(baseUrlAdmin); . I was looking for an answer, including updating Selenium, choosing Firefox Webdriver as Binary file, but all methods failed. 我一直在寻找答案,包括更新Selenium,选择Firefox Webdriver作为Binary文件,但是所有方法均失败。

As I was debbuging, I can see that the problem is with org.apache.maven.plugin.MojoFailureException instead of my code, therefore something in the pom.xml file must be missing - for now I haven't got any idea what more can be wrong... 当我进行调试时,我可以看到问题出在org.apache.maven.plugin.MojoFailureException而不是我的代码,因此pom.xml文件中的某些内容必定丢失了-现在我还不知道还有什么可能是错的...

You have already declared webdriver instance as 'driver' after class on line number 8. And inside setUp method of Before annotation you are again declaring the instance. 在第8行的类之后,您已经将webdriver实例声明为“ driver”。在Before注释的setUp方法中,您再次声明该实例。

Just use below inside setUp() and try :- 只需在setUp()中使用下面的方法,然后尝试:-

driver = new FirefoxDriver();

Update your CleanSubmissionTest.java file with below code. 使用以下代码更新您的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();
    }
}

If you are still facing any issue, Update your dependencies and plugins with below code and try 如果仍然遇到任何问题,请使用以下代码更新依赖项和插件,然后尝试

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

I tested it in my machine its working fine, let me know if it works for you 我在机器上测试了它的工作状况,让我知道它是否对您有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM