I am getting NullPointerException on executing my tests through testNg.xml, Any help?

70 views
Skip to first unread message

soumyaranjan pradhan

unread,
Sep 8, 2016, 2:39:00 AM9/8/16
to Selenium Users
java.lang.NullPointerException
at com.xxxxx.pagetests.LoginPageTests.testInvalidLogin(LoginPageTests.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:646)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:823)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1131)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:778)
at org.testng.TestRunner.run(TestRunner.java:632)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1225)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1150)
at org.testng.TestNG.runSuites(TestNG.java:1075)
at org.testng.TestNG.run(TestNG.java:1047)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:137)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:58)




My Test Class:
public class LoginPageTests extends BaseSetup{
   
   
    private WebDriver driver;
    private LoginPage loginPage;

   
   
    @BeforeTest
    public void setUp(){
        driver = getDriver();
    }   
   
   
   
   
    @Test
    public void testInvalidLogin(){
        loginPage =new LoginPage(driver);
        loginPage.enterWrongCredentials();
        loginPage.logIn();
        loginPage.verifyingTheLoginError();
    }


My LogIn page:

public class LoginPage implements LoginPage_OR {
   
    protected WebDriver driver;
    //Actions builder = new Actions(driver);

   
    public LoginPage(WebDriver driver) {
        this.driver = driver;
    }
   
    public  void enterWrongCredentials(){
        WebElement usernameField= driver.findElement(By.xpath(LoginPage_OR.userNameField));
        usernameField.sendKeys("InvalidUsername");
        driver.findElement(By.xpath(LoginPage_OR.passwordField)).sendKeys("InvalidPassword");
    }
    public void logIn(){
        driver.findElement(By.xpath(LoginPage_OR.loginButton)).click();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
       
    }
   
    public void clickLanguageSelector(){
        driver.findElement(By.xpath(LoginPage_OR.languageLocator)).click();
    }
}


My Base setup:

public class BaseSetup {
   
    private WebDriver driver;
    static String driverPath = "/Users/Soumya/Documents/Automation_Workspace/ChromeDriver/";

    public WebDriver getDriver() {
        return driver;
    }

    private void setDriver(String browserType, String appURL) {
        switch (browserType) {
        case "chrome":
            driver = initChromeDriver(appURL);
            break;
        case "firefox":
            driver = initFirefoxDriver(appURL);
            break;
        default:
            System.out.println("browser : " + browserType
                    + " is invalid, Launching Firefox as browser of choice..");
            driver = initFirefoxDriver(appURL);
        }
    }

    private static WebDriver initChromeDriver(String appURL) {
        System.out.println("Launching google chrome with new profile..");
        System.setProperty("webdriver.chrome.driver", driverPath
                + "chromedriver");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get(appURL);
        return driver;
    }

    private static WebDriver initFirefoxDriver(String appURL) {
        System.out.println("Launching Firefox browser..");
        FirefoxProfile prof = new FirefoxProfile();
        prof.setPreference("browser.startup.homepage_override.mstone", "ignore");
        prof.setPreference("xpinstall.signatures.required", false);
        prof.setPreference("toolkit.telemetry.reportingpolicy.firstRun", false);
        prof.setPreference("browser.startup.homepage", "about:blank");
        prof.setPreference("startup.homepage_welcome_url", "about:blank");
        prof.setPreference("startup.homepage_welcome_url.additional", "about:blank");
   
       
        FirefoxDriver driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get(appURL);
        return driver;
    }

    @Parameters({ "browserType", "appURL" })
    @BeforeClass
    public void initializeTestBaseSetup(String browserType, String appURL) {
        try {
            setDriver(browserType, appURL);

        } catch (Exception e) {
            System.out.println("Error....." + e.getStackTrace());
        }
    }
   
    @AfterClass
    public void tearDown() {
        driver.quit();
    }

}

⇜Krishnan Mahadevan⇝

unread,
Sep 8, 2016, 3:35:29 AM9/8/16
to Selenium Users
Have you checked what is in this line ?

at com.xxxxx.pagetests.LoginPageTests.testInvalidLogin(LoginPageTests.java:31)

Is this the complete stacktrace ?

My guess is that there is a problem with your webdriver instance initialization. You also haven't told us how you are running your tests. If its via a suite file, you would need to show that as well.

You can try changing the annotation @BeforeTest to @BeforeClass in LoginPageTests


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/



--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/5a8f9c78-d34a-44c3-9b29-c5a5da16f684%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

soumyaranjan pradhan

unread,
Sep 8, 2016, 5:01:13 AM9/8/16
to Selenium Users
Thank you for your reply, I deliberately did that(xxxxxxx), because that is my client's name.  And yes I am running through TestNg.xml.

@BeforeTest was @BeforeClass previously. I changed for debugging. My suite file:

<?xml version="1.0" encoding="UTF-8"?>
<suite name="xxxxxxx">
<parameter name="appURL" value="https://xxxxxxx"/>
<parameter name="browserType" value="chrome"/>
  <test name="LoginPageTests" enabled="true">
    <classes>
      <class name="com.xxxxxx.pagetests.LoginPageTests"/>
    </classes>
  </test>
</suite>

⇜Krishnan Mahadevan⇝

unread,
Sep 8, 2016, 6:13:54 AM9/8/16
to Selenium Users

>>>>>> I deliberately did that(xxxxxxx), because that is my client's name.

I was NOT talking about the xxxxxx. I was talking about what is in line 31 of LoginPageTests.java

at com.xxxxx.pagetests.LoginPageTests.testInvalidLogin(LoginPageTests.java:31)

The bug lies in your code.

Make the following changes in your test code and try again. You should be fine.

Change : private WebDriver driver;  in BaseSetup.java to protected WebDriver driver;

Remove : private WebDriver driver; from LoginPageTests.java 
and try again.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.

soumyaranjan pradhan

unread,
Sep 8, 2016, 7:41:58 AM9/8/16
to Selenium Users
Yes yes I debugged it and changed it sometime back. It is working fine. Thank you so much for your help.


On Thursday, 8 September 2016 12:09:00 UTC+5:30, soumyaranjan pradhan wrote:
Reply all
Reply to author
Forward
0 new messages