Reputation: 13
I have created Page Object Model for the selemium testcases. I have defined a login method in 'LoginPage' class and some methods in 'CommonMethods' class. Now when I run the test, it shows NullPointerException error. I did the same thing with JUnit and it worked fine with this setup but not working with TestNG.
This is my Page Object file:
package pages;
import org.openqa.selenium.WebDriver;
public class PageObject {
public WebDriver driver;
public PageObject(WebDriver driver) {
this.driver=driver;
}
}
CommonMethods file
package pages;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class CommonMethods extends PageObject {
public CommonMethods(WebDriver driver) {
super(driver);
}
public By ByLocator(String locator) {
By result=null;
if(locator.startsWith("/")||locator.startsWith("//"))
result=By.xpath(locator);
else if(locator.startsWith(".")||locator.startsWith("#"))
result=By.cssSelector(locator);
return result;
}
public void waitForElementPresent(By locator, int time) {
WebDriverWait wait = new WebDriverWait(driver, time);
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
}
}
LoginPage file
package pages;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class LoginPage extends PageObject {
public LoginPage(WebDriver driver) {
super(driver);
}
CommonMethods cm = new CommonMethods(driver);
By usernameField = cm.ByLocator("#login");
By passwordField = cm.ByLocator("#password");
By submitButton = cm.ByLocator("#signin_submit");
public void loginWithCustomer() {
driver.get("https://www.stg.keepcollective.com/signin");
//cm.waitForElementPresent(usernameField, 3000);
driver.findElement(usernameField).sendKeys("[email protected]");
driver.findElement(passwordField).sendKeys("111111");
}
}
Test file
package tests;
import org.testng.annotations.Test;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import java.io.*;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.ITestResult;
import org.testng.annotations.AfterClass;
import pages.*;
public class LoginCheck {
public WebDriver driver;
CommonMethods cm=new CommonMethods(driver);
LoginPage lp=new LoginPage(driver);
@BeforeClass
public void beforeClass() throws Exception {
System.setProperty("webdriver.chrome.driver", "/home/himanshu/Downloads/chromedriver_linux64/chromedriver");
driver= new ChromeDriver();
}
@Test
public void checkLoginFunctionality() {
lp.loginWithCustomer();
}
@AfterMethod
public void Screenshot(ITestResult result) throws IOException {
if(result.getStatus()==ITestResult.FAILURE) {
TakesScreenshot ss= (TakesScreenshot)driver;
File screenshotfile = ss.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshotfile, new File("./Screenshots/"+result.getName()+".png"));
}
}
@AfterClass
public void afterClass() {
driver.close();
}
}
I am getting this error:
FAILED: checkLoginFunctionality
java.lang.NullPointerException
at pages.LoginPage.loginWithCustomer(LoginPage.java:19)
at tests.LoginCheck.checkLoginFunctionality(LoginCheck.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
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)
Upvotes: 1
Views: 809
Reputation: 115
Move the initialization of CommonMethods cm
and LoginPage lp
in the beforeClass()
method and put it after the creation of ChromeDriver
. The @BeforeClass doesn't mean that this method will be executed before the initialization of the LoginCheck
class. What it means is that this is method will be executed before any other methods in this class is executed.
public class LoginCheck {
public WebDriver driver;
private CommonMethods cm;
private LoginPage lp;
@BeforeClass
public void beforeClass() throws Exception {
System.setProperty("webdriver.chrome.driver", "/home/himanshu/Downloads/chromedriver_linux64/chromedriver");
driver= new ChromeDriver();
cm = new CommonMethods(driver);
lp = new LoginPage(driver);
}
@Test
public void checkLoginFunctionality() {
lp.loginWithCustomer();
}
@AfterMethod
public void Screenshot(ITestResult result) throws IOException {
if(result.getStatus()==ITestResult.FAILURE) {
TakesScreenshot ss= (TakesScreenshot)driver;
File screenshotfile = ss.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshotfile, new File("./Screenshots/"+result.getName()+".png"));
}
}
@AfterClass
public void afterClass() {
driver.close();
}
}
Upvotes: 1