我有两个类,我想在“ B”类中调用“ A”类的公共void getResult(ITestResult result)方法。我收到NullPointerException
头等舱
public class loginPage{
public WebDriver driver;
Utility ut= new Utility();
ExtentHtmlReporter htmlReporter;
ExtentReports extent;
ExtentTest logger;
By EmailId= By.xpath("//*[@name='Email']");
By password = By.xpath("//*[@name='Password']");
By LoginBtn= By.xpath("//*[@id='frmLogIn']/div[4]/button");
public loginPage(WebDriver driver) {
this.driver=driver;
}
@BeforeTest
public void startReport(){
htmlReporter = new ExtentHtmlReporter(System.getProperty("user.dir") +"/test-output/ExtentReport.html");
extent = new ExtentReports ();
extent.attachReporter(htmlReporter);
extent.setSystemInfo("Host Name", "SoftwareTestingMaterial");
extent.setSystemInfo("Environment", "Automation Testing");
extent.setSystemInfo("User Name", "Rajkumar SM");
htmlReporter.config().setDocumentTitle("Title of the Report Comes here");
htmlReporter.config().setReportName("Name of the Report Comes here");
htmlReporter.config().setTestViewChartLocation(ChartLocation.TOP);
htmlReporter.config().setTheme(Theme.STANDARD);
}
@Test
public void setusername(WebDriver driver)
{
try
{
driver.findElement(EmailId).sendKeys("nilima@mailinator.com");
logger = extent.createTest("setusername");
Assert.assertTrue(true);
logger.log(Status.PASS, MarkupHelper.createLabel("Test Case Passed is setusername", ExtentColor.GREEN));
}
catch (TimeoutException e)
{
System.out.println("Time out exception " + e);
ut.capturescreenshot(driver, "setusername");
}
catch (ElementNotSelectableException e) {
System.out.println("Element not selectable exception " + e);
ut.capturescreenshot(driver, "setusername");
} catch (NoSuchElementException e) {
System.out.println("No such element found " + e);
ut.capturescreenshot(driver, "setusername");
} catch (ElementNotVisibleException e) {
e.printStackTrace();
ut.capturescreenshot(driver, "setusername");
} catch (Exception e) {
System.out.println("Something Wrong" + e);
ut.capturescreenshot(driver, "setusername");
}
}
@Test
public void setpassword(WebDriver driver)
{
try
{
driver.findElement(password).sendKeys("123123");
logger = extent.createTest("setpassword");
Assert.assertTrue(true);
logger.log(Status.PASS, MarkupHelper.createLabel("Test Case Passed is setpassword", ExtentColor.GREEN));
}
catch (TimeoutException e)
{
System.out.println("Time out exception " + e);
ut.capturescreenshot(driver, "setpassword");
}
catch (ElementNotSelectableException e) {
System.out.println("Element not selectable exception " + e);
ut.capturescreenshot(driver, "setpassword");
} catch (NoSuchElementException e) {
System.out.println("No such element found " + e);
ut.capturescreenshot(driver, "setpassword");
} catch (ElementNotVisibleException e) {
e.printStackTrace();
ut.capturescreenshot(driver, "setpassword");
} catch (Exception e) {
System.out.println("Something Wrong" + e);
ut.capturescreenshot(driver, "setpassword");
}
}
@Test
public void Login()
{
try
{
driver.findElement(LoginBtn).click();
String Title="Login | ORGCAP";
String GetTitle = driver.getTitle();
logger = extent.createTest("Login");
Assert.assertEquals(Title, GetTitle);
logger.log(Status.PASS, MarkupHelper.createLabel("Test Case Passed is Login", ExtentColor.GREEN));
}
catch (TimeoutException e)
{
System.out.println("Time out exception " + e);
ut.capturescreenshot(driver, "Login");
}
catch (ElementNotSelectableException e) {
System.out.println("Element not selectable exception " + e);
ut.capturescreenshot(driver, "Login");
} catch (NoSuchElementException e) {
System.out.println("No such element found " + e);
ut.capturescreenshot(driver, "Login");
} catch (ElementNotVisibleException e) {
e.printStackTrace();
ut.capturescreenshot(driver, "Login");
} catch (Exception e) {
System.out.println("Something Wrong" + e);
ut.capturescreenshot(driver, "Login");
}
}
@AfterMethod
public void getResult(ITestResult result){
if(result.getStatus() == ITestResult.FAILURE){
logger.log(Status.FAIL, MarkupHelper.createLabel(result.getName() + " - Test Case Failed", ExtentColor.RED));
logger.log(Status.FAIL, MarkupHelper.createLabel(result.getThrowable() + " - Test Case Failed", ExtentColor.RED));
}else if(result.getStatus() == ITestResult.SKIP){
logger.log(Status.SKIP, MarkupHelper.createLabel(result.getName() + " - Test Case Skipped", ExtentColor.ORANGE));
}
}
@AfterTest
public void endReport(){
extent.flush();
}
}
我的第二堂课
public class login extends TestBuild{
ITestResult result;
@Test()
public void Loginadmin() throws IOException{
TestBuild BrowserSetUp= new TestBuild();
BrowserSetUp.setup();
loginPage lp= new loginPage(driver);
lp.startReport();
lp.setusername(driver);
lp.setpassword(driver);
lp.Login();
lp.getResult(result);
lp.endReport();
}
}
我在以下位置收到NullPointerException:
if(result.getStatus()== ITestResult.FAILURE)行。
如何处理?
答案 0 :(得分:1)
您正在获取异常,因为您传递的ITestResult的对象结果没有任何值。它在@AfterMethod批注中获取值。
基本上有两种方法可以实现:
您可以创建具有Common Configuration方法的BaseClass,并且通过创建该类的对象,可以将这些方法调用到您的注释中。
您可以创建BaseClass,该类具有带有TestNG批注的通用配置方法,并且在每个测试中,您只需要管理@Test。无需在每个测试中维护每个单独的测试中的注释(如果您已扩展BaseClass,则适用)。
让我分享具有类似概念的示例。 Click Here
答案 1 :(得分:1)
您必须扩展编写ITestResult方法的基类。例如
public class BusinessGroup_Add extends CallAllFunctions{
WebDriver driver;
Company_Add company= new Company_Add(driver);
Utility ut= new Utility();
By BG_Menu=By.xpath("//*[@id='mainnav-menu']/li[1]/ul/li[2]/a");
By Open_BG_form= By.xpath("//*[@id='page-title']/div/div/div[2]/div/div[2]/a/i");
By BG_Name=By.name("Name");
By CountryDropDown= By.xpath("//*[@id='countryDiv']/div/a/span");
By AllCountry= By.xpath("//*[@id='countryDiv']/div/div/ul");
By StateDropdown= By.xpath("//*[@id='stateDiv']/div/a/span");
By AllState= By.xpath("//*[@id='stateDiv']/div/div/ul/li");
By Location= By.xpath("//*[@id='cityDiv']/div/a/span");
By AllLocation= By.xpath("//*[@id='cityDiv']/div/div/ul/li");
By Description= By.xpath("//*[@id='locModel']/div[6]/textarea");
By save= By.id("businessGrpSubmit");
By CancleSave= By.xpath("//*[@id='frmBusinessGrp']/div[4]/button[2]");
By ModalTitle=By.xpath("//*[@id='frmBusinessGrp']/div[1]/h4");
public BusinessGroup_Add(WebDriver driver)
{
this.driver=driver;
}
public void Click_BG(WebDriver driver)
{
try
{
Boolean staleElement = true;
while(staleElement){
try{
WebElement BGMenubar=driver.findElement(BG_Menu);
Boolean dropdownPresent =BGMenubar.isDisplayed();
if(dropdownPresent==true)
{
WebElement ele=(WebElement) new WebDriverWait(driver, 30).until(ExpectedConditions.visibilityOfElementLocated(BG_Menu));
ele.click();
test = extent.createTest("Navigate to BG");
String Actual=driver.findElement(company.PageHeader).getText();
String expcted="Business Group";
Assert.assertEquals(Actual, expcted);
test.log(Status.PASS, MarkupHelper.createLabel("Navigate to BG successfully", ExtentColor.GREEN));
}
else{
WebElement master1= (WebElement) new WebDriverWait(driver,60).until(ExpectedConditions.elementToBeClickable(company.master));
master1.click();
WebElement ele=(WebElement) new WebDriverWait(driver, 30).until(ExpectedConditions.elementToBeClickable(BG_Menu));
ele.click();
test = extent.createTest("Navigate to BG");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
String Actual=driver.findElement(company.PageHeader).getText();
System.out.println("Dashboard prnted+++++++++"+Actual);
String expcted="Business Group";
Assert.assertEquals(Actual, expcted);
test.log(Status.PASS, MarkupHelper.createLabel("Navigate to BG successfully", ExtentColor.GREEN));
}
staleElement = false;
} catch(StaleElementReferenceException e){
staleElement = true;
}
}
}
catch (TimeoutException e)
{
System.out.println("Time out exception " + e);
ut.capturescreenshot(driver, "Click_BG");
}
catch (ElementNotSelectableException e) {
System.out.println("Element not selectable exception " + e);
ut.capturescreenshot(driver, "Click_BG");
} catch (NoSuchElementException e) {
System.out.println("No such element found " + e);
ut.capturescreenshot(driver, "Click_BG");
} catch (ElementNotVisibleException e) {
e.printStackTrace();
ut.capturescreenshot(driver, "Click_BG");
} catch (Exception e) {
System.out.println("Something Wrong" + e);
ut.capturescreenshot(driver, "Click_BG");
}
}
您将在基类中获得结果