-2

Here is my code:

General.Java:

package com.hrms.lib;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

public class General extends Global
{
//Open Application
    @Test
    public void openApplication()
    {
        driver=new FirefoxDriver();
        driver.navigate().to(url);
    }
//Login
    @Test
    public void login() throws Exception
    {
     driver.findElement(By.name(txt_loginname)).sendKeys(un);
     driver.findElement(By.name(txt_password)).sendKeys(pwd);
     driver.findElement(By.name(btn_login)).click();
     Thread.sleep(2000);
    }

    @Test
    public void mouseover() throws Exception
    {
        elm=driver.findElement(By.linkText(NavigatePim));
        act=new Actions(driver);
        act.moveToElement(elm).build().perform();
        Thread.sleep(1000);
    }
    @Test
    public void mouseoverClick()
    {
        driver.findElement(By.linkText(Add_Emp)).click();
    }
//Enter Values in Textbox
    @Test
    public void save()
    {
        driver.findElement(By.name(txt_FName)).sendKeys(FName);
        driver.findElement(By.name(txt_LName)).sendKeys(LName);
        driver.findElement(By.linkText(btn_Save)).click();
    }
//Logout
    @Test
    public void logout()
    {

    driver.findElement(By.linkText(link_logout)).click();
    }
//Close Application
    @Test
    public void closeApplication()
    {
        driver.quit();
    }
}

Global.Java:

 package com.hrms.lib;
 import org.openqa.selenium.WebDriver;
 import org.openqa.selenium.WebElement;
 import org.openqa.selenium.interactions.Actions;
 public class Global 
{
    //Variables
    public WebDriver driver;
    public String url="http://opensource.demo.orangehrmlive.com/";
    public String un="admin";
    public String pwd="admin";
    public WebElement elm;
    public Actions act;
    public String FName="abc";
    public String LName="efg";

    //Objects
    public String txt_loginname="txtUsername";
    public String txt_password="txtPassword";
    public String btn_login="Submit";
    public String link_logout="Logout";
    public String NavigatePim="PIM";
    public String Add_Emp="Add Employee";
    public String txt_FName="firstName";
    public String txt_LName="lastName";
    public String btn_Save="Save";
}

TC_101:

package com.hrms.TestScripts;
import com.hrms.lib.*;
import org.testng.annotations.Test;

public class TC_Login_Logout 
{
    @Test
    public void tc() throws Exception
    {
   System.setProperty("webdriver.gecko.driver", "C:\\Users\\Madhu\\Downloads\\geckodriver.exe");

       General Gl=new General();
       Gl.openApplication();
       Gl.login();
       Gl.mouseover();
       Gl.mouseoverClick();
       Gl.save();
       Gl.logout();
       Gl.closeApplication();
    }
}

I am getting Null pointer exception error while executing this test case. Could anyone explain why I am getting this error?

closed as unclear what you're asking by c32hedge, alecxe Sep 26 '17 at 0:52

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    Welcome to SQA. Over here you are expected to solve the issue yourself still if you are not able to get the solution. Tells us what you have tried from your end. Describe you issue in detail. At which line are you getting the error. paste the stacktrace. – Alok Sep 10 '17 at 10:38
  • 2
    Actually the error message itself is more helpful than the code. As it always shows the linenumber that caused the exception. This way you should know where to look. Also try methods described here to resolve it: stackoverflow.com/questions/17871825/… – Niels van Reijmersdal Sep 11 '17 at 8:50
  • The error message would be very helpful. But I think all you need to do is get rid of the @Test annotation for all of your methods in the General class, to sum up my answer. Your driver would be null for all of those @Tests – mrfreester Sep 11 '17 at 16:41
1

There are a number of issues, I'll try to explain the overall issues that will help point you in the right direction.

  1. @Test represents an entire test to be run, but it appears you're using it as a test step, or a group of steps. This is your main issue.

******EDIT*****

I didn't look closely at your TC_101 class, it looks like this is your actual test. All of your methods in your General class should not have the @Test attribute. Just remove @Test from those methods and try again.

******End EDIT*****

  1. You need to use @BeforeTest and @AfterTest for things you want to happen before and after each @Test. For example, driver=new FirefoxDriver(); would be in @BeforeTest and driver.quit(); would be in @AfterTest

Currently as it is written, the only tests you have that don't start with the driver being NULL are openApplication and tc. The rest of your tests will fail right off of the bat with a NullPointer. The way the Testng test runner works, these objects don't persist between tests, and they also don't necessarily run in any particular order. This means your openApplication or your tc @Test will probably not run first.

Long story short... Make sure if you use the @Test annotation, that method has a path to run the entire test, from starting the driver, opening the startpage, to quitting the driver at the end. It should completely encapsulate an entire test and not just a few steps. @BeforeTest and @AfterTest are methods that will be included as part of that.

0

I think The following code that you have mentioned in the TC_101 should be placed in the General Class in the openApplication() method.

System.setProperty("webdriver.gecko.driver", "C:\\Users\\Madhu\\Downloads\\geckodriver.exe");

Not the answer you're looking for? Browse other questions tagged or ask your own question.