gpt4 book ai didi

selenium-webdriver - Selenium Web 驱动程序--TestNG 报告中未捕获失败屏幕截图

转载 作者:行者123 更新时间:2023-12-02 02:01:33 25 4
gpt4 key购买 nike

用下面的代码,如果测试用例通过-截屏成功并显示在报告中。但是当测试失败时-不显示屏幕截图。即使屏幕截图超链接也不显示在报告中。任何人都可以解决代码错误?

package listeners;

import java.io.File;
import java.io.IOException;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.TestListenerAdapter;
import java.util.logging.Logger;


@Listeners

public class CountryChoserLayer extends TestListenerAdapter {


@Test(priority=1)
public void choseCountry() throws Exception{

driver.findElement(By.id("intselect")).sendKeys("India");
driver.findElement(By.xpath(".//*[@id='countryChooser']/a/img")).click();
//window.onbeforeunload = null;
Date date=new Date();
Format formatter = new SimpleDateFormat("yyyy-MM-dd_hh-mm-ss");
File scrnsht = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
String NewFileNamePath=("C://Documents and Settings//vlakshm//workspace//MyTNG//test-output//Screenshots"+"//SearsINTL_"+ formatter.format(date)+".png");
FileUtils.copyFile(scrnsht, new File(NewFileNamePath));
System.out.println(NewFileNamePath);
Reporter.log("<a href=\"" + NewFileNamePath + "\">Passed Screenshot</a>");
System.out.println("---------------------------------------");
System.out.println("Country choser layer test case-Success");
System.out.println("---------------------------------------");

}


public String baseurl="http://www.sears.com/shc/s/CountryChooserView?storeId=10153&catalogId=12605";
public WebDriver driver;
public int Count = 0;

@Test(priority=0)
public void openBrowser() {

driver = new FirefoxDriver();
driver.manage().deleteAllCookies();
driver.get(baseurl);
}
@Test(priority=2)
public void closeBrowser() {
driver.quit();
}

@Override
public void onTestFailure(ITestResult result){

Reporter.log("Fail");
System.out.println("BBB");
//Reporter.setCurrentTestResult(result);
Date date=new Date();
Format formatter = new SimpleDateFormat("yyyy-MM-dd_hh-mm-ss");


File scrnsht = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
//File scrFile = ((TakesScreenshot) WebDriver.globalDriverInstance).getScreenshotAs(OutputType.FILE);
String NewFileNamePath=("C://Documents and Settings//vlakshm//workspace//MyTNG//test-output//Screenshots"+"//SearsINTL_"+ formatter.format(date)+".png");
//System.out.println("AAA" + NewFileNamePath);
try {
//System.out.println("CCC");
FileUtils.copyFile(scrnsht,new File(NewFileNamePath));
System.out.println(NewFileNamePath);


} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("DDD");
e.printStackTrace();
}
Reporter.log("<a href=\"" + NewFileNamePath + "\">Failed Screenshot</a>");
Reporter.setCurrentTestResult(null);
System.out.println("---------------------------------------");
System.out.println("Country choser layer test case Failed");
System.out.println("---------------------------------------");

}

@Override
public void onTestSkipped(ITestResult result) {
// will be called after test will be skipped
Reporter.log("Skip");
}
@Override
public void onTestSuccess(ITestResult result) {
// will be called after test will pass
Reporter.log("Pass");
}

}

最佳答案

您的 onTestFailure 方法没有被调用,因为您没有为您的测试类指定监听器。您在 @Listeners 注释中缺少一个值。应该是这样的

@Listeners({CountryChoserLayer.class})

您可以在 official TestNg's documentation 中找到更多指定监听器的方法.

您可能遇到的另一个问题是在 onTestFailure 方法中尝试截取屏幕截图时出现 NullPointerException。最简单的解决方法是将驱动程序字段的声明更改为静态。我运行带有这些修复程序的代码,并获得了带有屏幕截图的报告。

我必须补充一点,在我看来,将测试方法和监听器方法都放在一个类中并不是一个好的做法。

关于selenium-webdriver - Selenium Web 驱动程序--TestNG 报告中未捕获失败屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17009130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com