0

I am new to selenium. Please help me to solve this error.

I am using page object model.

Below is the page file,

package com.nielsen.naukri.pages.search;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.nielsen.jomapp.pages.home.WelcomePage;
import com.nielsen.jomapp.pages.prd.OneDemTerrainPage;
import com.nielsen.jomapp.pages.proxy.PageObject;
import com.nielsen.jomapp.pages.proxy.PageProvider;

public class Search_naukri_page implements PageObject<Search_naukri_page>{

    private static final Logger LOG = (Logger) LoggerFactory.getLogger(OneDemTerrainPage.class);
    public  PageProvider driver;
    private WelcomePage welcomepage;
    WebDriverWait wait;


    @FindBy(xpath = "//*[@id=\"qsb-keyskill-sugg\"]")
    public  WebElement keySkillfield;

    @FindBy(xpath = "//*[@class=\"mb10\"]/a[1]")
    private WebElement updateprofilebutton;

    @FindBy(xpath = "//*[@id=\"search-jobs\"]/button")
    private WebElement searchbutton;

    public Search_naukri_page(PageProvider driver2) {
        this.driver = driver2;
    }


    @Override
    public Search_naukri_page navigateTo() {
        // TODO Auto-generated method stub
        welcomepage = new WelcomePage(driver).navigateTo();
        return this;
    }

    public void enterkeyskill(String keyskill) {
        System.out.println("In page file keyskill loaded is :" +keyskill);
        //driver.waitForPageFullyLoad();
        PageProvider.implicitlyWaitSeconds(this.driver.getDriver(), 25);
        System.out.println("In page file keyskill loaded is :" +keyskill);
            this.keySkillfield.click();//getting error in this line
            this.keySkillfield.sendKeys(keyskill);
            LOG.debug("Keyskill entered is " +keyskill);
        }

public void updateprofile() {

    LOG.debug("Now in update profile method");
    driver.waitForPageFullyLoad();
        updateprofilebutton.click();
        LOG.debug("Update profile button clicked");
    }

    public void clickSearch() {


        this.searchbutton.click();
        LOG.debug("Search button clicked");
    }




    }

Below is the test file,

package com.nielsen.naukri.tests.search;

import java.sql.SQLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import com.nielsen.jomapp.pages.home.WelcomePage;
import com.nielsen.jomapp.pages.login.LoginPage;
import com.nielsen.jomapp.pages.prd.OneDemTerrainPage;
import com.nielsen.jomapp.pages.proxy.PageProvider;
import com.nielsen.naukri.pages.search.Search_naukri_page;
import com.nielsen.tests.Production.One_Dem_Terrain;
import com.nielsen.tests.properties.ChosenWebDriver;

public class Search_naukri_test {


    private static final Logger LOG = LoggerFactory.getLogger(One_Dem_Terrain.class);

    private Search_naukri_page search_naukri_page;

    public PageProvider driver;


    @BeforeMethod
    @Parameters({ "browser" })
    public void beforeTest(ChosenWebDriver driverName) {
        this.driver = new PageProvider(driverName.getDriver());
        PageProvider.implicitlyWaitSeconds(this.driver.getDriver(), 5);
        search_naukri_page = new Search_naukri_page(this.driver).navigateTo();
    }


    @AfterMethod
    public void afterTest() {
        this.driver.close();
    }


    @Test(dataProvider = "ParametersProvider", testName = "Search jobs", priority = 1)
    public void SearchJobs(String keyskill)  {



        LOG.debug("Now in @test method");

        PageProvider.implicitlyWaitSeconds(this.driver.getDriver(),25);
        System.out.println("printed" +keyskill);
        search_naukri_page.enterkeyskill(keyskill);
        search_naukri_page.clickSearch();


    }

    @DataProvider(name = "ParametersProvider")
    public static String[][] getDataFromDataProvider() {
        String[][] testData = {
                {"Python"}

                };
        return testData;
    }

}

Below is the error,

FAILED: SearchJobs("Python")
java.lang.NullPointerException
    at com.nielsen.naukri.pages.search.Search_naukri_page.enterkeyskill(Search_naukri_page.java:48)
    at com.nielsen.naukri.tests.search.Search_naukri_test.SearchJobs(Search_naukri_test.java:55)
    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:86)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:643)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
    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:782)
    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:1244)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
    at org.testng.TestNG.run(TestNG.java:1064)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
1

You didn't use PageFactory to instantiate fields of your Search_naukri_page. So they are null and make your code throwing NPE.

Try to add PageFactory.initElements(driver, this) as the last line of you constructor like shown below:

public Search_naukri_page(PageProvider driver2) {
    this.driver = driver2;
    PageFactory.initElements(driver, this)
}
  • @MohamedAli You can mark my answer as correct – Alexey R. Dec 19 '18 at 10:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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