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");
}
}
添加回答
舉報