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.liferay.faces.test.showcase.inputsecret.InputSecretValidationTester.java

License:Open Source License

@Test
public void runInputSecretValidationTest() throws Exception {

    Browser browser = Browser.getInstance();
    browser.navigateToURL(inputSecretURL + "/validation");

    // Wait to begin the test until the submit button is rendered.
    browser.waitForElementVisible(submitButtonXpath);

    // Test that the web page shows an error message when an invalid value is submitted.
    WebElement input = browser.getElement(inputXpath);
    input.clear();

    String invalidText = "HelloWorldcom";
    input.sendKeys(invalidText);//w  w  w .ja  va  2 s  .c om
    browser.clickAndWaitForAjaxRerender(submitButtonXpath);
    browser.assertElementVisible(errorXpath);

    // Test that a valid value submits successfully.
    input = browser.getElement(inputXpath);
    input.clear();

    String text = "Hello@World.com";
    input.sendKeys(text);
    browser.clickAndWaitForAjaxRerender(submitButtonXpath);
    browser.assertElementTextVisible(modelValueXpath, text);

    // Test that the web page shows an error message when an invalid value is submitted.
    input = browser.getElement(inputXpathRight);
    input.clear();
    input.sendKeys(invalidText);
    browser.clickAndWaitForAjaxRerender(submitButtonXpathRight);
    browser.assertElementVisible(errorXpath);

    // Test that a valid value submits successfully.
    input = browser.getElement(inputXpathRight);
    input.clear();
    input.sendKeys(text);
    browser.clickAndWaitForAjaxRerender(submitButtonXpathRight);
    browser.assertElementTextVisible(modelValueXpathRight, text);
}

From source file:com.liferay.faces.test.showcase.inputtext.InputTextConversionTester.java

License:Open Source License

@Test
public void runInputTextConversionTest() throws Exception {

    Browser browser = Browser.getInstance();
    browser.navigateToURL(inputTextURL + "/conversion");

    // Wait to begin the test until the submit button is rendered.
    browser.waitForElementVisible(submitButtonXpath);

    // Test that the web page shows an error message when an invalid value is submitted.
    WebElement input = browser.getElement(inputXpath);
    input.clear();

    String invalidText = "apr 3 33";
    input.sendKeys(invalidText);/*from   ww  w  .  j a va2s  .  co m*/
    browser.clickAndWaitForAjaxRerender(submitButtonXpath);
    browser.assertElementVisible(errorXpath);

    // Test that a valid value submits successfully.
    input = browser.getElement(inputXpath);
    input.clear();

    String text = "apr 3, 33";
    input.sendKeys(text);
    browser.clickAndWaitForAjaxRerender(submitButtonXpath);

    String textOutput = "Apr 3, 0033";
    browser.assertElementTextVisible(modelValueXpath, textOutput);

    // Test that the web page shows an error message when an invalid value is submitted.
    input = browser.getElement(inputXpathRight);
    input.clear();
    invalidText = "4/333";
    input.sendKeys(invalidText);
    browser.clickAndWaitForAjaxRerender(submitButtonXpathRight);
    browser.assertElementVisible(errorXpath);

    // Test that a valid value submits successfully.
    input = browser.getElement(inputXpathRight);
    input.clear();
    text = "4/3/33";
    input.sendKeys(text);
    browser.clickAndWaitForAjaxRerender(submitButtonXpathRight);
    textOutput = "04/03/0033";
    browser.assertElementTextVisible(modelValueXpathRight, textOutput);
}

From source file:com.liferay.faces.test.showcase.inputtext.InputTextTester.java

License:Open Source License

protected void runInputTextConversionTest(String componentName, String inputText1Xpath, String inputText2Xpath)
        throws Exception {

    BrowserDriver browserDriver = getBrowserDriver();
    navigateToUseCase(browserDriver, componentName, "conversion");

    // Test that the web page shows an error message when an invalid value is submitted.
    WebElement input = browserDriver.findElementByXpath(inputText1Xpath);
    input.clear();

    String invalidText1 = "apr 3 33";
    input.sendKeys(invalidText1);/*from ww w .ja va 2s .  com*/
    browserDriver.clickElementAndWaitForRerender(submitButton1Xpath);

    WaitingAsserter waitingAsserter = getWaitingAsserter();
    waitingAsserter.assertElementDisplayed(error1Xpath);

    // Test that a valid value submits successfully.
    input = browserDriver.findElementByXpath(inputText1Xpath);
    input.clear();

    String text1 = "apr 3, 33";
    input.sendKeys(text1);
    browserDriver.clickElementAndWaitForRerender(submitButton1Xpath);

    String textOutput1 = "Apr 3, 0033";
    waitingAsserter.assertTextPresentInElement(textOutput1, modelValue1Xpath);

    // Test that the web page shows an error message when an invalid value is submitted.
    input = browserDriver.findElementByXpath(inputText2Xpath);
    input.clear();

    String invalidText2 = "4/333";
    input.sendKeys(invalidText2);
    browserDriver.clickElementAndWaitForRerender(submitButton2Xpath);
    waitingAsserter.assertElementDisplayed(error1Xpath);

    // Test that a valid value submits successfully.
    input = browserDriver.findElementByXpath(inputText2Xpath);
    input.clear();

    String text2 = "4/3/33";
    input.sendKeys(text2);
    browserDriver.clickElementAndWaitForRerender(submitButton2Xpath);

    String textOutput2 = "04/03/0033";
    waitingAsserter.assertTextPresentInElement(textOutput2, modelValue2Xpath);
}

From source file:com.liferay.faces.test.showcase.inputtext.InputTextTester.java

License:Open Source License

protected void runInputTextValidationTest(String componentName, String inputText1Xpath, String inputText2Xpath)
        throws Exception {

    BrowserDriver browserDriver = getBrowserDriver();
    navigateToUseCase(browserDriver, componentName, "validation");

    // Test that the web page shows an error message when an invalid value is submitted.
    WebElement input = browserDriver.findElementByXpath(inputText1Xpath);
    input.clear();

    String invalidText = "HelloWorldcom";
    input.sendKeys(invalidText);/*  www  .j  a v a2 s .  c  o  m*/
    browserDriver.clickElementAndWaitForRerender(submitButton1Xpath);

    WaitingAsserter waitingAsserter = getWaitingAsserter();
    waitingAsserter.assertElementDisplayed(error1Xpath);

    // Test that a valid value submits successfully.
    input = browserDriver.findElementByXpath(inputText1Xpath);
    input.clear();

    String text = "Hello@World.com";
    input.sendKeys(text);
    browserDriver.clickElementAndWaitForRerender(submitButton1Xpath);
    waitingAsserter.assertTextPresentInElement(text, modelValue1Xpath);

    // Test that the web page shows an error message when an invalid value is submitted.
    input = browserDriver.findElementByXpath(inputText2Xpath);
    input.clear();
    input.sendKeys(invalidText);
    browserDriver.clickElementAndWaitForRerender(submitButton2Xpath);
    waitingAsserter.assertElementDisplayed(error1Xpath);

    // Test that a valid value submits successfully.
    input = browserDriver.findElementByXpath(inputText2Xpath);
    input.clear();
    input.sendKeys(text);
    browserDriver.clickElementAndWaitForRerender(submitButton2Xpath);
    waitingAsserter.assertTextPresentInElement(text, modelValue2Xpath);
}

From source file:com.liferay.faces.test.showcase.inputtext.InputTextValidationTester.java

License:Open Source License

@Test
public void runInputTextValidationTest() throws Exception {

    Browser browser = Browser.getInstance();
    browser.navigateToURL(inputTextURL + "/validation");

    // Wait to begin the test until the submit button is rendered.
    browser.waitForElementVisible(submitButtonXpath);

    // Test that the web page shows an error message when an invalid value is submitted.
    WebElement input = browser.getElement(inputXpath);
    input.clear();

    String invalidText = "HelloWorldcom";
    input.sendKeys(invalidText);/*from  w  ww . j a  va2  s .c o m*/
    browser.clickAndWaitForAjaxRerender(submitButtonXpath);
    browser.assertElementVisible(errorXpath);

    // Test that a valid value submits successfully.
    input = browser.getElement(inputXpath);
    input.clear();

    String text = "Hello@World.com";
    input.sendKeys(text);
    browser.clickAndWaitForAjaxRerender(submitButtonXpath);
    browser.assertElementTextVisible(modelValueXpath, text);

    // Test that the web page shows an error message when an invalid value is submitted.
    input = browser.getElement(inputXpathRight);
    input.clear();
    input.sendKeys(invalidText);
    browser.clickAndWaitForAjaxRerender(submitButtonXpathRight);
    browser.assertElementVisible(errorXpath);

    // Test that a valid value submits successfully.
    input = browser.getElement(inputXpathRight);
    input.clear();
    input.sendKeys(text);
    browser.clickAndWaitForAjaxRerender(submitButtonXpathRight);
    browser.assertElementTextVisible(modelValueXpathRight, text);
}

From source file:com.liferay.faces.test.showcase.inputtextarea.InputTextareaConversionTester.java

License:Open Source License

@Test
public void runInputTextareaConversionTest() throws Exception {

    Browser browser = Browser.getInstance();
    browser.navigateToURL(inputTextURL + "/conversion");

    // Wait to begin the test until the submit button is rendered.
    browser.waitForElementVisible(submitButtonXpath);

    // Test that the web page shows an error message when an invalid value is submitted.
    WebElement input = browser.getElement(inputXpath);
    input.clear();

    String invalidText = "apr 3 33";
    input.sendKeys(invalidText);/*from w  w w.j a  v  a2s . c om*/
    browser.clickAndWaitForAjaxRerender(submitButtonXpath);
    browser.assertElementVisible(errorXpath);

    // Test that a valid value submits successfully.
    input = browser.getElement(inputXpath);
    input.clear();

    String text = "apr 3, 33";
    input.sendKeys(text);
    browser.clickAndWaitForAjaxRerender(submitButtonXpath);

    String textOutput = "Apr 3, 0033";
    browser.assertElementTextVisible(modelValueXpath, textOutput);

    // Test that the web page shows an error message when an invalid value is submitted.
    input = browser.getElement(inputXpathRight);
    input.clear();
    invalidText = "4/333";
    input.sendKeys(invalidText);
    browser.clickAndWaitForAjaxRerender(submitButtonXpathRight);
    browser.assertElementVisible(errorXpath);

    // Test that a valid value submits successfully.
    input = browser.getElement(inputXpathRight);
    input.clear();
    text = "4/3/33";
    input.sendKeys(text);
    browser.clickAndWaitForAjaxRerender(submitButtonXpathRight);
    textOutput = "04/03/0033";
    browser.assertElementTextVisible(modelValueXpathRight, textOutput);
}

From source file:com.liferay.faces.test.showcase.inputtextarea.InputTextareaValidationTester.java

License:Open Source License

@Test
public void runInputTextareaValidationTest() throws Exception {

    Browser browser = Browser.getInstance();
    browser.navigateToURL(inputTextURL + "/validation");

    // Wait to begin the test until the submit button is rendered.
    browser.waitForElementVisible(submitButtonXpath);

    // Test that the web page shows an error message when an invalid value is submitted.
    WebElement input = browser.getElement(inputXpath);
    input.clear();

    String invalidText = "HelloWorldcom";
    input.sendKeys(invalidText);/*from w  w  w . j a v  a2s.c om*/
    browser.clickAndWaitForAjaxRerender(submitButtonXpath);
    browser.assertElementVisible(errorXpath);

    // Test that a valid value submits successfully.
    input = browser.getElement(inputXpath);
    input.clear();

    String text = "Hello@World.com";
    input.sendKeys(text);
    browser.clickAndWaitForAjaxRerender(submitButtonXpath);
    browser.assertElementTextVisible(modelValueXpath, text);

    // Test that the web page shows an error message when an invalid value is submitted.
    input = browser.getElement(inputXpathRight);
    input.clear();
    input.sendKeys(invalidText);
    browser.clickAndWaitForAjaxRerender(submitButtonXpathRight);
    browser.assertElementVisible(errorXpath);

    // Test that a valid value submits successfully.
    input = browser.getElement(inputXpathRight);
    input.clear();
    input.sendKeys(text);
    browser.clickAndWaitForAjaxRerender(submitButtonXpathRight);
    browser.assertElementTextVisible(modelValueXpathRight, text);
}

From source file:com.liferay.faces.test.util.TesterBase.java

License:Open Source License

public void signIn(WebDriver browser, WebElement user, WebElement pass, WebElement button, WebElement text,
        String textXpath, String u, String p) throws Exception {

    java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(Level.OFF);

    logger.log(Level.INFO, "browser.navigate().to(" + signInUrl + ")");
    browser.navigate().to(signInUrl);//from  w w  w .ja v a  2  s  .  com
    logger.log(Level.INFO, "browser.getTitle() = " + browser.getTitle() + " before signing in ...");

    if (browser.getTitle().contains("Status")) {
        logger.log(Level.INFO, "welcome-theme installed ...");
        String welcomeSignInUrl = signInUrl.replace("home", "welcome");
        logger.log(Level.INFO, "browser.navigate().to(" + welcomeSignInUrl + ")");
        browser.navigate().to(welcomeSignInUrl);
        logger.log(Level.INFO, "browser.getTitle() = " + browser.getTitle() + " before signing in ...");
    } else {
        logger.log(Level.INFO, "no welcome-theme, no problem ...");
    }

    if (isThere(browser, "//div[contains(text()[2],'was not found')]")) {

        // attempt to go to a Bridge Demos to get to the login page
        logger.log(Level.INFO, "Attempting to go to a Bridge Demos to get to the login page ...");
        String bridgeDemosSignInUrl = baseUrl + "/group/bridge-demos/";
        logger.log(Level.INFO, "browser.navigate().to(" + bridgeDemosSignInUrl + ")");
        browser.navigate().to(bridgeDemosSignInUrl);
        logger.log(Level.INFO, "browser.getTitle() = " + browser.getTitle() + " before signing in ...");

        waitForElement(browser, emailFieldXpath);
        user.clear();
        user.sendKeys(u);
        pass.clear();
        pass.sendKeys(p);
        button.click();

        waitForElement(browser, "//span[contains(text(),'Bridge Demos')]");

        return;
    }

    user.clear();
    user.sendKeys(u);
    pass.clear();
    pass.sendKeys(p);
    button.click();
    logger.log(Level.INFO, "browser.getTitle() = " + browser.getTitle()
            + " after clicking the sign in button.   Now waiting ...");
    waitForElement(browser, textXpath);
    logger.log(Level.INFO, text.getText());

}

From source file:com.mgmtp.jfunk.web.step.JFunkWebElement.java

License:Apache License

/**
 * @throws StepException/*from  ww  w.  j  a v a 2 s. c  o  m*/
 *             <ul>
 *             <li>if element specified by {@link By} object in the constructor cannot be found</li>
 *             <li>if a validation error occurred while checking the value of the WebElement
 *             against the desired value</li>
 *             </ul>
 */
@Override
public void execute() throws StepException {
    elementValue = retrieveElementValue();

    final WebDriverWait wait = new WebDriverWait(getWebDriver(), WebConstants.DEFAULT_TIMEOUT);
    final WebElement element = wait.until(new Function<WebDriver, WebElement>() {

        @Override
        public WebElement apply(final WebDriver input) {
            List<WebElement> webElements = input.findElements(by);
            if (webElements.isEmpty()) {
                throw new StepException("Could not find any matching element; By=" + by.toString());
            }
            /*
             * If the search using the By object does find more than one matching element we are
             * looping through all elements if we find at least one which matches the criteria
             * below. If not, an exception is thrown.
             */
            for (WebElement webElement : webElements) {
                if (webElement.isDisplayed()) {
                    if (webElement.isEnabled() || !webElement.isEnabled()
                            && (stepMode == StepMode.CHECK_DEFAULT || stepMode == StepMode.CHECK_VALUE)) {
                        return webElement;
                    }
                }
            }
            throw new StepException("All elements matching by=" + by + " were either invisible or disabled");
        }
    });

    switch (stepMode) {
    case CHECK_DEFAULT:
        // Check only for text input and textarea
        if (element.getTagName().equals(WebConstants.INPUT)
                && element.getAttribute(WebConstants.TYPE).equals(WebConstants.TEXT)
                || element.getTagName().equals(WebConstants.TEXTAREA)) {
            log.info(toString());
            String value = element.getAttribute(WebConstants.VALUE);
            if (!DataUtils.isDefaultValue(value)) {
                throw new ValidationException("Wrong default value=" + value + " of " + this);
            }
        }
        break;
    case CHECK_VALUE:
        String checkValue = elementValue;
        if (checkTrafo != null) {
            checkValue = checkTrafo.trafo(checkValue);
        }

        log.info(this + ", checkValue=" + checkValue);
        if (element.getTagName().equalsIgnoreCase(WebConstants.SELECT)) {
            Select select = new Select(element);
            String value = select.getFirstSelectedOption().getAttribute(WebConstants.VALUE);
            if (!Objects.equal(checkValue, value)) {
                String text = select.getFirstSelectedOption().getText();
                if (!Objects.equal(checkValue, text)) {
                    throw new InvalidValueException(element, checkValue,
                            text + " (option value=" + value + ")");
                }
            }
        } else if (WebConstants.INPUT.equalsIgnoreCase(element.getTagName())
                && WebConstants.RADIO.equals(element.getAttribute(WebConstants.TYPE))) {
            List<WebElement> elements = getWebDriver().findElements(by);
            for (WebElement webElement : elements) {
                if (webElement.isDisplayed() && webElement.isEnabled()) {
                    String elVal = webElement.getAttribute(WebConstants.VALUE);
                    if (elVal.equals(checkValue) && !webElement.isSelected()) {
                        throw new InvalidValueException(element, checkValue, elVal);
                    }
                }
            }
        } else if (WebConstants.CHECKBOX.equals(element.getAttribute(WebConstants.TYPE))) {
            boolean elVal = element.isSelected();
            if (elVal != Boolean.valueOf(checkValue)) {
                throw new InvalidValueException(element, checkValue, String.valueOf(elVal));
            }
        } else {
            String value = element.getAttribute(WebConstants.VALUE);
            if (!Objects.equal(checkValue, value)) {
                throw new InvalidValueException(element, checkValue, value);
            }
        }
        break;
    case SET_VALUE:
        String setValue = elementValue;
        if (setTrafo != null) {
            setValue = setTrafo.trafo(setValue);
        }
        log.info(this + (setTrafo != null ? ", setValue (after trafo)=" + setValue : ""));
        if (element.getTagName().equalsIgnoreCase(WebConstants.SELECT)) {
            Select select = new Select(element);
            // First check if a matching value can be found
            List<WebElement> options = select.getOptions();
            boolean found = false;
            for (WebElement option : options) {
                String optionValue = option.getAttribute(WebConstants.VALUE);
                if (StringUtils.equals(optionValue, setValue)) {
                    /*
                     * WebElement with matching value could be found --> we are finished
                     */
                    found = true;
                    select.selectByValue(setValue);
                    break;
                }
            }
            if (!found) {
                /*
                 * Fallback: look for a WebElement with a matching visible text
                 */
                for (WebElement option : options) {
                    String visibleText = option.getText();
                    if (StringUtils.equals(visibleText, setValue)) {
                        /*
                         * WebElement with matching value could be found --> we are finished
                         */
                        found = true;
                        select.selectByVisibleText(setValue);
                        break;
                    }
                }
            }
            if (!found) {
                throw new StepException(
                        "Could not find a matching option element in " + element + " , By: " + by.toString());
            }
        } else if (WebConstants.INPUT.equalsIgnoreCase(element.getTagName())
                && WebConstants.RADIO.equals(element.getAttribute(WebConstants.TYPE))) {
            List<WebElement> elements = getWebDriver().findElements(by);
            for (WebElement webElement : elements) {
                if (webElement.isDisplayed() && webElement.isEnabled()) {
                    String elVal = webElement.getAttribute(WebConstants.VALUE);
                    if (elVal.equals(setValue) && !webElement.isSelected()) {
                        webElement.click();
                    }
                }
            }
        } else if (WebConstants.CHECKBOX.equals(element.getAttribute(WebConstants.TYPE))) {
            if (Boolean.valueOf(setValue) && !element.isSelected()
                    || !Boolean.valueOf(setValue) && element.isSelected()) {
                element.click();
            }
        } else {
            if (element.isDisplayed() && element.isEnabled() && (element.getAttribute("readonly") == null
                    || element.getAttribute("readonly").equals("false"))) {
                element.clear();
                element.sendKeys(setValue);
            } else {
                log.warn("Element is invisible or disabled, value cannot be set");
            }
        }
        break;
    case NONE:
        // do nothing
        break;
    default:
        throw new IllegalArgumentException("Unhandled StepMode=" + stepMode);
    }
}

From source file:com.mgmtp.jfunk.web.util.FormInputHandler.java

License:Apache License

/**
 * Tries to find the field and sets or checks its value depending on the {@link StepMode}.
 *//*  ww  w  . j ava  2s  . c om*/
public void perform() {
    log.info(toString());

    WebElement element = finder.find();

    switch (stepMode) {
    case CHECK_VALUE:
        String checkValue = retrieveValue();
        if (checkTrafo != null) {
            checkValue = checkTrafo.trafo(checkValue);
        }
        checkValue(element, checkValue);
        break;

    case CHECK_DEFAULT:
        checkState(defaultsProvider != null, "No DefaultsProvider set when StepMode is CHECK_DEFAULT.");
        checkValue(element, defaultsProvider.get(element, dataSet, dataKey, dataIndex));
        break;

    case SET_VALUE:
        String setValue = retrieveValue();
        if (setTrafo != null) {
            setValue = setTrafo.trafo(setValue);
        }

        if (element.getTagName().equalsIgnoreCase(WebConstants.SELECT)) {

            Select select = new Select(element);
            // First check if a matching value can be found
            List<WebElement> options = select.getOptions();
            boolean found = false;
            for (WebElement option : options) {
                String optionValue = option.getAttribute(WebConstants.VALUE);
                if (StringUtils.equals(optionValue, setValue)) {
                    /*
                     * WebElement with matching value could be found --> we are finished
                     */
                    found = true;
                    select.selectByValue(setValue);
                    break;
                }
            }

            if (!found) {
                /*
                 * Fallback: look for a WebElement with a matching visible text
                 */
                for (WebElement option : options) {
                    String visibleText = option.getText();
                    if (StringUtils.equals(visibleText, setValue)) {
                        /*
                         * WebElement with matching value could be found --> we are finished
                         */
                        found = true;
                        select.selectByVisibleText(setValue);
                        break;
                    }
                }
            }

            if (!found) {
                throw new JFunkException(
                        "Could not find a matching option element in " + element + " , By: " + finder.getBy());
            }

        } else if (WebConstants.INPUT.equalsIgnoreCase(element.getTagName())
                && WebConstants.RADIO.equals(element.getAttribute(WebConstants.TYPE))) {

            List<WebElement> elements = finder.findAll();
            for (WebElement webElement : elements) {
                String elVal = webElement.getAttribute(WebConstants.VALUE);
                if (elVal.equals(setValue) && !webElement.isSelected()) {
                    webElement.click();
                }
            }

        } else if (WebConstants.CHECKBOX.equals(element.getAttribute(WebConstants.TYPE))) {

            if (Boolean.valueOf(setValue) && !element.isSelected()
                    || !Boolean.valueOf(setValue) && element.isSelected()) {
                element.click();
            }

        } else {

            if (element.getAttribute("readonly") == null || element.getAttribute("readonly").equals("false")) {
                element.clear();
                element.sendKeys(setValue);
            } else {
                log.warn("Element is invisible or disabled, value cannot be set");
            }

        }
        break;

    case NONE:
        // do nothing
        break;

    default:
        throw new IllegalArgumentException("Unhandled StepMode=" + stepMode);
    }
}