How Can We Help?

< All Topics

Integration with selenium

The integration process with Java and Selenium is designed to be straightforward and user-friendly. The primary task involves capturing a screenshot in the Base64 format. Once this is accomplished, the user simply needs to send a POST request as outlined here. This streamlined approach minimizes complexity and allows for quick and efficient integration, facilitating a smoother workflow and enabling developers to focus more on the development and testing of their applications rather than getting bogged down by intricate setup processes. This method ensures that integrating Java and Selenium becomes a less daunting task, opening up more possibilities for efficient and effective software testing and development.

The below code use rest- assured to make the API calls, however any library that supports API calls can be used.

import java.io.IOException;
import org.json.simple.JSONObject;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.Dimension;


public class google {

	static WebDriver driver;

	public static void main(String args[]) throws Exception {
		System.setProperty("webdriver.chrome.driver", "C:\\Users\\selenium\\chromedriver.exe");
		driver = new ChromeDriver();
		driver.get("https://www.google.com/");
		driver.manage().window().setSize(new Dimension(1024, 768));
		String scrBase64 = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BASE64);
		String uid = getUID("Google Home", "fca709ea-d527-4f30-abc2-fa7e2af8afaf");
		postRequest("Step 1", uid, scrBase64);
		driver.quit();
	}



	public static String getUID(String testName, String projectKey) {
		try {
			RequestSpecification request = RestAssured.given();
			request.header("content-type", "application/json");
			JSONObject json = new JSONObject();
			json.put("TestName", testName);
			json.put("ProjectKey", projectKey);
			request.body(json.toJSONString());
			Response response = request.when().post("http://192.168.10.13:90/api/GetUID");
			int code = response.getStatusCode();
			String response_id = response.getBody().asString();
			System.out.println("Response id:" + response_id);
			return response_id;
		} catch (Exception ex) {
			return ex.toString();
		}
	}

	public static void postRequest(String stepNam, String uid, String imagebase64) throws IOException {
		RequestSpecification request1 = RestAssured.given();
		request1.header("content-type", "application/json");
		JSONObject jo = new JSONObject();
		jo.put("TestRunID", uid.replace("\"", ""));
		jo.put("StepName", stepNam);
		jo.put("ImageBase64", imagebase64);
		System.out.println("imagebase64:" + imagebase64);
		request1.body(jo.toJSONString());
		Response response1 = request1.when().post("http://192.168.10.13:90/api/Validate");
		String response_id1 = response1.getBody().asString();
		System.out.println("response id1:" + response_id1);
	}

}

To take the full page screenshot, libraries like Ashot or Shutterbug can be used.

Images used should be of same resolution. You can fix the resolution as : driver.manage().window().setSize(new Dimension(1024, 768));

For the first run it will create the baseline. Any subsequent runs will be compared against baseline.