test base

public class TestBase {

    public WebDriver driver;
    public static Properties prop;
    public WebEventListener Wl;
    public EventFiringWebDriver Ef;

    public void OpenBrowser() throws IOException {

        prop = new Properties(); // properties class to allow the access to the config file

        FileInputStream fis = new FileInputStream(
                "path");

        prop.load(fis);

        String BrowserName = prop.getProperty("browser");
        // this returns a string from the config file which we put into a string object
        // named BrowserName

        if (BrowserName.equals("chrome"))
        // if the browserName is equal to chrome then this gets executed
        {
            System.setProperty("webdriver.chrome.driver", "C:\\Users\\Essa\\Documents\\Automation\\chromedriver.exe");

            driver = new ChromeDriver();
            driver.get(prop.getProperty("url"));

        } else if (BrowserName.equals("FireFox")) {
            /*
             * System.setProperty("webdriver.gecko.driver", "PathLocation");
             * 
             * driver = new FirefoxDriver(); driver.get(prop.getProperty("url"));
             */
        } else if (BrowserName.equalsIgnoreCase("InternetExplorer")) {
            /*
             * System.setProperty("webdriver.ie.driver", "PathLocation");
             * 
             * driver = new InternetExplorerDriver(); driver.get(prop.getProperty("url"));
             */
        }

        driver.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS);
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

        /*
         * Wl = new WebEventListener(); // this is the class that contains all the
         * implemented methods Ef = new EventFiringWebDriver(driver); //this is the
         * class that allows us to connect the webevent to the listener Ef.register(Wl);
         * // this is the connection
         * 
         * driver = Ef; // here we are giving the knowledge of the event listener to the
         * driver
         * 
         * driver.manage().window().maximize(); //this maximizes the browser
         * driver.manage().deleteAllCookies(); // this deletes all the cookies on a web
         * page
         * 
         */
    }
}

home page class

package Pages;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

import base.TestBase;

public class HomePage extends TestBase {

    public HomePage() {
        PageFactory.initElements(driver, this);
    }

    public @FindBy(xpath = "//input[@id='keyword']") 
    WebElement SearchField;

    public @FindBy(xpath = "(//img[@class='buttons_images'])[1]")
    WebElement SearchButton;

    public void searchInfo(String info) {
        System.out.println("hi2");
        SearchField.sendKeys(info);
    }

    public String verifyTitle() {
        return driver.getTitle();
    }

    public void HoverOver(WebElement ele) {
        Actions a = new Actions(driver);
        a.moveToElement(ele).build().perform();
        // in order to work with the actions class you have to build and perform that
        // action
    }
}

test class

package Tests;

import Pages.HomePage;
import Pages.SearchResultPage;
import base.TestBase;

public class WorkFlowTest extends TestBase {

    HomePage hp;
    SearchResultPage srp;

    @BeforeTest
    public void setUp() {
        try {
            OpenBrowser();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        hp = new HomePage();
        srp = new SearchResultPage();

    }

    @Test
    public void ToSearchResults() {

        System.out.println("hi");
        hp.SearchField.sendKeys(prop.getProperty("topic"));
        // hp.SearchButton.click();
        srp.selectTopic(prop.getProperty("topic"));

    }
}

The issue is that I am getting a null pointer exception that leads back to the homepage class but I have done this many times the same way and I never had a problem, I was hoping one of you guys could help me.

New contributor
Ali Ahmad is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • Can you share your exception stacktrace? – Alexey R. yesterday
  • Part of your problem might be that you're declaring your page object elements by declaring the @FindBy as Public instead of the webelement itself. I'm surprised it compiles, but maybe it ignores the metatag and skips to the webelement. Also, when you extend a class, you don't also need to import it. That's not your error (I don't think) but just a tip. – Bill Hileman yesterday
  • On second thought, the fact that you import it in all your other classes might be the problem - one copy of the driver is getting initialized, but not in the one that is included in the page object. It's a better design to pass the driver object to your page object and keep a local copy of the driver. – Bill Hileman yesterday

Your Answer

Ali Ahmad is a new contributor. Be nice, and check out our Code of Conduct.

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.