java 具有Selenium和Page对象模型的范围报表中出现TestNG NullPointer异常

qaxu7uf2  于 2023-01-29  发布在  Java
关注(0)|答案(1)|浏览(97)

'#错误:**
java.lang.NullPointerException:无法调用"com. aventstack. extentreports. extentTest. log(com. aventstack. extentreports.状态,字符串)",因为位于java. base/java. util. ArrayList. forEach(ArrayList. java:1511)的testCases. uTest_Method. exit_method(uTest_Method. java:93)处的"testCases. uTest_Method. test"为空...删除了32个堆栈帧
这是我为google搜索创建的类

一个月

package pageObjects;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;

public class Google_Search_Page {
    WebDriver driver;
    By g_search_text=By.xpath("//*//input[@name='q']");
    By utest=By.xpath("//*//h3[text()='uTest - The Professional Network for Testers']");
    public Google_Search_Page(WebDriver driver)
    {
        this.driver=driver;
    }
    
    public void google_search(String key_text)
    {
        driver.manage().window().maximize();
        driver.findElement(g_search_text).sendKeys(key_text,Keys.ENTER);
    }
    public void clickutest()
    {
        driver.findElement(utest).click();
    }

}

这是我为下页函数创建的类

一个月一个月

package pageObjects;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class uTest_Home_Page {
WebDriver driver;

    By bat_text=By.xpath("//*[@id=\"mainContent\"]/div[1]/div[2]/div/a");
    public uTest_Home_Page(WebDriver driver)
    {
        this.driver=driver;
    }
    
    public void click_become_a_tester()
    {
        driver.findElement(bat_text).click();
    }

}

这是我使用扩展报告创建的测试用例

uTest_Method.java

package testCases;

import java.io.File;

import java.io.IOException;
import java.time.Duration;

import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.io.FileHandler;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.reporter.configuration.Theme;

import commonFunctions.Common_Functions;
import pageObjects.Google_Search_Page;
import pageObjects.uTest_Home_Page;

public class uTest_Method {

    WebDriver driver;
    String base_url = "https://www.google.com";
    ExtentHtmlReporter reporter;        
    ExtentReports extent;               
    ExtentTest test;                    
    
    @BeforeTest
    public void launch_test()
    {
        reporter=new ExtentHtmlReporter("./uTest_Report/report1.html");
        reporter.config().setDocumentTitle("uTest_Automation_Report");
        reporter.config().setReportName("Functional_Test");
        reporter.config().setTheme(Theme.DARK);
        
        extent =new ExtentReports();
        extent.attachReporter(reporter);
        extent.setSystemInfo("Host_Name", "localhost");
        extent.setSystemInfo("OS", "Windws10");
        extent.setSystemInfo("Tester_Name", "PRAJIN");
        extent.setSystemInfo("Browser_Name", "Chrome");
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
        driver.get(base_url);
    }
    @BeforeMethod
    public void start_method()
    {
        
    }
    @Test (priority = 0)
    public void uTest_google()
    {
        Google_Search_Page ob1=new Google_Search_Page(driver);
        ob1.google_search("uTest");
        ob1.clickutest();
    }
    @Test (priority = 1)
    public void uTest_Home()
    {
        uTest_Home_Page ob1=new uTest_Home_Page(driver);
        ob1.click_become_a_tester();
    }
    @AfterMethod
    public void exit_method(ITestResult result) throws IOException  
    {
        if(result.getStatus()==ITestResult.FAILURE)
        {
            test.log(Status.FAIL, "TestCase Failed is "+result.getName());
            test.log(Status.FAIL, "TestCase Failed is "+result.getThrowable());
   
        }
        else if(result.getStatus()==ITestResult.SKIP)
        {
            test.log(Status.SKIP, "TestCase Skipped is "+result.getName());
        }
        else if(result.getStatus()==ITestResult.SUCCESS)
        {
            test.log(Status.PASS, base_url);
        }
    }
    @AfterTest
    public void exit_test()
    {
        extent.flush();
        //driver.quit();
    }

}`
erhoui1w

erhoui1w1#

外观扩展测试;未初始化。类似于

test = report.startTest("ExtentDemo");

此链接的示例为https://www.browserstack.com/guide/extent-reports-in-selenium

相关问题