forked from jonathanpenn/ui-auto-monkey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buttonHandler.js
131 lines (116 loc) · 4.78 KB
/
buttonHandler.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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright (c) 2015 Yahoo inc. (http://www.yahoo-inc.com)
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
"use strict";
//Conforms to the ConditionHandler protocol in UIAutoMonkey
//Usage
// var handlers = [ ];
// var handlerInterval = 20; //every how many events to process. Can vary by each handler, but often useful to group them
// handlers.push(new ButtonHandler("Done", handlerInterval, false)); //every 20 events, press "Done" button if found as a top level button (no nav bar).
// ...
// config.conditionHandlers = handlers
//
function ButtonHandler(buttonName, checkEveryNumber, useNavBar, optionalIsTrueFunction) {
this.buttonName = buttonName;
this.checkEveryNumber = checkEveryNumber || 10;
if (useNavBar == undefined) {
useNavBar = true;
};
this.useNavBar = useNavBar;
this.optionalIsTrueFunction = optionalIsTrueFunction || null;
//stats
this.statsIsTrueInvokedCount = 0;
this.statsIsTrueReturnedTrue = 0;
this.statsIsTrueReturnedFalse = 0;
this.statsHandleInvokedCount = 0;
this.statsHandleNotValidAndVisibleCount = 0;
this.statsHandleErrorCount = 0;
this.debugLogElementTreeOnIsTrue = false;
}
ButtonHandler.prototype.setDebugLogElementTreeOnIsTrue = function(aBool) {
this.debugLogElementTreeOnIsTrue = aBool;
}
ButtonHandler.prototype.isObjectNil = function(anObject) {
return anObject.toString() == "[object UIAElementNil]"
};
ButtonHandler.prototype.isValidAndVisible = function(aButtonOrNil) {
if (this.isObjectNil(aButtonOrNil)) {
return false;
}
//we now expect aButtonOrNil to be a button
return aButtonOrNil.checkIsValid() && aButtonOrNil.isVisible();
}
// return true if we our button is visible
ButtonHandler.prototype.isTrue = function(target, eventCount, mainWindow) {
this.statsIsTrueInvokedCount++;
var result;
if (this.optionalIsTrueFunction == null) {
if (this.debugLogElementTreeOnIsTrue) {
UIATarget.localTarget().logElementTree();
}
var aButton = this.findButton(target);
result = this.isValidAndVisible(aButton);
} else {
result = this.optionalIsTrueFunction(target, eventCount, mainWindow);
}
if (result) {
this.statsIsTrueReturnedTrue++;
} else {
this.statsIsTrueReturnedFalse++;
};
return result;
};
ButtonHandler.prototype.findButton = function(target) {
return this.useNavBar ?
target.frontMostApp().mainWindow().navigationBar().buttons()[this.buttonName] :
target.frontMostApp().mainWindow().buttons()[this.buttonName];
};
//every checkEvery() number of events our isTrue() method will be queried.
ButtonHandler.prototype.checkEvery = function() {
return this.checkEveryNumber;
};
// if true then after we handle an event consider the particular Monkey event handled, and don't process the other condition handlers.
ButtonHandler.prototype.isExclusive = function() {
return true;
};
// Press our button
ButtonHandler.prototype.handle = function(target, mainWindow) {
this.statsHandleInvokedCount++;
var button = this.findButton(target);
if (this.isValidAndVisible(button)) {
try{
button.tap();
} catch(err) {
this.statsHandleErrorCount++;
UIALogger.logWarning(err);
}
} else {
this.statsHandleNotValidAndVisibleCount++
//UIALogger.logWarning(this.toString() + " button is not validAndVisible");
};
};
ButtonHandler.prototype.toString = function() {
return ["MonkeyTest::ButtonHandler(" + this.buttonName, this.checkEveryNumber, this.useNavBar, ")"].join();
};
ButtonHandler.prototype.logStats = function() {
UIALogger.logDebug([this.toString(),
"IsTrueInvokedCount", this.statsIsTrueInvokedCount,
"IsTrueReturnedTrue", this.statsIsTrueReturnedTrue,
"IsTrueReturnedFalse", this.statsIsTrueReturnedFalse,
"HandleInvokedCount", this.statsHandleInvokedCount,
"HandleNotValidAndVisibleCount", this.statsHandleNotValidAndVisibleCount,
"HandleErrorCount", this.statsHandleErrorCount].join());
};