0

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

Michal
  • 443
  • 1
  • 10
  • 25
  • Can you cut & paste the dependencies within build tag, before the plugins & check? – undetected Selenium Mar 30 '17 at 12:37
  • sure. now the dependencies are within build tag, but I got an error `cvc-complex-type.2.4.a: Invalid content was found starting with element 'dependencies'. One of '{"http://maven.apache.org/POM/4.0.0":sourceDirectory, "http://maven.apache.org/POM/ 4.0.0":scriptSourceDirectory, "http://maven.apache.org/POM/4.0.0":testSourceDirectory(...)' is expected.` (I need to cut the whole message, too long). I tried to put it inside `` tag, but it doesn't change the error message – Michal Mar 30 '17 at 12:48
  • my bad, I never looked at the nullpointerexception, revert the dependencies to the original place – undetected Selenium Mar 30 '17 at 13:46

2 Answers2

1

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();
SelThroughJava
  • 321
  • 3
  • 14
  • I always thought that this kind of error should not stop whole process... I can see now that Eclipse show this line as a warning. Thank you for the answer :) – Michal Mar 31 '17 at 03:35
1

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

Akarsh
  • 967
  • 5
  • 9
  • It would be more helpful if you described what you changed so OP and other readers don't have to look line by line to see what you changed. – JeffC Mar 30 '17 at 16:28
  • Ha, this worked, thank you! I can see that in the pom there was an issue :) for other readers, I got also a gecko error after applying that code, the resolve can be found here: http://stackoverflow.com/questions/38676719/selenium-using-java-the-path-to-the-driver-executable-must-be-set-by-the-webdr – Michal Mar 31 '17 at 03:34