1

I'm trying to automate the search box but I'm getting null pointer exception.


package LICT.Maven_ReCheck_Search;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ReTest_Search {

    public WebDriver driver;
    
    @BeforeTest
    public void beforeTest() {
        WebDriverManager.chromedriver().setup();
        driver =new ChromeDriver();
        driver.manage().window().maximize();
        
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }
    
    @Test
    public void test() {
        Homepage_ReCheck_Search homepage = new Homepage_ReCheck_Search(driver);
        driver.get("http://automationpractice.com/index.php");
        homepage.input_search_keyword("T Shirt");
        homepage.input_search_submit();
        
    }
    
    @AfterTest
    public void afterTest() {
        driver.close();
    }
    
}

package LICT.Maven_ReCheck_Search;

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

public class Homepage_ReCheck_Search {
    public WebDriver driver;

    Homepage_ReCheck_Search(WebDriver driver){
        this.driver = driver;
        PageFactory.initElements(this.driver, driver);
        
    }
    
    @FindBy(xpath ="//input[@id='search_query_top']" )
    WebElement search_box;
    
    @FindBy(xpath ="//button[@name='submit_search']" )
    WebElement search_submit;
    
    
    public void input_search_keyword(String keyword) {
        search_box.sendKeys(keyword);
        System.out.println("Input is taken");
    }
    public void input_search_submit() {
        search_submit.click();
        System.out.println("Submit is done");
    }
}

[RemoteTestNG] detected TestNG version 7.3.0
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Starting ChromeDriver 84.0.4147.30 (48b3e868b4cc0aa7e8149519690b6f6949e110a8-refs/branch-heads/4147@{#310}) on port 22005
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Aug 19, 2020 4:13:59 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C

**FAILED: test
java.lang.NullPointerException**
    at 

LICT.Maven_ReCheck_Search.Homepage_ReCheck_Search.input_search_keyword(Homepage_ReCheck_Search.java:25)
    at LICT.Maven_ReCheck_Search.ReTest_Search.test(ReTest_Search.java:30)
    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:132)
    at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:599)
    at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:174)
    at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
    at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:822)
    at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:147)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
    at java.util.ArrayList.forEach(Unknown Source)
    at org.testng.TestRunner.privateRun(TestRunner.java:764)
    at org.testng.TestRunner.run(TestRunner.java:585)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:384)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337)
    at org.testng.SuiteRunner.run(SuiteRunner.java:286)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1218)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
    at org.testng.TestNG.runSuites(TestNG.java:1069)
    at org.testng.TestNG.run(TestNG.java:1037)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)


===============================================
    Default test
    Tests run: 1, Failures: 1, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Passes: 0, Failures: 1, Skips: 0
===============================================
0

1 Answer 1

3

The initElements method second argument should be a page class, you feed it the driver. I think you should change the constructor of Homepage_ReCheck_Search to:

PageFactory.initElements(driver, this);

The search_box never gets initialized, because you do not init a page. Thus the search_box stays NULL its default value. Meaning you cannot interact with it.

0

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.