Forum

Forum
Visual validation o...
 
Notifications
Clear all

[Solved] Visual validation of individual elements in a webpage

New Member Guest
Joined: 3 years ago
Posts: 1
Topic starter  

We are using Java/Maven and selenium based automation framework. There is a need in our project to compare only a specific a specific element in the webpage instead of the whole page. Is there any way to do this?

 

We understand there is a way to exclude unwanted regions but we don't want to use it. Instead just want to compare only some part of the webpage like in Applitools .


   
Amrita Kaur
Member Admin
Joined: 3 years ago
Posts: 107
 

Yes it is possible. Just pass the base64 image of only the required element.

We can get the element screenshot by cropping entire page screenshot as below:

driver.get("http://www.google.com");
WebElement ele = driver.findElement(By.id("hplogo"));

// Get entire page screenshot
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage  fullImg = ImageIO.read(screenshot);

// Get the location of element on the page
Point point = ele.getLocation();

// Get width and height of the element
int eleWidth = ele.getSize().getWidth();
int eleHeight = ele.getSize().getHeight();

// Crop the entire page screenshot to get only element screenshot
BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(),
    eleWidth, eleHeight);

//convert to vase64 image
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(eleScreenshot, "PNG", out);
byte[] bytes = out.toByteArray();
String base64bytes = Base64.encode(bytes);

//Get unique TestID for a specific project using Project key and Test name
String uid = getUID("Test Element", "fca709ea-d527-4f30-abc2-fa7e2af8afaf");
		
//Send a Validate Request using the Unique TestID, Step name & Base64 Image 
postRequest("Step 1",uid, base64bytes);

 

More details and sample on getUID and postRequest methods can be seen here