How Can We Help?

< All Topics

Multiple page/step validations

In most of web applications we have multiple pages. 15- 20 pages are common for any medium sized application. So in those cases we may need to test multiple pages in a single test case.

This situation can be handled by adding more steps in same test case. TestRunID would remain same for all the steps.

package Refined;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.apache.commons.codec.binary.Base64;
import org.json.simple.JSONObject;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import javax.imageio.ImageIO;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;


public class fullpageAshot {

	static WebDriver driver;

	public static void main(String args[]) throws Exception
	{
		//Using selenium Launch browser and navigate to URL
		System.setProperty("webdriver.chrome.driver", "C:\\Users\\...\\Desktop\\selenium\\chromedriver.exe");
		driver = new ChromeDriver();
		driver.manage().window().maximize();
		driver.get("https://getbootstrap.com/docs/4.0/utilities/position/");
				
		// change the position from sticky to relative to remove stickiness
		((JavascriptExecutor) driver).executeScript("document.getElementsByClassName(\"navbar navbar-expand navbar-dark flex-column flex-md-row bd-navbar\")[0].style.position='relative';");
				
		
		//Get full page screenshot using Ashot and generate base64 image
		Screenshot screenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
		ByteArrayOutputStream out = new  ByteArrayOutputStream(); 
		ImageIO.write(screenshot.getImage(),"PNG", out);
		byte[] bytes = out.toByteArray();
		String imagebase64 = Base64.encodeBase64String(bytes);		
		
		
		//Get unique TestRunID for a specific project using Project key and Test name
		String uid = getUID("Multi Page", "fca709ea-d527-4f30-abc2-fa7e2af8afaf");
		
		//Send a Validate Request using the Unique TestID, Step name & Base64 Image 
		postRequest("Step 1 - Sticky",uid,imagebase64);
		
		
		///For Page 2
		driver.get("https://www.google.com");
		
		
		//Get full page screenshot using Ashot and generate base64 image
		screenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
		out = new  ByteArrayOutputStream(); 
		ImageIO.write(screenshot.getImage(),"PNG", out);
		bytes = out.toByteArray();
		imagebase64 = Base64.encodeBase64String(bytes);		
		
		
		//Same TestRunID will be used for multiple pages of same test cases
		//Send a Validate Request using the Unique TestID, Step name & Base64 Image 
		postRequest("Step 2 - Google",uid,imagebase64);
		
		driver.quit();
		
	}
	
	
	
	//Get unique Test ID for a specific project using Rest Assured 
	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.20.13:90/api/GetUID");
			int code = response.getStatusCode();
			String response_id = response.getBody().asString();
			System.out.println("TestID: " + response_id);
			return response_id;
		} catch (Exception ex) {
			return ex.toString();
		}
	}

	//Post a request using
	public static void postRequest(String stepName, 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", stepName);
		jo.put("ImageBase64", imagebase64);
		System.out.println("imagebase64:" + imagebase64);
		request1.body(jo.toJSONString());
		Response response1 = request1.when().post("http://192.168.20.13:90/api/Validate");
		String response_id1 = response1.getBody().asString();
		System.out.println("Response: " + response_id1);
	}


}