How Can We Help?

< All Topics

Just like selenium, Integration of Imagium with Appium is extremely simple. All you need to do is capture the screenshot in Base64 format and send a Post request as mentioned here .

package Appium.copy;

import java.io.IOException;
import java.net.URL;
import org.json.simple.JSONObject;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;


public class AirCanada {

	static WebDriver driver;
	static int linkcount = 0; 
	static String uid = "";

	public static void main(String args[]) throws Exception
	{
		
		//Set up desired capabilities and pass the Android app-activity and app-package to Appium
		DesiredCapabilities capabilities = new DesiredCapabilities();
		capabilities.setCapability("BROWSER_NAME", "Android");
		capabilities.setCapability("VERSION", "9.0"); 
		capabilities.setCapability("deviceName","test2");
		capabilities.setCapability("platformName","Android");
		capabilities.setCapability("appPackage", "com.android.calculator2");		
		capabilities.setCapability("appActivity","com.android.calculator2.Calculator"); 
	//Create RemoteWebDriver instance and connect to the Appium server
	 //It will launch the Calculator App in Android Device using the configurations specified in Desired Capabilities
	   driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
		
	   
	   //Get unique ID for test case
	   String uid = getUID("Appium Calculator", "6ba4e61f-44c8-4543-b72a-c049bf306f61");
	   String scrBase64 = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BASE64);
	   postRequest("Step 1", uid, scrBase64);
		
		//Screenshot for step 2
		driver.findElement(By.xpath("//*[@text='2']")).click();
		Thread.sleep(1000);
		scrBase64 = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BASE64);
		postRequest("Step 2", uid, scrBase64);

	}
	
	//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.2.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 for validation
	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.2.13:90/api/Validate");
		String response_id1 = response1.getBody().asString();
		System.out.println("Response: " + response_id1);
	}


}