Skip to content

Commit 8cb990b

Browse files
author
Mat Walker
committed
Added lots of Element finding tests and fixed errors
1 parent 9fb8a09 commit 8cb990b

File tree

3 files changed

+26
-24
lines changed

3 files changed

+26
-24
lines changed

src/main/java/TeamControlium/Controlium/HTMLElement.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ public HTMLElement() {
3939

4040
public HTMLElement(Object parent, WebElement underlyingWebElement, ObjectMapping mapping) {
4141
setParentOfThisElement(parent);
42-
setSeleniumWebElement(underlyingWebElement);
4342
setMappingDetails(mapping);
43+
setSeleniumWebElement(underlyingWebElement);
44+
4445
}
4546

4647

src/main/java/TeamControlium/Controlium/ObjectMapping.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ private void InitObjectMap(String findLogic, String friendlyName)
3232
{
3333
this._findLogicOriginal = findLogic;
3434
_friendlyNameOriginal = (friendlyName==null) ? findLogic : friendlyName;
35-
_friendlyName = null;
35+
_friendlyName = _friendlyNameOriginal;
3636
}
3737

3838

39-
public ByType getMappingType() { return _mappingType; }
39+
public ByType getMappingType() { if (_mappingType==null) processFindLogic((_findLogicActual==null)?_findLogicOriginal:_findLogicActual); return _mappingType; }
4040

4141

4242

@@ -97,17 +97,7 @@ public String getFriendlyName() {
9797
public ObjectMapping copy() {
9898
try {
9999
//
100-
// We do a dodgy here. When copying a mapping we want to reset the Actual Find Logic as the copy has not yet been used
101-
// and may be part of being built (IE. from FindElements). So we want to clear the Actual Find Logic which cannot be done
102-
// publically (obviously!). So, we temporarily clear the actual find logic and then put it back after the clone...
103-
//
104-
// Note. In Java Strings are immutable, so doing the assignment works and is safe. Note though that this no way
105-
// thread-safe!!
106-
//
107-
String originalActualFindLogic = _findLogicActual;
108-
_findLogicActual=null;
109-
ObjectMapping clone = (ObjectMapping)this.clone();
110-
_findLogicActual = originalActualFindLogic;
100+
ObjectMapping clone = new ObjectMapping(_findLogicActual,_friendlyName);
111101
return clone;
112102
}
113103
catch (Exception e) {

src/main/java/TeamControlium/Controlium/SeleniumDriver.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,10 @@ public HTMLElement findElement(HTMLElement parentElement,ObjectMapping objectMap
197197

198198
Logger.WriteLine(Logger.LogLevels.TestDebug, "Timeout - %s", durationFormatted(timeout));
199199

200+
StopWatch timer = StopWatch.createStarted();
200201
while (true) {
201202

202-
StopWatch timer = StopWatch.createStarted();
203+
203204

204205
// Loop while:-
205206
// - we have no find results
@@ -226,7 +227,7 @@ public HTMLElement findElement(HTMLElement parentElement,ObjectMapping objectMap
226227

227228
}
228229
if (clauseResults.size() > 1 && !allowMultipleMatches) {
229-
String errorText = String.format("Found %d matching elements using ([%s] - %s) from [%s] (Waited upto %dmS). allowMultipleMatches=false so error!",
230+
String errorText = String.format("Found %d matching elements using [%s] ([%s]) from [%s]. Not allowing mutiple matches and timeout reached after [%dmS]",
230231
clauseResults.size(),
231232
objectMapping.getActualFindLogic(),
232233
objectMapping.getFriendlyName(),
@@ -258,18 +259,18 @@ public HTMLElement findElement(HTMLElement parentElement,ObjectMapping objectMap
258259
break;
259260
} else {
260261
if (timer.getTime()>=totalTimeoutMillis) {
261-
Logger.WriteLine(Logger.LogLevels.Error, "From [%s], find [%s (%s)] returned %d matches (%s multiple matches). We must wait until stable, element 0 is NOT stable and timeout reached so throwing",
262+
Logger.WriteLine(Logger.LogLevels.Error, "From [%s], find [%s (%s)] returned %d matches (%sllowing multiple matches). Element NOT stable after timeout reached so throwing",
262263
(parentElement == null) ? "DOM Top Level" : parentElement.getMappingDetails().getFriendlyName(),
263264
objectMapping.getActualFindLogic(),
264265
objectMapping.getFriendlyName(),
265266
clauseResults.size(),
266-
(allowMultipleMatches) ? "Allowing" : "Not");
267-
throw new RuntimeException(String.format("From [%s], find [%s (%s)] returned %d matches (%s multiple matches). We must wait until stable and element 0 NOT stable and timeout reached after %dmS.",
267+
(allowMultipleMatches) ? "A" : "Not A");
268+
throw new RuntimeException(String.format("From [%s], find [%s (%s)] returned %d matches (%sllowing multiple matches). Element NOT stable after timeout reached ([%dmS]).",
268269
(parentElement == null) ? "DOM Top Level" : parentElement.getMappingDetails().getFriendlyName(),
269270
objectMapping.getActualFindLogic(),
270271
objectMapping.getFriendlyName(),
271272
clauseResults.size(),
272-
(allowMultipleMatches) ? "Allowing" : "Not",
273+
(allowMultipleMatches) ? "A" : "Not A",
273274
timer.getTime()));
274275
}
275276
Logger.WriteLine(Logger.LogLevels.TestDebug, "From [%s], find [%s (%s)] returned %d matches (%s multiple matches). Element 0 is NOT stable and we must wait until stable...",
@@ -279,6 +280,8 @@ public HTMLElement findElement(HTMLElement parentElement,ObjectMapping objectMap
279280
clauseResults.size(),
280281
(allowMultipleMatches) ? "Allowing" : "Not");
281282
}
283+
} else {
284+
break;
282285
}
283286
}
284287
return clauseResults.get(0);
@@ -301,7 +304,15 @@ public List<HTMLElement> findElements(HTMLElement parentElement, ObjectMapping m
301304

302305
try {
303306
Logger.WriteLine(Logger.LogLevels.FrameworkDebug,"Calling Selenium WebDriver findElements with By = [%s]",seleniumFindBy.toString());
304-
foundElements = (parentElement==null) ? webDriver.findElements(seleniumFindBy) : parentElement.getSeleniumnWebElement().findElements(seleniumFindBy);
307+
if (parentElement==null) {
308+
foundElements = webDriver.findElements(seleniumFindBy);
309+
}
310+
else {
311+
foundElements=parentElement.getSeleniumnWebElement().findElements(seleniumFindBy);
312+
}
313+
}
314+
catch (InvalidSelectorException e) {
315+
throw new RuntimeException(String.format("Selenium Driver error. Find Logic [%s] for [%s] is invalid!",mapping.getActualFindLogic(),mapping.getFriendlyName(),e));
305316
}
306317
catch (WebDriverException e)
307318
{
@@ -320,9 +331,9 @@ public List<HTMLElement> findElements(HTMLElement parentElement, ObjectMapping m
320331
}
321332

322333
if (parentElement==null)
323-
Logger.WriteLine(Logger.LogLevels.TestInformation,"Found [%d] elements matching [%s] (%s)",foundElements.size(),mapping.getOriginalFindLogic(),mapping.getFriendlyName());
334+
Logger.WriteLine(Logger.LogLevels.FrameworkDebug,"Found [%d] elements matching [%s] (%s)",foundElements.size(),mapping.getOriginalFindLogic(),mapping.getFriendlyName());
324335
else
325-
Logger.WriteLine(Logger.LogLevels.TestInformation,"Found [%d] elements matching [%s] (%s) offset from [%s]",foundElements.size(),mapping.getOriginalFindLogic(),mapping.getFriendlyName(),parentElement.getMappingDetails().getFriendlyName());
336+
Logger.WriteLine(Logger.LogLevels.FrameworkDebug,"Found [%d] elements matching [%s] (%s) offset from [%s]",foundElements.size(),mapping.getOriginalFindLogic(),mapping.getFriendlyName(),parentElement.getMappingDetails().getFriendlyName());
326337

327338

328339
for(int index = 0;index<foundElements.size();index++) {
@@ -797,7 +808,7 @@ private String CallingMethodDetails(StackTraceElement methodBase)
797808
private void checkIfConnectionIssue(Exception e) throws RuntimeException {
798809
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
799810
StackTraceElement caller = stackTraceElements[2];
800-
if (e.getCause().getClass()==ConnectException.class) {
811+
if (e.getClass()==ConnectException.class) {
801812
throw new RuntimeException(String.format("Selenium Driver method [%s] called but Selenium WebDriver not connected!",CallingMethodDetails(caller)),e);
802813
}
803814
}

0 commit comments

Comments
 (0)