Selenium webdriver 如何从另一个类调用ItestResult方法?

Selenium webdriver 如何从另一个类调用ItestResult方法?,selenium-webdriver,testng,pageobjects,selenium-extent-report,Selenium Webdriver,Testng,Pageobjects,Selenium Extent Report,我有两个类,我想调用类“B”中类“A”的public void getResult(ITestResult result)方法如何调用它?我得到了NullPointerException 头等舱 public class loginPage{ public WebDriver driver; Utility ut= new Utility(); ExtentHtmlReporter htmlReporter; ExtentReports extent; ExtentTest logger;

我有两个类,我想调用类“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)行


如何处理此问题?

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

基本上有两种方法可以实现这一点:

  • 您可以创建具有公共配置方法的基类,通过创建该类的对象,您可以将这些方法调用到注释中

  • 您可以创建基类,它具有带有TestNG注释的公共配置方法,并且在每个测试中,您只需管理@test即可。在每个测试中,不需要在每个单独的测试中维护注释(如果您已经扩展了基类,则可以使用注释)


  • 让我举个例子,这个例子有着相似的概念

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

    基本上有两种方法可以实现这一点:

  • 您可以创建具有公共配置方法的基类,通过创建该类的对象,您可以将这些方法调用到注释中

  • 您可以创建基类,它具有带有TestNG注释的公共配置方法,并且在每个测试中,您只需管理@test即可。在每个测试中,不需要在每个单独的测试中维护注释(如果您已经扩展了基类,则可以使用注释)


  • 让我举个例子,这个例子有着相似的概念

    您必须在编写ITestResult方法的地方扩展基类。e、 g

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

    您将在基类中获得结果

    您必须在编写ITestResult方法的地方扩展基类。e、 g

    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");
        }
    
    
    
    }
    
    你将在基类中得到你的结果