-
Notifications
You must be signed in to change notification settings - Fork 1
/
GoogleCloudOCR
93 lines (84 loc) · 4.43 KB
/
GoogleCloudOCR
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
This one uses Google cloud vision for image text verification. The API key needs to be passed as a parameter for the custom function.
Text in the screenshot image is stored in a string and then the text2match is verified within that string.
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Base64;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.jayway.jsonpath.JsonPath;
import okhttp3.*;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
public class ImageTextVerification extends TestsigmaCustomFunctions {
private final MediaType JSON
= MediaType.get("application/json; charset=utf-8");
private OkHttpClient okHttpClient = new OkHttpClient();
private StringBuilder stringBuilder = new StringBuilder();
@CustomTestStep
public TestStepResult checkJSONContent(String apiKey, String text2match){
TestStepResult result = new TestStepResult();
try {
Response response = getTextFromImageUsingGCVREST(apiKey);
String responseJSON = response.body().string();
//System.out.println(responseJSON.length());
String imageText = JsonPath.parse(responseJSON).read(
"$.responses[0].textAnnotations[0].description");
//stringBuilder.append("ImageText: "+imageText);
if (imageText.contains(text2match)) {
//int matchTextPosition = imageText.indexOf(text2match);
stringBuilder.append(String.format("Given text '%s' found in the screenshot.", text2match));
result.setStatus(ResultConstants.SUCCESS);
}
else{
stringBuilder.append(String.format("Given text '%s' not found in the screenshot.", text2match));
result.setStatus(ResultConstants.FAILURE);
}
}catch (MalformedURLException malformedURLException) {
stringBuilder.append(ExceptionUtils.getStackTrace(malformedURLException));
result.setMessage(stringBuilder.toString());
result.setStatus(ResultConstants.FAILURE);
result.setMessage("malformedURLException:"+malformedURLException.getMessage());
} catch (IOException ioException) {
stringBuilder.append(ExceptionUtils.getStackTrace(ioException));
result.setMessage(stringBuilder.toString());
result.setStatus(ResultConstants.FAILURE);
result.setMessage("ioException:"+ioException.getMessage());
} catch (Exception exception){
stringBuilder.append(ExceptionUtils.getStackTrace(exception));
result.setMessage(stringBuilder.toString());
result.setStatus(ResultConstants.FAILURE);
result.setMessage("exception:"+exception.getMessage());
}
result.setMessage(stringBuilder.toString());
return result;
}
public RequestBody generateRESTRequestBody() throws IOException {
String jsonString = "{\"requests\": [{\"features\": [{\"type\": \"TEXT_DETECTION\",\"maxResults\": 1}],\"image\": {\"content\": \""+getScreenshotAsB64(getScreenShot())+"\"}}]}";
return RequestBody.create(JSON,jsonString);
}
public Response getTextFromImageUsingGCVREST(String apiKey) throws IOException {
Request postRequest = new Request.Builder()
.url("https://vision.googleapis.com/v1/images:annotate?key="+apiKey)
.post(generateRESTRequestBody())
.build();
Response response = okHttpClient.newCall(postRequest).execute();
return response;
}
public String getScreenshotAsB64(File screenshot) throws IOException {
byte[] fileContent = FileUtils.readFileToByteArray(screenshot);
return Base64.getEncoder().encodeToString(fileContent);
}
public File getScreenShot() throws IOException {
File screenshotFile = File.createTempFile("TSScreenshot_",".png");
FileUtils.copyFile(((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE), screenshotFile);
//stringBuilder.append("\nScreenshot copied to "+screenshotFile.getAbsolutePath());
//stringBuilder.append("==============");
return screenshotFile;
}
}