Reputation: 1
I am creating selenium framework using Page Object Model. Currently I have two test cases in separate classes. If, I run the testng.xml file, only the first class gets executed. Null pointer exception occurs when the second class is initiated. When I run the classes separately, both are working fine. But, when I try to run them both only the first class gets executed. Null pointer exception occurs on the second class.
Base class.java
package com.midcities.pages;
import java.io.File;
import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.testng.ITestResult;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeSuite;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.MediaEntityBuilder;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.midcities.utility.BrowserConfig;
import com.midcities.utility.ConfigDataProvider;
import com.midcities.utility.ExcelDataProvider;
import com.midcities.utility.Helper;
public class BaseClass {
public WebDriver driver;
public ExcelDataProvider excel;
public ConfigDataProvider config;
public ExtentReports reports;
public ExtentTest logger;
@BeforeSuite
public void setupSuite() {
//This is base class
excel = new ExcelDataProvider();
config = new ConfigDataProvider();
ExtentHtmlReporter extent = new ExtentHtmlReporter(new File(
System.getProperty("user.dir") + "/Reports/Midcities_" + Helper.getCurrentDateTime() + ".html"));
reports = new ExtentReports();
reports.attachReporter(extent);
}
@BeforeClass
public void startUp() {
**//When the second class is being run, null pointer exception occurs in this line**
driver = BrowserConfig.startApplication(driver, config.getBrowser(), config.getUrl());
}
@AfterClass
public void finish() throws InterruptedException {
Thread.sleep(3000);
BrowserConfig.quitBrowser(driver);
}
@AfterMethod
public void screenshotHandler(ITestResult result) throws IOException {
if (result.getStatus() == ITestResult.FAILURE) {
// Helper.captureScreenshot(driver,result.getName());
logger.fail("Test failed", MediaEntityBuilder
.createScreenCaptureFromPath(Helper.captureScreenshot(driver, result.getName())).build());
} else if (result.getStatus() == ITestResult.SUCCESS) {
logger.pass("Test passed", MediaEntityBuilder
.createScreenCaptureFromPath(Helper.captureScreenshot(driver,result.getName())).build());
}
reports.flush();
}
}
LoginPageElements.java
package com.midcities.pages;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import com.paulhammant.ngwebdriver.ByAngular;
public class LoginPageElements {
WebDriver driver;
public LoginPageElements(WebDriver driver){
this.driver = driver;
}
public void Login_Positive(String uname, String pass) {
driver.findElement(By.id("mat-input-0")).sendKeys(uname);
driver.findElement(By.id("mat-input-1")).sendKeys(pass);
driver.findElement(ByAngular.buttonText("SIGN IN")).click();
}
public void Login_Negative(String uname, String pass) {
String errorMessage = "Invalid Email Id or Password";
driver.findElement(By.id("mat-input-0")).sendKeys(uname);
driver.findElement(By.id("mat-input-1")).sendKeys(pass);
driver.findElement(ByAngular.buttonText("SIGN IN")).click();
String error = driver.findElement(By.cssSelector("small[class*='form-error-msg']")).getText();
Assert.assertEquals(error, errorMessage);
}
}
Login_Positive_Case.java
package com.midcities.testcases;
import org.testng.annotations.Test;
import com.midcities.pages.BaseClass;
import com.midcities.pages.LoginPageElements;
public class Login_Positive_Case extends BaseClass{
@Test
public void loginIntoTheApplication() {
logger = reports.createTest("Login Positive case");
LoginPageElements loginFunc = new LoginPageElements(driver);
logger.info("Starting Application");
loginFunc.Login_Positive(excel.getStringCellData("Login", 1, 0), excel.getStringCellData("Login", 1, 1));
}
}
Login_Negative_Case.java
package com.midcities.testcases;
import org.testng.annotations.Test;
import com.midcities.pages.BaseClass;
import com.midcities.pages.LoginPageElements;
public class Login_Negative_Case extends BaseClass{
@Test
public void loginNegativeInput() {
logger = reports.createTest("Login Negative case");
LoginPageElements loginFunc = new LoginPageElements(driver);
loginFunc.Login_Negative(excel.getStringCellData("Login", 2, 0), excel.getStringCellData("Login", 2, 1));
}
}
BrowserConfig.java
package com.midcities.utility;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import com.paulhammant.ngwebdriver.NgWebDriver;
public class BrowserConfig {
public static WebDriver startApplication(WebDriver driver, String browsername, String AppUrl) {
if(browsername.equalsIgnoreCase("chrome")) {
System.setProperty("webdriver.chrome.driver", "./Drivers/chromedriver.exe");
driver = new ChromeDriver();
} else if(browsername.equalsIgnoreCase("firefox")){
System.setProperty("webdriver.gecko.driver", "./Drivers/geckodriver.exe");
driver = new FirefoxDriver();
} else {
System.out.println("Browser not supported");
}
NgWebDriver ngWebDriver = new NgWebDriver((JavascriptExecutor) driver);
ngWebDriver.waitForAngularRequestsToFinish();
driver.manage().window().maximize();
driver.get(AppUrl);
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
return driver;
}
public static void quitBrowser(WebDriver driver) {
driver.quit();
}
}
ConfigDataProvider.java
package com.midcities.utility;
import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
public class ConfigDataProvider {
Properties pro;
public ConfigDataProvider() {
File src = new File("./Config/config.properties");
try {
FileInputStream fis = new FileInputStream(src);
pro = new Properties();
pro.load(fis);
} catch (Exception e) {
System.out.println("Configuration file not loaded>>" + e.getMessage());
}
}
public String getDataFromConfig(String keyToSearch) {
return pro.getProperty(keyToSearch);
}
public String getBrowser() {
return pro.getProperty("Browser");
}
public String getUrl() {
return pro.getProperty("stagUrl");
}
}
testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test thread-count="5" name="Login_Positive">
<classes>
<class name="com.midcities.testcases.Login_Positive_Case"/>
<class name="com.midcities.testcases.Login_Negative_Case"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
Console
[ERROR] Tests run: 5, Failures: 1, Errors: 0, Skipped: 3, Time elapsed: 27.505 s <<< FAILURE! - in TestSuite
[ERROR] com.midcities.testcases.Login_Negative_Case.startUp Time elapsed: 6.407 s <<< FAILURE!
java.lang.NullPointerException
[INFO]
[INFO] Results:
[INFO]
[ERROR] Failures:
[ERROR] Login_Negative_Case>BaseClass.startUp:52 » NullPointer
[INFO]
[ERROR] Tests run: 5, Failures: 1, Errors: 0, Skipped: 3
Upvotes: 0
Views: 451
Reputation: 1
Try changing BaseClass. Change BeforeClass to BeforeMethod. Merge AfterClass to AfterMethod.
Upvotes: 0
Reputation: 189
You could try specifying the package name rather than the classes which should run both of the tests.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test thread-count="5" name="Login_Positive">
<packages>
<package name="com.midcities.testcases.*"/>
</packages>
</test> <!-- Test -->
</suite> <!-- Suite -->
Upvotes: 1