Search in sources :

Example 21 with HtmlUnitDriver

use of org.openqa.selenium.htmlunit.HtmlUnitDriver in project archiva by apache.

the class AbstractSeleniumTest method goToLoginPage.

// Go to Login Page
public void goToLoginPage() {
    logger.info("Goto login page");
    loadPage(baseUrl, 30);
    WebDriverWait wait = new WebDriverWait(getWebDriver(), 30);
    wait.until(ExpectedConditions.presenceOfElementLocated(By.id("topbar-menu")));
    wait.until(ExpectedConditions.or(ExpectedConditions.visibilityOfElementLocated(By.id("logout-link")), ExpectedConditions.visibilityOfElementLocated(By.id("login-link-a"))));
    // are we already logged in ?
    if (// isElementPresent( "logoutLink" ) )
    isElementVisible("logout-link")) {
        logger.info("Logging out ");
        // so logout
        clickLinkWithLocator("logout-link-a", false);
    }
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("login-link-a")));
    clickLinkWithLocator("login-link-a", false);
    // TODO: Check after changing jquery, bootstrap or htmlunit version
    if (getWebDriver() instanceof HtmlUnitDriver) {
        ((JavascriptExecutor) getWebDriver()).executeScript("$('#modal-login').show();");
    }
    // END OF WORKAROUND
    wait = new WebDriverWait(getWebDriver(), 20);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("modal-login")));
    assertLoginModal();
}
Also used : JavascriptExecutor(org.openqa.selenium.JavascriptExecutor) WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait) HtmlUnitDriver(org.openqa.selenium.htmlunit.HtmlUnitDriver)

Example 22 with HtmlUnitDriver

use of org.openqa.selenium.htmlunit.HtmlUnitDriver in project selenified by Coveros.

the class TestSetup method setupDriver.

/**
 * this creates the webdriver object, which will be used to interact with
 * for all browser web tests
 *
 * @param browser      - what browser is being tested on
 * @param capabilities - what capabilities are being tested with
 * @return WebDriver: the driver to interact with for the test
 * @throws InvalidBrowserException If a browser that is not one specified in the
 *                                 Selenium.Browser class is used, this exception will be thrown
 */
public static WebDriver setupDriver(Browser browser, DesiredCapabilities capabilities) throws InvalidBrowserException {
    WebDriver driver;
    // check the browser
    switch(browser) {
        case HTMLUNIT:
            capabilities.setBrowserName("htmlunit");
            capabilities.setJavascriptEnabled(true);
            System.getProperties().put("org.apache.commons.logging.simplelog.defaultlog", "fatal");
            java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(Level.OFF);
            java.util.logging.Logger.getLogger("org.apache.http").setLevel(Level.OFF);
            driver = new HtmlUnitDriver(capabilities);
            break;
        case FIREFOX:
            FirefoxDriverManager.getInstance().forceCache().setup();
            FirefoxOptions firefoxOptions = new FirefoxOptions(capabilities);
            if (System.getProperty(HEADLESS_INPUT) != null && "true".equals(System.getProperty(HEADLESS_INPUT))) {
                firefoxOptions.setHeadless(true);
            }
            driver = new FirefoxDriver(firefoxOptions);
            break;
        case CHROME:
            ChromeDriverManager.getInstance().forceCache().setup();
            ChromeOptions chromeOptions = new ChromeOptions();
            chromeOptions = chromeOptions.merge(capabilities);
            if (System.getProperty(HEADLESS_INPUT) != null && "true".equals(System.getProperty(HEADLESS_INPUT))) {
                chromeOptions.setHeadless(true);
            }
            driver = new ChromeDriver(chromeOptions);
            break;
        case INTERNETEXPLORER:
            InternetExplorerDriverManager.getInstance().forceCache().setup();
            InternetExplorerOptions internetExplorerOptions = new InternetExplorerOptions(capabilities);
            driver = new InternetExplorerDriver(internetExplorerOptions);
            break;
        case EDGE:
            EdgeDriverManager.getInstance().forceCache().setup();
            EdgeOptions edgeOptions = new EdgeOptions();
            edgeOptions = edgeOptions.merge(capabilities);
            driver = new EdgeDriver(edgeOptions);
            break;
        case SAFARI:
            SafariOptions safariOptions = new SafariOptions(capabilities);
            driver = new SafariDriver(safariOptions);
            break;
        case OPERA:
            OperaDriverManager.getInstance().forceCache().setup();
            driver = new OperaDriver(capabilities);
            break;
        case PHANTOMJS:
            PhantomJsDriverManager.getInstance().forceCache().setup();
            driver = new PhantomJSDriver(capabilities);
            break;
        // if the browser is not listed, throw an error
        default:
            throw new InvalidBrowserException("The selected browser " + browser + " is not an applicable choice");
    }
    return driver;
}
Also used : WebDriver(org.openqa.selenium.WebDriver) PhantomJSDriver(org.openqa.selenium.phantomjs.PhantomJSDriver) OperaDriver(org.openqa.selenium.opera.OperaDriver) InternetExplorerDriver(org.openqa.selenium.ie.InternetExplorerDriver) HtmlUnitDriver(org.openqa.selenium.htmlunit.HtmlUnitDriver) InternetExplorerOptions(org.openqa.selenium.ie.InternetExplorerOptions) SafariOptions(org.openqa.selenium.safari.SafariOptions) SafariDriver(org.openqa.selenium.safari.SafariDriver) FirefoxOptions(org.openqa.selenium.firefox.FirefoxOptions) EdgeOptions(org.openqa.selenium.edge.EdgeOptions) EdgeDriver(org.openqa.selenium.edge.EdgeDriver) FirefoxDriver(org.openqa.selenium.firefox.FirefoxDriver) ChromeOptions(org.openqa.selenium.chrome.ChromeOptions) ChromeDriver(org.openqa.selenium.chrome.ChromeDriver) InvalidBrowserException(com.coveros.selenified.exceptions.InvalidBrowserException)

Example 23 with HtmlUnitDriver

use of org.openqa.selenium.htmlunit.HtmlUnitDriver in project ats-framework by Axway.

the class HiddenHtmlElementUtils method mouseClick.

@PublicAtsApi
public static void mouseClick(HtmlUnitWebElement webElement) {
    Method getElementForOperationMethod = null;
    boolean getElementForOperationMethodAccessible = false;
    Method moveOutIfNeededMethod = null;
    boolean moveOutIfNeededMethodAccessible = false;
    Method updateActiveElementMethod = null;
    boolean updateActiveElementMethodAccessible = false;
    try {
        // Allow invoking click on non-visible elements. Prevents HtmlUnit check for currently visible element
        // TODO: remove this possibility by removing UiEngineConfigurator#workWithInvisibleElements()
        // change access modifiers of some methods
        getElementForOperationMethod = HtmlUnitMouse.class.getDeclaredMethod("getElementForOperation", Coordinates.class);
        getElementForOperationMethodAccessible = getElementForOperationMethod.isAccessible();
        getElementForOperationMethod.setAccessible(true);
        moveOutIfNeededMethod = HtmlUnitMouse.class.getDeclaredMethod("moveOutIfNeeded", DomElement.class);
        moveOutIfNeededMethodAccessible = moveOutIfNeededMethod.isAccessible();
        moveOutIfNeededMethod.setAccessible(true);
        updateActiveElementMethod = HtmlUnitMouse.class.getDeclaredMethod("updateActiveElement", DomElement.class);
        updateActiveElementMethodAccessible = updateActiveElementMethod.isAccessible();
        updateActiveElementMethod.setAccessible(true);
        // get the target element
        HtmlUnitDriver htmlUnitDriver = (HtmlUnitDriver) webElement.getWrappedDriver();
        HtmlUnitMouse mouse = (HtmlUnitMouse) htmlUnitDriver.getMouse();
        HtmlElement element = (HtmlElement) getElementForOperationMethod.invoke(mouse, webElement.getCoordinates());
        moveOutIfNeededMethod.invoke(mouse, element);
        if (htmlUnitDriver.isJavascriptEnabled()) {
            if (!(element instanceof HtmlInput)) {
                element.focus();
            }
            element.mouseOver();
            element.mouseMove();
        }
        HtmlUnitKeyboard keyboard = (HtmlUnitKeyboard) htmlUnitDriver.getKeyboard();
        element.click(keyboard.isShiftPressed(), keyboard.isCtrlPressed(), keyboard.isAltPressed());
        updateActiveElementMethod.invoke(mouse, element);
    } catch (IOException ioe) {
        throw new WebDriverException(ioe);
    } catch (ScriptException e) {
        // we need only our exception if such exists
        Throwable uiEngineException = e.getCause();
        while (uiEngineException != null && !uiEngineException.getClass().getName().toLowerCase().contains("com.axway.ats")) {
            uiEngineException = uiEngineException.getCause();
        }
        if (uiEngineException != null) {
            throw (RuntimeException) uiEngineException;
        }
        // Log the exception with level WARN, because in the main Selenium implementation
        // (HtmlUnitMouse.click(coordinates)) the exception is even skipped
        log.warn("Script error while clicking web element. " + webElement.toString(), e);
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        // Restore accessibility modifier
        if (getElementForOperationMethod != null) {
            getElementForOperationMethod.setAccessible(getElementForOperationMethodAccessible);
        }
        if (moveOutIfNeededMethod != null) {
            moveOutIfNeededMethod.setAccessible(moveOutIfNeededMethodAccessible);
        }
        if (updateActiveElementMethod != null) {
            updateActiveElementMethod.setAccessible(updateActiveElementMethodAccessible);
        }
    }
}
Also used : HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) Coordinates(org.openqa.selenium.interactions.Coordinates) Method(java.lang.reflect.Method) IOException(java.io.IOException) HtmlInput(com.gargoylesoftware.htmlunit.html.HtmlInput) WebDriverException(org.openqa.selenium.WebDriverException) IOException(java.io.IOException) ScriptException(com.gargoylesoftware.htmlunit.ScriptException) HtmlUnitDriver(org.openqa.selenium.htmlunit.HtmlUnitDriver) ScriptException(com.gargoylesoftware.htmlunit.ScriptException) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) HtmlUnitMouse(org.openqa.selenium.htmlunit.HtmlUnitMouse) HtmlUnitKeyboard(org.openqa.selenium.htmlunit.HtmlUnitKeyboard) WebDriverException(org.openqa.selenium.WebDriverException) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 24 with HtmlUnitDriver

use of org.openqa.selenium.htmlunit.HtmlUnitDriver in project acceptance-test-harness by jenkinsci.

the class Scroller method scrollIntoView.

/**
 * The framework is expected to take care of the correct scrolling. When you are tempted to scroll from PageObjects
 * or tests, there is likely a framework problem to be fixed.
 */
public void scrollIntoView(WebElement e, WebDriver driver) {
    if (driver instanceof HtmlUnitDriver || (driver instanceof WrapsDriver && ((WrapsDriver) driver).getWrappedDriver() instanceof HtmlUnitDriver)) {
        return;
    }
    WebElement element = e;
    if (Objects.equals(element.getTagName(), "option")) {
        // scroll select into view not option
        element = e.findElement(By.xpath(".."));
    }
    final int eYCoord = element.getLocation().getY();
    final int eXCoord = element.getLocation().getX();
    final String id = element.getAttribute("id");
    final JavascriptExecutor executor = (JavascriptExecutor) driver;
    // Wait until web element is successfully scrolled.
    try {
        new Wait<>(Boolean.TRUE).withTimeout(5, // Wall-clock time
        TimeUnit.SECONDS).until(() -> (Boolean) executor.executeScript(scrollJs, eYCoord, eXCoord, id));
    } catch (TimeoutException ex) {
        // Scrolling failed, but sometimes the element to click is already visible, let the test continue and eventually fail later
        // This log message should be sufficient to diagnose the issue
        LOGGER.log(Level.WARNING, "Scrolling failed, letting the test to continue anyways, but \"Element is not clickable\" error will likely be thrown", ex);
    }
}
Also used : JavascriptExecutor(org.openqa.selenium.JavascriptExecutor) WebElement(org.openqa.selenium.WebElement) Wait(org.jenkinsci.test.acceptance.junit.Wait) WrapsDriver(org.openqa.selenium.WrapsDriver) HtmlUnitDriver(org.openqa.selenium.htmlunit.HtmlUnitDriver) TimeoutException(org.openqa.selenium.TimeoutException)

Example 25 with HtmlUnitDriver

use of org.openqa.selenium.htmlunit.HtmlUnitDriver in project cia by Hack23.

the class AbstractRoleSystemTest method getWebDriver.

/**
 * Gets the web driver.
 *
 * @return the web driver
 */
protected final synchronized WebDriver getWebDriver() {
    WebDriver driver = null;
    if ("firefox".equals(browser)) {
        final DesiredCapabilities capabilities = DesiredCapabilities.firefox();
        capabilities.setCapability("marionette", true);
        driver = new FirefoxDriver(capabilities);
        driver.manage().window().maximize();
    } else if ("chrome".equals(browser)) {
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("--allow-insecure-localhost", "--start-maximized");
        driver = new ChromeDriver(chromeOptions);
    } else if ("htmlunit-firefox".equals(browser)) {
        final HtmlUnitDriver htmlUnitDriver = new HtmlUnitDriver(BrowserVersion.FIREFOX_45);
        htmlUnitDriver.setJavascriptEnabled(true);
        driver = htmlUnitDriver;
    } else if ("htmlunit-ie11".equals(browser)) {
        final HtmlUnitDriver htmlUnitDriver = new HtmlUnitDriver(BrowserVersion.INTERNET_EXPLORER);
        htmlUnitDriver.setJavascriptEnabled(true);
        driver = htmlUnitDriver;
    } else if ("htmlunit-edge".equals(browser)) {
        final HtmlUnitDriver htmlUnitDriver = new HtmlUnitDriver(BrowserVersion.EDGE);
        htmlUnitDriver.setJavascriptEnabled(true);
        driver = htmlUnitDriver;
    } else if ("htmlunit-chrome".equals(browser)) {
        final HtmlUnitDriver htmlUnitDriver = new HtmlUnitDriver(BrowserVersion.CHROME);
        htmlUnitDriver.setJavascriptEnabled(true);
        driver = htmlUnitDriver;
    } else {
        fail("No valid browser parameter:" + browser);
    }
    driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
    driver.manage().timeouts().setScriptTimeout(30, TimeUnit.SECONDS);
    webDriverMap.put(browser, driver);
    return driver;
}
Also used : WebDriver(org.openqa.selenium.WebDriver) FirefoxDriver(org.openqa.selenium.firefox.FirefoxDriver) DesiredCapabilities(org.openqa.selenium.remote.DesiredCapabilities) ChromeOptions(org.openqa.selenium.chrome.ChromeOptions) ChromeDriver(org.openqa.selenium.chrome.ChromeDriver) HtmlUnitDriver(org.openqa.selenium.htmlunit.HtmlUnitDriver)

Aggregations

HtmlUnitDriver (org.openqa.selenium.htmlunit.HtmlUnitDriver)39 WebDriver (org.openqa.selenium.WebDriver)12 WebElement (org.openqa.selenium.WebElement)10 ChromeDriver (org.openqa.selenium.chrome.ChromeDriver)9 ChromeOptions (org.openqa.selenium.chrome.ChromeOptions)9 FirefoxDriver (org.openqa.selenium.firefox.FirefoxDriver)7 InternetExplorerDriver (org.openqa.selenium.ie.InternetExplorerDriver)6 Before (org.junit.Before)5 DesiredCapabilities (org.openqa.selenium.remote.DesiredCapabilities)5 Test (org.junit.Test)4 SafariDriver (org.openqa.selenium.safari.SafariDriver)4 PublicAtsApi (com.axway.ats.common.PublicAtsApi)3 BrowserVersion (com.gargoylesoftware.htmlunit.BrowserVersion)3 WebClient (com.gargoylesoftware.htmlunit.WebClient)3 File (java.io.File)3 URL (java.net.URL)3 TimeoutException (org.openqa.selenium.TimeoutException)3 EdgeDriver (org.openqa.selenium.edge.EdgeDriver)3 PhantomJSDriver (org.openqa.selenium.phantomjs.PhantomJSDriver)3 RemoteWebDriver (org.openqa.selenium.remote.RemoteWebDriver)3