3

It runs first test, but then I'm getting null pointer exception when running Edit Profile test.
Not sure why.
I globally declared public driver.
Please help. Thanks

package test;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import com.tapanalytics.config.Configuration;
import com.tapanalytics.pom.domain.analyticsEditProfilePage;
import com.tapanalytics.pom.domain.analyticsLandingPage;
import com.tapanalytics.pom.domain.analyticsLoginPage;

public class TestLogin {

public  WebDriver driver;// = new FirefoxDriver();
public String baseURL = "mytestsite.com";

@BeforeTest
 public void setBaseURL(){
   driver = new FirefoxDriver();
   driver.manage().timeouts().implicitlyWait(20L, TimeUnit.SECONDS);
   driver.get(baseURL);
}

//Login test
@Test
public void testLogin() throws InterruptedException{

    driver.manage().timeouts().implicitlyWait(20L, TimeUnit.SECONDS);

    analyticsLoginPage mylogin = PageFactory.initElements(driver, analyticsLoginPage.class);
    analyticsLandingPage landingpage = mylogin.login("username", "password");
    Thread.sleep(3000);
}

//Edit Profile test

@Test//(dependsOnMethods = { "testLogin" })
public void verifyProfile() throws InterruptedException{
    // driver = new FirefoxDriver();

    Thread.sleep(3000);
    analyticsLandingPage landingpage = new analyticsLandingPage();
    Thread.sleep(3000);
    landingpage.gotoProfile();



    //driver.findElement(By.xpath(".//*[@id='left-frame']/div/div[2]/a[3]/span")).click();
    //Thread.sleep(5000);
    analyticsEditProfilePage editprofile = PageFactory.initElements(driver, analyticsEditProfilePage.class);
    editprofile.verifyEditFirstName();
    editprofile.verifyEditLastName();
    editprofile.verifyCompanyName();
    editprofile.verifyReportingProfile();
    editprofile.verifyUsageStatistics();


    }

@AfterTest
public static void quitDriver(){
    driver.quit();
}       

PASSED: testLogin

FAILED: verifyProfile

> java.lang.NullPointerException    at
> com.tapanalytics.pom.domain.analyticsLandingPage.gotoProfile(analyticsLandingPage.java:42)
>   at test.TestLogin.verifyProfile(TestLogin.java:64)  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:497)     at
> org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
>   at org.testng.internal.Invoker.invokeMethod(Invoker.java:639)   at
> org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816)    at
> org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124)  at
> org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
>   at
> org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
>   at org.testng.TestRunner.privateRun(TestRunner.java:774)    at
> org.testng.TestRunner.run(TestRunner.java:624)    at
> org.testng.SuiteRunner.runTest(SuiteRunner.java:359)  at
> org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)  at
> org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)   at
> org.testng.SuiteRunner.run(SuiteRunner.java:261)  at
> org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)  at
> org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)   at
> org.testng.TestNG.runSuitesSequentially(TestNG.java:1215)     at
> org.testng.TestNG.runSuitesLocally(TestNG.java:1140)  at
> org.testng.TestNG.run(TestNG.java:1048)   at
> org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:112)     at
> org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:205)  at
> org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:176)
1
  • Can you show the code from the goToProfile() method?
    – FDM
    Commented Feb 12, 2016 at 9:07

4 Answers 4

6

There are two issues with your code.

The first:

It seems like you never apply the PageFactory on this class. Therefore, when trying the line profile.click() the variable is null (because never initialized). Hence your exception.

Solution: add the following line like you did for the other classes.

analyticsLandingPage landing = PageFactory.initElements(driver, analyticsLandingPage.class);

The second:

You are currently creating a new object like this:

analyticsLandingPage landingpage = new analyticsLandingPage();

However, you posted your class like this:

public class analyticsLandingPage 
{ 
     WebDriver driver; 

     public analyticsLandingPage(WebDriver driver){ 
          this.driver = driver; 
     } 

     @FindBy(xpath = Configuration.profile) 
     public WebElement profile; 

     public void gotoProfile(){ 
         profile.click();   
     } 
}

Problem: you are calling the default constructor of the class, in which the driver variable does not get initialized.

Solution: make sure you pass the parameter so you call the custom constructor.

analyticsLandingPage landingpage = new analyticsLandingPage(driver);
0
1

Might be your driver is not initialized in landingpage.gotoProfile();

Check in gotoProfile() method if driver is initialized, properly.

Things you can try

  1. Pass the driver to gotoProfile() method, as shown below

landingpage.gotoProfile(driver);

and accepting argument in gotoProfile method as bellow

public void gotoProfile(WebDriver driver) {
// code of go to Profile
}

OR

  1. While creating object of analyticsLandingPage class pass the driver as argument and write a constructor which will initialize the value of driver in analyticsLandingPage class, as shown below.

Creating object

analyticsLandingPage landingpage = new analyticsLandingPage(driver);

Initialize driver value in analyticsLandingPage class

public class analyticsLandingPage {

WebDriver driver;

public analyticsLandingPage(WebDriver driver) {
this.driver = driver;
}
 // Rest of analyticsLandingPage code
}

Edit Profile class

package com.tapanalytics.pom.domain;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

import com.tapanalytics.config.Configuration;

public class analyticsLandingPage {

          WebDriver driver;

       public analyticsLandingPage(WebDriver driver){
        this.driver = driver;
    }

@FindBy(xpath = Configuration.manage_dashboard)
public WebElement manage_dashboard;

@FindBy(xpath = Configuration.manage_services)
public WebElement manage_services;

@FindBy(xpath = Configuration.profile)
public WebElement profile;

@FindBy(xpath = Configuration.support)
public WebElement support;

@FindBy(xpath = Configuration.logout)
public WebElement logout;

public void gotoMangeDashboards(){
    manage_dashboard.click();
}

public void gotoServices(){
    manage_services.click();        
}

public void gotoProfile(){
    profile.click();    
}

public void gotoSupport(){
    support.click();    
}

public void Logout(){
    logout.click(); 
}


}
4
  • Thanks , I tried both options and still getting same error. Here is my goto Profile class.
    – jim
    Commented Feb 12, 2016 at 19:22
  • package com.tapanalytics.pom.domain; public class analyticsLandingPage { WebDriver driver; public analyticsLandingPage(WebDriver driver){ this.driver = driver; } @FindBy(xpath = Configuration.profile) public WebElement profile; public void gotoProfile(){ profile.click(); } }
    – jim
    Commented Feb 12, 2016 at 19:24
  • Here is copy of my testng.xml
    – jim
    Commented Feb 12, 2016 at 19:31
  • <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "testng.org/testng-1.0.dtd"> <suite name="suite" parallel="none"> <test name="Test"> <classes> <class name="test.TestLogin"/> </classes> </test> <!-- Test --> </suite> <!-- Suite -->
    – jim
    Commented Feb 12, 2016 at 19:31
0
public  WebDriver driver;// = new FirefoxDriver();
public String baseURL = "";

@BeforeTest
 public void setBaseURL(){
   driver = new FirefoxDriver();
   driver.manage().timeouts().implicitlyWait(20L, TimeUnit.SECONDS);
   baseURL ="https://www.mytestsite.com";
   driver.get(baseURL);
}
1
  • 1
    Please edit your answer to explain to the OP why your code will help. A code-only answer without any further information is not especially helpful, especially for a question as old as this one.
    – Kate Paulk
    Commented Dec 11, 2018 at 18:12
-1

try using public static

public static webdriver driver in each class files

public static is not needed if you are running a single test case, but since in a suit where parallelization may occur for multiple cases, so public ststic is needed for multiple test cases

1
  • This depends on if you want to use it as a static class or not within the suite. Please improve your answer by explaining why this approach might help the questioner. What are the disadvantages?
    – ECiurleo
    Commented Aug 29, 2017 at 10:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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