0

I'm having class with setup() and login().

Created another class Edit profile, calling setup and login in @BeforeClass and getting null poiinter exception.

    public class TestLogin {

        public static  WebDriver driver;// = new FirefoxDriver();
        public static String baseURL = Configuration.testsite;




        @BeforeTest
        @Parameters("browser")


            public void setup(String browser) throws Exception{

                //Check if parameter passed from TestNG is 'firefox'

                if(browser.equalsIgnoreCase("firefox")){

                //create firefox instance
                    driver = new FirefoxDriver();
                }

                //Check if parameter passed as 'chrome'
                else if(browser.equalsIgnoreCase("chrome")){
                    //set path to chromedriver.exe You may need to download it from http://code.google.com/p/selenium/wiki/ChromeDriver     
                    System.setProperty("webdriver.chrome.driver","//Users/eugeneshapo/Documents/chromedriver");
                    //create chrome instance
                    driver = new ChromeDriver();                       
                }

            else if(browser.equalsIgnoreCase("safari")){
                    //create chrome instance
                    driver = new SafariDriver();
                }
                else{
                    //If no browser passed throw exception
                    throw new Exception("Browser is not correct");
                }
                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

            }


    //Login test
            @Test
            public void testLogin() throws InterruptedException{
                driver.get(baseURL);
                driver.manage().timeouts().implicitlyWait(20L, TimeUnit.SECONDS);
                analyticsLoginPage mylogin = PageFactory.initElements(driver, analyticsLoginPage.class);
                mylogin.verifylogin("username", "password");
                Thread.sleep(3000);
            }

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

@Test//(dependsOnMethods = { "testLogin" })
public void verifyEditProfile() throws InterruptedException{

*****//instantiated web driver in the line below//

    WebDriver driver = new ChromeDriver();

    analyticsLandingPage landingpage = PageFactory.initElements(driver, analyticsLandingPage.class);
    analyticsEditProfilePage editprofile = PageFactory.initElements(driver, analyticsEditProfilePage.class);

    landingpage.gotoProfile();


    editprofile.verifyEditFirstName();
    editprofile.verifyEditLastName();
    editprofile.verifyCompanyName();
    editprofile.verifyReportingProfile();
    editprofile.verifyUsageStatistics();
}

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

}
}
user16626
  • 25
  • 2
  • 7

1 Answers1

0

The problem is in your code.

The reason why you see a null pointer exception in your Login() method [@BeforeClass annotated] is because you are calling

login.setup("chrome");

but you dont seem to have created an instance for login i.e.,

TestLogin login = new TestLogin();

is missing in your code.

Once you fix the problem you are most likely to see another NullPointerException in your EditProfile#verifyEditProfile() [ @Test annotated method ] because of the below line

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

Here you don't seem to have instantiated the driver instance and so it may also cause another NullPointerException

Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66
  • Thanks Krishnan. I'm able to login. But you are right i'm getting another Null pointer. I added WebDriver driver = new ChromeDriver(); but still getting error. Did I do it wrong? – user16626 Feb 27 '16 at 00:24
  • Thanks again Krishnan. If you could help here:http://stackoverflow.com/questions/35678351/created-new-class-edit-profile-login-works-fine-but-getting-null-pointer-on-ve – user16626 Feb 28 '16 at 03:29