1

I am in the process of automating a set of manual tests on a website.

As the website is still in the process of being developed and tested, it is constantly under change. Therefore, my idea is to create a standard set of 'master' tests that are used in most (if not all) tests, that I can call from other tests and that can be easily changed where necessary.

I have created a package called 'Master', containing two classes called 'Login' and 'Logout'. I have created another package called 'SmokeTests', containing a class called 'Smoke003'.

The Login script runs successfully when I call it from Smoke003 however, I cannot seem to write/run the Logout script successfully.

Please see below my code for the 3 different classes:

Login:

package Master;

import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import static junit.framework.TestCase.assertEquals;

public class Login {

static WebDriver driver;

@Test
public void testLogin(){
    String loginname1 = "EMAIL HERE";
    String password1 = "PASSWORD HERE";

    driver = new FirefoxDriver();
    driver.get("WEBSITE HERE");

driver.findElement(By.id("LoginButton")).click();
driver.findElement(By.id("loginEmail")).sendKeys(loginname1);
driver.findElement(By.id("loginPassword")).sendKeys(password1);
driver.findElement(By.id("loginOKButton")).click();

    new     WebDriverWait(driver,5).until(ExpectedConditions.textToBePresentInElementLocated(By.className("underline"),"Events"));
    assertEquals("TEXT HERE",driver.findElement(By.className("TEXT HERE")).getText(),"TEXT HERE");

}

}

Logout:

package Master;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class Logout {

static WebDriver driver;

@Test
public void testLogout(){

    driver.findElement(By.cssSelector("TEXT HERE")).click();

}

}

SMOKE003:

package SmokeTests;

import Master.Login;
import Master.Logout;
import org.junit.Test;

public class Smoke_003 {

public static void main(String args[]) {
    Login Login01 = new Login();
    Login01.testLogin();

    Logout Logout01 = new Logout();
    Logout01.testLogout();
}

Is anybody please able to help with why I keep receiving the error message 'java.lang.NullPointerException' when running/debugging the logout test? I am aware that there is no driver.get website specified in the Logout script. I don't believe I need to do this as by the time I come to logout I will already be logged in?

NOTE: I have edited some information such as email address etc. for confidentiality reasons.

UPDATE: If I run Smoke003, it calls the login script and logs in successfully but fails when trying to call the logout script with the following error message in full:

Exception in thread "main" java.lang.NullPointerException
at Master.Logout.testLogout(Logout.java:14)
at SmokeTests.Smoke_003.main(Smoke_003.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at     sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
  • Does Smoke_003 work correctly? If Smoke_003 works, maybe you need to login first before test logout. – longhua Jul 29 '14 at 13:26
  • Smoke 003 runs the login call successfully and it logs into the website successfully, but then fails when it comes to calling the logout test. I will edit the original post now and include the full error message. – David-SeniorTestAnalyst Jul 29 '14 at 13:30
  • Would you please try to use the same driver for test both login and logout? – longhua Jul 29 '14 at 13:33
  • You can just move testLogin and testLogout to the same class and have a try. Do remember you need to initialise properly for logout test. – longhua Jul 29 '14 at 13:38
  • I copy and pasted 'driver.findElement(By.cssSelector("[href='/logout']")).click();' into the Login script and the Login script worked. It logged in and then logged out using the logout button successfully. – David-SeniorTestAnalyst Jul 29 '14 at 13:49

2 Answers2

1

You forgot to instanciate the driver. Try the following:

public class Logout {

static WebDriver driver;

@Test
public void testLogout(){

    driver = new FirefoxDriver();
    driver.get("WEBSITE HERE");

    driver.findElement(By.cssSelector("TEXT HERE")).click();

}
bluelDe
  • 395
  • 8
  • 17
0

You should add an @Before method to instantiate the driver. A method annotated with @Before will run before every test

@Before
public void setup(){
    driver = new FirefoxDriver();
}
Chris
  • 839
  • 2
  • 10
  • 33