Skip to content

Commit

Permalink
Taking XML namespaces into account when searching by XPath. Checked t…
Browse files Browse the repository at this point in the history
…o work in Firefox. Chrome supports namespaces out of the box. Need to update IE and Safari drivers to use the updated atom and test them carefully.
  • Loading branch information
barancev committed Nov 2, 2014
1 parent d1f5eff commit 4f5f96d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
10 changes: 10 additions & 0 deletions java/client/test/org/openqa/selenium/ElementFindingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@
import static org.junit.Assume.assumeFalse;
import static org.openqa.selenium.testing.Ignore.Driver.ANDROID;
import static org.openqa.selenium.testing.Ignore.Driver.CHROME;
import static org.openqa.selenium.testing.Ignore.Driver.HTMLUNIT;
import static org.openqa.selenium.testing.Ignore.Driver.IE;
import static org.openqa.selenium.testing.Ignore.Driver.IPHONE;
import static org.openqa.selenium.testing.Ignore.Driver.MARIONETTE;
import static org.openqa.selenium.testing.Ignore.Driver.OPERA;
import static org.openqa.selenium.testing.Ignore.Driver.OPERA_MOBILE;
import static org.openqa.selenium.testing.Ignore.Driver.REMOTE;
import static org.openqa.selenium.testing.Ignore.Driver.SAFARI;
import static org.openqa.selenium.testing.TestUtilities.isOldIe;

import static org.hamcrest.Matchers.is;
Expand Down Expand Up @@ -399,6 +401,14 @@ public void testFindingALinkByXpathUsingContainsKeywordShouldWork() {
assertThat(element.getText(), containsString("hello world"));
}

@Ignore({ANDROID, HTMLUNIT, IE, IPHONE, OPERA, OPERA_MOBILE, MARIONETTE, SAFARI})
@Test
public void testShouldBeAbleToFindElementByXPathWithNamespace() {
driver.get(pages.svgPage);
WebElement element = driver.findElement(By.xpath("//svg:svg//svg:text"));
assertThat(element.getText(), is("Test Chart"));
}

// By.xpath negative

@Test(expected = NoSuchElementException.class)
Expand Down
28 changes: 25 additions & 3 deletions javascript/atoms/locators/xpath.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,31 @@ bot.locators.xpath.evaluate_ = function(node, path, resultType) {
}

try {
var resolver = doc.createNSResolver ?
doc.createNSResolver(doc.documentElement) :
bot.locators.xpath.DEFAULT_RESOLVER_;
var reversedNamespaces = {};
var allNodes = doc.getElementsByTagName("*");
for (var i = 0; i < allNodes.length; ++i) {
var node = allNodes[i];
var ns = node.namespaceURI;
if (!reversedNamespaces[ns]) {
var prefix = node.lookupPrefix(ns);
if (!prefix) {
var m = ns.match('.*/(\\w+)/?$');
if (m) {
prefix = m[1];
} else {
prefix = 'xhtml';
}
}
reversedNamespaces[ns] = prefix;
}
}
var namespaces = {};
for (var key in reversedNamespaces) {
namespaces[reversedNamespaces[key]] = key;
}
var resolver = function(prefix) {
return namespaces[prefix] || null;
}
if (goog.userAgent.IE && !goog.userAgent.isVersionOrHigher(7)) {
// IE6, and only IE6, has an issue where calling a custom function
// directly attached to the document object does not correctly propagate
Expand Down

0 comments on commit 4f5f96d

Please sign in to comment.