10

I am a new automation tester, Working on sample test scripts, Need some help from you guys, I have tried to use POM and basic TestNG.

I have created 2 packages - pages and testcases.

I am getting some error when I am trying to access the "ClickJoin, Enterusername" methods from my pages packages. I had tried figuring out the issue, but was unable to get to why this error is occurring.

Below is the Error: java.lang.NullPointerException at com.google.common.base.Preconditions.checkNotNull

My Code from the "pages" package: package com.gptoday.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Login {

public WebDriver driver;
public WebDriverWait wait;
By join = By.xpath("//*[@id='members']/a[1]");
By username = By.id("username");
By password = By.id("password");
By loginButton = By.xpath("//*[@id='normallogin']/div[4]/p/input[2]");

public Login (WebDriver driver){
    this.driver=driver;
}
    public void ClickJoin(){
        wait = new WebDriverWait(driver,10);
        //driver.findElement(join).click();

wait.until(ExpectedConditions.visibilityOfElementLocated(join)).click();
    }

    public void EnterUsername(){
        wait = new WebDriverWait(driver, 10);


wait.until(ExpectedConditions.visibilityOfElementLocated(username)).click();
        wait.until(ExpectedConditions.visibilityOfElementLocated(username)).sendKeys("Test username");
        System.out.println("Username Entered");
    }

    public void EnterPassword(){
        driver.findElement(password).clear();
        driver.findElement(password).click();
        System.out.println("Password Entered");
    }

        public void ClickButton(){
            wait = new WebDriverWait(driver, 10);
            //driver.findElement(loginButton).click();
 wait.until(ExpectedConditions.visibilityOfElementLocated(loginButton)).click();
            System.out.println("Login Button Clicked");
        }
    }

My Code from the "TestCases" package: This code is of my base class.

package com.gptoday.com.gptoday.testcases;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.testng.annotations.AfterMethod;
import org.openqa.selenium.firefox.internal.ProfilesIni;

public class Base {
    public WebDriver driver;

    @BeforeMethod
    public void BaseSetup(){
        ProfilesIni prof = new ProfilesIni();
        FirefoxProfile ffProfile= prof.getProfile ("vishvesh");
        ffProfile.setAcceptUntrustedCertificates(true);
        ffProfile.setAssumeUntrustedCertificateIssuer(false);

        String BaseUrl = "https://www.gptoday.com";
        System.setProperty("webdriver.gecko.driver", "G:/Workplace/AutomationSetupFiles/Geckdriver/geckodriver.exe"); 
        driver = new FirefoxDriver (ffProfile);
        driver.get(BaseUrl);
        driver.manage().window().maximize();
    }

    @AfterMethod
    public void afterTest() {
        System.out.println("Success");
      }
}

**Below the the code for the test case.**
package com.gptoday.com.gptoday.testcases;

import org.testng.annotations.Test;
import com.gptoday.pages.Login;
import org.openqa.selenium.WebDriver;


public class LoginVerification extends Base {

    public WebDriver driver;
    public Login obj = new Login(driver);

    @Test
    public void Verify()
    {
        obj.ClickJoin();
        obj.EnterUsername();
    }
}

**Error:**
Success
FAILED: Verify
java.lang.NullPointerException
    at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:770)
    at org.openqa.selenium.support.ui.FluentWait.<init>(FluentWait.java:96)
    at org.openqa.selenium.support.ui.WebDriverWait.<init>(WebDriverWait.java:71)
    at org.openqa.selenium.support.ui.WebDriverWait.<init>(WebDriverWait.java:45)
    at com.gptoday.pages.Login.ClickJoin(Login.java:22)
    at com.gptoday.com.gptoday.testcases.LoginVerification.Verify(LoginVerification.java:16)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:661)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:744)
    at org.testng.TestRunner.run(TestRunner.java:602)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
    at org.testng.SuiteRunner.run(SuiteRunner.java:289)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
    at org.testng.TestNG.runSuites(TestNG.java:1144)
    at org.testng.TestNG.run(TestNG.java:1115)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
Vishvesh Savant
  • 105
  • 1
  • 2
  • 11
  • `com.google.common.base.Preconditions` contains a set of utilities that allow a library author to quickly check a *precondition* (i.e. an invariant on inputs/state that the user of the library must adhere to). Since you haven't provided a stacktrace, we can't tell you exactly what check was made. Edit your post to include the full stacktrace. – nanofarad Jun 11 '19 at 02:07
  • Please refer this solution. Definitely your error will resolve. https://stackoverflow.com/a/62222352/11441405[solution][1] – NITIN SURANGE Jun 05 '20 at 18:58

2 Answers2

0

Here's the reason behind the error you are getting:

In both Login & EnterUsername methods while you defined the wait you have passed the instance of Webdriver & interval. Now whenever Selenium detects this particular type of argument Selenium treats this as FluentWait. Now when you actually implement a FluentWait you have to import those two libraries "com.google.common.base.Function" which will address the error you are facing. Of course you have to import "FluentWait" as well.

Let me know if this helps you.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • @VishveshSavant Can you update the Question with the code changes you have done to get around the error and any new error appearing? – undetected Selenium Mar 22 '17 at 05:39
  • I did not change anything with the code, I just imported the lib. import org.openqa.selenium.support.ui.FluentWait; import com.google.common.base.Function; But these imports are not used in the code. – Vishvesh Savant Mar 22 '17 at 07:27
  • okay, I might have missed another point. First up add `import com.google.common.base.Preconditions` Some how are you using guava jars? I am not sure about the methods there. – undetected Selenium Mar 22 '17 at 07:56
  • Actually this code was working but once I moved some of the repetitive code into the "Base" class.. it started giving me these errors. Tried importing "preconditions" too, nothing changed. – Vishvesh Savant Mar 22 '17 at 09:20
  • I was able to solve the issue, By modifying the code in the "Login.Java" class.. The issue was related to driver being null as I did not extend "Base" in the "Login" class. Thanks neways – Vishvesh Savant Mar 22 '17 at 11:16
-1

By modifying the code in the "Login.Java" class.. The issue was related to driver being null as I did not extend "Base" in the "Login" class.

Vishvesh Savant
  • 105
  • 1
  • 2
  • 11