I've used POM model with 2 pages . LoginPage & HomePage . these 2 pages has 2 test classes (LoginTest & HomeTest) along with BaseTest(Driver initialization purposes & being inherited by all 4 classes) . While clicking loginpage.verifyLogin(email_id , pswrd) , the homepage is supposed to be opened . I'm getting java.lang.ExceptionInInitializerError while executing login testcase.
PS. It is working fine if return type is changed to void for verifyLogin().
Refer the BaseTest.
public class BaseTest
{
public static WebDriver driver ;
public static String chrome_path ="/home/downloads/chromedriver";
public static String property_file_path ="/home/CompleteMotorJourneyAutoTest/src/test/java/com/qa/utils/loginDetails.properties";
public BaseTest()
{
try {
FileInputStream fis = new FileInputStream(property_file_path);
Properties prop = new Properties();
prop.load(fis);
}catch(Exception e) {System.out.println("Exception occured during reading the property file !");}
}
public static void initialization()
{ System.setProperty("webdriver.chrome.driver",chrome_path);
driver=new ChromeDriver();
driver.get(prop.getProperty("URL"));
}
}
LoginPage
public class LoginPage extends BaseTest
{
@FindBy(xpath="//*[@id=\"loginform\"]/div[1]/div[2]/input")
WebElement email_id_field;
@FindBy(xpath="//*[@id=\"loginform\"]/div[2]/div[2]/input")
WebElement password_field;
@FindBy(xpath="//*[@id='loginform']/input")
WebElement login_button_Field;
public LoginPage()
{ PageFactory.initElements(driver, this); }
public HomePage verifyLogin(String username,String password)
{ email_id_field.sendKeys(username);
password_field.sendKeys(password);
login_button_Field.click();
return new HomePage();
}
}
LoginTest
public class LoginTest extends BaseTest
{ LoginPage loginpage;
HomePage homepage;
public LoginTest()
{ super(); }
//Browser initialization/Environment setup <----------------------
@BeforeMethod
public void setup()
{ initialization();
loginpage = new LoginPage(); }
//login with correct credentials <----------------------
@Test(priority=0)
public void loginWithCorrectCredentials()
{ homepage=loginpage.verifyLogin(prop.getProperty("login_id"),prop.getProperty("login_password"));
}
//Browser closure <----------------------
@AfterMethod
public void tearDown()
{ driver.quit(); }
}
I need to test Homepage as well but stuck since getting
[RemoteTestNG] detected TestNG version 7.0.1
Starting ChromeDriver 86.0.4240.22 (398b0743353ff36fb1b82468f63a3a93b4e2e89e-refs/branch-heads/4240@{#378}) on port 8944
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Oct 28, 2020 3:55:51 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
FAILED CONFIGURATION: @BeforeMethod setup
java.lang.ExceptionInInitializerError
at com.qa.testpages.LoginPage.verifyLogin(LoginPage.java:115)
at com.qa.testcase.HomeTest.setup(HomeTest.java:35)
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:566)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:134)
at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:63)
at org.testng.internal.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:348)
at org.testng.internal.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:302)
at org.testng.internal.TestInvoker.runConfigMethods(TestInvoker.java:695)
at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:523)
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:1541)
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)
Caused by: java.lang.NullPointerException
at com.qa.testpages.HomePage.<clinit>(HomePage.java:32)
... 34 more
SKIPPED CONFIGURATION: @AfterMethod tearDown
Can anyone please help . Thanks in advance !