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.testmax.runner.SeleniumTestRunner.java

License:CDDL license

protected WebElement executeAll() throws TestMaxException {
    long executionTime = System.currentTimeMillis();
    List<WebElement> controls = null;
    try {//ww w .j a  va2  s .co m
        this.setImpilicitTimeInMiliSec(new Integer(timeout));

        //handle custom operations
        if (this.isBypassCustomMethod()) {
            if (method.equalsIgnoreCase("get")) {
                this.setImpilicitTimeInMiliSec(new Integer(timeout));
                this.waitToPageLoad(new Integer(timeout));
                if (!url.contains("http") && !url.contains("file:")) {
                    url = this.baseUrl + url;
                }
                url = url.replace("[env]", ConfigLoader.getConfig("QA_TEST_ENV"));

                if (this.baseUrl.contains("https")) {
                    url = url.replace("http:", "https:");
                }
                this.printCommand(this.tagdesc + " URL=" + url);
                driver.get(url);
                this.timeTaken = this.timeTaken + (System.currentTimeMillis() - executionTime);
                printMessage("**** Executed Open URL= " + url + " for Control=" + this.controlIdentifier + ":"
                        + addMessage());
                return null;
            } else if (method.equalsIgnoreCase("closewindow")) {
                this.driver.close();
                return null;
            } else if (method.equalsIgnoreCase("sleep")) {
                this.executeSleep();
                return null;
            } else if (method.equalsIgnoreCase("executescript")) {
                this.setImpilicitTimeInMiliSec(new Integer(timeout));
                Thread.sleep(100);
                this.executeScript();
                this.timeTaken = this.timeTaken + (System.currentTimeMillis() - executionTime);
                this.printCommand(this.tagdesc + " for control with identifier=" + this.controlIdentifier
                        + "\n Script=" + addMessage());
                printMessage("**** Executed Java Script= " + addMessage() + " for Control="
                        + this.controlIdentifier + ":" + addMessage());
                return null;
            } else if (method.equalsIgnoreCase("executeoperation")) {
                this.setImpilicitTimeInMiliSec(new Integer(timeout));
                Thread.sleep(100);
                this.executeOperation();
                this.timeTaken = this.timeTaken + (System.currentTimeMillis() - executionTime);
                this.printCommand(this.tagdesc + " for control with identifier=" + this.controlIdentifier
                        + " value=" + addMessage());
                printMessage("**** Executed Java Script= " + addMessage() + " for Control="
                        + this.controlIdentifier + ":" + addMessage());
                return null;
            } else if (this.method.equalsIgnoreCase("urlextract")) {
                String currenturl = this.driver.getCurrentUrl();
                if (currenturl != null && !currenturl.isEmpty()) {

                    this.addTestExtract(this.value, currenturl);
                    printMessage("#### Extracted parameters from URL=" + currenturl);
                }
                return null;

            }
        }
        controls = this.getWebElements();

        for (WebElement control : controls) {
            this.setImpilicitTimeInMiliSec(new Integer(timeout));
            if (!isBypassDisplay()) {
                while (!control.isDisplayed()) {
                    this.setImpilicitTimeInMiliSec(new Integer(timeout));
                }
            }
            if (method.equalsIgnoreCase("click")) {
                try {
                    printMessage("**** Clicking " + this.controlIdentifier);
                    this.printCommand(this.tagdesc + " for control with identifier=" + this.controlIdentifier);
                    control.click();
                    printMessage("**** Clicked " + this.controlIdentifier + ":" + addMessage());
                    return control;
                } catch (Exception e) {
                    WebElement target = executeClick(control);
                    if (target != null) {
                        return target;
                    }
                }
            } else if (method.equalsIgnoreCase("selectByValue")) {
                this.printCommand(this.tagdesc + " for control with identifier=" + this.controlIdentifier
                        + " value=" + addMessage());
                this.setImpilicitTimeInMiliSec(new Integer(timeout));
                while (!control.isDisplayed()) {
                    this.setImpilicitTimeInMiliSec(new Integer(timeout));
                }
                new Select(control).selectByValue(sendkeyValue);
                this.timeTaken = this.timeTaken + (System.currentTimeMillis() - executionTime);
                printMessage("**** Selected Value= " + addMessage() + " for Control=" + this.controlIdentifier
                        + ":" + addMessage());
                return control;
            } else if (method.equalsIgnoreCase("deselectByVisibleText")) {
                this.setImpilicitTimeInMiliSec(new Integer(timeout));
                while (!control.isDisplayed()) {
                    this.setImpilicitTimeInMiliSec(new Integer(timeout));
                }
                new Select(control).deselectByVisibleText(sendkeyValue);
                this.timeTaken = this.timeTaken + (System.currentTimeMillis() - executionTime);
                printMessage("**** Deselected Value= " + addMessage() + " for Control=" + this.controlIdentifier
                        + ":" + addMessage());
                return control;
            } else if (method.equalsIgnoreCase("selectByVisibleText")) {
                this.printCommand(this.tagdesc + " for control with identifier=" + this.controlIdentifier
                        + " value=" + addMessage());
                this.setImpilicitTimeInMiliSec(new Integer(timeout));
                while (!control.isDisplayed()) {
                    this.setImpilicitTimeInMiliSec(new Integer(timeout));
                }
                new Select(control).selectByVisibleText(sendkeyValue);
                this.timeTaken = this.timeTaken + (System.currentTimeMillis() - executionTime);
                printMessage("**** Selected Value= " + addMessage() + " for Control=" + this.controlIdentifier
                        + ":" + addMessage());
                return control;
            } else if (method.equalsIgnoreCase("clear")) {
                try {
                    this.printCommand(this.tagdesc + " for control with identifier=" + this.controlIdentifier);
                    this.setImpilicitTimeInMiliSec(new Integer(timeout));
                    while (!control.isDisplayed()) {
                        this.setImpilicitTimeInMiliSec(new Integer(timeout));
                    }
                    Thread.sleep(100);
                    if (control.isEnabled())
                        control.clear();
                    this.timeTaken = this.timeTaken + (System.currentTimeMillis() - executionTime);
                    printMessage("**** Cleared " + this.controlIdentifier + ":" + addMessage());
                    return control;
                } catch (Exception e) {
                    this.clearElement();
                    Thread.sleep(500);
                    return control;
                }
            } else if (method.equalsIgnoreCase("movemouse")) {
                this.printCommand(this.tagdesc + " for control with identifier=" + this.controlIdentifier);
                this.setImpilicitTimeInMiliSec(new Integer(timeout));
                while (!control.isDisplayed()) {
                    this.setImpilicitTimeInMiliSec(new Integer(timeout));
                }
                Thread.sleep(100);
                this.mouseMove(control);
                this.timeTaken = this.timeTaken + (System.currentTimeMillis() - executionTime);
                printMessage("**** Moved Mouse " + this.controlIdentifier + ":" + addMessage());
                return control;
            } else if (method.equalsIgnoreCase("download")) {
                this.printCommand(this.tagdesc + " for control with identifier=" + this.controlIdentifier);
                this.setImpilicitTimeInMiliSec(new Integer(timeout));
                while (!control.isDisplayed()) {
                    this.setImpilicitTimeInMiliSec(new Integer(timeout));
                }
                Thread.sleep(100);
                this.downloadFile(control);
                if (this.value != null && !this.value.isEmpty() && this.value.contains("[")) {
                    this.modifyexcel(this.value);
                }
                this.timeTaken = this.timeTaken + (System.currentTimeMillis() - executionTime);
                printMessage("**** Download File " + this.controlIdentifier + ":" + addMessage());
                return control;
            } else if (method.equalsIgnoreCase("uploadfile")) {

                this.setImpilicitTimeInMiliSec(new Integer(timeout));
                while (!control.isDisplayed()) {
                    this.setImpilicitTimeInMiliSec(new Integer(timeout));
                }
                Thread.sleep(100);
                String dest = ConfigLoader.getWmOutputWebServicePath() + "modifytest.xls";
                File mfile = new File(dest);
                String modifyfile = mfile.getCanonicalPath();
                this.printCommand(this.tagdesc + " for control with identifier=" + this.controlIdentifier
                        + " file=" + modifyfile);
                control.sendKeys(modifyfile);
                this.timeTaken = this.timeTaken + (System.currentTimeMillis() - executionTime);
                printMessage("**** Entered Value= " + addMessage() + " for Control=" + this.controlIdentifier
                        + ":" + addMessage());
                return control;
            } else if (method.equalsIgnoreCase("submit")) {
                this.printCommand(this.tagdesc + " for control with identifier=" + this.controlIdentifier);
                this.setImpilicitTimeInMiliSec(new Integer(timeout));
                while (!control.isDisplayed()) {
                    this.setImpilicitTimeInMiliSec(new Integer(timeout));
                }
                Thread.sleep(100);
                control.submit();
                this.timeTaken = this.timeTaken + (System.currentTimeMillis() - executionTime);
                printMessage("**** Submitted " + this.controlIdentifier + ":" + addMessage());
                return control;
            } else if (method.equalsIgnoreCase("asserttext")) {
                try {
                    this.setImpilicitTimeInMiliSec(new Integer(timeout));
                    Thread.sleep(100);
                    String actual = control.getText();
                    if (actual == null || actual.isEmpty()) {
                        actual = control.getAttribute("value");
                        printMessage(
                                "**** Control won't have Text Value. Extracted Value using attribute Name=value. Found value= "
                                        + actual + " for " + this.controlIdentifier + ":" + addMessage());
                    }
                    this.timeTaken = this.timeTaken + (System.currentTimeMillis() - executionTime);
                    this.printCommand(this.tagdesc + " for control with identifier=" + this.controlIdentifier
                            + " value=" + addMessage() + ", actual=" + actual);
                    printMessage("**** verifying Text Value= " + actual + " for " + this.controlIdentifier + ":"
                            + addMessage());
                    if (actual != null && !actual.isEmpty()) {
                        if (value != null && value.contains("@")) {
                            this.addTestExtract(value, actual);
                        } else {
                            this.assertText(value, actual);
                        }
                        return control;
                    }
                } catch (Exception e) {
                }
            } else if (method.equalsIgnoreCase("sendKeys")) {
                this.printCommand(this.tagdesc + " for control with identifier=" + this.controlIdentifier
                        + " value=" + addMessage());
                this.setImpilicitTimeInMiliSec(new Integer(timeout));
                while (!control.isDisplayed()) {
                    this.setImpilicitTimeInMiliSec(new Integer(timeout));
                }
                Thread.sleep(100);
                control.sendKeys(sendkeyValue);
                this.timeTaken = this.timeTaken + (System.currentTimeMillis() - executionTime);
                printMessage("**** Entered Value= " + addMessage() + " for Control=" + this.controlIdentifier
                        + ":" + addMessage());
                return control;
            }

        }
    } catch (NumberFormatException e) {

        this.printCommand(
                " FAILED  with Exception for Command=" + this.tagdesc + " for control with identifier="
                        + this.controlIdentifier + " file=" + addMessage() + "\n Message=" + e.getMessage());
        this.printCommand(WmLog.getStackTrace(e));
        // TODO Auto-generated catch block
        String v_msg = ">>Dataset:" + this.threadIndex + " FAILED TO EXECUTE CONTROL IN " + addMessage() + " "
                + (e.getMessage() != null && e.getMessage().length() > 100 ? e.getMessage().substring(0, 100)
                        : e.getMessage());
        e.printStackTrace();
        throw new TestMaxException(v_msg);
        //handleTimeOut(v_msg);
    } catch (InterruptedException e) {

        // TODO Auto-generated catch block
        this.printCommand(
                " FAILED  with Exception for Command=" + this.tagdesc + " for control with identifier="
                        + this.controlIdentifier + " file=" + addMessage() + "\n Message=" + e.getMessage());
        this.printCommand(WmLog.getStackTrace(e));
        String v_msg = ">>Dataset:" + this.threadIndex + " FAILED TO EXECUTE CONTROL IN " + addMessage() + " "
                + (e.getMessage() != null && e.getMessage().length() > 100 ? e.getMessage().substring(0, 100)
                        : e.getMessage());
        e.printStackTrace();
        throw new TestMaxException(v_msg);
        //handleTimeOut(v_msg);

    } catch (Exception e) {
        // TODO Auto-generated catch block
        this.printCommand(
                " FAILED  with Exception for Command=" + this.tagdesc + " for control with identifier="
                        + this.controlIdentifier + " file=" + addMessage() + "\n Message=" + e.getMessage());
        this.printCommand(WmLog.getStackTrace(e));
        String v_msg = ">>Dataset:" + this.threadIndex + " FAILED TO EXECUTE CONTROL IN " + addMessage() + " "
                + (e.getMessage() != null && e.getMessage().length() > 100 ? e.getMessage().substring(0, 100)
                        : e.getMessage());
        e.printStackTrace();
        throw new TestMaxException(v_msg);
        //handleTimeOut(v_msg);
    }

    //if you are here then quit
    //driver.quit();

    return null;
}

From source file:com.thoughtworks.inproctester.tests.InProcessHtmlUnitDriverTest.java

License:Apache License

@Test
public void shouldSupportGetAndPostRequests() {

    WebDriver htmlUnitDriver = new InProcessHtmlUnitDriver(httpAppTester);

    htmlUnitDriver.get("http://localhost/contacts/add");

    assertThat(htmlUnitDriver.getTitle(), is("Test Application"));
    assertThat(htmlUnitDriver.findElement(By.tagName("h3")).getText(), is("Contact Details"));
    WebElement contactNameElement = htmlUnitDriver.findElement(By.name("contactName"));

    contactNameElement.clear();
    contactNameElement.sendKeys("My Contact");

    htmlUnitDriver.findElement(By.tagName("form")).submit();

    assertThat(htmlUnitDriver.getCurrentUrl(), is("http://localhost/contacts/1"));
    assertThat(htmlUnitDriver.findElement(By.name("contactName")).getAttribute("value"), is("My Contact"));

}

From source file:com.thoughtworks.inproctester.tests.InProcessHtmlUnitDriverTest.java

License:Apache License

@Test
public void shouldSupportUtfEncodedData() {

    WebDriver htmlUnitDriver = new InProcessHtmlUnitDriver(httpAppTester);

    htmlUnitDriver.get("http://localhost/contacts/add");
    WebElement contactNameElement = htmlUnitDriver.findElement(By.name("contactName"));
    contactNameElement.clear();
    contactNameElement.sendKeys("? ");
    htmlUnitDriver.findElement(By.tagName("form")).submit();

    assertThat(htmlUnitDriver.findElement(By.name("contactName")).getAttribute("value"),
            is("? "));

}

From source file:com.thoughtworks.selenium.webdriven.commands.AttachFile.java

License:Apache License

@Override
protected Void handleSeleneseCommand(WebDriver driver, String locator, String value) {
    File file = downloadFile(value);

    WebElement element = finder.findElement(driver, locator);
    element.clear();
    element.sendKeys(file.getAbsolutePath());

    return null;//  w  ww  .j a  v a 2  s  .  c o m
}

From source file:com.thoughtworks.selenium.webdriven.commands.Type.java

License:Apache License

@Override
protected Void handleSeleneseCommand(WebDriver driver, String locator, String value) {
    alertOverride.replaceAlertMethod(driver);

    if (state.controlKeyDown || state.altKeyDown || state.metaKeyDown)
        throw new SeleniumException(
                "type not supported immediately after call to controlKeyDown() or altKeyDown() or metaKeyDown()");

    String valueToUse = state.shiftKeyDown ? value.toUpperCase() : value;

    WebElement element = finder.findElement(driver, locator);

    String tagName = element.getTagName();
    String elementType = element.getAttribute("type");
    if ("input".equals(tagName.toLowerCase()) && elementType != null
            && "file".equals(elementType.toLowerCase())) {
        log.warning("You should be using attachFile to set the value of a file input element");
        element.sendKeys(valueToUse);//  w w w.  j  a  va 2 s .co m
        return null;
    }

    if (!"input".equals(tagName.toLowerCase())) {
        if (driver instanceof JavascriptExecutor) {
            ((JavascriptExecutor) driver).executeScript("arguments[0].value = '';", element);
        }
        element.sendKeys(valueToUse);
        return null;
    }

    if (driver instanceof JavascriptExecutor) {
        js.executeScript(driver, type, element, valueToUse);
    } else {
        element.clear();
        element.sendKeys(valueToUse);
    }

    return null;
}

From source file:com.ttdev.wicketpagetest.sample.spring.PageExtractedByWicketIdsTest.java

License:Open Source License

public void testWicketLocator() {
    WicketSeleniumDriver ws = WebPageTestContext.getWicketSelenium();
    ws.openBookmarkablePage(PageExtractedByWicketIds.class);
    ws.subscribeAjaxDoneHandler();/*  w  ww  .j a va  2s .  c o  m*/
    assert ws.getValue("//eachRow[0]//v").equals("3");
    assert ws.getValue("//eachRow[1]//v").equals("2");
    assert ws.getValue("//eachRow[2]//v").equals("8");
    assert ws.getText("//total").equals("13");
    WebElement v1 = ws.findWicketElement("//eachRow[1]//v");
    v1.clear();
    v1.sendKeys("5");
    WebElement ok = ws.findWicketElement("//eachRow[1]//ok");
    ok.click();
    ws.waitUntilAjaxDone();
    assert ws.getText("//total").equals("16") : ws.getText("//total");
    assert ws.getText("//id.containing.dots").equals("xyz");
}

From source file:com.vaadin.addon.spreadsheet.elements.SheetCellElement.java

License:Open Source License

/**
 * Sets the given value to this cell. This method does not support setting
 * values to cells containing custom editors. To set values to custom
 * editors, you should directly set the value to the custom editor which is
 * a child of this element.//w ww .j  ava2  s  .c  o  m
 * 
 * Note: Calling this method will set the current selection to this cell.
 * 
 * @param newValue
 *            Value to set.
 */
public void setValue(String newValue) {
    if (isNormalCell()) {
        doubleClick();
        WebElement cellValueInput = parent.getCellValueInput();
        cellValueInput.clear();
        cellValueInput.sendKeys(newValue);
        cellValueInput.sendKeys(Keys.TAB);
        getCommandExecutor().waitForVaadin();
    }
}

From source file:com.vaadin.framework7.samples.SpringCrudIT.java

License:Apache License

@Test
public void updateProduct() {
    doLogin();//from   w  w w .  j a v  a2  s  .co m

    int index = 1;

    List<WebElement> rows = getRows();

    rows.get(index).findElement(By.tagName("td")).click();

    WebElement form = findElement(By.className("product-form"));

    List<WebElement> fields = form.findElements(By.className("v-textfield"));

    WebElement productName = fields.get(0);
    productName.clear();
    productName.sendKeys("Updated Product Name");
    form.findElement(By.className("primary")).click();

    //        fixme This check fails because of some PhantomJS issues.
    //        disabled, because it is not life-critical
    //        checkFormLocation(form);

    rows = getRows();

    WebElement firstNameColumn = rows.get(index).findElements(By.tagName("td")).get(1);
    hasText(firstNameColumn, "Updated Product Name");
}

From source file:com.vaadin.framework7.samples.SpringCrudIT.java

License:Apache License

@Test
public void validationError() {
    doLogin();//ww w .  ja  v a  2s  .co m

    int index = 3;

    List<WebElement> rows = getRows();

    rows.get(index).findElement(By.tagName("td")).click();

    WebElement form = findElement(By.className("product-form"));

    Assert.assertFalse(isElementPresent(By.className("v-errorindicator")));
    Assert.assertFalse(isElementPresent(By.className("v-textfield-error")));

    List<WebElement> fields = form.findElements(By.className("v-textfield"));

    WebElement productName = fields.get(0);
    productName.clear();
    productName.sendKeys("a");
    productName.sendKeys(Keys.TAB);

    Assert.assertTrue(isElementPresent(By.className("v-errorindicator")));
    Assert.assertTrue(isElementPresent(By.className("v-textfield-error")));
}

From source file:com.vaadin.framework8.samples.SpringCrudIT.java

License:Apache License

@Test
public void updateContact() {
    doLogin();//from w ww .  ja v a 2  s.  c  o m

    int index = 1;

    List<WebElement> rows = getRows();

    rows.get(index).findElement(By.tagName("td")).click();

    WebElement form = findElement(By.className("product-form"));

    List<WebElement> fields = form.findElements(By.className("v-textfield"));

    WebElement productName = fields.get(0);
    productName.clear();
    productName.sendKeys("Updated Product Name");

    form.findElement(By.className("primary")).click();
    //      This check does not work under PhantomJS
    //      checkFormLocation(form);

    rows = getRows();

    WebElement firstNameColumn = rows.get(index).findElements(By.tagName("td")).get(1);
    hasText(firstNameColumn, "Updated Product Name");
}