Example usage for org.openqa.selenium WebElement clear

List of usage examples for org.openqa.selenium WebElement clear

Introduction

In this page you can find the example usage for org.openqa.selenium WebElement clear.

Prototype

void clear();

Source Link

Document

If this element is a form entry element, this will reset its value.

Usage

From source file:com.pentaho.ctools.utils.ElementHelper.java

License:Apache License

/**
 * This method find the element and sendkeys.
 * /*ww  w .  j ava  2 s  . c o m*/
 * @param driver
 * @param locator
 * @param text
 */
public void SendKeysAndSubmit(final WebDriver driver, final By locator, final CharSequence... keysToSend) {
    this.log.debug("SendKeysAndSubmit::Enter");

    try {
        WebElement element = WaitForElementPresenceAndVisible(driver, locator);
        if (element != null) {
            element.clear();
            element.sendKeys(keysToSend);
            element.submit();
        } else {
            this.log.error("Element is null!");
        }
    } catch (StaleElementReferenceException e) {
        this.log.warn("Stale Element Reference Exception");
        SendKeysAndSubmit(driver, locator, keysToSend);
    }
    this.log.debug("SendKeysAndSubmit::Exit");
}

From source file:com.pentaho.ctools.utils.ElementHelper.java

License:Apache License

/**
 * This method find the element and clear the contents in the element 
 * (input or textarea).// w w w.ja  v a  2s.c o  m
 * 
 * @param driver
 * @param locator
 */
public void Clear(final WebDriver driver, final By locator) {
    this.log.debug("Clear::Enter");

    try {
        WebElement element = WaitForElementPresenceAndVisible(driver, locator);
        if (element != null) {
            element.clear();
        } else {
            this.log.error("Element is null!");
        }
    } catch (StaleElementReferenceException e) {
        this.log.warn("Stale Element Reference Exception");
        Clear(driver, locator);
    }
    this.log.debug("SendKeys::Exit");
}

From source file:com.pentaho.ctools.utils.ElementHelper.java

License:Apache License

/**
 * This method find the element, clear the field and sendkeys.
 * // w ww.  j av  a  2 s. com
 * @param driver
 * @param locator
 * @param text
 */
public void ClearAndSendKeys(final WebDriver driver, final By locator, final CharSequence... keysToSend) {
    this.log.debug("ClearAndSendKeys::Enter");

    try {
        WebElement element = WaitForElementPresenceAndVisible(driver, locator);
        if (element != null) {
            element.clear();
            element.sendKeys(keysToSend);
        } else {
            this.log.error("Element is null!");
        }
    } catch (StaleElementReferenceException e) {
        this.log.warn("Stale Element Reference Exception");
        SendKeys(driver, locator, keysToSend);
    }
    this.log.debug("ClearAndSendKeys::Exit");
}

From source file:com.pramati.wavemaker.pages.Deployment.java

License:Open Source License

/**
 * Set's username and password in Account info dialog box
 * //  w  w  w  .  ja v  a 2 s  .c o m
 * @param username
 * @param password
 */
public void setUserPassword(String username, String password) {
    waitForElementLocatedByID(USERNAME, getTimeOutInSeconds());
    WebElement userEle = Deployment().findElement(By.id(USERNAME))
            .findElement(By.cssSelector(ACCOUNT_USERNAME));
    userEle.clear();
    userEle.sendKeys(username);
    log.info("In Deployment page, Setting username in username field of Dialog window " + username);
    WebElement passEle = Deployment().findElement(By.id(PASSWORD))
            .findElement(By.cssSelector(ACCOUNT_PASSWORD));
    passEle.clear();
    log.info("In Deployment page, Setting password in password field of Dialog window " + password);
    passEle.sendKeys(password);
    log.info("In Deployment page, Clicking on Ok button of Dialog window ");
    Deployment().findElement(By.id(OK_BTN)).click();
    log.info("In Deployment page, Waiting for wait dialog window to close");
    waitForElementToDisableByID(STUDIO_DIALOG);
}

From source file:com.screenslicer.core.scrape.QueryCommon.java

License:Open Source License

public static boolean typeText(RemoteWebDriver driver, WebElement element, String text, boolean validate,
        boolean newline) {
    String elementVal = null;/*from  w  ww.j av  a 2 s.  com*/
    if (validate) {
        elementVal = element.getAttribute("value");
    }
    if (!validate || !text.equalsIgnoreCase(elementVal)) {
        Util.click(driver, element);
        if (validate) {
            element.clear();
            Util.driverSleepVeryShort();
        }
        if (!validate || !CommonUtil.isEmpty(element.getAttribute("value"))) {
            element.sendKeys(QueryForm.delete);
            Util.driverSleepVeryShort();
        }
        element.sendKeys(text);
        Util.driverSleepVeryShort();
        if (newline) {
            element.sendKeys("\n");
            Util.driverSleepLong();
        }
        return true;
    }
    return false;
}

From source file:com.screenslicer.core.scrape.QueryForm.java

License:Open Source License

public static void perform(RemoteWebDriver driver, FormQuery context) throws ActionFailed {
    try {//ww  w  . j  a v  a  2s.  c o  m
        Util.get(driver, context.site, true);
        Util.doClicks(driver, context.preAuthClicks, null);
        QueryCommon.doAuth(driver, context.credentials);
        Util.doClicks(driver, context.preSearchClicks, null);
        Map<String, HtmlNode> formControls = new HashMap<String, HtmlNode>();
        for (int i = 0; i < context.formSchema.length; i++) {
            formControls.put(context.formSchema[i].guid, context.formSchema[i]);
        }
        Map<String, List<String>> formData = context.formModel;
        boolean valueChanged = false;
        int count = 0;
        final int MAX_TRIES = 3;
        Element body = Util.openElement(driver, null, null, null);
        do {
            ++count;
            valueChanged = false;
            for (Map.Entry<String, List<String>> entry : formData.entrySet()) {
                try {
                    HtmlNode formControl = formControls.get(entry.getKey());
                    if (!CommonUtil.isEmpty(entry.getValue())) {
                        if ("select".equalsIgnoreCase(formControl.tagName)) {
                            if (WebApp.DEBUG) {
                                System.out.println("Query Form: select");
                            }
                            Select select = new Select(Util.toElement(driver, formControl, body));
                            if (select.isMultiple()) {
                                select.deselectAll();
                            }
                            List<WebElement> selectedElements = select.getAllSelectedOptions();
                            List<String> selectedStrings = new ArrayList<String>();
                            for (WebElement selectedElement : selectedElements) {
                                String selectedString = selectedElement.getAttribute("value");
                                if (!CommonUtil.isEmpty(selectedString)) {
                                    selectedStrings.add(selectedString);
                                }
                            }
                            boolean matches = true;
                            for (String selectedString : selectedStrings) {
                                if (!entry.getValue().contains(selectedString)) {
                                    matches = false;
                                    break;
                                }
                            }
                            if (!matches || selectedStrings.size() != entry.getValue().size()) {
                                for (String val : entry.getValue()) {
                                    valueChanged = true;
                                    select.selectByValue(val);
                                    Util.driverSleepVeryShort();
                                }
                            }
                        } else if ("input".equalsIgnoreCase(formControl.tagName)
                                && ("text".equalsIgnoreCase(formControl.type)
                                        || "search".equalsIgnoreCase(formControl.type))) {
                            if (WebApp.DEBUG) {
                                System.out.println("Query Form: input[text|search]");
                            }
                            WebElement element = Util.toElement(driver, formControl, body);
                            valueChanged = QueryCommon.typeText(driver, element, entry.getValue().get(0), true,
                                    false);
                        } else if ("input".equalsIgnoreCase(formControl.tagName)
                                && ("checkbox".equalsIgnoreCase(formControl.type)
                                        || "radio".equalsIgnoreCase(formControl.type))) {
                            if (WebApp.DEBUG) {
                                System.out.println("Query Form: input[checkbox|radio]");
                            }
                            WebElement element = Util.toElement(driver, formControl, body);
                            if (entry.getValue() != null && !entry.getValue().isEmpty()) {
                                if ("radio".equalsIgnoreCase(formControl.type)) {
                                    String elementVal = element.getAttribute("value");
                                    String schemaVal = formControl.value;
                                    String modelVal = entry.getValue().get(0);
                                    if (elementVal != null && schemaVal != null
                                            && elementVal.equalsIgnoreCase(schemaVal)
                                            && modelVal.equalsIgnoreCase(schemaVal)) {
                                        if (!element.isSelected()) {
                                            if (WebApp.DEBUG) {
                                                System.out.println("Clicking radio button");
                                            }
                                            valueChanged = Util.click(driver, element);
                                        }
                                    }
                                } else if (!element.isSelected()) {
                                    if (WebApp.DEBUG) {
                                        System.out.println("Clicking [checkbox|radio]");
                                    }
                                    valueChanged = Util.click(driver, element);
                                }
                            } else {
                                if (element.isSelected()) {
                                    if (WebApp.DEBUG) {
                                        System.out.println("Deselecting [checkbox|radio]");
                                    }
                                    valueChanged = true;
                                    element.clear();
                                    Util.driverSleepVeryShort();
                                }
                            }
                        }
                    }
                } catch (Throwable t) {
                    Log.exception(t);
                }
            }
        } while (valueChanged && count < MAX_TRIES);
        doSubmit(driver, context.formId);
        Util.doClicks(driver, context.postSearchClicks, null);
    } catch (Throwable t) {
        Log.exception(t);
        throw new ActionFailed(t);
    }
}

From source file:com.screenslicer.core.scrape.QueryKeyword.java

License:Open Source License

private static String doSearch(RemoteWebDriver driver, List<WebElement> searchBoxes, String searchQuery)
        throws ActionFailed {
    try {/*from   w w  w .j a  v  a 2  s .  c  om*/
        for (WebElement element : searchBoxes) {
            try {
                Util.click(driver, element);
                element.clear();
                Util.driverSleepVeryShort();
                if (!CommonUtil.isEmpty(element.getAttribute("value"))) {
                    element.sendKeys(delete);
                    Util.driverSleepVeryShort();
                }
                element.sendKeys(searchQuery);
                Util.driverSleepVeryShort();
                String beforeSource = driver.getPageSource();
                String beforeTitle = driver.getTitle();
                String beforeUrl = driver.getCurrentUrl();
                String windowHandle = driver.getWindowHandle();
                element.sendKeys("\n");
                Util.driverSleepLong();
                Util.cleanUpNewWindows(driver, windowHandle);
                String afterSource = driver.getPageSource();
                String afterTitle = driver.getTitle();
                String afterUrl = driver.getCurrentUrl();
                if (!beforeTitle.equals(afterTitle) || !beforeUrl.equals(afterUrl)
                        || Math.abs(beforeSource.length() - afterSource.length()) > MIN_SOURCE_DIFF) {
                    handleIframe(driver);
                    return driver.getPageSource();
                }
            } catch (Throwable t) {
                Log.exception(t);
            }
        }
    } catch (Throwable t) {
        Log.exception(t);
    }
    throw new ActionFailed();
}

From source file:com.sebuilder.interpreter.steptype.SetElementText.java

License:Apache License

@Override
public boolean run(TestRun ctx) {
    WebElement el = ctx.locator("locator").find(ctx);
    el.click();//w  ww .  j a v  a  2 s .  c  o  m
    el.clear();
    el.sendKeys(ctx.string("text"));
    return true;
}

From source file:com.smartqa.engine.WebEngine.java

License:Apache License

/**
 * fill web element//from w  ww  .  j a v a2s  .c o  m
 * 
 * @param name - name stands for web element
 * @param value - fill value
 * @return WebEngine
 */
public WebEngine fill(String name, String value) {
    WebElement element = locate(name);
    element.clear();
    element.sendKeys(value);
    CommonUtils.waiting(speed);
    return this;
}

From source file:com.sol.androidphone.AbstractAndroidPageObject.java

License:Apache License

/**
 * Type text into the specified element//from ww w.  j ava2 s. c om
 * @param element - element to be typed into
 * @param textToType - text to be typed
 * @param toClearBefore - if true the element will be cleared before typing
 * (as in the method clear()). false indicates that the method will not be
 * cleared
 */
protected void type(final WebElement element, final String textToType, boolean toClearBefore) {
    assert null != element;
    if (toClearBefore) {
        element.clear();
    }
    element.sendKeys(textToType);
}