use of com.axway.ats.uiengine.exceptions.SeleniumOperationException in project ats-framework by Axway.
the class RealHtmlMultiSelectList method unsetValue.
/**
* unselect a value
*
* @param value the value to unselect
*/
@Override
@PublicAtsApi
public void unsetValue(String value) {
new RealHtmlElementState(this).waitToBecomeExisting();
WebElement element = RealHtmlElementLocator.findElement(this);
Select select = new Select(element);
// select.deselectByVisibleText( value ); // this method doesn't throw an exception if the option doesn't exist
for (WebElement option : select.getOptions()) {
if (option.getText().equals(value)) {
if (option.isSelected()) {
option.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 RealHtmlMultiSelectList method setValue.
/**
* select a value
*
* @param value the value to select
*/
@Override
@PublicAtsApi
public void setValue(String value) {
new RealHtmlElementState(this).waitToBecomeExisting();
try {
WebElement element = RealHtmlElementLocator.findElement(this);
Select select = new Select(element);
select.selectByVisibleText(value);
} catch (NoSuchElementException nsee) {
throw new SeleniumOperationException("Option with label '" + value + "' not found. (" + this.toString() + ")");
}
UiEngineUtilities.sleep();
}
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);
}
}
use of com.axway.ats.uiengine.exceptions.SeleniumOperationException in project ats-framework by Axway.
the class HiddenHtmlMultiSelectList method getValues.
/**
* @return the selected value
*/
@Override
@PublicAtsApi
public String[] getValues() {
new HiddenHtmlElementState(this).waitToBecomeExisting();
HtmlUnitWebElement selectElement = HiddenHtmlElementLocator.findElement(this);
List<String> values = new ArrayList<String>();
List<WebElement> optionElements = selectElement.findElements(By.tagName("option"));
for (WebElement element : optionElements) {
if (element.isSelected()) {
values.add(element.getText());
}
}
if (values.isEmpty()) {
throw new SeleniumOperationException("There is no selected 'option' in " + this.toString());
}
return values.toArray(new String[0]);
}
Aggregations