Skip to content

Commit b193a21

Browse files
committed
Added methods to ControlBase - Not ready
1 parent 5783fb4 commit b193a21

File tree

3 files changed

+274
-7
lines changed

3 files changed

+274
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package TeamControlium.Controlium;
22

33

4+
import TeamControlium.Utilities.Logger;
5+
import org.apache.commons.lang3.time.StopWatch;
46
import org.apache.http.MethodNotSupportedException;
57

68
import java.util.Objects;
@@ -13,51 +15,230 @@ public abstract class ControlBase {
1315

1416

1517
private ObjectMapping _Mapping;
18+
1619
public ObjectMapping getMapping() {
1720
return _Mapping;
1821
}
22+
1923
protected ObjectMapping setMapping(ObjectMapping mapping) {
20-
_Mapping=mapping;
24+
_Mapping = mapping;
2125
return _Mapping;
2226
}
2327

2428
private HTMLElement _RootElement;
29+
2530
public HTMLElement getRootElement() {
2631
if (_RootElement == null) {
27-
throw new RuntimeException(String.format("Control [%s] root element NULL. Has control been located on the page (IE. SetControl(...)?",getMapping().getFriendlyName()));
28-
}
29-
else {
32+
throw new RuntimeException(String.format("Control [%s] root element NULL. Has control been located on the page (IE. SetControl(...)?", getMapping().getFriendlyName()));
33+
} else {
3034
return _RootElement;
3135
}
3236
}
37+
3338
protected HTMLElement setRootElement(HTMLElement element) {
3439
_RootElement = element;
3540
setMapping(element.getMappingDetails());
3641
return _RootElement;
3742
}
43+
3844
protected HTMLElement setRootElement(ObjectMapping mapping) {
3945
setRootElement(new HTMLElement(mapping));
4046
return _RootElement;
4147
}
4248

4349
private SeleniumDriver _SeleniumDriver;
50+
4451
public SeleniumDriver getSeleniumDriver() {
4552
return _SeleniumDriver;
4653
}
47-
private SeleniumDriver setSeleniumDriver(SeleniumDriver seleniumDriver) {
54+
55+
public SeleniumDriver setSeleniumDriver(SeleniumDriver seleniumDriver) {
4856
_SeleniumDriver = seleniumDriver;
4957
return _SeleniumDriver;
5058
}
5159

60+
private ControlBase _parentControl;
61+
62+
public ControlBase getParentControl() {
63+
return _parentControl;
64+
}
65+
66+
public ControlBase setParentControl(ControlBase parentControl) {
67+
_parentControl = parentControl;
68+
return _parentControl;
69+
}
70+
71+
5272
public static void clearCache() {
5373
throw new RuntimeException("No caching implemented yet!");
5474
}
5575

5676

5777
//
58-
// Sets on a child control with the childs find logic being applied this controls root element.
78+
// Sets on a child control with the child's find logic being applied to this controls root element.
5979
// Method can be overridden by control implementations.
6080
//
81+
public <T extends ControlBase> T setControl(T newControl) {
82+
return setControl(this, newControl);
83+
}
84+
85+
public static <T extends ControlBase> T setControl(ControlBase parentControl, T newControl) {
86+
return setControl(parentControl.getSeleniumDriver(), parentControl, newControl);
87+
}
88+
public static <T extends ControlBase> T setControl(SeleniumDriver seleniumDriver, T newControl) {
89+
return setControl(seleniumDriver, null, newControl);
90+
}
91+
public static <T extends ControlBase> T setControl(SeleniumDriver seleniumDriver, ControlBase parentControl, T newControl) {
92+
if (newControl == null) throw new RuntimeException("newControl Null!");
93+
94+
StopWatch timeWaited = StopWatch.createStarted();
95+
96+
try {
97+
Logger.WriteLine(Logger.LogLevels.TestInformation, "Setting on Control [%s] from Parent [%s]",
98+
newControl.getMapping() == null ? "No mapping logic!" : newControl.getMapping().getFriendlyName(),
99+
parentControl == null ? "No parent Control - So Top Level control" : parentControl.getMapping() == null ? "No mapping logic!" : parentControl.getMapping().getFriendlyName());
100+
101+
//
102+
// Check if ParentControl has become stale (has been redrawn). If so, refresh it (force a new findElement on it). Note that this
103+
// will effectively ripple up to the top level
104+
//
105+
if (parentControl != null && parentControl.isStale()) {
106+
Logger.WriteLine(Logger.LogLevels.TestInformation, "Parent control is stale. Refreshing");
107+
parentControl.setRootElement((HTMLElement) null);
108+
ControlBase refreshedParentControl = ControlBase.SetControl(parentControl.getSeleniumDriver(), parentControl.getParentControl(), parentControl);
109+
parentControl = refreshedParentControl;
110+
}
111+
112+
//
113+
// We may just be wrapping an Element in a Control that has already been found. In which case, dont bother
114+
// to do a find for it....
115+
//
116+
if (newControl.getRootElement() == null || !newControl.getRootElement().isBoundToAWebElement()) {
117+
Logger.WriteLine(Logger.LogLevels.TestDebug, "New control root element is null or unbound to a Selenium element. So finding using Control mapping");
118+
119+
ControlFindElement finder = FindToUse(SeleniumDriver, ParentControl);
120+
HTMLElement element = FindControlRootElement(finder, newControl.getMapping());
121+
newControl.setRootElement(element);
122+
}
123+
124+
//
125+
// Populate new Control object.
126+
//
127+
newControl.setSeleniumDriver(seleniumDriver);
128+
newControl.setParentControl(parentControl); // This may be null. So, new control is top level....
129+
130+
//
131+
// THIS IS WHERE THE CACHE CONTROL WILL BE DONE.... No NEED FOR MVP. WE ARE SOOOO AGILE! lol
132+
// For now, we will just assume Cache Miss
133+
//
134+
newControl.controlBeingSet(true);
135+
136+
return newControl;
137+
} catch (Exception e) {
138+
Logger.WriteLine(Logger.LogLevels.Error, "Error setting on control: %s", e.getMessage());
139+
throw new RuntimeException(String.format("Error setting on control: %s", e.getMessage()));
140+
}
141+
}
142+
143+
//
144+
// All Controls must implement a ControlBeingSet. This is called when the Control is set upon (IE. Find logic applied and bound to a Selenium element). It really
145+
// will become useful when caching is implemented. It is used by a Control to do stuff when located in the Dom - IE. A dropdown control may click on it whenever
146+
// SET on to expose the dropdown...
147+
//
148+
abstract void controlBeingSet(boolean isFirstSetting);
61149

150+
public void clearElement(ObjectMapping mapping) {
151+
findElement(mapping).clear();
152+
}
153+
public void clearElement() {
154+
getRootElement().clear();
155+
}
156+
157+
//
158+
// Click root element of Control. Most times controls will override this and click on the most appropriate
159+
// element in the control
160+
//
161+
public void click() {
162+
getRootElement().click();
163+
}
164+
public void click(ObjectMapping mapping) {
165+
findElement(mapping).click();
166+
}
167+
168+
public void clickIfExists(ObjectMapping mapping) {
169+
HTMLElement element;
170+
if ((element = getRootElement().findElementOrNull(mapping))==null) {
171+
Logger.WriteLine(Logger.LogLevels.TestInformation, "[%s] didnt find any match. No click.",mapping==null?"Null mapping!":mapping.getFriendlyName());
172+
}
173+
else {
174+
element.click();
175+
}
176+
}
177+
178+
public String getAttribute(ObjectMapping mapping, String attributeName) {
179+
HTMLElement element = findElement(mapping);
180+
try {
181+
return element.getAttribute(attributeName);
182+
}
183+
catch (Exception e) {
184+
return "";
185+
}
186+
}
187+
public String getAttribute(String attributeName) {
188+
try {
189+
return getRootElement().getAttribute(attributeName);
190+
}
191+
catch (Exception e) {
192+
return "";
193+
}
194+
}
195+
196+
public boolean hasAttribute(ObjectMapping mapping, String attributeName) {
197+
HTMLElement element = findElement(mapping);
198+
try {
199+
return element.hasAttribute(attributeName);
200+
}
201+
catch (Exception e) {
202+
return false;
203+
}
204+
}
205+
public boolean hasAttribute(String attributeName) {
206+
try {
207+
return getRootElement().hasAttribute(attributeName);
208+
}
209+
catch (Exception e) {
210+
return false;
211+
}
212+
}
213+
214+
public String getText(ObjectMapping mapping, String attributeName) {
215+
HTMLElement element = findElement(mapping);
216+
try {
217+
return element.getText();
218+
}
219+
catch (Exception e) {
220+
return "";
221+
}
222+
}
223+
public String getText(String attributeName) {
224+
try {
225+
return getRootElement().getText();
226+
}
227+
catch (Exception e) {
228+
return "";
229+
}
230+
}
231+
232+
public void setText(ObjectMapping mapping, String text) {
233+
HTMLElement element = findElement(mapping);
234+
element.setText(text);
235+
}
236+
public void setText(String text) {
237+
getRootElement().setText(text);
238+
}
239+
240+
//
241+
// MAT CARRY ON FIND TO USE...
242+
//
62243

63244
}

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

+34
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,26 @@ public HTMLElement findElement(ObjectMapping mapping,boolean waitUntilSingle,boo
340340
return getSeleniumDriver().findElement(this,mapping,waitUntilSingle,waitUntilStable);
341341
}
342342

343+
public HTMLElement findElementOrNull(ObjectMapping mapping) {
344+
// We can only do this if we have an instance of SeleniumDriver
345+
throwIfUnbound();
346+
return getSeleniumDriver().findElementOrNull(this,mapping);
347+
}
348+
public HTMLElement findElementOrNull(ObjectMapping mapping,boolean allowMultipleMatches) {
349+
// We can only do this if we have an instance of SeleniumDriver
350+
throwIfUnbound();
351+
return getSeleniumDriver().findElementOrNull(this,mapping,allowMultipleMatches);
352+
}
353+
public HTMLElement findElementOrNull(ObjectMapping mapping,boolean waitUntilSingle,boolean waitUntilStable) {
354+
// We can only do this if we have an instance of SeleniumDriver
355+
throwIfUnbound();
356+
return getSeleniumDriver().findElementOrNull(this,mapping,waitUntilSingle,waitUntilStable);
357+
}
358+
public Exception getLastFindException() {
359+
return getSeleniumDriver().getLastException();
360+
}
361+
362+
343363
public boolean waitForHeightStable(Duration timeout) {
344364
return waitForElementStable(StabilityType.HEIGHT, timeout);
345365
}
@@ -519,6 +539,20 @@ public String getAttribute(String attribute) {
519539
}
520540
}
521541

542+
public boolean hasAttribute(String attribute) {
543+
throwIfUnbound();
544+
try {
545+
return getSeleniumDriver().hasAttribute(getUnderlyingWebElement(),attribute);
546+
}
547+
catch (InvalidElementState ex) {
548+
throw ex;
549+
}
550+
catch (Exception e) {
551+
Logger.WriteLine(Logger.LogLevels.TestDebug,"Error thrown getting attribute [%s] from [%s]. Assume does not have attribute: %s",getFriendlyName(),e.getMessage());
552+
return false;
553+
}
554+
}
555+
522556
public void click() {
523557
throwIfUnbound();
524558
try {

0 commit comments

Comments
 (0)