Last active
October 26, 2017 21:18
-
-
Save sauceaaron/f0461c831510bcfc0378e2ff707f7bd4 to your computer and use it in GitHub Desktop.
Demonstrate getting the source of an HTML table using WebDriver and using XPath to parse it locally to reduce the number of network requests.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.saucelabs.examples; | |
| import com.saucelabs.examples.util.Xpath; | |
| import org.openqa.selenium.By; | |
| import org.openqa.selenium.WebDriver; | |
| import org.openqa.selenium.remote.DesiredCapabilities; | |
| import org.openqa.selenium.remote.RemoteWebDriver; | |
| import org.testng.annotations.AfterMethod; | |
| import org.testng.annotations.BeforeMethod; | |
| import org.testng.annotations.Test; | |
| import java.net.MalformedURLException; | |
| import java.net.URL; | |
| public class TableTest | |
| { | |
| public static final String SAUCE_URL = "https://SAUCE_USERNAME:[email protected]:443/wd/hub"; | |
| public String SAUCE_USERNAME = System.getenv("SAUCE_USERNAME"); | |
| public String SAUCE_ACCESS_KEY = System.getenv("SAUCE_ACCESS_KEY"); | |
| public String SELENIUM_PLATFORM = "Windows 10"; | |
| public String SELENIUM_BROWSER = "Firefox"; | |
| public String SELENIUM_VERSION = "latest"; | |
| public String testName = "parse HTML table locally"; | |
| WebDriver driver; | |
| public static class PageWithTables | |
| { | |
| public static String URL = "https://www.w3schools.com/bootstrap/bootstrap_tables.asp"; | |
| public static By table = By.cssSelector("table.table-striped"); | |
| } | |
| @BeforeMethod | |
| public void setup() throws MalformedURLException | |
| { | |
| URL remoteUrl = getSauceURL(SAUCE_USERNAME, SAUCE_ACCESS_KEY); | |
| DesiredCapabilities capabilities = getDesiredCapabilities(SELENIUM_PLATFORM, SELENIUM_BROWSER, SELENIUM_VERSION); | |
| driver = new RemoteWebDriver(remoteUrl, capabilities); | |
| } | |
| @Test | |
| public void parseTableLocally() throws Exception | |
| { | |
| driver.get(PageWithTables.URL); | |
| String html = driver.findElement(PageWithTables.table).getAttribute("outerHTML"); | |
| System.out.println(html); | |
| String heading = Xpath.find(html, "//th[1]"); | |
| System.out.println("heading: " + heading); | |
| String first_name = Xpath.find(html, "//td[1]"); | |
| System.out.println("first_name: " + first_name); | |
| } | |
| @AfterMethod | |
| public void teardown() | |
| { | |
| driver.quit(); | |
| } | |
| public static URL getSauceURL(String username, String accessKey) throws MalformedURLException | |
| { | |
| URL url = new URL(SAUCE_URL | |
| .replace("SAUCE_USERNAME", username) | |
| .replace("SAUCE_ACCESS_KEY", accessKey)); | |
| return url; | |
| } | |
| public DesiredCapabilities getDesiredCapabilities(String platform, String browserName, String browserVersion) | |
| { | |
| DesiredCapabilities capabilities = new DesiredCapabilities(); | |
| capabilities.setCapability("platform", platform); | |
| capabilities.setCapability("browserName", browserName); | |
| capabilities.setCapability("version", browserVersion); | |
| capabilities.setCapability("name", testName); | |
| return capabilities; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.saucelabs.examples.util; | |
| import org.w3c.dom.Document; | |
| import org.xml.sax.InputSource; | |
| import org.xml.sax.SAXException; | |
| import javax.xml.parsers.DocumentBuilder; | |
| import javax.xml.parsers.DocumentBuilderFactory; | |
| import javax.xml.parsers.ParserConfigurationException; | |
| import javax.xml.xpath.XPath; | |
| import javax.xml.xpath.XPathExpressionException; | |
| import javax.xml.xpath.XPathFactory; | |
| import java.io.IOException; | |
| import java.io.StringReader; | |
| public class Xpath | |
| { | |
| public static String find(String html, String expression) throws ParserConfigurationException, IOException, SAXException, XPathExpressionException | |
| { | |
| DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); | |
| DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder(); | |
| InputSource inputSource = new InputSource(new StringReader(html)); | |
| Document document = documentBuilder.parse(inputSource); | |
| XPath xpath = XPathFactory.newInstance().newXPath(); | |
| return xpath.compile(expression).evaluate(document); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment