无法使用firefox拍摄失败的测试用例的屏幕截图

时间:2014-05-27 13:03:40

标签: firefox selenium

我无法使用Selenium webdriver和Firefox拍摄失败的测试用例的屏幕截图。

@RunWith(Parameterized.class)

public class screenshot{
public String Email;
public String password;
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();




public screenshot(String username,String password){
    this.Email =username;
    this.password=password;
}


@Parameters
public static Collection<Object[]>getdata(){
    Object[] [] data=new Object[2][2];
    data[0][0] =" xyz@gmail.com";
    data[0][1] ="12344";
    data[1][0] ="86826332@gmail.com";
    data[1][1] ="ytwey";



    return Arrays.asList(data);

}



  @Before
  public void setUp() throws Exception {

  driver = new FirefoxDriver();
  baseUrl = "https://accounts.google.com/";
  driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test

  public void testUntitled5() throws IOException {
  try{
   driver.get(baseUrl + "gmail login page");
   driver.findElement(By.id("Email")).clear();
   driver.findElement(By.id("Email")).sendKeys(Email);

   driver.findElement(By.id("Passwd")).sendKeys(password);
   driver.findElement(By.id("signIn")).click();
   Thread.sleep(1000);
  }catch(Exception e){
 File scrFile =        ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);  
FileUtils.copyFile(scrFile, new File("C:\\Documents and       Settings\\japarna\\Desktop\\deepikafailure.png")); 
  }

   }

程序运行成功,但我无法获取失败的测试用例的屏幕截图。 除此之外还有其他替代编码吗?它适用于Firefox版本30测试版,但Firefox版本29版本无效。是版本问题还是需要添加任何代码?

1 个答案:

答案 0 :(得分:0)

这是我使用的方法,它适用于Firefox 29

public String captureScreen(WebDriver driver) {

        String path;
        try {
            // WebDriver augmentedDriver = new Augmenter().augment(driver);
            File source = ((TakesScreenshot) driver)
                    .getScreenshotAs(OutputType.FILE);
            String datestamp = new SimpleDateFormat("yyyy_MM_dd_hh_mm_")
                    .format(new Date());
            path = "C:\\workspace\\screenshot\\errors\\" + datestamp
                    + source.getName();
            FileUtils.copyFile(source, new File(path));
        } catch (IOException e) {
            path = "Failed to capture screenshot: " + e.getMessage();
        }
        return path;

    }