Search in sources :

Example 1 with LocalFileDetector

use of org.openqa.selenium.remote.LocalFileDetector in project carina by qaprosoft.

the class ExtendedWebElement method overrideAction.

// single place for all supported UI actions in carina core
private Object overrideAction(ACTION_NAME actionName, Object... inputArgs) {
    Object output = executeAction(actionName, new ActionSteps() {

        @Override
        public void doClick() {
            try {
                DriverListener.setMessages(Messager.ELEMENT_CLICKED.getMessage(getName()), Messager.ELEMENT_NOT_CLICKED.getMessage(getNameWithLocator()));
                try {
                    element.click();
                } catch (ElementNotInteractableException e) {
                    // TODO: investigate if more exceptions should be catched
                    // not visible so we can't interact using selenium or actions
                    LOGGER.warn("Trying to do click by JavascriptExecutor because element '" + getNameWithLocator() + "' is not visible...");
                    JavascriptExecutor executor = (JavascriptExecutor) getDriver();
                    executor.executeScript("arguments[0].click();", element);
                }
            } catch (WebDriverException e) {
                if (e != null && (e.getMessage().contains("Other element would receive the click:"))) {
                    LOGGER.warn("Trying to do click by Actions due to the: " + e.getMessage());
                    Actions actions = new Actions(getDriver());
                    actions.moveToElement(element).click().perform();
                } else {
                    throw e;
                }
            }
        }

        @Override
        public // click for mobile devices
        void doTap() {
            DriverListener.setMessages(Messager.ELEMENT_CLICKED.getMessage(getName()), Messager.ELEMENT_NOT_CLICKED.getMessage(getNameWithLocator()));
            element.click();
        }

        @Override
        public void doDoubleClick() {
            DriverListener.setMessages(Messager.ELEMENT_DOUBLE_CLICKED.getMessage(getName()), Messager.ELEMENT_NOT_DOUBLE_CLICKED.getMessage(getNameWithLocator()));
            WebDriver drv = getDriver();
            Actions action = new Actions(drv);
            action.moveToElement(element).doubleClick(element).build().perform();
        }

        @Override
        public void doHover(Integer xOffset, Integer yOffset) {
            DriverListener.setMessages(Messager.ELEMENT_HOVERED.getMessage(getName()), Messager.ELEMENT_NOT_HOVERED.getMessage(getNameWithLocator()));
            WebDriver drv = getDriver();
            Actions action = new Actions(drv);
            if (xOffset != null && yOffset != null) {
                action.moveToElement(element, xOffset, yOffset).build().perform();
            } else {
                action.moveToElement(element).build().perform();
            }
        }

        @Override
        public void doSendKeys(Keys keys) {
            DriverListener.setMessages(Messager.KEYS_SEND_TO_ELEMENT.getMessage(keys.toString(), getName()), Messager.KEYS_NOT_SEND_TO_ELEMENT.getMessage(keys.toString(), getNameWithLocator()));
            element.sendKeys(keys);
        }

        @Override
        public void doType(String text) {
            final String decryptedText = cryptoTool.decryptByPattern(text, CRYPTO_PATTERN);
            /*				if (!element.getText().isEmpty()) {
    				DriverListener.setMessages(Messager.KEYS_CLEARED_IN_ELEMENT.getMessage(getName()),
    						Messager.KEYS_NOT_CLEARED_IN_ELEMENT.getMessage(getNameWithLocator()));
    				element.clear();
				}
*/
            DriverListener.setMessages(Messager.KEYS_CLEARED_IN_ELEMENT.getMessage(getName()), Messager.KEYS_NOT_CLEARED_IN_ELEMENT.getMessage(getNameWithLocator()));
            element.clear();
            String textLog = (!decryptedText.equals(text) ? "********" : text);
            DriverListener.setMessages(Messager.KEYS_SEND_TO_ELEMENT.getMessage(textLog, getName()), Messager.KEYS_NOT_SEND_TO_ELEMENT.getMessage(textLog, getNameWithLocator()));
            element.sendKeys(decryptedText);
        }

        @Override
        public void doAttachFile(String filePath) {
            final String decryptedText = cryptoTool.decryptByPattern(filePath, CRYPTO_PATTERN);
            String textLog = (!decryptedText.equals(filePath) ? "********" : filePath);
            DriverListener.setMessages(Messager.FILE_ATTACHED.getMessage(textLog, getName()), Messager.FILE_NOT_ATTACHED.getMessage(textLog, getNameWithLocator()));
            ((JavascriptExecutor) getDriver()).executeScript("arguments[0].style.display = 'block';", element);
            ((RemoteWebDriver) castDriver(getDriver())).setFileDetector(new LocalFileDetector());
            element.sendKeys(decryptedText);
        }

        @Override
        public String doGetText() {
            String text = element.getText();
            LOGGER.debug(Messager.ELEMENT_ATTRIBUTE_FOUND.getMessage("Text", text, getName()));
            return text;
        }

        @Override
        public Point doGetLocation() {
            Point point = element.getLocation();
            LOGGER.debug(Messager.ELEMENT_ATTRIBUTE_FOUND.getMessage("Location", point.toString(), getName()));
            return point;
        }

        @Override
        public Dimension doGetSize() {
            Dimension dim = element.getSize();
            LOGGER.debug(Messager.ELEMENT_ATTRIBUTE_FOUND.getMessage("Size", dim.toString(), getName()));
            return dim;
        }

        @Override
        public String doGetAttribute(String name) {
            String attribute = element.getAttribute(name);
            LOGGER.debug(Messager.ELEMENT_ATTRIBUTE_FOUND.getMessage(name, attribute, getName()));
            return attribute;
        }

        @Override
        public void doRightClick() {
            DriverListener.setMessages(Messager.ELEMENT_RIGHT_CLICKED.getMessage(getName()), Messager.ELEMENT_NOT_RIGHT_CLICKED.getMessage(getNameWithLocator()));
            WebDriver drv = getDriver();
            Actions action = new Actions(drv);
            action.moveToElement(element).contextClick(element).build().perform();
        }

        @Override
        public void doCheck() {
            DriverListener.setMessages(Messager.CHECKBOX_CHECKED.getMessage(getName()), null);
            boolean isSelected = element.isSelected();
            if (element.getAttribute("checked") != null) {
                isSelected |= element.getAttribute("checked").equalsIgnoreCase("true");
            }
            if (!isSelected) {
                click();
            }
        }

        @Override
        public void doUncheck() {
            DriverListener.setMessages(Messager.CHECKBOX_UNCHECKED.getMessage(getName()), null);
            boolean isSelected = element.isSelected();
            if (element.getAttribute("checked") != null) {
                isSelected |= element.getAttribute("checked").equalsIgnoreCase("true");
            }
            if (isSelected) {
                click();
            }
        }

        @Override
        public boolean doIsChecked() {
            boolean res = element.isSelected();
            if (element.getAttribute("checked") != null) {
                res |= element.getAttribute("checked").equalsIgnoreCase("true");
            }
            return res;
        }

        @Override
        public boolean doSelect(String text) {
            final String decryptedSelectText = cryptoTool.decryptByPattern(text, CRYPTO_PATTERN);
            String textLog = (!decryptedSelectText.equals(text) ? "********" : text);
            DriverListener.setMessages(Messager.SELECT_BY_TEXT_PERFORMED.getMessage(textLog, getName()), Messager.SELECT_BY_TEXT_NOT_PERFORMED.getMessage(textLog, getNameWithLocator()));
            final Select s = new Select(getCachedElement());
            // [VD] do not use selectByValue as modern controls could have only visible value without value
            s.selectByVisibleText(decryptedSelectText);
            return true;
        }

        @Override
        public boolean doSelectValues(String[] values) {
            boolean result = true;
            for (String value : values) {
                if (!select(value)) {
                    result = false;
                }
            }
            return result;
        }

        @Override
        public boolean doSelectByMatcher(BaseMatcher<String> matcher) {
            DriverListener.setMessages(Messager.SELECT_BY_MATCHER_TEXT_PERFORMED.getMessage(matcher.toString(), getName()), Messager.SELECT_BY_MATCHER_TEXT_NOT_PERFORMED.getMessage(matcher.toString(), getNameWithLocator()));
            final Select s = new Select(getCachedElement());
            String fullTextValue = null;
            for (WebElement option : s.getOptions()) {
                if (matcher.matches(option.getText())) {
                    fullTextValue = option.getText();
                    break;
                }
            }
            s.selectByVisibleText(fullTextValue);
            return true;
        }

        @Override
        public boolean doSelectByPartialText(String partialSelectText) {
            DriverListener.setMessages(Messager.SELECT_BY_TEXT_PERFORMED.getMessage(partialSelectText, getName()), Messager.SELECT_BY_TEXT_NOT_PERFORMED.getMessage(partialSelectText, getNameWithLocator()));
            final Select s = new Select(getCachedElement());
            String fullTextValue = null;
            for (WebElement option : s.getOptions()) {
                if (option.getText().contains(partialSelectText)) {
                    fullTextValue = option.getText();
                    break;
                }
            }
            s.selectByVisibleText(fullTextValue);
            return true;
        }

        @Override
        public boolean doSelectByIndex(int index) {
            DriverListener.setMessages(Messager.SELECT_BY_INDEX_PERFORMED.getMessage(String.valueOf(index), getName()), Messager.SELECT_BY_INDEX_NOT_PERFORMED.getMessage(String.valueOf(index), getNameWithLocator()));
            final Select s = new Select(getCachedElement());
            s.selectByIndex(index);
            return true;
        }

        @Override
        public String doGetSelectedValue() {
            final Select s = new Select(getCachedElement());
            return s.getAllSelectedOptions().get(0).getText();
        }

        @Override
        public List<String> doGetSelectedValues() {
            final Select s = new Select(getCachedElement());
            List<String> values = new ArrayList<String>();
            for (WebElement we : s.getAllSelectedOptions()) {
                values.add(we.getText());
            }
            return values;
        }
    }, inputArgs);
    return output;
}
Also used : WebDriver(org.openqa.selenium.WebDriver) EventFiringWebDriver(org.openqa.selenium.support.events.EventFiringWebDriver) RemoteWebDriver(org.openqa.selenium.remote.RemoteWebDriver) JavascriptExecutor(org.openqa.selenium.JavascriptExecutor) Actions(org.openqa.selenium.interactions.Actions) LocalFileDetector(org.openqa.selenium.remote.LocalFileDetector) Point(org.openqa.selenium.Point) Dimension(org.openqa.selenium.Dimension) WebElement(org.openqa.selenium.WebElement) RemoteWebElement(org.openqa.selenium.remote.RemoteWebElement) IWebElement(com.qaprosoft.carina.core.foundation.utils.IWebElement) Point(org.openqa.selenium.Point) ElementNotInteractableException(org.openqa.selenium.ElementNotInteractableException) Keys(org.openqa.selenium.Keys) Select(org.openqa.selenium.support.ui.Select) List(java.util.List) ArrayList(java.util.ArrayList) WebDriverException(org.openqa.selenium.WebDriverException)

Example 2 with LocalFileDetector

use of org.openqa.selenium.remote.LocalFileDetector in project elastest-instrumentation-manager by elastest.

the class EimPluginBaseTest method installElasTestPlugin.

protected void installElasTestPlugin(WebDriver webDriver) throws IOException {
    int secondsTimeout = 60;
    // Install plugin
    log.info("Installing plugin from: {}", pluginPath);
    By inputFileName = By.name("name");
    WebElement uploadFile = webDriver.findElement(inputFileName);
    ((RemoteWebElement) uploadFile).setFileDetector(new LocalFileDetector());
    uploadFile.sendKeys(pluginPath);
    By uploadButton = By.xpath("//button[contains(string(), 'Upload')]");
    webDriver.findElement(uploadButton).click();
    // Check the plugin installation is ok
    log.info("Checking installation status");
    boolean pluginAlreadyInstalled = false;
    try {
        String installationStatusXpath = "//table/tbody/tr/td[contains(string(), 'Success')]";
        getElementByXpath(webDriver, ExpectedConditionsEnum.VISIBILITY_OF_ELEMENT_LOCATED, installationStatusXpath, secondsTimeout);
    } catch (TimeoutException te) {
        String alreadyInstalledMessageXpath = "//table/tbody/tr/td[contains(string(), 'elastest plugin is already installed')]";
        getElementByXpath(webDriver, ExpectedConditionsEnum.VISIBILITY_OF_ELEMENT_LOCATED, alreadyInstalledMessageXpath, secondsTimeout);
        pluginAlreadyInstalled = true;
    }
    if (pluginAlreadyInstalled) {
        navigateTo(webDriver, jenkinsRestartRelPath);
        By yesButton = By.xpath("//button[contains(string(), 'Yes')]");
        webDriver.findElement(yesButton).click();
        // wait for login form
        getElementById(webDriver, ExpectedConditionsEnum.VISIBILITY_OF_ELEMENT_LOCATED, "j_username", 180);
        loginOnJenkins(webDriver);
    }
    log.info("Plugin installation finished");
    log.info("Navigate to main page");
    By homeLink = By.linkText("Jenkins");
    webDriver.findElement(homeLink).click();
}
Also used : LocalFileDetector(org.openqa.selenium.remote.LocalFileDetector) By(org.openqa.selenium.By) RemoteWebElement(org.openqa.selenium.remote.RemoteWebElement) WebElement(org.openqa.selenium.WebElement) RemoteWebElement(org.openqa.selenium.remote.RemoteWebElement) TimeoutException(org.openqa.selenium.TimeoutException)

Example 3 with LocalFileDetector

use of org.openqa.selenium.remote.LocalFileDetector in project muikku by otavanopisto.

the class AbstractUITest method createSauceWebDriver.

protected RemoteWebDriver createSauceWebDriver() throws MalformedURLException {
    final DesiredCapabilities capabilities = new DesiredCapabilities();
    final String seleniumVersion = System.getProperty("it.selenium.version");
    final String browser = getBrowser();
    final String browserVersion = getBrowserVersion();
    final String browserResolution = getBrowserResolution();
    final String platform = getSaucePlatform();
    capabilities.setCapability(CapabilityType.BROWSER_NAME, browser);
    capabilities.setCapability(CapabilityType.VERSION, browserVersion);
    capabilities.setCapability(CapabilityType.PLATFORM, platform);
    capabilities.setCapability("name", getClass().getSimpleName() + ':' + testName.getMethodName());
    capabilities.setCapability("tags", Arrays.asList(String.valueOf(getTestStartTime())));
    capabilities.setCapability("build", getProjectVersion());
    capabilities.setCapability("video-upload-on-pass", false);
    capabilities.setCapability("capture-html", true);
    capabilities.setCapability("timeZone", "Universal");
    capabilities.setCapability("seleniumVersion", seleniumVersion);
    if (!StringUtils.isBlank(browserResolution)) {
        capabilities.setCapability("screenResolution", browserResolution);
    }
    if (getSauceTunnelId() != null) {
        capabilities.setCapability("tunnel-identifier", getSauceTunnelId());
    }
    RemoteWebDriver remoteWebDriver = new RemoteWebDriver(new URL(String.format("http://%s:%s@ondemand.saucelabs.com:80/wd/hub", getSauceUsername(), getSauceAccessKey())), capabilities);
    remoteWebDriver.setFileDetector(new LocalFileDetector());
    return remoteWebDriver;
}
Also used : LocalFileDetector(org.openqa.selenium.remote.LocalFileDetector) RemoteWebDriver(org.openqa.selenium.remote.RemoteWebDriver) DesiredCapabilities(org.openqa.selenium.remote.DesiredCapabilities) URL(java.net.URL)

Aggregations

LocalFileDetector (org.openqa.selenium.remote.LocalFileDetector)3 WebElement (org.openqa.selenium.WebElement)2 RemoteWebDriver (org.openqa.selenium.remote.RemoteWebDriver)2 RemoteWebElement (org.openqa.selenium.remote.RemoteWebElement)2 IWebElement (com.qaprosoft.carina.core.foundation.utils.IWebElement)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 By (org.openqa.selenium.By)1 Dimension (org.openqa.selenium.Dimension)1 ElementNotInteractableException (org.openqa.selenium.ElementNotInteractableException)1 JavascriptExecutor (org.openqa.selenium.JavascriptExecutor)1 Keys (org.openqa.selenium.Keys)1 Point (org.openqa.selenium.Point)1 TimeoutException (org.openqa.selenium.TimeoutException)1 WebDriver (org.openqa.selenium.WebDriver)1 WebDriverException (org.openqa.selenium.WebDriverException)1 Actions (org.openqa.selenium.interactions.Actions)1 DesiredCapabilities (org.openqa.selenium.remote.DesiredCapabilities)1 EventFiringWebDriver (org.openqa.selenium.support.events.EventFiringWebDriver)1