From 09c9fc6c96d62d2696e07284566fc5673cfd26be Mon Sep 17 00:00:00 2001 From: hollingsworthd Date: Mon, 16 May 2016 16:34:25 -0400 Subject: [PATCH] getText() should not throw exception unless it's decoupled from use by other APIs; return empty string when not visible. Fixes #103 --- .../machinepublishers/jbrowserdriver/ElementServer.java | 9 ++++++--- .../jbrowserdriver/diagnostics/Test.java | 1 + .../jbrowserdriver/diagnostics/test.htm | 2 ++ 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/com/machinepublishers/jbrowserdriver/ElementServer.java b/src/com/machinepublishers/jbrowserdriver/ElementServer.java index 9e9887e5..5b391f57 100644 --- a/src/com/machinepublishers/jbrowserdriver/ElementServer.java +++ b/src/com/machinepublishers/jbrowserdriver/ElementServer.java @@ -493,9 +493,12 @@ public String getText() { new Sync() { @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 ""; } }); } diff --git a/src/com/machinepublishers/jbrowserdriver/diagnostics/Test.java b/src/com/machinepublishers/jbrowserdriver/diagnostics/Test.java index a43f55fc..7fc88dff 100644 --- a/src/com/machinepublishers/jbrowserdriver/diagnostics/Test.java +++ b/src/com/machinepublishers/jbrowserdriver/diagnostics/Test.java @@ -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 diff --git a/src/com/machinepublishers/jbrowserdriver/diagnostics/test.htm b/src/com/machinepublishers/jbrowserdriver/diagnostics/test.htm index 3536bd20..9cc6d779 100644 --- a/src/com/machinepublishers/jbrowserdriver/diagnostics/test.htm +++ b/src/com/machinepublishers/jbrowserdriver/diagnostics/test.htm @@ -15,12 +15,14 @@ anchor anchor anchor + invisible anchor
Testing
invisible
text.
+