Search in sources :

Example 1 with SeleniumOperationException

use of com.axway.ats.uiengine.exceptions.SeleniumOperationException in project ats-framework by Axway.

the class AbstractRealBrowserDriver method waitForPageLoaded.

public void waitForPageLoaded(WebDriver driver, int timeoutInSeconds) {
    /*InternetExplorer is unable to wait for document's readyState to be complete.*/
    if (this instanceof com.axway.ats.uiengine.InternetExplorerDriver) {
        return;
    }
    ExpectedCondition<Boolean> expectation = new ExpectedCondition<Boolean>() {

        public Boolean apply(WebDriver driver) {
            return "complete".equals(((JavascriptExecutor) driver).executeScript("return document.readyState"));
        }
    };
    Wait<WebDriver> wait = new WebDriverWait(driver, timeoutInSeconds);
    try {
        wait.until(expectation);
    } catch (Exception e) {
        throw new SeleniumOperationException("Timeout waiting for Page Load Request to complete.", e);
    }
}
Also used : WebDriver(org.openqa.selenium.WebDriver) RemoteWebDriver(org.openqa.selenium.remote.RemoteWebDriver) WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait) ExpectedCondition(org.openqa.selenium.support.ui.ExpectedCondition) SeleniumOperationException(com.axway.ats.uiengine.exceptions.SeleniumOperationException) VerificationException(com.axway.ats.uiengine.exceptions.VerificationException) SeleniumOperationException(com.axway.ats.uiengine.exceptions.SeleniumOperationException) ElementNotFoundException(com.axway.ats.uiengine.exceptions.ElementNotFoundException) NoAlertPresentException(org.openqa.selenium.NoAlertPresentException)

Example 2 with SeleniumOperationException

use of com.axway.ats.uiengine.exceptions.SeleniumOperationException in project ats-framework by Axway.

the class AbstractRealBrowserDriver method start.

@Override
@PublicAtsApi
public void start() {
    try {
        log.info("Starting selenium browser with " + this.getClass().getSimpleName());
        if (browserType == BrowserType.FireFox) {
            com.axway.ats.uiengine.FirefoxDriver firefoxDriver = new com.axway.ats.uiengine.FirefoxDriver(url, browserPath, remoteSeleniumURL);
            FirefoxProfile profile = null;
            if (firefoxDriver.getProfileName() != null) {
                profile = new ProfilesIni().getProfile(firefoxDriver.getProfileName());
                if (profile == null) {
                    throw new SeleniumOperationException("Firefox profile '" + firefoxDriver.getProfileName() + "' doesn't exist");
                }
            } else if (firefoxDriver.getProfileDirectory() != null) {
                File profileDirectory = new File(firefoxDriver.getProfileDirectory());
                profile = new FirefoxProfile(profileDirectory);
            } else {
                profile = new FirefoxProfile();
                String downloadDir = UiEngineConfigurator.getInstance().getBrowserDownloadDir();
                // for default browser. Now will FIX this behavior
                if (downloadDir.endsWith("/") || downloadDir.endsWith("\\")) {
                    downloadDir = downloadDir.substring(0, downloadDir.length() - 1);
                }
                // Following options are described in http://kb.mozillazine.org/Firefox_:_FAQs_:_About:config_Entries
                profile.setPreference("browser.download.dir", downloadDir);
                profile.setPreference("browser.download.folderList", 2);
                profile.setPreference("browser.helperApps.neverAsk.saveToDisk", UiEngineConfigurator.getInstance().getBrowserDownloadMimeTypes());
                // set to  "Always Activate"
                profile.setPreference("plugin.state.java", 2);
                // set to  "Always Activate"
                profile.setPreference("plugin.state.flash", 2);
                profile.setAcceptUntrustedCertificates(true);
                profile.setEnableNativeEvents(true);
            }
            DesiredCapabilities capabilities = DesiredCapabilities.firefox();
            capabilities.setCapability(FirefoxDriver.PROFILE, profile);
            setFirefoxProxyIfAvailable(capabilities);
            if (this.browserPath != null) {
                capabilities.setCapability(FirefoxDriver.BINARY, this.browserPath);
            }
            if (this.remoteSeleniumURL != null) {
                webDriver = new RemoteWebDriver(new URL(this.remoteSeleniumURL), capabilities);
            } else {
                webDriver = new FirefoxDriver(capabilities);
            }
        } else if (browserType == BrowserType.InternetExplorer) {
            if (this.remoteSeleniumURL != null) {
                webDriver = new RemoteWebDriver(new URL(this.remoteSeleniumURL), DesiredCapabilities.internetExplorer());
            } else {
                webDriver = new org.openqa.selenium.ie.InternetExplorerDriver();
            }
        } else if (browserType == BrowserType.Edge) {
            if (this.remoteSeleniumURL != null) {
                webDriver = new RemoteWebDriver(new URL(this.remoteSeleniumURL), DesiredCapabilities.edge());
            } else {
                webDriver = new org.openqa.selenium.edge.EdgeDriver();
            }
        } else if (browserType == BrowserType.Chrome) {
            DesiredCapabilities capabilities = DesiredCapabilities.chrome();
            // apply Chrome options
            ChromeOptions options = UiEngineConfigurator.getInstance().getChromeDriverOptions();
            if (options == null) {
                options = new ChromeOptions();
            }
            /* set browser download dir for Chrome Browser */
            String downloadDir = UiEngineConfigurator.getInstance().getBrowserDownloadDir();
            HashMap<String, Object> prefs = new HashMap<String, Object>();
            prefs.put("profile.default_content_settings.popups", 0);
            prefs.put("download.default_directory", downloadDir);
            options.setExperimentalOption("prefs", prefs);
            capabilities.setCapability(ChromeOptions.CAPABILITY, options);
            if (this.remoteSeleniumURL != null) {
                webDriver = new RemoteWebDriver(new URL(this.remoteSeleniumURL), capabilities);
            } else {
                webDriver = new org.openqa.selenium.chrome.ChromeDriver(capabilities);
            }
        } else if (browserType == BrowserType.Safari) {
            if (this.remoteSeleniumURL != null) {
                webDriver = new RemoteWebDriver(new URL(this.remoteSeleniumURL), DesiredCapabilities.safari());
            } else {
                webDriver = new org.openqa.selenium.safari.SafariDriver();
            }
        } else if (browserType == BrowserType.PhantomJS) {
            DesiredCapabilities capabilities = DesiredCapabilities.phantomjs();
            capabilities.setJavascriptEnabled(true);
            capabilities.setCapability("acceptSslCerts", true);
            capabilities.setCapability("browserConnectionEnabled", true);
            capabilities.setCapability("takesScreenshot", true);
            // See: https://github.com/ariya/phantomjs/wiki/API-Reference-WebPage#settings-object
            if (System.getProperty(PhantomJsDriver.SETTINGS_PROPERTY) != null) {
                Map<String, String> settings = extractPhantomJSCapabilityValues(System.getProperty(PhantomJsDriver.SETTINGS_PROPERTY));
                for (Entry<String, String> capability : settings.entrySet()) {
                    capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX + capability.getKey(), capability.getValue());
                }
            }
            // See:  https://github.com/ariya/phantomjs/wiki/API-Reference-WebPage#wiki-webpage-customHeaders
            if (System.getProperty(PhantomJsDriver.CUSTOM_HEADERS_PROPERTY) != null) {
                Map<String, String> customHeaders = extractPhantomJSCapabilityValues(System.getProperty(PhantomJsDriver.CUSTOM_HEADERS_PROPERTY));
                for (Entry<String, String> header : customHeaders.entrySet()) {
                    capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_CUSTOMHEADERS_PREFIX + header.getKey(), header.getValue());
                }
            }
            if (this.browserPath != null) {
                capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, this.browserPath);
                System.setProperty(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, // required from the create screenshot method
                this.browserPath);
            }
            // See:  https://github.com/ariya/phantomjs/wiki/API-Reference#command-line-options
            List<String> cliArgsCapabilities = new ArrayList<String>();
            cliArgsCapabilities.add("--web-security=false");
            cliArgsCapabilities.add("--ignore-ssl-errors=true");
            if (System.getProperty(PhantomJsDriver.SSL_PROTOCOL_PROPERTY) != null) {
                cliArgsCapabilities.add("--ssl-protocol=" + System.getProperty(PhantomJsDriver.SSL_PROTOCOL_PROPERTY));
            } else {
                cliArgsCapabilities.add("--ssl-protocol=any");
            }
            if (System.getProperty(PhantomJsDriver.HTTP_ONLY_COOKIES_PROPERTY) != null) {
                cliArgsCapabilities.add("--cookies-file=" + PhantomJsDriver.cookiesFile);
            }
            // cliArgsCapabilities.add( "--local-to-remote-url-access=true" );
            setPhantomJSProxyIfAvailable(cliArgsCapabilities);
            capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliArgsCapabilities);
            if (this.remoteSeleniumURL != null) {
                webDriver = new RemoteWebDriver(new URL(this.remoteSeleniumURL), capabilities);
            } else {
                webDriver = new org.openqa.selenium.phantomjs.PhantomJSDriver(capabilities);
            }
        }
        log.info("Openning URL: " + url);
        webDriver.get(url);
        if (this instanceof com.axway.ats.uiengine.PhantomJsDriver) {
            webDriver.manage().window().setSize(new Dimension(1280, 1024));
        } else if (!(this instanceof com.axway.ats.uiengine.EdgeDriver)) {
            webDriver.manage().window().maximize();
        }
        int browserActionTimeout = UiEngineConfigurator.getInstance().getBrowserActionTimeout();
        if (browserActionTimeout > 0) {
            webDriver.manage().timeouts().setScriptTimeout(browserActionTimeout, TimeUnit.SECONDS);
        }
        if (!(this instanceof com.axway.ats.uiengine.EdgeDriver)) {
            webDriver.manage().timeouts().pageLoadTimeout(browserActionTimeout, TimeUnit.SECONDS);
        }
        // waiting for the "body" element to be loaded
        waitForPageLoaded(webDriver, UiEngineConfigurator.getInstance().getWaitPageToLoadTimeout());
    } catch (Exception e) {
        throw new SeleniumOperationException("Error starting Selenium", e);
    }
}
Also used : HashMap(java.util.HashMap) FirefoxProfile(org.openqa.selenium.firefox.FirefoxProfile) SeleniumOperationException(com.axway.ats.uiengine.exceptions.SeleniumOperationException) URL(java.net.URL) Entry(java.util.Map.Entry) FirefoxDriver(org.openqa.selenium.firefox.FirefoxDriver) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) RemoteWebDriver(org.openqa.selenium.remote.RemoteWebDriver) DesiredCapabilities(org.openqa.selenium.remote.DesiredCapabilities) Dimension(org.openqa.selenium.Dimension) VerificationException(com.axway.ats.uiengine.exceptions.VerificationException) SeleniumOperationException(com.axway.ats.uiengine.exceptions.SeleniumOperationException) ElementNotFoundException(com.axway.ats.uiengine.exceptions.ElementNotFoundException) NoAlertPresentException(org.openqa.selenium.NoAlertPresentException) ProfilesIni(org.openqa.selenium.firefox.internal.ProfilesIni) ChromeOptions(org.openqa.selenium.chrome.ChromeOptions) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 3 with SeleniumOperationException

use of com.axway.ats.uiengine.exceptions.SeleniumOperationException in project ats-framework by Axway.

the class HiddenBrowserDriver method fixHtmlUnitBehaviour.

/**
     * Fixing refresh handler to skip Refresh meta tags
     * Allowing connections to any host, regardless of whether they have valid certificates or not
     * Fixing JSESSIONID cookie value
     * Some applications expect double quotes in the beginning and at the end of the JSESSIONID cookie value
     */
private void fixHtmlUnitBehaviour() {
    Field webClientField = null;
    boolean fieldAccessibleState = false;
    try {
        TargetLocator targetLocator = webDriver.switchTo();
        webClientField = targetLocator.getClass().getDeclaringClass().getDeclaredField("webClient");
        fieldAccessibleState = webClientField.isAccessible();
        webClientField.setAccessible(true);
        final WebClient webClient = (WebClient) webClientField.get(targetLocator.defaultContent());
        // Allowing connections to any host, regardless of whether they have valid certificates or not
        webClient.getOptions().setUseInsecureSSL(true);
        // Set Http connection timeout (in milliseconds). The default value is 90 seconds, because in Firefox >= 16
        // the "network.http.connection-timeout" property is 90. But this value is not enough for some cases.
        // NOTE: use 0 for infinite timeout
        webClient.getOptions().setTimeout(5 * 60 * 1000);
        webClient.getOptions().setRedirectEnabled(true);
        webClient.getOptions().setJavaScriptEnabled(true);
        webClient.getOptions().setThrowExceptionOnScriptError(true);
        webClient.getOptions().setPrintContentOnFailingStatusCode(true);
        // Hide CSS Warnings
        webClient.setCssErrorHandler(new SilentCssErrorHandler());
        // Suppress warnings like: "Expected content type ... but got ..."
        webClient.setIncorrectnessListener(new IncorrectnessListener() {

            //                private final Log log = LogFactory.getLog( this.getClass() );
            @Override
            public void notify(final String message, final Object origin) {
            //                    log.warn( message );
            }
        });
        if (!Boolean.parseBoolean(System.getProperty(ALLOW_META_REFRESH_TAG))) {
            /*
                 * Fix for refresh meta tags eg. "<meta http-equiv="refresh" content="300">"
                 * The default refresh handler is with Thread.sleep(refreshSecondsFromMetaTag) in the main thread!!!
                     *
                     * Maybe we should check and test this handler: webClient.setRefreshHandler( new ThreadedRefreshHandler() );
                 */
            webClient.setRefreshHandler(new RefreshHandler() {

                @Override
                public void handleRefresh(Page page, URL url, int seconds) throws IOException {
                }
            });
        }
        /*
             * Fix JSessionId
             */
        // WebConnectionWrapper constructs a WebConnection object wrapping the connection of the WebClient
        // and places itself (in the constructor) as connection of the WebClient.
        new WebConnectionWrapper(webClient) {

            public WebResponse getResponse(WebRequest request) throws IOException {
                Cookie jsCookie = webClient.getCookieManager().getCookie("JSESSIONID");
                if (jsCookie != null && (!jsCookie.getValue().startsWith("\"") && !jsCookie.getValue().endsWith("\""))) {
                    Cookie newCookie = new Cookie(jsCookie.getDomain(), jsCookie.getName(), "\"" + jsCookie.getValue() + "\"", jsCookie.getPath(), jsCookie.getExpires(), jsCookie.isSecure());
                    webClient.getCookieManager().removeCookie(jsCookie);
                    webClient.getCookieManager().addCookie(newCookie);
                }
                return super.getResponse(request);
            }
        };
    } catch (Exception e) {
        throw new SeleniumOperationException("Error retrieving internal Selenium web client", e);
    } finally {
        if (webClientField != null) {
            webClientField.setAccessible(fieldAccessibleState);
        }
    }
}
Also used : Cookie(com.gargoylesoftware.htmlunit.util.Cookie) Page(com.gargoylesoftware.htmlunit.Page) IOException(java.io.IOException) SeleniumOperationException(com.axway.ats.uiengine.exceptions.SeleniumOperationException) WebClient(com.gargoylesoftware.htmlunit.WebClient) IncorrectnessListener(com.gargoylesoftware.htmlunit.IncorrectnessListener) URL(java.net.URL) SeleniumOperationException(com.axway.ats.uiengine.exceptions.SeleniumOperationException) IOException(java.io.IOException) Field(java.lang.reflect.Field) WebRequest(com.gargoylesoftware.htmlunit.WebRequest) TargetLocator(org.openqa.selenium.WebDriver.TargetLocator) SilentCssErrorHandler(com.gargoylesoftware.htmlunit.SilentCssErrorHandler) RefreshHandler(com.gargoylesoftware.htmlunit.RefreshHandler) WebConnectionWrapper(com.gargoylesoftware.htmlunit.util.WebConnectionWrapper)

Example 4 with SeleniumOperationException

use of com.axway.ats.uiengine.exceptions.SeleniumOperationException in project ats-framework by Axway.

the class RealHtmlSingleSelectList method getAllPossibleValues.

/**
     * @return  a list with all possible selection values
     */
@Override
@PublicAtsApi
public List<String> getAllPossibleValues() {
    List<String> values = new ArrayList<String>();
    new RealHtmlElementState(this).waitToBecomeExisting();
    WebElement element = RealHtmlElementLocator.findElement(this);
    Select select = new Select(element);
    Iterator<WebElement> iterator = select.getOptions().iterator();
    if (!select.getAllSelectedOptions().isEmpty()) {
        while (iterator.hasNext()) {
            values.add(iterator.next().getText());
        }
        return values;
    }
    throw new SeleniumOperationException("There is no selectable 'option' in " + this.toString());
}
Also used : RealHtmlElementState(com.axway.ats.uiengine.utilities.realbrowser.html.RealHtmlElementState) ArrayList(java.util.ArrayList) Select(org.openqa.selenium.support.ui.Select) WebElement(org.openqa.selenium.WebElement) SeleniumOperationException(com.axway.ats.uiengine.exceptions.SeleniumOperationException) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 5 with SeleniumOperationException

use of com.axway.ats.uiengine.exceptions.SeleniumOperationException in project ats-framework by Axway.

the class HiddenHtmlSingleSelectList method setValue.

/**
     * set the single selection value
     *
     * @param value the value to select
     */
@Override
@PublicAtsApi
public void setValue(String value) {
    new HiddenHtmlElementState(this).waitToBecomeExisting();
    HtmlUnitWebElement selectElement = HiddenHtmlElementLocator.findElement(this);
    List<WebElement> optionElements = selectElement.findElements(By.tagName("option"));
    for (WebElement el : optionElements) {
        if (el.getText().equals(value)) {
            ((HtmlUnitWebElement) el).click();
            UiEngineUtilities.sleep();
            return;
        }
    }
    throw new SeleniumOperationException("Option with label '" + value + "' not found. (" + this.toString() + ")");
}
Also used : HiddenHtmlElementState(com.axway.ats.uiengine.utilities.hiddenbrowser.HiddenHtmlElementState) HtmlUnitWebElement(org.openqa.selenium.htmlunit.HtmlUnitWebElement) WebElement(org.openqa.selenium.WebElement) HtmlUnitWebElement(org.openqa.selenium.htmlunit.HtmlUnitWebElement) SeleniumOperationException(com.axway.ats.uiengine.exceptions.SeleniumOperationException) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Aggregations

SeleniumOperationException (com.axway.ats.uiengine.exceptions.SeleniumOperationException)22 PublicAtsApi (com.axway.ats.common.PublicAtsApi)16 WebElement (org.openqa.selenium.WebElement)13 HiddenHtmlElementState (com.axway.ats.uiengine.utilities.hiddenbrowser.HiddenHtmlElementState)7 RealHtmlElementState (com.axway.ats.uiengine.utilities.realbrowser.html.RealHtmlElementState)7 HtmlUnitWebElement (org.openqa.selenium.htmlunit.HtmlUnitWebElement)6 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 Select (org.openqa.selenium.support.ui.Select)4 ElementNotFoundException (com.axway.ats.uiengine.exceptions.ElementNotFoundException)3 Field (java.lang.reflect.Field)3 URL (java.net.URL)3 VerificationException (com.axway.ats.uiengine.exceptions.VerificationException)2 VerifyEqualityException (com.axway.ats.uiengine.exceptions.VerifyEqualityException)2 VerifyNotEqualityException (com.axway.ats.uiengine.exceptions.VerifyNotEqualityException)2 IncorrectnessListener (com.gargoylesoftware.htmlunit.IncorrectnessListener)2 Page (com.gargoylesoftware.htmlunit.Page)2 WebClient (com.gargoylesoftware.htmlunit.WebClient)2 WebRequest (com.gargoylesoftware.htmlunit.WebRequest)2 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)2