-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathAssertion.java
More file actions
162 lines (144 loc) · 5.83 KB
/
Copy pathAssertion.java
File metadata and controls
162 lines (144 loc) · 5.83 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package jp.vmi.selenium.selenese.command;
import java.util.Arrays;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.commons.text.StringEscapeUtils;
import org.openqa.selenium.NotFoundException;
import org.openqa.selenium.StaleElementReferenceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import jp.vmi.selenium.selenese.Context;
import jp.vmi.selenium.selenese.result.Failure;
import jp.vmi.selenium.selenese.result.Result;
import jp.vmi.selenium.selenese.result.Warning;
import jp.vmi.selenium.selenese.subcommand.ISubCommand;
import jp.vmi.selenium.selenese.utils.SeleniumUtils;
import static jp.vmi.selenium.selenese.command.ArgumentType.*;
import static jp.vmi.selenium.selenese.result.Success.*;
/**
* Commands of "assert*", "verify*", and "waitFor*".
*/
public class Assertion extends AbstractCommand {
private static final Logger log = LoggerFactory.getLogger(Assertion.class);
private static final int RETRY_INTERVAL = 100 /* ms */;
private enum Type {
ASSERT("assert"), // throw exception
VERIFY("verify"), // logging only
WAIT_FOR("waitFor"); // wait timeout
private String assertion;
private Type(String assertion) {
this.assertion = assertion;
}
public static Type of(String assertion) {
for (Type type : values())
if (type.assertion.equals(assertion))
return type;
return null;
}
}
private final Type type;
private final ISubCommand<?> getterSubCommand;
private final boolean isBoolean;
private final boolean isInverse;
private static ArgumentType[] getArgumentTypes(String assertion, ISubCommand<?> getterSubCommand, boolean isBoolean) {
ArgumentType[] argTypes = getterSubCommand.getArgumentTypes();
if (Type.WAIT_FOR.assertion.equals(assertion) || !isBoolean)
return ArrayUtils.add(argTypes, VALUE);
else
return argTypes;
}
Assertion(int index, String name, String[] args, String assertion, ISubCommand<?> getterSubCommand, boolean isBoolean,
boolean isInverse) {
super(index, name, args, getArgumentTypes(assertion, getterSubCommand, isBoolean));
this.type = Type.of(assertion);
this.getterSubCommand = getterSubCommand;
this.isBoolean = isBoolean;
this.isInverse = isInverse;
}
@Override
public boolean mayUpdateScreen() {
return false;
}
private int getTimeout(Context context, String[] args) {
if (type != Type.WAIT_FOR || args.length < 2)
return context.getTimeout();
int timeout = NumberUtils.toInt(args[1]);
return timeout > 0 ? timeout : context.getTimeout();
}
@Override
protected Result executeImpl(Context context, String... curArgs) {
String[] getterArgs;
String expected;
if (isBoolean) {
getterArgs = curArgs;
expected = null;
} else {
int newLen = getterSubCommand.getArgumentTypes().length;
getterArgs = Arrays.copyOf(curArgs, newLen);
expected = curArgs[newLen];
}
boolean found = true;
String message = null;
int timeout = getTimeout(context, curArgs);
long breakAfter = System.currentTimeMillis() + timeout;
while (true) {
found = true;
if (isBoolean) {
try {
boolean result = (Boolean) getterSubCommand.execute(context, getterArgs);
if (result ^ isInverse)
return SUCCESS;
message = String.format("Assertion failed (Result: [%s] / Expected: [%s])", result, !result);
} catch (NotFoundException | StaleElementReferenceException e) {
if (isInverse)
return SUCCESS;
message = String.format("Assertion failed (%s)", e.getMessage());
found = false;
}
} else {
try {
String resultString = SeleniumUtils.convertToString(getterSubCommand.execute(context, getterArgs));
if (SeleniumUtils.patternMatches(expected, resultString) ^ isInverse)
return SUCCESS;
message = String.format("Assertion failed (Result: [%s] / %sExpected: [%s])",
StringEscapeUtils.escapeJava(resultString),
isInverse ? "Not " : "", StringEscapeUtils.escapeJava(expected));
} catch (NotFoundException | StaleElementReferenceException e) {
if (isInverse)
return SUCCESS;
message = String.format("Assertion failed (%s)", e.getMessage());
found = false;
}
}
if (type != Type.WAIT_FOR || System.currentTimeMillis() > breakAfter)
break;
try {
Thread.sleep(RETRY_INTERVAL);
} catch (InterruptedException e) {
log.warn(e.getMessage());
break;
}
}
switch (type) {
case ASSERT:
return new Failure(message);
case VERIFY:
return found ? new Warning(message) : new Failure(message);
default: // == WAIT_FOR
String m = String.format("Timed out after %dms (%s)", timeout, message);
if (getSideCommand() != null)
return new Failure(m);
else
return new Warning(m);
}
}
@Override
public int getArgumentCount() {
int count = getterSubCommand.getArgumentCount();
if (!isBoolean)
count++;
if (type == Type.WAIT_FOR)
count++;
return count;
}
}