0

我有两个类,我想在类“B”中调用类“A”的public 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) 行。

如何处理?

4

2 回答 2

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");
    }



}

你会在基类中得到你的结果

于 2019-04-09T07:20:41.093 回答
1

您收到异常,因为您传递的 ITestResult 的对象结果没有价值。它在 @AfterMethod 注释中获得价值。

基本上有两种方法,通过实现这一点:

  1. 您可以创建具有通用配置方法的 BaseClass,并通过创建该类的对象,您可以将这些方法调用到您的注释中。

  2. 您可以创建具有带有 TestNG 注释的通用配置方法的 BaseClass,并且在您的每个测试中,您只需要管理您的 @Test。无需在每个测试中维护每个单独的测试中的注释(如果您扩展了 BaseClass,则适用)。

让我分享一个例子,它有类似的概念。点击这里

于 2019-03-27T10:58:49.917 回答