2017-01-30 57 views
2

大家好再林triying使Web应用程序的自动化测试,所以我需要知道如何在测试过程中采取多种截屏以多屏幕截图硒和java

这就是我的代码

@Test 
    public void TestJavaS1() { 


     WebDriver driver; 
     System.setProperty("webdriver.gecko.driver", "C:\\selenium\\geckodriver.exe"); 


     driver = new FirefoxDriver(); 


     Screenshot.captureScreenShot(driver); 


     driver.get("http://hotmail.com"); 
     Take.captureScreenShot(driver); 
+3

那怎么服用单截图知道吗? – acikojevic

+0

任何想法@acikojevic? –

+0

你只是在寻找如何截图? http://stackoverflow.com/questions/3422262/take-a-screenshot-with-selenium-webdriver – mrfreester

回答

0

有多种方法可以做到这一点。

创建一个与ScreenCapture不同的类文件,并在此类文件内创建两个方法。

一种方法用于何时成功运行特定的测试用例,另一种方法用于在执行测试脚本期间测试用例失败的情况。

我给你提供了一个类文件。现在

package com.dummy; 

import java.io.File; 
import java.io.IOException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 

import org.apache.commons.io.FileUtils; 
import org.openqa.selenium.OutputType; 
import org.openqa.selenium.TakesScreenshot; 

public class ScreenCapture { 

    public static void passScreenCapture() throws IOException 
    { 
     Date d = new Date(); 
     System.out.println(d.toString()); 

     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");   // Your each screenshot will be taken as this format "Year-Month-Date-Hours-Minutes-Seconds" 
     File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); 
     FileUtils.copyFile(scrFile, new File("D:\\RND\\"+sdf.format(d)+".png"));  //your screenshot path and convert date string to SimpleDateFormat because windows can't capture screenshot with(:) 
    } 

    public static void failScreenCapture() throws IOException 
    { 
     Date d = new Date(); 
     System.out.println(d.toString()); 

     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HHmmss"); 
     File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); 
     // Now you can do whatever you need to do with it, for example copy somewhere 
     FileUtils.copyFile(scrFile, new File("D:\\RND\\"+sdf.format(d)+".png")); 

    } 

} 

您的抓屏类文件是用两种不同的方法一起准备。 你需要调用这个方法,你想调用的地方。

你可以直接调用这个方法到任何类如下。

ScreenCapture.passScreenCapture();  //classname.methodname 
ScreenCapture.failScreenCapture(); 

OR

的另一种方法如下。

创建一个类文件,如下所示。

package com.dummy; 

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

import org.apache.commons.io.FileUtils; 
import org.openqa.selenium.OutputType; 
import org.openqa.selenium.TakesScreenshot; 
import org.testng.annotations.Test; 

public class ScreenShots { 

    public void captureScreen() throws IOException 
    { 
     File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); 
     // Now you can do whatever you need to do with it, for example copy somewhere 
     FileUtils.copyFile(scrFile, new File("D:\\RND\\Modulename.png")); 
    } 

} 

调用此方法的任何类,你可以调用这个方法,这样

public void captureScreen() throws Exception 
{ 
     File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); 
     // Now you can do whatever you need to do with it, for example copy somewhere 
     FileUtils.copyFile(scrFile, new File("D:\\RND\\Modulepage.png")); 
     System.out.println("Module Page Screen is taken successfully."); 
} 
0

的每一页多屏幕截图,只要创建一个需要截图一个常用的方法,并调用该方法你的代码在任何你想要截图的地方。正如@Jainish所说。

另一种选择是,如果您想在某段时间间隔后进行截图,例如应该每隔5秒捕捉一次截图。您可以使用java一些调度的任务 -

将这个在你的代码

Runnable takeScreenshot = new Runnable() 
{ 
     public void run() { 
      try { 
       captureScreenShot(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
}; 
    ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); 
    executor.scheduleAtFixedRate(takeScreenshot, 0, 3, TimeUnit.SECONDS); 

方法

public void captureScreenShot() throws IOException 
{ 
    Date d =new Date(); 
    File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); 
    // Now you can do whatever you need to do with it, for example copy somewhere 
    FileUtils.copyFile(scrFile, new File("D:\\My_Folder\\"+d.toString().replace(":", "_")+".png")); 
}