use of java.lang.RuntimeException in project selenium_java by sergueik.
the class AppTest method findElements.
private List<WebElement> findElements(String selectorKind, String selectorValue, WebElement parent) {
SearchContext finder;
String parent_css_selector = null;
String parent_xpath = null;
List<WebElement> elements = null;
Hashtable<String, Boolean> supportedSelectorStrategies = new Hashtable<String, Boolean>();
supportedSelectorStrategies.put("css_selector", true);
supportedSelectorStrategies.put("xpath", true);
if (selectorKind == null || !supportedSelectorStrategies.containsKey(selectorKind) || !supportedSelectorStrategies.get(selectorKind)) {
return null;
}
if (parent != null) {
parent_css_selector = cssSelectorOfElement(parent);
parent_xpath = xpathOfElement(parent);
finder = parent;
} else {
finder = driver;
}
if (selectorKind == "css_selector") {
String extended_css_selector = String.format("%s %s", parent_css_selector, selectorValue);
try {
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(extended_css_selector)));
} catch (RuntimeException timeoutException) {
return null;
}
elements = finder.findElements(By.cssSelector(selectorValue));
}
if (selectorKind == "xpath") {
String extended_xpath = String.format("%s/%s", parent_xpath, selectorValue);
try {
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(extended_xpath)));
} catch (RuntimeException timeoutException) {
return null;
}
elements = finder.findElements(By.xpath(selectorValue));
}
return elements;
}
use of java.lang.RuntimeException in project selenium_java by sergueik.
the class App method find_elements.
private static List<WebElement> find_elements(String selector_type, String selector_value, WebElement parent) {
int flexible_wait_interval = 5;
SearchContext finder;
long wait_polling_interval = 500;
String parent_css_selector = null;
String parent_xpath = null;
WebDriverWait wait = new WebDriverWait(driver, flexible_wait_interval);
wait.pollingEvery(wait_polling_interval, TimeUnit.MILLISECONDS);
List<WebElement> elements = null;
Hashtable<String, Boolean> supported_selectors = new Hashtable<String, Boolean>();
supported_selectors.put("css_selector", true);
supported_selectors.put("xpath", true);
if (selector_type == null || !supported_selectors.containsKey(selector_type) || !supported_selectors.get(selector_type)) {
return null;
}
if (parent != null) {
parent_css_selector = css_selector_of(parent);
parent_xpath = xpath_of(parent);
finder = parent;
} else {
finder = driver;
}
if (selector_type == "css_selector") {
String extended_css_selector = String.format("%s %s", parent_css_selector, selector_value);
try {
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(extended_css_selector)));
} catch (RuntimeException timeoutException) {
return null;
}
elements = finder.findElements(By.cssSelector(selector_value));
}
if (selector_type == "xpath") {
String extended_xpath = String.format("%s/%s", parent_xpath, selector_value);
try {
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(extended_xpath)));
} catch (RuntimeException timeoutException) {
return null;
}
elements = finder.findElements(By.xpath(selector_value));
}
return elements;
}
use of java.lang.RuntimeException in project selenium_java by sergueik.
the class App method testUpload.
@Test
public static void testUpload() throws Exception {
// Wait for the page to load ( check that logo / title is updated )
WebElement element = driver.findElement(By.cssSelector("a.brand"));
assertThat(element.getAttribute("title"), containsString("Translate text, documents and websites for free"));
// optional:
// driver.manage().window().maximize();
WebElement uploadButton = driver.findElement(By.cssSelector("div[id = 'upload-button']"));
highlight(uploadButton, 1500);
WebElement uploadElement = driver.findElement(By.cssSelector("input[class='ajaxupload-input']"));
assertThat(uploadElement, notNullValue());
assertEquals("file", uploadElement.getAttribute("type"));
assertEquals("file", uploadElement.getAttribute("name"));
// the uploadElement is not visible
LocalFileDetector detector = new LocalFileDetector();
File remoteFile = detector.getLocalFile(localPath);
// https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/remote/LocalFileDetector.html
// driver.setFileDetector(new LocalFileDetector());
((RemoteWebElement) uploadElement).setFileDetector(detector);
System.err.format("Uploading the file '%s'.\n", remoteFile.getAbsolutePath());
// Populate upload input
uploadElement.sendKeys(remoteFile.getAbsolutePath());
// TODO : locate the progressbar
// hard wait
Thread.sleep(2000);
try {
wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("gw-download-link")));
} catch (RuntimeException timeoutException) {
// continue - assertion follows
}
WebElement downloadLink = driver.findElement(By.className("gw-download-link"));
assertEquals("Download", downloadLink.getText());
WebElement downloaIndicator = driver.findElement(By.cssSelector("div [class='status-text'] img[class *= 'gw-icon']"));
assertThat(downloaIndicator, notNullValue());
// System.err.println(downloaIndicator.getAttribute("outerHTML"));
highlight(downloaIndicator);
String downloadHref = downloadLink.getAttribute("href");
System.err.println("Reading '" + downloadHref + "'");
sendGet(downloadHref);
// assertEquals("file", downloadElement.getAttribute("name"));
}
use of java.lang.RuntimeException in project selenium_java by sergueik.
the class AppTest method getPageContent.
private String getPageContent(String pagename) {
try {
URI uri = AppTest.class.getClassLoader().getResource(pagename).toURI();
System.err.println("Testing: " + uri.toString());
return uri.toString();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
use of java.lang.RuntimeException in project selenium_java by sergueik.
the class AppTest method testUpload.
@Ignore
@Test
public // https://www.youtube.com/watch?v=8OfnQEfzfmw
void testUpload() {
String openButtonImage = fullPath("open_1920x1080.png");
Pattern filenameTextBox = new Pattern(fullPath("filename_1920x1080.png"));
driver.navigate().to(getPageContent("upload.html"));
try {
File tmpFile = File.createTempFile("foo", ".png");
WebElement element = driver.findElement(By.tagName("input"));
element.click();
try {
screen.exists(openButtonImage, sikuliTimeout);
screen.type(filenameTextBox, tmpFile.getCanonicalPath());
System.out.println("Uploading: " + tmpFile.getCanonicalPath());
screen.click(openButtonImage, 0);
} catch (FindFailed e) {
verificationErrors.append(e.toString());
}
try {
element = driver.findElement(By.tagName("input"));
highlight(element);
assertThat(element.getAttribute("value"), containsString(tmpFile.getName()));
} catch (Error e) {
verificationErrors.append(e.toString());
}
} catch (IOException e) {
throw new RuntimeException(e.toString());
}
}
Aggregations