我正在尝试在测试失败时在 IOS 和 Android 模拟器上捕获屏幕截图,但我的驱动程序返回 Null。当我在计算机上的 Chrome 上运行代码时,它运行良好并在失败时截取屏幕截图,问题出在它在模拟器上运行时。请参阅下面的代码以截取驱动程序返回 Null 的屏幕截图。我不确定我做错了什么,请帮助我。
public class GetScreenShot {
public static String capture(WebDriver driver, String screenshotName) throws Exception
{
String dateName = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
TakesScreenshot ts = (TakesScreenshot)driver;
File source = ts.getScreenshotAs(OutputType.FILE);
String destination = System.getProperty("user.dir") + "/ErrorScreenshots/"+screenshotName+dateName+".png";
File finalDestination = new File(destination);
FileUtils.copyFile(source, finalDestination);
return destination;
}
}
And here is the code on the Report.
public class ExtentReportsClass{
//AndroidDriver driver;
public static ExtentHtmlReporter htmlReporter;
public static ExtentReports extent;
public static ExtentTest logger;
public static WebDriver driver=null;
@BeforeSuite
public void config()
{
htmlReporter = new ExtentHtmlReporter(System.getProperty("user.dir") +"/test-output/ABSA_Test_Report.html");
extent = new ExtentReports();
extent.attachReporter(htmlReporter);
extent.setSystemInfo("Host Name", "Software Testing");
extent.setSystemInfo("Environment", "Production");
extent.setSystemInfo("User Name", "Kgotso Mashabela");
htmlReporter.config().setDocumentTitle("Automation Report");
htmlReporter.config().setReportName("ABSA Automation Report");
htmlReporter.config().setTestViewChartLocation(ChartLocation.TOP);
htmlReporter.config().setTheme(Theme.STANDARD);
}
@AfterMethod
public void getResult(ITestResult result) throws Exception
{
if (result.getStatus() == ITestResult.FAILURE)
{
String screenShotPath = GetScreenShot.capture(driver, result.getName());
logger.log(Status.FAIL, MarkupHelper.createLabel(result.getName()+" Test case FAILED due to below issues:", ExtentColor.RED));
logger.fail(result.getThrowable());
logger.fail("Snapshot below: " + logger.addScreenCaptureFromPath(screenShotPath));
//test.fail("Screenshot below: " + test.addScreenCaptureFromPath(takeScreenShot(result.getMethod().getMethodName())));
}
else if(result.getStatus() == ITestResult.SUCCESS)
{
logger.log(Status.PASS, MarkupHelper.createLabel(result.getName()+" Test Case PASSED", ExtentColor.GREEN));
}
else
{
logger.log(Status.SKIP, MarkupHelper.createLabel(result.getName()+" Test Case SKIPPED", ExtentColor.ORANGE));
logger.skip(result.getThrowable());
}
}
@AfterSuite
public void tearDown()
{
extent.flush();
}
}
这是我的课。
公共类 IOSClass 扩展 ExtentReportsClass{
AppiumDriver driver;
Map<String, Object[]> data = new HashMap<String, Object[]>();
DesiredCapabilities capabilities = new DesiredCapabilities();
@BeforeTest
public void OpenSafari() throws MalformedURLException {
System.out.println("********************");
System.out.println("Running Safari on IOS...");
logger = extent.createTest("OpenSafari");
capabilities.setCapability(CapabilityType.BROWSER_NAME, "Safari");
capabilities.setCapability("platformName", "iOS");
capabilities.setCapability("platformVersion", "11.1");
capabilities.setCapability("deviceName", "iPhone 6");
capabilities.setCapability("udid", "B7D8F638-F689-4155-BA7F-F7CDD149D75D");
driver = new IOSDriver(new URL("http://0.0.0.0:4723/wd/hub"),capabilities);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
logger.log(Status.PASS,"Capabilities Configured Successfully ");
}
@Test(priority=1, dataProvider = "MultiBrowser")
public void OpenSafariBrowser(String AppURL,String Ffacebook,String FUsername,String FPassword)throws Exception {
Thread.sleep(5000);
SearchFacebook(AppURL,Ffacebook);
Thread.sleep(5000);
SignUp(FUsername,FPassword);
Thread.sleep(5000);
//Mmessage.PopUpMessage(driver);
WriteIntoExcel();
}
@DataProvider(name = "MultiBrowser")
public Object[][] provideTestParam(ITestContext context) {
String ExcelLocation = context.getCurrentXmlTest().getParameter("ExcelLocation");
Object[][] retObjArr = {{ ExcelLocation }};
try
{
File path = new File("/Users/Documents/Screenshots/TestData.xlsx");
FileInputStream file = new FileInputStream(path);
XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFSheet sheet = workbook.getSheetAt(0);
retObjArr = new Object[sheet.getPhysicalNumberOfRows() - 1][4];
for(int rownum = 0; rownum < sheet.getPhysicalNumberOfRows() - 1; rownum++){
Row row = sheet.getRow(rownum + 1);
retObjArr[rownum][0] = row.getCell(0).getStringCellValue();
retObjArr[rownum][1] = row.getCell(1).getStringCellValue();
retObjArr[rownum][2] = row.getCell(2).getStringCellValue();
retObjArr[rownum][3] = row.getCell(3).getStringCellValue();
}
file.close();
}
catch (Exception e)
{
e.printStackTrace();
}
return(retObjArr);
}
public void SearchFacebook(String AppURL,String Ffacebook) throws InterruptedException{
logger = extent.createTest("SearchFacebook");
System.out.println("Search for Facebook...");
driver.navigate().to(AppURL);
// logger.log(Status.PASS,"Browser Launched Successfully ");
//logger.log(Status.PASS,"The Current URL of the Web page is : "+AppURL);
String strPageTitle = driver.getTitle();
System.out.println("Page tittle "+strPageTitle);
Thread.sleep(5000);
Assert.assertTrue(0 > 1);
// CaptureScreenShort.captureScreenShot(driver);
Thread.sleep(5000);
data.put("1", new Object[] {1d, "Test Description", "Test Step","Screenshots"});
driver.findElement(By.id("lst-ib")).sendKeys(Ffacebook);
data.put("2", new Object[] {1d, "User entered " + Ffacebook +" to Search", "Field Validation: Text Entered Successfully"});
// CaptureScreenShort.captureScreenShot(driver);
driver.findElement(By.cssSelector("#tsbb")).click();
data.put("3", new Object[] {1d, "User Clicked Button to Search", "Button Validation: Button clicked Successfully"});
// CaptureScreenShort.captureScreenShot(driver);
driver.findElement(By.partialLinkText("Facebook")).click();
data.put("4", new Object[] {1d, "User Facebook Link to Open", "Link Validation: Link clicked Successfully"});
// CaptureScreenShort.captureScreenShot(driver);
Thread.sleep(5000);
Assert.assertTrue(0 > 1);
Thread.sleep(5000);
String title = driver.getTitle();
Assert.assertEquals("facebook - Google Search1", title);
System.out.println("Successfully opened Facebook");
System.out.println("********************");
}
请参阅下面的日志。
java.lang.NullPointerException
at com.example.helloworld.GetScreenShot.capture(GetScreenShot.java:20)
at com.example.helloworld.ExtentReportsClass.getResult(ExtentReportsClass.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:510)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:211)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:703)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:774)
at org.testng.TestRunner.run(TestRunner.java:624)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:359)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)
at org.testng.SuiteRunner.run(SuiteRunner.java:261)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1215)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
at org.testng.TestNG.run(TestNG.java:1048)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)