-1

[RemoteTestNG] detected TestNG version 6.14.3 1553664942228 mozrunner::runner INFO Running command: "C:\Program Files\Mozilla Firefox\firefox.exe" "-marionette" "-foreground" "-no-remote" "-profile" "C:\Users\Admin\AppData\Local\Temp\rust_mozprofile.itROC1fctTdz" 1553664942542 addons.webextension.screenshots@mozilla.org WARN Loading extension 'screenshots@mozilla.org': Reading manifest: Invalid extension permission: mozillaAddons 1553664942542 addons.webextension.screenshots@mozilla.org WARN Loading extension 'screenshots@mozilla.org': Reading manifest: Invalid extension permission: resource://pdf.js/ 1553664942542 addons.webextension.screenshots@mozilla.org WARN Loading extension 'screenshots@mozilla.org': Reading manifest: Invalid extension permission: about:reader* 1553664943661 Marionette INFO Listening on port 53094 1553664943965 Marionette WARN TLS certificate errors will be ignored for this session Mar 27, 2019 11:05:44 AM org.openqa.selenium.remote.ProtocolHandshake createSession INFO: Detected dialect: W3C CTS Logged in SELECT REPORTS Date entered successfully Successfully Execute the Reports Back to the Reports FAILED: Reports java.lang.NullPointerException at ctsreports.Timesheetbyday.dailytimesheet(Timesheetbyday.java:15) at ctsreports.Initial.Reports(Initial.java:31) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)


First Class file

package ctsreports;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class Timesheetbyweek {

    WebDriver driver;


    @Test (priority=1)
    public void Logincts() throws InterruptedException {

        //DRIVER INITIALIZATION
        System.setProperty("webdriver.gecko.driver", "E://Automation Backup file//Selenium//Firefox//geckodriver.exe");
        driver = new FirefoxDriver();

        // OPENED THE REQUIRED URL  
        driver.get("http://localhost:5740/#/Login/");
        driver.manage().window().maximize();

        // LOGIN WITH VALID CREDENTIALS
        driver.findElement(By.id("emailBox")).sendKeys("sathish@cybertree.com.au");
        driver.findElement(By.id("loginStaff")).click();
        driver.findElement(By.id("pwdBox")).sendKeys("Cyber12345");
        driver.findElement(By.id("loginStaff")).click();
        Thread.sleep(10000);
        System.out.println("CTS Logged in");
    }

        @Test (priority=2)
        public void reportsbyweek() throws InterruptedException {


            // LOGIN WITH VALID CREDENTIALS
            driver.findElement(By.xpath("//*[@id=\"navigation\"]/li[12]/a/span")).click();    
            Thread.sleep(1000);
            System.out.println("SELECT REPORTS");
            //driver.findElement(By.cssSelector("div.row:nth-child(2) > div:nth-child(1) > div:nth-child(2) > label:nth-child(1) > a:nth-child(2) > span:nth-child(1)")).click();
            driver.findElement(By.xpath("/html/body/div[3]/ng-view/div/div[2]/form/div[2]/div[1]/div[2]/label/a/span")).click();
            Thread.sleep(1000);

              driver.findElement(By.id("searchPopup")).clear();
              driver.findElement(By.id("searchPopup")).sendKeys("25-03-2019");  

              driver.findElement(By.id("searchPopup1")).clear();
              driver.findElement(By.id("searchPopup1")).sendKeys("31-03-2019");

              System.out.println("Date entered successfully");

              driver.findElement(By.xpath(" //*[@id=\"filterForm\"]/div[4]/button")).click();
              System.out.println("Successfully Execute the Reports");
              Thread.sleep(10000);




        }

    /*
     * @Test (priority=3) public void export() throws InterruptedException {
     * 
     * driver.findElement(By.xpath("//*[@id=\"filterForm\"]/div[5]/button[1]")).
     * click(); Thread.sleep(5000);
     * driver.findElement(By.xpath("//*[@id=\"filterForm\"]/div[5]/button[2]")).
     * click();
     * 
     * System.out.println("Export timesheet by week");
     * 
     * 
     * 
     * }
     */



}

Second Class file

package ctsreports;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;

public class Timesheetbyday {

    WebDriver driver;

    @Test (priority=4)
    public void dailytimesheet() throws InterruptedException {
            Thread.sleep(10000);

            System.out.println("Back to the Reports");
            driver.findElement(By.xpath("//*[@id=\"navigation\"]/li[12]/a/span")).click();

            System.out.println("Choose Timesheetbyday");
            driver.findElement(By.xpath("/html/body/div[3]/ng-view/div/div[2]/form/div[2]/div[1]/div[3]/label/a/span")).click();   
            Thread.sleep(1000);

              driver.findElement(By.id("searchPopup")).clear();
              driver.findElement(By.id("searchPopup")).sendKeys("25-03-2019");  
              System.out.println("First date");
              driver.findElement(By.id("searchPopup1")).clear();
              driver.findElement(By.id("searchPopup1")).sendKeys("31-03-2019");
              System.out.println("Second date");

              driver.findElement(By.xpath(" //*[@id=\"filterForm\"]/div[4]/button")).click();
              System.out.println("Successfully Execute the Reports");                     

        }

    /*
     * @Test (priority=5) public void exportdailytimesheet() throws
     * InterruptedException {
     * 
     * driver.findElement(By.xpath("//*[@id=\"filterForm\"]/div[5]/button[1]")).
     * click(); Thread.sleep(5000);
     * driver.findElement(By.xpath("//*[@id=\"filterForm\"]/div[5]/button[2]")).
     * click();
     * 
     * 
     * 
     * }
     */
}

Main File

package ctsreports;
import ctsreports.Timesheetbyweek;
import ctsreports.Timesheetbyday;

import org.testng.annotations.Test;

public class Initial 
    {

    Timesheetbyweek timesheetweek = new Timesheetbyweek();
    Timesheetbyday timesheet = new Timesheetbyday();

    public static void mail (String args[]) throws InterruptedException
    {
        Timesheetbyweek timesheetweek = new Timesheetbyweek();
        Timesheetbyday timesheet = new Timesheetbyday();
        timesheetweek.Logincts();
        timesheetweek.reportsbyweek();
    //  timesheetweek.export();
        timesheet.dailytimesheet();
        //timesheet.exportdailytimesheet();
    }




  @Test
  public void Reports() throws InterruptedException {

      timesheetweek.Logincts();
      timesheetweek.reportsbyweek();
     // timesheetweek.export();
      timesheet.dailytimesheet();
    //  timesheet.exportdailytimesheet();

      System.out.println("Reports Executed successfully");
  }





}
  • Here i have mentioned the error clearly in below ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [RemoteTestNG] detected TestNG version 6.14.3 CTS Logged in SELECT REPORTS Date entered successfully Successfully Execute the Reports Back to the Reports FAILED: Reports java.lang.NullPointerException at ctsreports.Timesheetbyday.dailytimesheet(Timesheetbyday.java:15) at ctsreports.Initial.Reports(Initial.java:31) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) – Sathish Mar 27 at 5:56
0

You are using Timesheetbyday which is in a different class, but I can't see that you are passing in the active Driver instance when calling the method.

i.e Timesheetbyweek works as it initialises the Driver instance then uses it, but Timesheetbyday doesn't get passed the driver instance and instead will use the driver for it's own class definition (which is NULL). I'd also expect to see the Timesheetbyweek tests fail if you change the order so that Logincts() isn't first (as then the browser instance wouldn't exist for the other tests until that one runs).

Also try not to use Thread.sleep(1000). It will seriously slow down the test running. Instead have a look at implicit / explicit waits e.g.

wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOf(webElement));
wait.until(ExpectedConditions.elementToBeClickable(webElement));
0

Your exception is saying that you have NPE in Timesheetbyday on line 15. Line 15 is stating:

driver.findElement(By.xpath("//*[@id=\"navigation\"]/li[12]/a/span")).click();

which is basically the very first call to your driver object.

So, when your code in test method is attempting to access driver object it only knows about the driver that is declared as a field of your Timesheetbydayclass. However you only declare that field. It is empty since you have not neither instantiated it nor assigned any existing object to it. This is why you get NPE there.

Solution: consider the architecture that would pass your webdriver to all the places it is used at. As one of the ways you can introduce @Before method in a Base class and make your test classes extending that Base class. In that "architecture" you would have webdriver field in Base class that would have either protected or public access modifier.

0

Wrong way of creating the driver instance.

As you have created the instance of driver inside the first @Test so this will only work for your Test(priority=1) not for other test, so for this condition you have to create a different Base class where you will create the instance of driver and then by extending your Base class to other test classes you can use this.
Like this:-

public class BaseTest {
    public static WebDriver driver;
@BeforeTest
  public void launchApplication() 
  {       
      driver= new FirefoxDriver();
      driver.get(baseURL);
      driver.manage().window().maximize();

  }
 @AfterTest
  public void closeApplication()
  {

      driver.quit();
  }
}  

Now extends this in your test classes like this:-

    public class Timesheetbyweek extends BaseTest {
      @Test(priority=1)
    public void Logincts() throws InterruptedException {
    //Your steps.
     }
      @Test (priority=2)
     public void reportsbyweek() throws InterruptedException {
//your steps.
     }
    } 

Hope this will work for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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