Michal
Michal

Reputation: 464

Selenium Webdriver fails in open Firefox

I was learning how to create a new project using Maven for Selenium Webdriver. I created a pom.xml file and one basic test file containing the test.

Here they are:

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);. I was looking for an answer, including updating Selenium, choosing Firefox Webdriver as Binary file, but all methods failed.

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

Upvotes: 0

Views: 1009

Answers (2)

Akarsh
Akarsh

Reputation: 967

Update your CleanSubmissionTest.java file with below code.

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

Upvotes: 1

SelThroughJava
SelThroughJava

Reputation: 331

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.

Just use below inside setUp() and try :-

driver = new FirefoxDriver();

Upvotes: 1

Related Questions