페인트와 같은 GWT 응용 프로그램을 테스트 중이며 여기서 문제가 발생했습니다. 지금까지 작성한 테스트 코드는 다음을 수행합니다.실패한 테스트 용으로 만 셀레늄에서 GWT 응용 프로그램 캔버스의 스크린 샷을 찍는 방법?
- HTML5 Canvas에 요소를 삽입하십시오.
- 캔버스의 스크린 샷을 찍은 다음 로컬 디렉토리에 저장합니다.
- 이미 로컬 디렉토리에있는 이미지와 스크린 샷을 비교합니다.
- 테스트 사례가 통과했는지 실패했는지 여부에 대한 결과를 제공합니다.
여러 대의 컴퓨터에서 브라우저 간 병렬 테스트를하기 위해 Selenium Grid에서 TestNG 프레임 워크를 사용하고 있습니다. 내 앞에있는 작업은 테스트가 실패 할 때만 스크린 샷을 저장하는 것입니다. 나는 많은 논리를 독자적으로 시도해 보았으며 인터넷에서 같은 논리를위한 많은 방법을 보았지만 그 중 누구도 나를 위해 일하지 않았다. 한편, 여기 내 코드입니다. 내가 같은 작업을 수행하기위한 TestNG를의 ITestListener 인터페이스가 많은 블로그에서 읽었습니다하지만 내 경우에는 그것을 구현하는 방법으로 혼동하고 있지만
public class InsertPortTest extends Browser {
//Browser class is where I have written the code for configuring the hub and browser capabilties.
@Test
public void testCanvas() throws InterruptedException, IOException {
getDriver().manage().window().maximize();
getDriver().manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS) ;
getDriver().get("xxxx.com");
//pause code execution until canvas element is click-able on the page
WebDriverWait wait = new WebDriverWait(getDriver(), 20);
wait.until(ExpectedConditions.titleContains("***"));
wait.until(ExpectedConditions.elementToBeClickable(By.id("xxx")));
Thread.sleep(3000);
WebElement canvas = getDriver().findElement(By.id("xxx"));
/* Then there is a code for inserting the elements on the canvas which I am not excluding here because it is of no use.*/
//After inserting the elements, the main work starts.
//fileBase1 and fileBase2 refers to the image files with which I will compare the captured screenshots
File fileBase1 = new File("./src/Images/baseDrawingchrome.png");
File fileBase2 = new File("./src/Images/baseDrawingfirefox.png");
Capabilities cap = ((RemoteWebDriver) getDriver()).getCapabilities();
String browserName = cap.getBrowserName().toLowerCase();
File fileActual = new File("./src/Images/actualDrawing"+browserName+".png");
File elementImage = this.takeElementScreenshot(canvas,"png");
FileUtils.copyFile(elementImage, fileActual);
if(browserName.equalsIgnoreCase("chrome")) {
this.compareImage(fileBase1,fileActual);
} else if(browserName.equalsIgnoreCase("firefox")) {
this.compareImage(fileBase2,fileActual);
}
Thread.sleep(3000);
}
//function for taking the screenshot
public File takeElementScreenshot(WebElement element, String imageFormat) throws IOException {
Point elementXY = element.getLocation();
int elementHeight = element.getSize().getHeight();
int elementWidth = element.getSize().getWidth();
Rectangle elementRectArea = new Rectangle(elementWidth,elementHeight);
WrapsDriver wrapsDriver = (WrapsDriver) element;
File pageImage = ((TakesScreenshot)wrapsDriver.getWrappedDriver()).getScreenshotAs(OutputType.FILE);
BufferedImage bufferedImage = ImageIO.read(pageImage);
BufferedImage elementImage = bufferedImage.getSubimage(elementXY.getX(), elementXY.getY(), elementRectArea.width, elementRectArea.height);
ImageIO.write(elementImage, imageFormat, pageImage);
return pageImage;
}
//function for comparing the images
public void compareImage(File fileBase, File fileActual) {
try {
BufferedImage bufferedImage = ImageIO.read(fileBase);
DataBuffer dataBufferFirst = bufferedImage.getData().getDataBuffer();
int sizeFirst = dataBufferFirst.getSize();
BufferedImage bufferedImage2 = ImageIO.read(fileActual);
DataBuffer dataBufferSecond = bufferedImage2.getData().getDataBuffer();
int sizeSecond = dataBufferSecond.getSize();
int count=0;
Assert.assertEquals(sizeFirst, sizeSecond,"Size of Base Drawing and actual Drawing is not same");
if(sizeFirst == sizeSecond){
for(int i=0; i<sizeFirst; i++){
if(dataBufferFirst.getElem(i) != dataBufferSecond.getElem(i)) {
//getElem() returns the data array element at the specified index.
count++;
}
}
Assert.assertEquals(count, 0,"Both images are not same");
}
} catch (Exception e) {
Assert.fail("Failed to compare image files...!!!");
}
}
}
. 도와주세요!!