0

I am facing an issue in POM selenium webdriver. This is my code for one class.

package Pages;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class KnowledgeBase {
private static WebDriver driver = null;

public void opening_correlation_explorer()
{
    driver.findElement(By.xpath("//*[@id='warren-nav']/div[2]/ul/li[3]/a")).click();
    driver.findElement(By.xpath("//*[@id='warren-nav']/div[2]/ul/li[3]/ul/li[1]/a")).click();

}

This is my code for another class:

package Pages;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class LoginPage {
    private static WebDriver driver = null;

    public void login()
    {
        System.setProperty("webdriver.chrome.driver", "chromedriver.exe");

        ChromeOptions options = new ChromeOptions();
        options.addArguments("--start-maximized");
        driver = new ChromeDriver(options);
        driver.get("https://www.website.com");

        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        WebElement loginButton = driver.findElement(By.xpath("/html/body/nav/div/a[7]"));
        loginButton.click();

        WebElement Email = driver.findElement(By.xpath("//*[@id='loginform']/div[2]/div[1]/div[1]/input[1]"));
        Email.sendKeys("abc@gmail.com");

        WebElement Password = driver.findElement(By.xpath("//*[@id='loginform']/div[2]/div[1]/div[1]/input[2]"));
        Password.sendKeys("123456");

        WebElement signin_button = driver.findElement(By.xpath("//*[@id='login']"));
        signin_button.click();
    }

}

And I am running a test case where user logs in (from class LoginPage) and then redirects to a new page (from class KnowledgeBase). Test case class is as follows:

package TestCases;

import Pages.KnowledgeBase;
import Pages.LoginPage;

public class correlation_explorer {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        LoginPage user_login = new LoginPage();
        KnowledgeBase correlation = new KnowledgeBase();

        user_login.login();
        correlation.opening_correlation_explorer();

    }

}

In this test case class, I made 2 objects of both classes from which I had to call methods, but when I run this test, only LoginPage class test is run. After that it gives me the following exception.

Exception in thread "main" java.lang.NullPointerException
    at Pages.KnowledgeBase.opening_correlation_explorer(KnowledgeBase.java:20)
    at TestCases.correlation_explorer.main(correlation_explorer.java:18)

Any help will be highly appreciated.

  • Could you please tell me which line is line 20 on KnowledgeBase.java? There is some variable that is either not being initialized or when you create it with "new Something();" you're getting a null value. For the explanation of NullPointerException, please look here: http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it – George Sep 01 '15 at 16:33
  • @GeorgeRappel the first command of opening_correlation_explorer() method is the line no 20 –  Sep 02 '15 at 06:44

1 Answers1

1

I didn't notice any initialization for your driver in

public class KnowledgeBase {
private static WebDriver driver = null;

So the NullPointer means your driver in KnowledgeBase is null. I suggest you

  1. Don't initialize WebDriver in a single method. It's unreasonable.
  2. For your process, return a KnowledgeBase type in you LoginPage.login.

Code like following

public static void main(String[] args) {
    Webdriver driver = new ChromDriver();
    //Here I suggest you pass driver to every page Object as their Constructor parameter.
    LoginPage user_login = new LoginPage(driver);

    //in login() method you must return a new KnowledgeBase(driver).
    KnowledgeBase correlation = user_login.login();

    correlation.opening_correlation_explorer();

}
J.Lyu
  • 932
  • 7
  • 16