forked from vkolgi/tuneup_js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_assertion.js
65 lines (51 loc) · 2.09 KB
/
image_assertion.js
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
var ImageAsserter = (function() {
function ImageAsserter(tuneUpPath, outputPath, refImagesPath) {
if (!outputPath || !refImagesPath || !tuneUpPath) {
throw new AssertionException("output, refImages, tuneUp pathes can't be null");
}
this.tuneUpPath = tuneUpPath;
this.outputPath = outputPath;
this.refImagesPath = refImagesPath;
var target = UIATarget.localTarget();
if (!target) {
throw new AssertionException("unable to get localTarget");
}
this.host = target.host();
if (!this.host) {
throw new AssertionException("unable to get current UAIHost");
}
}
ImageAsserter.prototype.assertImageNamed = function(imageName, threshold) {
var command,
taskResult,
assertSuccessfull = false,
SUCCESS_EXIT_CODE = 0,
TIMEOUT = 5,
args = [this.outputPath, this.refImagesPath, imageName, threshold];
command = this.tuneUpPath + '/image_asserter';
taskResult = this.host.performTaskWithPathArgumentsTimeout(command,
args,
TIMEOUT);
assertSuccessful = (taskResult.exitCode === SUCCESS_EXIT_CODE);
if (!assertSuccessful) {
UIALogger.logError(taskResult.stderr);
}
return assertSuccessful;
};
return ImageAsserter;
}());
function createImageAsserter(tuneUpPath, outputPath, refImagesPath) {
this.imageAsserter = new ImageAsserter(tuneUpPath, outputPath, refImagesPath);
}
function assertScreenMatchesImageNamed(imageName, message, threshold) {
if (!this.imageAsserter) {
throw new AssertionException("imageAsserter isn't created.");
}
UIATarget.localTarget().captureAppScreenWithName(imageName);
UIATarget.localTarget().delay(1); // delay for screenshot to be saved
var assertionPassed = this.imageAsserter.assertImageNamed(imageName, threshold);
if (!assertionPassed) {
if (!message) message = 'Assertion of the image ' + imageName + ' failed.';
throw new AssertionException(message);
}
}