[Java – Webdriver 04] – Capture multi-image

HƯỚNG DẪN TỰ ĐỘNG CAPTURE MULTI-IMAGE

Đây là một trong những dự án nhỏ đầu tiên hồi mình mới bước chân vào làm test automation. Bây giờ thì thấy nó đơn giản mà hồi đó mày mò mất tới 2-3 ngày 😀

Yêu cầu: Hiện tại tui có khoảng 500 -> 1000 site (muốn bao nhiêu thì các bạn tự thêm vào), giờ tui code ra 1 cái extension add vào 4 trình duyệt Chrome, Firefox, Safari, IE. Làm sao sau mỗi lần deploy tui kiểm tra cả 1000 site đó có tương thích về mặt giao diện (có xuất hiện và đúng vị trí) trên cả 4 trình duyệt hay không. Đến đoạn này thì mình đã làm xong, tiếp tục đến bước kiểm tra chức năng của extension trên từng trình duyệt thì bị việc auto bị pause lại. Bạn nào hứng thú thì làm tiếp cái này nhé, rất hay để có kinh nghiệm auto khi gặp dự án yêu cầu chạy trên nhiều trình duyệt cùng lúc.

Đây là document mình còn lưu lại:

Chạy trên nhiều máy (tất cả đặt mặc định trong ổ C):

Firefox:
• Firefox.bat
• Firefox.jar
• EuroBonus.xpi
• ReportFirefox (thư mục lưu kết quả hình ảnh được xuất ra)

Chrome:
• Chrome.bat
• Chrome.jar
• Chrome.zip (extension đã được đóng gói)
• chromedriver_win32 (Driver của trình duyệt)
• ReportChrome (thư mục lưu kết quả hình ảnh được xuất ra)

Safari:
• Safari.bat
• Safari.jar
• stoolbar.safariextz (extension đã được đóng gói)
• ReportSafari (thư mục lưu kết quả hình ảnh được xuất ra)

Internet Explorer: (đã cài đặt EuroBonus_Finder_IE_v1.0 39)
• IE.bat
• IE.jar
• IEDriverServerr (Driver của trình duyệt)
• ReportIE (thư mục lưu kết quả hình ảnh được xuất ra)

Cơ chế:
Sau khi chạy chương trình sẽ tự động duyệt qua từng link, chụp hình và lưu vào thư mục mặc định (Report), nếu gặp site lỗi/ ko thể kết nối đến server trình duyệt sẽ tự động đóng và bật 1 trình duyệt mới với link tiếp theo (đã capture lại link lỗi)

Source demo:

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.annotations.Test;

public class Firefox {
public String fileName;
public WebDriver driver;

@Test
public void runTestcase() throws Exception {
FirefoxProfile profile = new FirefoxProfile();
File extension = new File(“C:\\EuroBonus.xpi”);
profile.addExtension(extension);
driver = new FirefoxDriver(profile);

ArrayList<String> arrDomainList = new ArrayList<String>();
arrDomainList.add(“https://daominhdam.wordpress.com”);
arrDomainList.add(“https://vntesters.com”);

try {
for (int i = 0; i < arrDomainList.size(); i++) {
driver.get(arrDomainList.get(i));
System.out.println(arrDomainList.get(i));
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20000, TimeUnit.SECONDS);

fileName = driver.getCurrentUrl();
fileName = fileName.replace(“https”, “”);
fileName = fileName.replace(“http”, “”);
fileName = fileName.replace(“<“, “”);
fileName = fileName.replace(“;”, “”);
fileName = fileName.replace(“:”, “”);
fileName = fileName.replace(“‘”, “”);
fileName = fileName.replace(“{“, “”);
fileName = fileName.replace(“}”, “”);
fileName = fileName.replace(“[“, “”);
fileName = fileName.replace(“]”, “”);
fileName = fileName.replace(“|”, “”);
Firefox.takeSnapShot(driver, “C:\\ReportFirefox\\” + fileName + “.png”);
}
} catch (Exception e) {
driver.quit();
driver = new FirefoxDriver(profile);
}
driver.close();
}

public static void takeSnapShot(WebDriver webdriver, String fileName) {
TakesScreenshot scrShot = ((TakesScreenshot) webdriver);
File sourceFile = scrShot.getScreenshotAs(OutputType.FILE);
File destFile = new File(fileName);
try {
FileUtils.copyFile(sourceFile, destFile);
} catch (IOException e) {
e.printStackTrace();
}
}
}

Video demo: (1p50s là phần report chứa image được capture):