Skip to content

Instantly share code, notes, and snippets.

@sauceaaron
Created October 27, 2017 22:36
Show Gist options
  • Save sauceaaron/ff6c3a4007ec9c2614ec3719c1d35dcd to your computer and use it in GitHub Desktop.
Save sauceaaron/ff6c3a4007ec9c2614ec3719c1d35dcd to your computer and use it in GitHub Desktop.
import com.gargoylesoftware.htmlunit.StringWebResponse;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.HTMLParser;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
public class ParseHtmlTableLocally extends SauceTestNGBase
{
@Test
void usingHtmlUnit() throws IOException
{
/* get table source HTML using Selenium */
driver.get(PageWithTables.URL);
WebElement element = driver.findElement(PageWithTables.table);
String html = element.getAttribute("outerHTML");
System.out.println(html);
/* build HtmlUnit HtmlPage from string representation <table>...</table> */
URL url = new URL("http://www.example.com");
StringWebResponse response = new StringWebResponse(html, url);
WebClient client = new WebClient();
HtmlPage page = HTMLParser.parseHtml(response, client.getCurrentWindow());
/* create object to save results */
ArrayList<ArrayList<String>> data = new ArrayList<>();
/* get headings */
ArrayList<String> headings = new ArrayList<>();
for (DomElement th : page.getElementsByTagName("th"))
{
String heading = th.getTextContent();
headings.add(heading);
}
data.add(headings);
/* get each row */
for (DomElement tr : page.getElementsByTagName("tr"))
{
ArrayList<String> row = new ArrayList<>();
/* get each cell from the row */
for(DomElement td : tr.getElementsByTagName("td"))
{
String cell = td.getTextContent();
row.add(cell);
}
if (row.size() > 0)
{
data.add(row);
}
}
System.out.println("data" + data);
}
}
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 java.net.MalformedURLException;
import java.net.URL;
public abstract class SauceTestNGBase
{
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 = "Default Test Name";
WebDriver driver;
@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);
}
@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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment