0

I am trying to run 2 testcases from same class but test case PatientSearchTest getting passed and second one getting Failed, Its giving NullPointerException. Here is code of BaseClass

public class BaseClassTest {

    ReadConfig readconfig = new ReadConfig();

    public String baseURL = readconfig.getApplicationURL();
    public String username =readconfig.getUsername();
    public String password = readconfig.getPassword();
    public static WebDriver driver;
//  public static Logger Logger;


    @Parameters("browser")
    @BeforeClass
        public void setUp(String br)
    {
        /*System.setProperty("webdriver.chrome.driver",System.getProperty("user.dir")+"\\Drivers\\chromedriver.exe");
        System..getProperty can be used only in java classes & ./--> in java classes & in properties*/
        if(br.equals("chrome"))
        {
        System.setProperty("webdriver.chrome.driver",readconfig.getChromePath());
        driver = new ChromeDriver();
        }
        else if (br.contentEquals("firefox"))
        {
            System.setProperty("webdriver.gecko.driver",readconfig.getFirefoxPath());
            driver = new FirefoxDriver();
        }
        else if(br.contentEquals("ie"))
        {
            System.setProperty("webdriver.ie.driver",readconfig.getIEPath());
            driver = new InternetExplorerDriver();
        }
        driver.get(baseURL);
        driver.manage().window().maximize();
        driver.manage().deleteAllCookies();
        driver.manage().timeouts().pageLoadTimeout(20,TimeUnit.SECONDS);
        driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);

        }

    @AfterClass
    public void tearDown() {


        driver.quit();
    } 

public void login()
    {
         LoginPage lp = new LoginPage(driver);
         lp.setUserName(username);
         lp.setPassword(password);
         lp.clickSubmit();

    }

Here is code of class file

public class CM_TC01_PatientSearchTest extends BaseClassTest {

    CM_PatientSearchPage ps = new  CM_PatientSearchPage(driver);

    @BeforeClass    
    public void loginintoApp()
    {
        login();

    }


    @Test(priority=1)
    public void PatientSearchTest() throws Exception
    {

        ExtentTestManager.getTest().log(Status.INFO,"Hey i am in Patient Search Test"); 

        Thread.sleep(2000);
        ps.hoverTest();
        ps.clickContractOptionTextbox();
        ps.selectContractOption();
        ps.enterPatientName("Peggy");
        Thread.sleep(2500);

    }
    @Test(priority=2)
    public void clearRecordsButton() throws Exception
    {
        CM_PatientSearchPage ps = null;
        ps.clearPatientSearchRecords();
    }


}

Here is code of testng.xml file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="" parallel="false">
    <listeners>
        <listener class-name="com.hospital.extentReport.TestListener"/>
    </listeners>

  <test name="PPP Test">
    <parameter name="browser" value="chrome"/>
    <classes>
      <class name="com.hospitalanalytics.testCases.CM_TC01_PatientSearchTest"/>

    </classes>
  </test> <!-- Test -->/>

</suite> <!--  Suite -->

Anyone can help me out? WIll be a great help to me. Thanks in advance.

Appi
  • 95
  • 1
  • 10

1 Answers1

0

It's giving NullPointerException because you set ps to null and then try to call its method. See here for more details on NPEs.

Your code will work if you change the order:

public class CM_TC01_PatientSearchTest extends BaseClassTest {

    CM_PatientSearchPage ps = null;

    @BeforeClass    
    public void beforeClass()
    {
        ps = new  CM_PatientSearchPage(driver);
        login();
    }


    @Test(priority=1)
    public void PatientSearchTest() throws Exception
    {

        ExtentTestManager.getTest().log(Status.INFO,"Hey i am in Patient Search Test"); 

        Thread.sleep(2000);
        ps.hoverTest();
        ps.clickContractOptionTextbox();
        ps.selectContractOption();
        ps.enterPatientName("Peggy");
        Thread.sleep(2500);

    }
    @Test(priority=2)
    public void clearRecordsButton() throws Exception
    {
        ps.clearPatientSearchRecords();
    }
}

I have moved initialization of ps into the @BeforeClass-annotated method (renaming it from loginintoApp, as now it does more). This way, ps will be available to all test methods in your class.

kenny_k
  • 3,831
  • 5
  • 30
  • 41
  • If i don't set as null its asking to initialize value for 'ps' when i hover it."The local variable 'ps' may have not been initialized. – Appi Oct 03 '19 at 13:38
  • Do you want to use the same `ps` that you've defined at the beginning of the class? What are you trying to achieve with these 2 lines? – kenny_k Oct 03 '19 at 17:41
  • 'ps' is an object of CM_PatientSearchPage, methods are defined in that page without creating an object how do i call methods of pageobject. My simple question is how do i call methods defined in page object for multiple @Test, without creating an object(ps) i can not call method right!. I am declaring globally than also its not working and when i am creating for individual Testcase its not working. – Appi Oct 04 '19 at 14:02
  • I see. I have updated my response - see if that works now. – kenny_k Oct 04 '19 at 16:13
  • "Not working" is not a helpful reply. Not working how? Certainly there is no NPE, so what's the problem? – kenny_k Oct 04 '19 at 21:28
  • Apologize for my previous reply but i did not get your answer ---( I have updated my response - see if that works now). – Appi Oct 07 '19 at 19:20
  • As you can see from my answer, I have removed the first line from `clearRecordsButton()` - it is not necessary. Try to do the same, it should work. – kenny_k Oct 07 '19 at 19:24
  • Thank you so much it runs successfully. Its really great helped to me. I do have one more qsn When we run all the testcases as 'Run as TestNG suite' it should login number of times as many script we are running or should login only once and execute all other scripts.one after another in Hybrid Framework concept.As i am developing a hybrid framework where in application 10 modules are there and each having 5-6 links and opening a new page. At present i am executing each and every scripts by login, execute script and then logout then again login and new script.Pls reply,it going 2 b great help. – Appi Oct 08 '19 at 21:23
  • You're welcome! Just so you know, the preferred way of saying "thanks" around here is **upvoting** any helpful answers, and **accepting** one if it helped solve your problem (which will also grant you a few reputation points). If you have additional questions please create them as new, separate questions. – kenny_k Oct 09 '19 at 07:08
  • I followed for upvoting and accepting as per your suggestion. Thanks i learnt how stack overflow works. I am posting my another question please look into it and please reply it. Thanks. – Appi Oct 09 '19 at 13:40
  • Sure! I would also recommend [**taking the tour**](https://stackoverflow.com/tour) and reading [**how to ask**](https://stackoverflow.com/help/how-to-ask) and [**how to create a minimum, reproducible example**](https://stackoverflow.com/help/minimal-reproducible-example), as following those guidelines will ensure the best results when using the site. Good luck! – kenny_k Oct 09 '19 at 14:09
  • Sure i will go-through it. Thanks. – Appi Oct 09 '19 at 14:44
  • Hi kenny, Above code is working fine as you suggested but when using with @AfterClass annotation method just with driver.quit; statement. Its run with first testcase successfully and skipping the second testcase.Not getting login second time and showing below message: " org.openqa.selenium.NoSuchSessionException: Session ID is null. Using WebDriver after calling quit()? – Appi Oct 16 '19 at 22:14