Skip to content

Commit

Permalink
getText() should not throw exception unless it's decoupled from use b…
Browse files Browse the repository at this point in the history
…y other APIs; return empty string when not visible. Fixes #103
  • Loading branch information
hollingsworthd committed May 16, 2016
1 parent a504b88 commit 09c9fc6
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/com/machinepublishers/jbrowserdriver/ElementServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -493,9 +493,12 @@ public String getText() {
new Sync<String>() {
@Override
public String perform() {
validate(true);
Object text = node.getMember("innerText");
return text instanceof String ? ((String) text).trim() : "";
validate(false);
if ((Boolean) node.eval(IS_VISIBLE)) {
Object text = node.getMember("innerText");
return text instanceof String ? ((String) text).trim() : "";
}
return "";
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ public void run() {
test(rect.height == dimension.getHeight());
test("Testing\ntext.".equals(driver.findElement(By.id("text-node1")).getText()));
test("".equals(driver.findElement(By.id("text-node2")).getText()));
test("".equals(driver.findElement(By.id("text-node3")).getText()));

/*
* Cookie manager
Expand Down
2 changes: 2 additions & 0 deletions src/com/machinepublishers/jbrowserdriver/diagnostics/test.htm
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
<a href="1" id="anchor1">anchor</a>
<a href="2" id="anchor2">anchor</a>
<a href="/" id="anchor3">anchor</a>
<a style="display:none;">invisible anchor</a>
<a id="anchor4"></a>
<button onclick="javascript:alert('test-alert');confirm('test-confirm');document.getElementById('testspan').innerHTML=prompt('test-prompt');">test button</button>
<span id="testspan"></span>
<input type="text" id="text-input">
<div id="text-node1">Testing<div style="display:none;">invisible</div><div>text<span>.</span></div></div>
<div id="text-node2" />
<div id="text-node3" style="display:none;">invisible text node</div>
<script>
document.cookie ='jsCookieName1=jsCookieValue1';
document.cookie ='jsCookieName2=jsCookieValue2';
Expand Down

0 comments on commit 09c9fc6

Please sign in to comment.