use of com.axway.ats.uiengine.exceptions.SeleniumOperationException in project ats-framework by Axway.
the class HiddenHtmlMultiSelectList method setValue.
/**
* select a value
*
* @param value the value to select
*/
@Override
@PublicAtsApi
public void setValue(String value) {
new HiddenHtmlElementState(this).waitToBecomeExisting();
HtmlUnitWebElement selectElement = HiddenHtmlElementLocator.findElement(this);
if (selectElement.getAttribute("multiple") == null) {
throw new SeleniumOperationException("Not a multi-select. You may only add a selection to a select that supports multiple selections. (" + this.toString() + ")");
}
List<WebElement> optionElements = selectElement.findElements(By.tagName("option"));
for (WebElement el : optionElements) {
if (el.getText().equals(value)) {
if (!el.isSelected()) {
((HtmlUnitWebElement) el).click();
UiEngineUtilities.sleep();
}
return;
}
}
throw new SeleniumOperationException("Option with label '" + value + "' not found. (" + this.toString() + ")");
}
use of com.axway.ats.uiengine.exceptions.SeleniumOperationException in project ats-framework by Axway.
the class AbstractHtmlEngine method reloadFrames.
@PublicAtsApi
public void reloadFrames() {
// real browsers reloads the frames automatically
if (webDriver instanceof HtmlUnitDriver) {
Field webClientField = null;
boolean fieldAccessibleState = false;
try {
// Retrieve current WebClient instance (with the current page) from the Selenium WebDriver
TargetLocator targetLocator = webDriver.switchTo();
webClientField = targetLocator.getClass().getDeclaringClass().getDeclaredField("webClient");
fieldAccessibleState = webClientField.isAccessible();
webClientField.setAccessible(true);
WebClient webClient = (WebClient) webClientField.get(targetLocator.defaultContent());
HtmlPage page = (HtmlPage) webClient.getCurrentWindow().getEnclosedPage();
for (final FrameWindow frameWindow : page.getFrames()) {
final BaseFrameElement frame = frameWindow.getFrameElement();
// use == and not equals(...) to identify initial content (versus URL set to "about:blank")
if (frame.getEnclosedPage().getWebResponse().getWebRequest().getUrl() == WebClient.URL_ABOUT_BLANK) {
String src = frame.getSrcAttribute();
if (src != null && !src.isEmpty()) {
final URL url;
try {
url = ((HtmlPage) frame.getEnclosedPage()).getFullyQualifiedUrl(src);
} catch (final MalformedURLException e) {
String message = "Invalid src attribute of " + frame.getTagName() + ": url=[" + src + "]. Ignored.";
final IncorrectnessListener incorrectnessListener = webClient.getIncorrectnessListener();
incorrectnessListener.notify(message, this);
return;
}
if (isAlreadyLoadedByAncestor(url, ((HtmlPage) frame.getEnclosedPage()))) {
String message = "Recursive src attribute of " + frame.getTagName() + ": url=[" + src + "]. Ignored.";
final IncorrectnessListener incorrectnessListener = webClient.getIncorrectnessListener();
incorrectnessListener.notify(message, this);
log.info("Frame already loaded: " + frame.toString());
return;
}
try {
final WebRequest request = new WebRequest(url);
request.setAdditionalHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9, text/*;q=0.7, */*;q=0.5");
if (frameWindow.getName() == null || frameWindow.getName().isEmpty()) {
frameWindow.setName("frame_" + page.getFrames().indexOf(frameWindow));
}
webClient.loadWebResponseInto(webClient.loadWebResponse(request), frameWindow);
log.info("Frame loaded: " + frame.toString());
} catch (IOException e) {
log.error("Error when getting content for " + frame.getTagName() + " with src=" + url, e);
}
}
} else {
log.info("Frame already loaded: " + frame.toString());
}
}
} catch (Exception e) {
throw new SeleniumOperationException("Error retrieving internal Selenium web client", e);
} finally {
if (webClientField != null) {
webClientField.setAccessible(fieldAccessibleState);
}
}
}
}
use of com.axway.ats.uiengine.exceptions.SeleniumOperationException in project ats-framework by Axway.
the class RealHtmlTable method getColumnCount.
/**
* @return how many columns this table has
*/
@PublicAtsApi
public int getColumnCount() {
new RealHtmlElementState(this).waitToBecomeExisting();
String css = this.getElementProperty("_css");
try {
if (!StringUtils.isNullOrEmpty(css)) {
StringBuilder sb = new StringBuilder(css);
sb.append(" tr:nth-child(1) td");
int count = webDriver.findElements(By.cssSelector(sb.toString())).size();
sb = new StringBuilder(css);
sb.append(" tr:nth-child(1) th");
count += webDriver.findElements(By.cssSelector(sb.toString())).size();
return count;
} else {
// get elements matching the following xpath
return this.webDriver.findElements(By.xpath("(" + properties.getInternalProperty(HtmlElementLocatorBuilder.PROPERTY_ELEMENT_LOCATOR) + "//tr[th or td])[1]/*")).size();
}
} catch (Exception e) {
throw new SeleniumOperationException(this, "getColumnsCount", e);
}
}
use of com.axway.ats.uiengine.exceptions.SeleniumOperationException in project ats-framework by Axway.
the class HtmlFileBrowse method setFileInputValue.
/**
*
* @param webDriver {@link WebDriver} instance
* @param value the file input value to set
*/
protected void setFileInputValue(WebDriver webDriver, String value) {
String locator = this.getElementProperties().getInternalProperty(HtmlElementLocatorBuilder.PROPERTY_ELEMENT_LOCATOR);
String css = this.getElementProperty("_css");
WebElement element = null;
if (!StringUtils.isNullOrEmpty(css)) {
element = webDriver.findElement(By.cssSelector(css));
} else {
element = webDriver.findElement(By.xpath(locator));
}
try {
element.sendKeys(value);
} catch (ElementNotVisibleException enve) {
if (!UiEngineConfigurator.getInstance().isWorkWithInvisibleElements()) {
throw enve;
}
// try to make the element visible overriding some CSS properties
// but keep in mind that it can be still invisible using another CSS and/or JavaScript techniques
String styleAttrValue = element.getAttribute("style");
JavascriptExecutor jsExec = (JavascriptExecutor) webDriver;
try {
jsExec.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, "display:'block'; visibility:'visible'; top:'auto'; left:'auto'; z-index:999;" + "height:'auto'; width:'auto';");
element.sendKeys(value);
} finally {
jsExec.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, styleAttrValue);
}
} catch (InvalidElementStateException e) {
throw new SeleniumOperationException(e.getMessage(), e);
}
}
use of com.axway.ats.uiengine.exceptions.SeleniumOperationException in project ats-framework by Axway.
the class HiddenHtmlElement method clickAndDownloadFile.
/**
* Click the element and download file
*/
protected void clickAndDownloadFile() {
WebWindow currentWindow = null;
Field currentWindowField = null;
boolean fieldAccessibleState = false;
try {
currentWindowField = htmlUnitDriver.getClass().getDeclaredField("currentWindow");
fieldAccessibleState = currentWindowField.isAccessible();
currentWindowField.setAccessible(true);
currentWindow = (WebWindow) currentWindowField.get(htmlUnitDriver);
} catch (Exception e) {
throw new SeleniumOperationException("Error retrieving internal Selenium web client", e);
} finally {
if (currentWindowField != null) {
currentWindowField.setAccessible(fieldAccessibleState);
}
}
String elementXPath = properties.getProperty("xpath");
// find element and download the file
HtmlPage page = (HtmlPage) currentWindow.getEnclosedPage();
List<?> foundElementsList = page.getByXPath(elementXPath);
if (foundElementsList != null && !foundElementsList.isEmpty()) {
InputStream in = null;
FileOutputStream fos = null;
try {
com.gargoylesoftware.htmlunit.html.HtmlElement element = (com.gargoylesoftware.htmlunit.html.HtmlElement) foundElementsList.get(0);
// Use generic Page. Exact page type returned depends on the MIME type set in response header
Page result = element.click();
String fileName = null;
String contentDisposition = result.getWebResponse().getResponseHeaderValue("Content-Disposition");
if (contentDisposition != null) {
Matcher m = contentDispositionPattern.matcher(contentDisposition);
if (m.matches()) {
fileName = m.group(1);
log.debug("Download file name extracted from the 'Content-Disposition' header is " + fileName);
}
}
if (fileName == null) {
String url = result.getWebResponse().getWebRequest().getUrl().getFile().trim();
Matcher m = urlFileNamePattern.matcher(url);
if (m.matches()) {
fileName = m.group(1);
log.debug("Download file name extracted from the request URL is " + fileName);
} else {
fileName = String.valueOf(new Date().getTime()) + ".bin";
log.debug("Downloaded file name constructed the current timestamp is " + fileName);
}
}
in = result.getWebResponse().getContentAsStream();
String fileAbsPath = UiEngineConfigurator.getInstance().getBrowserDownloadDir() + fileName;
fos = new FileOutputStream(new File(fileAbsPath), false);
byte[] buff = new byte[BUFFER_LENGTH];
int len;
while ((len = in.read(buff)) != -1) {
fos.write(buff, 0, len);
}
fos.flush();
log.info("Downloaded file: " + fileAbsPath);
} catch (IOException e) {
throw new SeleniumOperationException("Error downloading file", e);
} finally {
IoUtils.closeStream(fos);
IoUtils.closeStream(in);
}
} else {
throw new ElementNotFoundException("Can't find element by XPath: " + elementXPath);
}
}
Aggregations