I have a base class as follows where I load the property file and initialize browser
package com.mystore.base;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
//import org.apache.log4j.xml.DOMConfigurator;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.BeforeTest;
import io.github.bonigarcia.wdm.WebDriverManager;
import com.mystore.actiondriver.ActionDriver;
public class BaseClass1 {
public static Properties prop;
public static WebDriver driver;
//ActionDriver a = new ActionDriver();
@BeforeTest
public void loadConfig() {
try {
prop = new Properties();
System.out.println("super constructor invoked");
FileInputStream ip = new FileInputStream(
System.getProperty("user.dir") + "/Configuration/Config.properties");
prop.load(ip);
System.out.println("driver: "+ driver);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void launchApp() {
String browserName = prop.getProperty("browser");
if (browserName.equalsIgnoreCase("Chrome")) {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
System.out.println("driver: "+ driver);
} else if (browserName.equalsIgnoreCase("FireFox")) {
WebDriverManager.firefoxdriver().setup();
driver = new FirefoxDriver();
} else if (browserName.equalsIgnoreCase("IE")) {
WebDriverManager.iedriver().setup();
driver = new InternetExplorerDriver();
}
ActionDriver.implicitWait(driver, 10);
ActionDriver.pageLoadTimeOut(driver, 30);
driver.get(prop.getProperty("url"));
}
}
Index page class which inherits BaseClass and code below
package com.mystore.pageobjects;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import com.mystore.actiondriver.ActionDriver;
import com.mystore.base.BaseClass;
import com.mystore.base.BaseClass1;
public class IndexPage1 extends BaseClass1{
@FindBy(xpath = "//a[@class='login']")
WebElement signInBtn;
@FindBy(xpath = "//img[@class='logo img-responsive']")
WebElement myStoreLogo;
@FindBy(id="search_query_top")
WebElement searchProductBox;
@FindBy(name="submit_search")
WebElement searchButton;
public IndexPage1() {
PageFactory.initElements(driver, this);
}
public LoginPage clickOnSignIn() {
ActionDriver.click(driver,signInBtn);
return new LoginPage();
}
public boolean validateLogo() throws Throwable{
System.out.println("Inside Validate Logo");
return myStoreLogo.isDisplayed();
//return true;
//return ActionDriver.isDisplayed(driver, myStoreLogo);
}
public String getMyStoreTitle() {
String myStoreTitle = driver.getTitle();
return myStoreTitle;
}
public SearchResultPage searchProduct(String productName) throws InterruptedException {
Thread.sleep(10000);
searchProductBox.sendKeys(productName);
Thread.sleep(10000);
System.out.println("I passed!");
ActionDriver.type(searchProductBox, productName);
ActionDriver.implicitWait(driver, 60);
ActionDriver.click(driver, searchButton);
return new SearchResultPage();
}
}
IndexPageTest class with test - where I try to call verifyLogo()
test which checks if webElement myStoreLogo is displayed and should return true but returns false and NPE .
package com.mystore.testcases;
import org.openqa.selenium.WebDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import com.mystore.base.BaseClass;
import com.mystore.base.BaseClass1;
import com.mystore.pageobjects.IndexPage;
import com.mystore.pageobjects.IndexPage1;
public class IndexPageTest1 extends BaseClass1{
IndexPage1 indexPage1 = new IndexPage1();
@BeforeMethod
public void setup() {
launchApp();
}
//@AfterMethod
public void tearDown() {
driver.quit();
}
@Test()
public void verifyLogo() throws Throwable{
//indexPage1= new IndexPage();
boolean result=indexPage1.validateLogo();
System.out.println("The value of result is:"+ result);
Assert.assertTrue(result);
}
@Test(enabled = false)
public void verifySearchProduct() throws InterruptedException {
indexPage1.searchProduct("t-shirt");
}
@Test(enabled=false)
public void verifyTitle() {
String actTitle=indexPage1.getMyStoreTitle();
Assert.assertEquals(actTitle, "My Store");
}
}
Gives results below
driver: ChromeDriver: chrome on MAC (fa4c8f6fb20e625d6d7e2b148850b17b)
Inside Validate Logo
FAILED: verifyLogo
java.lang.NullPointerException
at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
at com.sun.proxy.$Proxy12.isDisplayed(Unknown Source)
at com.mystore.pageobjects.IndexPage1.validateLogo(IndexPage1.java:47)
at com.mystore.testcases.IndexPageTest1.verifyLogo(IndexPageTest1.java:40)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:134)
at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:597)
at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:173)
at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:816)
at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1540)
at org.testng.TestRunner.privateRun(TestRunner.java:766)
at org.testng.TestRunner.run(TestRunner.java:587)
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:1187)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1109)
at org.testng.TestNG.runSuites(TestNG.java:1039)
at org.testng.TestNG.run(TestNG.java:1007)
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
===============================================
Please help, I'm not sure why the webelement is not identified. I tried with other web elements and they also get the same NPE.