在使用TestNG侦听器捕获屏幕截图时并行运行测试时遇到问题。
如果我运行 STATIC 网络驱动程序,则我的测试似乎互相发送了错误的数据,但侦听器似乎正在运行。...
当我运行 NON STATIC 网络驱动程序时,线程都可以并行并行运行,但是我在侦听器文件上收到NullPointException。
有没有解决的办法?我看过将线程作为线程安全或本地线程运行,但不确定我是否正确实现了
这是我的基类代码
BASEREMOTEPARALLEL2.java
package resources;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
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.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.io.FileHandler;
public class baseRemoteParallel2 {
public WebDriver driver;
public WebDriver initialiseDriver(String browser) throws IOException {
if (browser.equalsIgnoreCase("chrome")) {
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Documents\\Webdriver_Plugins\\chromedriver.exe");
driver = new ChromeDriver();
} else if (browser.equals("firefox")) {
System.setProperty("webdriver.gecko.driver",
"C:\\Users\\Documents\\Webdriver_Plugins\\geckodriver.exe");
driver = new FirefoxDriver();
} else if (browser.equals("ie")) {
System.setProperty("webdriver.ie.driver",
"C:\\Users\\Documents\\Webdriver_Plugins\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
}
// Set GLOBAL timeout to 5 seconds
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
return driver;
}
public void getScreenshotPassed(String result) throws IOException {
File src = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileHandler.copy(src,
new File("C:\\Users\\Documents\\Automated_screenshots\\PASSED\\Testname - " + result + ".png"));
}
public void getScreenshotFailed(String result) throws IOException {
File src = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileHandler.copy(src,
new File("C:\\Users\\Documents\\Automated_screenshots\\FAILED\\Testname - " + result + ".png"));
}
}
这是我的Listener.java中的代码
package resources;
import java.io.IOException;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
public class listeners implements ITestListener {
baseRemoteParallel2 b = new baseRemoteParallel2();
public void onTestStart(ITestResult result) {
// TODO Auto-generated method stub
}
public void onTestSuccess(ITestResult result) {
// TODO Auto-generated method stub
try {
b.getScreenshotPassed(result.getName());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void onTestFailure(ITestResult result) {
// TODO Auto-generated method stub
try {
b.getScreenshotFailed(result.getName());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void onTestSkipped(ITestResult result) {
// TODO Auto-generated method stub
}
public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
// TODO Auto-generated method stub
}
public void onStart(ITestContext context) {
// TODO Auto-generated method stub
}
public void onFinish(ITestContext context) {
// TODO Auto-generated method stub
}
}
错误。
java.lang.NullPointerException
at resources.baseRemoteParallel.getScreenshotPassed(baseRemoteParallel.java:101)
at resources.listeners.onTestSuccess(listeners.java:24)
at org.testng.internal.TestListenerHelper.runTestListeners(TestListenerHelper.java:70)
at org.testng.internal.Invoker.runTestListeners(Invoker.java:1389)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:636)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.access$000(SuiteRunner.java:40)
at org.testng.SuiteRunner$SuiteWorker.run(SuiteRunner.java:489)
at org.testng.internal.thread.ThreadUtil$1.call(ThreadUtil.java:52)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
我的代码上的第101行是
。 File src = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
答案 0 :(得分:0)
您可以使用如下界面:
import java.io.File;
public interface RemoteParallelAccess {
public baseRemoteParallel2 getRemoteAccess();
}
您的测试类必须实现该接口:
class MyTest implements RemoteParallelAccess {
baseRemoteParallel2 b = new baseRemoteParallel2();
@Test
public void doSomeSeleniumStuff() {
}
@Override
public void baseRemoteParallel2 getRemoteAccess() {
return b;
}
}
您可以在侦听器类中使用
if (result.getInstance() instanceof RemoteParallelAccess) {
RemoteParallelAccess t = (RemoteParallelAccess) result.getInstance();
baseRemoteParallel2 b = t.getRemoteAccess();
}
该接口是访问每个测试类中封装的Webdriver的通用方法。如果跨多个测试类使用静态Webdriver,则Webdriver的状态可能不一致。多个TestNG线程也可能存在并发问题。