1 回答
TA贡献1891条经验 获得超3个赞
您将 TestNG 侦听器和 Testclass 相关注释混合在一起,这就是导致NullPointerException
我猜你有一个也在扩展BaseTest
. 为了示例,我们将该类称为RegressionTest
.
TestNG 现在创建两个实例:
RegressionTest
1个实例testNgListners
1 个班级实例。
但要注意的是,testNgListners
创建实例时,根本不会调用@BeforeMethod
和@AfterMethod
注释,因为这些注释在侦听器的上下文中没有任何相关性。
要解决此问题,您需要执行以下操作:
重构
testNgListners
使其不扩展BaseTest
现在
BaseTest
从中移动浏览器实例化和清理逻辑。您应该通过 TestNG 侦听器管理浏览器实例化,因为只要出现故障,您的 testng 也将使用驱动程序对象来截取屏幕截图。
下面是修改后的监听器的样子:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.ITestListener;
import org.testng.ITestResult;
import org.testng.Reporter;
public class BrowserManagementListener implements ITestListener {
private static final String BROWSER = "browser";
private File file =
new File("\\\\192.168.70.39\\IT Share\\Automation\\ERP Automation\\credential.properties");
private Properties prop = new Properties();
public static RemoteWebDriver getDriver() {
ITestResult result = Reporter.getCurrentTestResult();
if (result == null) {
throw new IllegalStateException("could not detect a valid test result");
}
Object object = result.getAttribute(BROWSER);
if (object == null) {
throw new IllegalStateException("could not find a browser");
}
return (RemoteWebDriver)object;
}
@Override
public void onTestStart(ITestResult result) {
// This line retrieves the value of
// <parameter name="browser" value="chrome"/> from your testng suite xml
String browser = result.getTestContext().getCurrentXmlTest().getParameter("browser");
if ("chrome".equalsIgnoreCase(browser)) {
FileInputStream fileInput = null;
try {
fileInput = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
prop.load(fileInput);
} catch (Exception e) {
e.printStackTrace();
}
}
ChromeOptions options = new ChromeOptions();
options.setPageLoadStrategy(PageLoadStrategy.NONE);
RemoteWebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
result.setAttribute(BROWSER, driver);
}
@Override
public void onTestSuccess(ITestResult result) {
cleanUpBrowser(result);
}
@Override
public void onTestFailure(ITestResult result) {
Object object = result.getAttribute(BROWSER);
if (object == null) {
return;
}
RemoteWebDriver driver = (RemoteWebDriver) object;
File srcFile = driver.getScreenshotAs(OutputType.FILE);
File destFile = new File("test-output/" + result.getName() + ".png");
try {
FileUtils.copyFile(srcFile, destFile);
System.out.println("Screenshot is been taken for failed test case: " + result.getName());
System.err.println("Screenshot below" + destFile.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
} finally {
cleanUpBrowser(result);
}
}
private void cleanUpBrowser(ITestResult result) {
Object driver = result.getAttribute(BROWSER);
if (driver != null) {
((RemoteWebDriver) driver).quit();
result.setAttribute(BROWSER, null);
}
}
}
这是测试类的样子
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
@Listeners(BrowserManagementListener.class)
public class SampleTestClass {
@Test
public void testMethod() {
BrowserManagementListener.getDriver().get("http://www.google.com");
throw new RuntimeException("Simulating an error");
}
}
添加回答
举报