use of com.axway.ats.uiengine.exceptions.ElementNotFoundException in project ats-framework by Axway.
the class SwingElementLocator method getWindowFixture.
/**
* Change window by specified name.
* For internal use
* @param driver Swing driver
* @param windowTitle if null look for any visible window
* @param isDialog
* @return the {@link ContainerFinxture}
*/
public static WindowFixture<?> getWindowFixture(SwingDriverInternal driver, final String windowTitle, boolean isDialog) throws ElementNotFoundException {
WindowFixture<?> windowFixture = driver.getWindowFixture();
Robot robot = null;
if (windowFixture != null) {
// use the current robot instance
robot = windowFixture.robot;
} else {
robot = BasicRobot.robotWithCurrentAwtHierarchy();
}
try {
if (windowTitle != null) {
if (isDialog) {
windowFixture = WindowFinder.findDialog(new GenericTypeMatcher<Dialog>(Dialog.class) {
protected boolean isMatching(Dialog dialog) {
return windowTitle.equals(dialog.getTitle()) && dialog.isShowing();
}
}).withTimeout(UiEngineConfigurator.getInstance().getElementStateChangeDelay()).using(robot);
} else {
windowFixture = WindowFinder.findFrame(new GenericTypeMatcher<Frame>(Frame.class) {
protected boolean isMatching(Frame frame) {
return windowTitle.equals(frame.getTitle()) && frame.isShowing();
}
}).withTimeout(UiEngineConfigurator.getInstance().getElementStateChangeDelay()).using(robot);
}
} else {
if (isDialog) {
windowFixture = WindowFinder.findDialog(new GenericTypeMatcher<Dialog>(Dialog.class) {
protected boolean isMatching(Dialog dialog) {
return dialog.isShowing();
}
}).withTimeout(UiEngineConfigurator.getInstance().getElementStateChangeDelay()).using(robot);
} else {
windowFixture = WindowFinder.findFrame(new GenericTypeMatcher<Frame>(Frame.class) {
protected boolean isMatching(Frame frame) {
if (log.isTraceEnabled()) {
log.trace("WindowFinder isMatching(): Title: " + frame.getTitle() + ", Frame + " + frame + ", Owner: " + frame.getOwner());
}
// owner == null - top frame. Two independent frames are both considered a top ones
return frame.isShowing() && frame.getOwner() == null;
}
}).withTimeout(UiEngineConfigurator.getInstance().getElementStateChangeDelay()).using(robot);
}
}
return windowFixture;
} catch (WaitTimedOutError wtoe) {
throw new ElementNotFoundException("Unable to find " + (isDialog ? "dialog" : "frame") + (windowTitle != null ? " with title '" + windowTitle + "'" : " without title specified (null passed)"), wtoe);
}
}
use of com.axway.ats.uiengine.exceptions.ElementNotFoundException in project ats-framework by Axway.
the class HtmlNavigator method navigateToFrame.
@PublicAtsApi
public void navigateToFrame(WebDriver webDriver, UiElement element) {
if (lastWebDriver != webDriver) {
// this is a new WebDriver instance
lastWebDriver = webDriver;
lastFramesLocation = "";
}
String newFramesLocationProperty = element != null ? element.getElementProperty("frame") : null;
try {
if (newFramesLocationProperty == null) {
// No frame selection. Go to top frame if not there yet
if (!"".equals(lastFramesLocation)) {
log.debug("Go to TOP frame");
webDriver.switchTo().defaultContent();
lastFramesLocation = "";
}
} else {
lastFramesLocation = newFramesLocationProperty;
log.debug("Go to frame: " + newFramesLocationProperty);
String[] newFramesLocation = newFramesLocationProperty.split("\\->");
webDriver.switchTo().defaultContent();
for (String frame : newFramesLocation) {
if (frame.startsWith("/") || frame.startsWith("(/")) {
WebElement frameElement = webDriver.findElement(By.xpath(frame.trim()));
webDriver.switchTo().frame(frameElement);
} else {
webDriver.switchTo().frame(frame.trim());
}
}
}
} catch (NotFoundException nfe) {
String msg = "Frame not found. Searched by: '" + (element != null ? element.getElementProperty("frame") : "") + "'";
log.debug(msg);
throw new ElementNotFoundException(msg, nfe);
}
}
use of com.axway.ats.uiengine.exceptions.ElementNotFoundException 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