Last active
August 29, 2015 14:07
-
-
Save wallace7souza/0b6014f072702fc1da35 to your computer and use it in GitHub Desktop.
Java classes for selenium crawler,
This file contains 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
import java.util.ArrayList; | |
import java.util.Date; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Objects; | |
import org.openqa.selenium.By; | |
import org.openqa.selenium.WebDriver; | |
import org.openqa.selenium.WebElement; | |
import org.openqa.selenium.htmlunit.HtmlUnitDriver; | |
import org.openqa.selenium.support.ui.ExpectedCondition; | |
import org.openqa.selenium.support.ui.WebDriverWait; | |
/** | |
* Classe que abstrai as chamadas de navegacao do selenium | |
* | |
* @author wallace - wallace.c.souza@gmail.com | |
* | |
* | |
*/ | |
public final class SeleniumCrawlerBuilder { | |
private String urlRoot; | |
private Boolean jsEnabled = Boolean.FALSE; | |
private WebDriver driver = null; | |
private List<StepCrawler> steps = new ArrayList<StepCrawler>(); | |
public SeleniumCrawlerBuilder(String urlRoot) { | |
this.urlRoot = urlRoot; | |
} | |
public SeleniumCrawlerBuilder(String ulrRoot, Boolean jsEnabled) { | |
this(ulrRoot); | |
this.jsEnabled = jsEnabled; | |
} | |
public String getUrlRoot() { | |
return urlRoot; | |
} | |
public void setUrlRoot(String urlRoot) { | |
this.urlRoot = urlRoot; | |
} | |
public Boolean getJsEnabled() { | |
return jsEnabled; | |
} | |
public void setJsEnabled(Boolean jsEnabled) { | |
this.jsEnabled = jsEnabled; | |
} | |
private void setOpen() { | |
this.steps.add(0, new StepCrawler() { | |
@Override | |
public void perform() { | |
System.out.println("open driver..."); | |
driver = new HtmlUnitDriver(jsEnabled); | |
driver.get(urlRoot); | |
} | |
}); | |
} | |
public SeleniumCrawlerBuilder clear() { | |
this.steps.clear(); | |
return this; | |
} | |
public SeleniumCrawlerBuilder setElementInputByXpath(final String xPath, | |
final String inputValue) { | |
this.steps.add(new StepCrawler() { | |
@Override | |
public void perform() { | |
String value = inputValue; | |
WebElement findElement = driver.findElements(By.xpath(xPath)) | |
.get(0); | |
if (findElement != null) { | |
findElement.sendKeys(value); | |
} | |
} | |
}); | |
return this; | |
} | |
public SeleniumCrawlerBuilder setElementInputById(final String idElem, | |
final String inputValue) { | |
this.steps.add(new StepCrawler() { | |
@Override | |
public void perform() { | |
String value = inputValue; | |
WebElement findElement = driver.findElement(By.id(idElem)); | |
if (findElement != null) { | |
findElement.sendKeys(value); | |
} | |
} | |
}); | |
return this; | |
} | |
public SeleniumCrawlerBuilder forwardBySrcElem(final String id) { | |
this.steps.add(new StepCrawler() { | |
@Override | |
public void perform() { | |
WebElement element = driver.findElement(By.id(id)); | |
if (Objects.requireNonNull(element, String.format( | |
"Elemento com id %s n�o encontrado", id)) != null) { | |
if (element.getTagName().equalsIgnoreCase("a")) { | |
element.click(); | |
} else if (element.getTagName().equalsIgnoreCase("iframe")) { | |
driver.get(element.getAttribute("src")); | |
} | |
} | |
} | |
}); | |
return this; | |
} | |
public SeleniumCrawlerBuilder forward(final String target) { | |
this.steps.add(new StepCrawler() { | |
@Override | |
public void perform() { | |
driver.get(target); | |
} | |
}); | |
return this; | |
} | |
public SeleniumCrawlerBuilder click(final String idElem) { | |
this.steps.add(new StepCrawler() { | |
@Override | |
public void perform() { | |
By id = null; | |
if (idElem.startsWith("/")) { | |
id = By.xpath(idElem); | |
} else { | |
id = By.id(idElem); | |
} | |
WebElement elem = driver.findElement(id); | |
elem.click(); | |
} | |
}); | |
return this; | |
} | |
public SeleniumCrawlerBuilder click(final String idElem, final String xpath) { | |
this.steps.add(new StepCrawler() { | |
@Override | |
public void perform() { | |
WebElement elem = driver.findElement(By.id(idElem)); | |
elem.click(); | |
(new WebDriverWait(driver, 10)) | |
.until(new ExpectedCondition<Boolean>() { | |
public Boolean apply(WebDriver d) { | |
WebElement elem = d.findElement(By.xpath(xpath)); | |
return elem != null; | |
} | |
}); | |
} | |
}); | |
return this; | |
} | |
public SeleniumCrawlerBuilder click(final String idElem, | |
final ExpectedCondition<Boolean> expected) { | |
this.steps.add(new StepCrawler() { | |
@Override | |
public void perform() { | |
By id = null; | |
if (idElem.startsWith("/")) { | |
id = By.xpath(idElem); | |
} else { | |
id = By.id(idElem); | |
} | |
WebElement elem = driver.findElement(id); | |
elem.click(); | |
(new WebDriverWait(driver, 3)).until(expected); | |
} | |
}); | |
return this; | |
} | |
public SeleniumCrawlerBuilder populateResult(final Map<String, Object> ref) { | |
this.steps.add(new StepCrawler() { | |
@Override | |
public void perform() { | |
System.out.println(new Date()); | |
for (String key : ref.keySet()) { | |
String str = (String) ref.get(key); | |
if (str.contains("::value")) { | |
WebElement findElement = driver.findElement(By.id(str | |
.replaceAll("::value", ""))); | |
ref.put(key, findElement.getAttribute("value")); | |
} else if (str.contains("::text")) { | |
WebElement findElement = driver.findElement(By.id(str | |
.replaceAll("::text", ""))); | |
ref.put(key, findElement.getText().trim()); | |
}else if (str.contains("::xpathOptions")) { | |
String xpath = str.replaceAll("::xpathOptions", ""); | |
List<WebElement> findElements = driver.findElements(By.xpath(xpath)); | |
List<Map<String,String>> valuesArray = new ArrayList<Map<String,String>>(); | |
for (int i = 0; i < findElements.size(); i++) { | |
WebElement findElement = findElements.get(i); | |
HashMap<String,String> entry = new HashMap<String,String>(); | |
entry.put("value", findElement.getAttribute("value")); | |
entry.put("text", findElement.getText().trim()); | |
valuesArray.add(entry); | |
} | |
ref.put(key, valuesArray); | |
} | |
else if (str.contains("::xpathArrayValue")) { | |
String xpath = str.replaceAll("::xpathArrayValue", ""); | |
List<WebElement> findElements = driver.findElements(By | |
.xpath(xpath)); | |
String[] valuesArray = new String[findElements.size()]; | |
for (int i = 0; i < findElements.size(); i++) { | |
WebElement findElement = findElements.get(i); | |
valuesArray[i] = findElement.getAttribute("value") | |
.trim(); | |
} | |
ref.put(key, valuesArray); | |
} | |
else if (str.contains("::xpathArray")) { | |
String xpath = str.replaceAll("::xpathArray", ""); | |
List<WebElement> findElements = driver.findElements(By | |
.xpath(xpath)); | |
String[] valuesArray = new String[findElements.size()]; | |
for (int i = 0; i < findElements.size(); i++) { | |
WebElement findElement = findElements.get(i); | |
valuesArray[i] = findElement.getText().trim(); | |
} | |
ref.put(key, valuesArray); | |
} else if (str.contains("::xpath")) { | |
// System.out.println(driver.getTitle()); | |
String xpath = str.replaceAll("::xpath", ""); | |
// System.out.println(key+" :: "+xpath); | |
List<WebElement> findElements = driver.findElements(By | |
.xpath(xpath)); | |
WebElement findElement = findElements.get(0); | |
ref.put(key, findElement.getText().trim()); | |
} | |
} | |
System.out.println(new Date()); | |
} | |
}); | |
return this; | |
} | |
private SeleniumCrawlerBuilder setQuit() { | |
this.steps.add(this.steps.size(), new StepCrawler() { | |
@Override | |
public void perform() { | |
driver.close(); | |
} | |
}); | |
return this; | |
} | |
public SeleniumCrawlerBuilder switchToFrame(final String ref) { | |
this.steps.add(this.steps.size(), new StepCrawler() { | |
@Override | |
public void perform() { | |
By xpath2 = (ref.startsWith("/")) ? By.xpath(ref) : By.id(ref); | |
; | |
WebElement iframeElement = driver.findElement(xpath2); | |
driver.switchTo().frame(iframeElement); | |
} | |
}); | |
return this; | |
} | |
public void execute() { | |
setOpen(); | |
setQuit(); | |
int step = 0; | |
for (StepCrawler stepCrawler : this.steps) { | |
System.out.println("passo :" + (step++)); | |
stepCrawler.perform(); | |
} | |
this.steps.clear(); | |
} | |
public void closeDriver() { | |
this.driver.close(); | |
} | |
public SeleniumCrawlerBuilder submit(final String ref) { | |
this.steps.add(this.steps.size(), new StepCrawler() { | |
@Override | |
public void perform() { | |
By xpath2 = (ref.startsWith("/")) ? By.xpath(ref) : By.id(ref); | |
; | |
WebElement elem = driver.findElement(xpath2); | |
elem.submit(); | |
} | |
}); | |
return this; | |
} | |
public SeleniumCrawlerBuilder submitByName(final String ref) { | |
this.steps.add(this.steps.size(), new StepCrawler() { | |
@Override | |
public void perform() { | |
By xpath2 = By.name(ref); | |
; | |
WebElement elem = driver.findElement(xpath2); | |
elem.submit(); | |
} | |
}); | |
return this; | |
} | |
public SeleniumCrawlerBuilder setElementInputByName(final String idElem, | |
final String inputValue) { | |
this.steps.add(new StepCrawler() { | |
@Override | |
public void perform() { | |
String value = inputValue; | |
By id = By.name(idElem); | |
WebElement findElement = driver.findElement(id); | |
if (findElement != null) { | |
findElement.sendKeys(value); | |
} | |
} | |
}); | |
return this; | |
} | |
} |
This file contains 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
public abstract class StepCrawler { | |
public abstract void perform(); | |
/* | |
public static void main(String[] args) { | |
SeleniumCrawlerBuilder scb = new SeleniumCrawlerBuilder("http://www.google.com"); | |
HashMap<String, Object> populate = new HashMap<String, Object>(); | |
populate.put("title", "//title::xpath"); | |
scb.setElementInputByName("q","cheese") | |
.submitByName("q").populateResult(populate).execute(); | |
System.out.println(populate); | |
} | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment