1

I am building tests using selenium. I was trying to follow some tutorial and also look at information here but I can not figure out why my test will not run correctly.

I have the following Test Set up class

import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;

import java.util.Arrays;

public class TestSetUp {

private WebDriver driver;

public WebDriver getDriver() {
    return driver;
}

private void setDriver(int type, String url) {

    switch (type) {
        case Config.FIREFOX_DRIVER:
            System.out.println("Setting drivers for Firefox...");
            driver = initFireFoxDriver(url);
            break;
        default:
            System.out.print("invalid browser type");
    }
}

private static WebDriver initFireFoxDriver(String url) {
    System.out.println("Launching Firefox browser...");
    WebDriver driver = new FirefoxDriver();
    driver.manage().window().maximize();
    driver.navigate().to(url);
    return driver;
}

@BeforeClass
public void initializeTestBaseSetup(int type, String url) {
    try {
        setDriver(1, "google.com");
    } catch (Exception e) {
        System.out.println("Error....." + Arrays.toString(e.getStackTrace()));
    }
}

@AfterClass
public void tearDown() {
    driver.quit();
}

@Test
public void startTests() {
    System.out.println("Starting Tests...");

}
}

I then have a test class for log on here

public class LoginPageTest extends TestSetUp {

private WebDriver driver;

@Before
public void setUp() {
    driver = getDriver();
}

@Test
public void verifyLoginInFunction() {
    System.out.println("Log In functionality being tested...");

    LoginPage loginPage = new LoginPage(driver);

    Assert.assertTrue("The url is incorrect", loginPage.verifyURL());
    Assert.assertTrue("Title did not match", loginPage.verifyLoginTitle());
    Assert.assertTrue("Error message did not match", loginPage.verifySignIn());
    Assert.assertTrue("\'Forgot Password?\' link is missing", loginPage.verifyForgotPassword());
    Assert.assertTrue("Create Account setting not working", loginPage.verifyCreateAccount());
    Assert.assertTrue("Additional Licence Key setting not working", loginPage.verifyLicencing());

    HomePage homePage = loginPage.signIn(Config.STUDENT, Config.STUDENTPASS);

    Assert.assertTrue("Login did not work", homePage.verifyURL());
}
}

My current out put is

Starting Tests...

Log In functionality being tested...

java.lang.NullPointerException

at verifyURL(LoginPage.java:20)

at verifyLoginInFunction(LoginPageTest.java:28)

.

.

.

Starting Tests...

Process finished with exit code -1

I did leave out the paths of my packages here but I can't figure out why the browser will not start up. It never does initializeTestBaseSetup so it doesn't get to the rest of the set up. Any help or links would be greatly appreciated.

FIXED: I needed to add and xml file to run rather then a test suite. Here is my testing.xml

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Page Object test example">
    <parameter name="url" value="http://google.com/"/>
    <parameter name="type" value="firefox"/>
    <test name="sample test">
        <classes>
            <class name="LoginPageTest"/>
        </classes>
    </test>
</suite>
Michael Gibson
  • 29
  • 1
  • 12
  • 1
    What exactly is on line 20 in the file LoginPage? notice the stacktrace `LoginPage.java:20` – Austin Aug 04 '15 at 17:52
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – David Aug 04 '15 at 17:53
  • @Austin The issue here is that it never even launches the browser, I may have to edit the title. The reason I am getting the exception is because there is no url to be found. – Michael Gibson Aug 04 '15 at 17:55

1 Answers1

0

According to the docs, the @BeforeClass method should be static and should have no args:

public static void initializeTestBaseSetup() {
    // ...
}

This is necessary because, as the name implies, this is being invoked before the object itself is instantiated. And since the framework has no way of knowing what to supply for arguments, it can't pass anything to that method.

It would seem that your initialization is simply being ignored by JUnit as a result. (This doesn't present itself as an error or warning of any kind?)

Whether or not this directly causes your null pointer exception, we can't say. That's taking place on line 20 in the verifyURL method of your LoginPage class, which isn't included in the question. Though it seems like a reasonable conclusion.

David
  • 208,112
  • 36
  • 198
  • 279
  • Thank you for your answer. I did figure out what I was doing wrong. I needed to create and xml file to run my classes rather then a suite. I was trying to do it without it. You can check the tuturial I was using here: [link](http://seleniumeasy.com/selenium-tutorials/simple-page-object-model-framework-example). Now I am running into a new issue that I must figure out haha – Michael Gibson Aug 04 '15 at 18:41