-
-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: removed code smells and refactored project structure #141
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,7 +44,7 @@ public class HtmlUnitAlert implements Alert { | |
|
||
private final HtmlUnitDriver driver_; | ||
private AlertHolder holder_; | ||
private boolean quitting_; | ||
private boolean autoDismissOnQuit; | ||
private final Lock lock_ = new ReentrantLock(); | ||
private final Condition condition_ = lock_.newCondition(); | ||
private WebWindow webWindow_; | ||
|
@@ -60,7 +60,7 @@ public class HtmlUnitAlert implements Alert { | |
} | ||
|
||
private void alertHandler(final Page page, final String message) { | ||
if (quitting_) { | ||
if (autoDismissOnQuit) { | ||
return; | ||
} | ||
webWindow_ = page.getEnclosingWindow(); | ||
|
@@ -69,7 +69,7 @@ private void alertHandler(final Page page, final String message) { | |
} | ||
|
||
private boolean confirmHandler(final Page page, final String message) { | ||
if (quitting_) { | ||
if (autoDismissOnQuit) { | ||
return false; | ||
} | ||
webWindow_ = page.getEnclosingWindow(); | ||
|
@@ -83,21 +83,25 @@ private void awaitCondition() { | |
lock_.lock(); | ||
try { | ||
if (driver_.isProcessAlert()) { | ||
try { | ||
condition_.await(5, TimeUnit.SECONDS); | ||
} | ||
catch (final InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
waitFor(5); | ||
} | ||
} | ||
finally { | ||
lock_.unlock(); | ||
} | ||
} | ||
|
||
private void waitFor(int time) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any plans to reuse this somewhere else? |
||
try { | ||
condition_.await(time, TimeUnit.SECONDS); | ||
} | ||
catch (final InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private String promptHandler(final Page page, final String message, final String defaultMessage) { | ||
if (quitting_) { | ||
if (autoDismissOnQuit) { | ||
return null; | ||
} | ||
webWindow_ = page.getEnclosingWindow(); | ||
|
@@ -108,7 +112,7 @@ private String promptHandler(final Page page, final String message, final String | |
} | ||
|
||
private boolean onbeforeunloadHandler(final Page page, final String returnValue) { | ||
if (quitting_) { | ||
if (autoDismissOnQuit) { | ||
return true; | ||
} | ||
webWindow_ = page.getEnclosingWindow(); | ||
|
@@ -123,7 +127,7 @@ WebWindow getWebWindow() { | |
} | ||
|
||
public void setAutoAccept(final boolean autoAccept) { | ||
this.quitting_ = autoAccept; | ||
this.autoDismissOnQuit = autoAccept; | ||
} | ||
|
||
public void handleBrowserCapabilities(final Capabilities capabilities) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -187,12 +187,14 @@ public WebElement activeElement() { | |
@Override | ||
public Alert alert() { | ||
final HtmlUnitAlert alert = driver_.getAlert(); | ||
final int SLEEP_TIME = 50; | ||
final int NUMBER_OF_TRIES = 5; | ||
|
||
if (!alert.isLocked()) { | ||
for (int i = 0; i < 5; i++) { | ||
for (int i = 0; i < NUMBER_OF_TRIES; i++) { | ||
if (!alert.isLocked()) { | ||
try { | ||
Thread.sleep(50); | ||
Thread.sleep(SLEEP_TIME); | ||
} | ||
catch (final InterruptedException e) { | ||
throw new RuntimeException(e); | ||
|
@@ -208,13 +210,18 @@ public Alert alert() { | |
final WebWindow alertWindow = alert.getWebWindow(); | ||
final WebWindow currentWindow = driver_.getCurrentWindow().getWebWindow(); | ||
|
||
if (alertWindow != currentWindow && !isChild(currentWindow, alertWindow) | ||
&& !isChild(alertWindow, currentWindow)) { | ||
if (isAlertTimedOut(alertWindow , currentWindow)) { | ||
throw new TimeoutException(); | ||
} | ||
|
||
return alert; | ||
} | ||
|
||
private static boolean isAlertTimedOut(WebWindow alertWindow , WebWindow currentWindow) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looks a bit strange, when checking the method for figuring out why the alerts timed out i will not see anything regarding a timeout - maybe a better name? |
||
return (alertWindow != currentWindow && !isChild(currentWindow, alertWindow) | ||
&& !isChild(alertWindow, currentWindow)); | ||
} | ||
|
||
private static boolean isChild(final WebWindow parent, final WebWindow potentialChild) { | ||
for (WebWindow child = potentialChild; child != null; child = child.getParentWindow()) { | ||
if (child == parent) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,13 +81,6 @@ | |
*/ | ||
public class HtmlUnitWebElement implements WrapsDriver, WebElement, Coordinates, Locatable { | ||
|
||
private static final String[] booleanAttributes = {"async", "autofocus", "autoplay", "checked", "compact", | ||
"complete", "controls", "declare", "defaultchecked", "defaultselected", "defer", "disabled", "draggable", | ||
"ended", "formnovalidate", "hidden", "indeterminate", "iscontenteditable", "ismap", "itemscope", "loop", | ||
"multiple", "muted", "nohref", "noresize", "noshade", "novalidate", "nowrap", "open", "paused", "pubdate", | ||
"readonly", "required", "reversed", "scoped", "seamless", "seeking", "selected", "spellcheck", "truespeed", | ||
"willvalidate"}; | ||
|
||
private final HtmlUnitDriver driver_; | ||
private final int id_; | ||
private final DomElement element_; | ||
|
@@ -255,7 +248,7 @@ void switchFocusToThisIfNeeded() { | |
final boolean jsEnabled = driver_.isJavascriptEnabled(); | ||
final boolean oldActiveEqualsCurrent = oldActiveElement.equals(this); | ||
try { | ||
final boolean isBody = oldActiveElement.getTagName().toLowerCase().equals("body"); | ||
final boolean isBody = oldActiveElement.getTagName().equalsIgnoreCase("body"); | ||
if (jsEnabled && !oldActiveEqualsCurrent && !isBody) { | ||
oldActiveElement.element_.blur(); | ||
} | ||
|
@@ -285,6 +278,12 @@ public String getAttribute(final String name) { | |
assertElementNotStale(); | ||
|
||
final String lowerName = name.toLowerCase(); | ||
final String[] booleanAttributes = {"async", "autofocus", "autoplay", "checked", "compact", | ||
"complete", "controls", "declare", "defaultchecked", "defaultselected", "defer", "disabled", "draggable", | ||
"ended", "formnovalidate", "hidden", "indeterminate", "iscontenteditable", "ismap", "itemscope", "loop", | ||
"multiple", "muted", "nohref", "noresize", "noshade", "novalidate", "nowrap", "open", "paused", "pubdate", | ||
"readonly", "required", "reversed", "scoped", "seamless", "seeking", "selected", "spellcheck", "truespeed", | ||
"willvalidate"}; | ||
|
||
if (element_ instanceof HtmlInput && ("selected".equals(lowerName) || "checked".equals(lowerName))) { | ||
return trueOrNull(((HtmlInput) element_).isChecked()); | ||
|
@@ -410,21 +409,11 @@ public String getDomProperty(final String name) { | |
return ScriptRuntime.toCharSequence(ScriptableObject.getProperty(scriptable, lowerName)).toString(); | ||
} | ||
|
||
// js disabled, fallback to some hacks | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a can't see any improvement from these changes in terms of readability |
||
if ("disabled".equals(lowerName)) { | ||
if (element_ instanceof DisabledElement) { | ||
return trueOrFalse(((DisabledElement) element_).isDisabled()); | ||
} | ||
} | ||
String element_1 = disabledJsCheck(lowerName); | ||
if (element_1 != null) return element_1; | ||
|
||
if ("checked".equals(lowerName)) { | ||
if (element_ instanceof HtmlCheckBoxInput) { | ||
return trueOrFalse(((HtmlCheckBoxInput) element_).isChecked()); | ||
} | ||
else if (element_ instanceof HtmlRadioButtonInput) { | ||
return trueOrFalse(((HtmlRadioButtonInput) element_).isChecked()); | ||
} | ||
} | ||
String element_2 = isElementChecked(lowerName); | ||
if (element_2 != null) return element_2; | ||
|
||
final String value = element_.getAttribute(lowerName); | ||
if (ATTRIBUTE_NOT_DEFINED == value) { | ||
|
@@ -438,6 +427,27 @@ else if (element_ instanceof HtmlRadioButtonInput) { | |
return value; | ||
} | ||
|
||
private String isElementChecked(String lowerName) { | ||
if ("checked".equals(lowerName)) { | ||
if (element_ instanceof HtmlCheckBoxInput) { | ||
return trueOrFalse(((HtmlCheckBoxInput) element_).isChecked()); | ||
} | ||
else if (element_ instanceof HtmlRadioButtonInput) { | ||
return trueOrFalse(((HtmlRadioButtonInput) element_).isChecked()); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private String disabledJsCheck(String lowerName) { | ||
if ("disabled".equals(lowerName)) { | ||
if (element_ instanceof DisabledElement) { | ||
return trueOrFalse(((DisabledElement) element_).isDisabled()); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public String getDomAttribute(final String name) { | ||
assertElementNotStale(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
better naming makes sense but please stay with the overall rule to add a _ at the end