有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

selenium web驱动程序中错误页面的java屏幕截图,显示错误发生的日期以及错误发生的页面或链接

我的情况是,在我的网站上点击几个按钮/链接时,我会看到相同的错误页面。我有一个根据日期时间戳截图的代码示例,但我还想获取它发生在哪个按钮或链接单击事件上,并想将该事件名称附加到截图文件中。 请帮忙。 提前谢谢

到目前为止,这就是我所拥有的

try{
  My code where the error occures on button click
}
catch(Exception ex)
{
            File scrn=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

            // extracting date for folder name.
            SimpleDateFormat sdfDate1 = new SimpleDateFormat("yyyy-MM-dd");//dd/MM/yyyy
            Date now1 = new Date();
            String strDate1 = sdfDate1.format(now1);

            // extracting date and time for snapshot file
            SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");//dd/MM/yyyy
            Date now = new Date();
            String strDate = sdfDate.format(now);

            String filefolder="D:/Home/Snapshot/"+strDate1+"/";  // create a folder as snapshot in  your project directory

            // Creating folders and files
            File f = new File(filefolder+strDate+".jpeg");

            FileUtils.copyFile(scrn, new File(f.getPath()));
}

共 (3) 个答案

  1. # 1 楼答案

    /**
     * Define path for Screenshot file.
     */
    public String getScreenshotSavePath() {
    
        String className = this.getClass().getName();
        File dir = new File(System.getProperty("user.dir")+File.separator+"screenshots"+File.separator + className + File.separator);
        dir.mkdirs();
        return dir.getAbsolutePath();
    }
    
    
    /**
     * Take Screenshot on failure.
     */
    public void getScreenshot() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String date = sdf.format(new Date());
        String url = driver.getCurrentUrl().replaceAll("[\\/:*\\?\"<>\\|]", "_");
        String ext = ".png";
        String path = getScreenshotSavePath().toString()+ File.separator + date + "_" + url + ext;
    
        try {
            if (driver instanceof TakesScreenshot) {
                File tmpFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
                org.openqa.selenium.io.FileHandler.copy(tmpFile, new File(path));
                auto_logs.error("Captured Screenshot for Failure: "+path);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    
  2. # 2 楼答案

    将屏幕截图的整个代码放在一个方法中,比如take_screenshot,然后将其放在一个公共位置,比如,并将一个参数传递给它,该参数的名称为

    try {
        // some action which causes an error
    }
    catch(Exception ex)
    {
        MyLib.TakeScreenshot("click button")  // argument name, 'action' for example
        // do other things for error handling
    }
    

    而且MyLib.TakeScreenshot有你上面写的代码。排队

    // Creating folders and files
    File f = new File(filefolder+strDate+".jpeg");
    

    改为:

    File f = new File(filefolder + strDate + action + ".jpeg"); //action name added to filename
    
  3. # 3 楼答案

    一种方法是创建一个代理类,该类实现WebDriver接口(或仅实现所需的方法),并将真正的WebDriver对象作为构造函数参数。通过这种方式,您可以使用代理对象的方法,代理对象将截图显示引发异常的事件的信息

    public class WebDriverProxy {
       private WebDriver driver;
    
       public WebDriverProxy(WebDriver driver) {
             this.driver = driver;
       }
    
       public void clickLink (String linkText) {
          try{ 
            driver.click(By.linkText(linkText);
          } catch (Exception e) {
            this.takeScreenshot(linkText);
            throw e;
          }
       }
    
       private void takeScreenshot(String event) {
          /** take the screenshot and save it with String event in the filename **/
       }
    }