2017-01-30 53 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")); 
}