Java Code Examples for org.openqa.selenium.WebElement.clear()

The following are Jave code examples for showing how to use clear() of the org.openqa.selenium.WebElement class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: marathonv5   File: JTextFieldTest.java   Source Code and License Vote up 6 votes
public void clear() throws Throwable {
    driver = new JavaDriver();
    WebElement textField = driver.findElement(By.cssSelector("text-field"));
    textField.clear();
    AssertJUnit.assertEquals("", textField.getText());
    textField.sendKeys("Lewis Carroll");
    AssertJUnit.assertEquals("Lewis Carroll", textField.getText());
    textField.clear();
    AssertJUnit.assertEquals("", textField.getText());
}
 
Example 2
Project: ServiceNow_Selenium   File: ServiceNow.java   Source Code and License Vote up 6 votes
/**
 * Types into the search bar in the top right and searches.
 * 
 * @param content
 *            - The value to type into the search bar.
 */
public void searchFor(String content) {
	this.focusForm(false);
	WebElement mGTypeArea = _wait.until(ExpectedConditions.presenceOfElementLocated(By.name("sysparm_search")));
	if (!mGTypeArea.getClass().toString().contains("focus")) {
		WebElement magnifyingGlass = _wait
				.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("[action='textsearch.do']")));
		magnifyingGlass.click();
	}
	if(!mGTypeArea.getAttribute("value").equalsIgnoreCase("")) {
		mGTypeArea.clear();
	}
	mGTypeArea.sendKeys(content);
	mGTypeArea.sendKeys(Keys.ENTER);
}
 
Example 3
Project: kheera   File: AbstractPage.java   Source Code and License Vote up 6 votes
@Override
public boolean putText(WebElement w, String... args) throws Exception{
	boolean result = false;
	String d = args[0];
	boolean includesReturn = false;
	if(d.indexOf("\\n") > -1 || d.indexOf("\\r") > -1){
		d = d.replace("\\n", "");
		d = d.replace("\\r", "");
		includesReturn = true;
	}
	try{
		w.clear();
		w.sendKeys(d);
		if(includesReturn){
			w.sendKeys(Keys.RETURN);
		}
		result = true;
	}catch(Exception e){
		e.printStackTrace();
		result = false;
	}
	Assert.assertTrue(result, w.toString() + " insert text " + d);
	return result;
}
 
Example 4
Project: Selenium-Foundation   File: ComponentContainer.java   Source Code and License Vote up 6 votes
/**
 * Update the specified element with the indicated value
 * 
 * @param element target element (input, select)
 * @param value desired value
 * @return 'true' if element value changed; otherwise 'false'
 */
public static boolean updateValue(WebElement element, String value) {
    Objects.requireNonNull(element, "[element] must be non-null");
    
    String tagName = element.getTagName().toLowerCase();
    if ("input".equals(tagName)) {
        if ("checkbox".equals(element.getAttribute("type"))) {
            return updateValue(element, Boolean.parseBoolean(value));
        } else if (!valueEquals(element, value)) {
            if (value == null) {
                element.clear();
            } else {
                WebDriverUtils.getExecutor(element).executeScript("arguments[0].select();", element);
                element.sendKeys(value);
            }
            return true;
        }
    } else if ("select".equals(tagName) && !valueEquals(element, value)) {
        new Select(element).selectByValue(value);
        return true;
    }
    return false;
}
 
Example 5
Project: mot-automated-testsuite   File: WebDriverWrapper.java   Source Code and License Vote up 6 votes
/**
 * Enters the specified text into the field, within the specified fieldset.
 * @param text          The text to enter
 * @param fieldLabel    The field label
 * @param fieldsetLabel The fieldset label
 */
public void enterIntoFieldInFieldset(String text, String fieldLabel, String fieldsetLabel) {
    WebElement fieldsetElement;

    try {
        // find the fieldset with the fieldset label
        fieldsetElement = webDriver.findElement(
                By.xpath("//label[contains(text(),'" + fieldsetLabel + "')]/ancestor::fieldset[1]"));

    } catch (NoSuchElementException noSuchElement) {
        fieldsetElement = findFieldsetByLegend(fieldsetLabel);
    }

    // find the specified label (with the for="id" attribute)...
    WebElement labelElement = fieldsetElement.findElement(
            By.xpath(".//label[contains(text(),'" + fieldLabel + "')]"));

    // find the text element with id matching the for attribute
    // (search in the fieldset rather than the whole page, to get around faulty HTML where id's aren't unique!)
    WebElement textElement = fieldsetElement.findElement(By.id(labelElement.getAttribute("for")));
    textElement.clear();
    textElement.sendKeys(text);
}
 
Example 6
Project: marathonv5   File: JPasswordFieldTest.java   Source Code and License Vote up 6 votes
public void login() throws Throwable {
    driver = new JavaDriver();
    WebElement passField = driver.findElement(By.cssSelector("password-field"));
    AssertJUnit.assertEquals("true", passField.getAttribute("enabled"));
    passField.clear();
    AssertJUnit.assertEquals("", passField.getText());
    passField.sendKeys("password");
    WebElement button = driver.findElement(By.cssSelector("button"));
    button.click();
    driver.switchTo().window("Error Message");
    WebElement label = driver.findElement(By.cssSelector("label[text*='Invalid']"));
    AssertJUnit.assertEquals("Invalid password. Try again.", label.getText());
    WebElement button1 = driver.findElement(By.cssSelector("button"));
    button1.click();
    driver.switchTo().window("JPasswordFieldTest");
    passField.clear();
    passField.sendKeys("bugaboo", Keys.ENTER);
    driver.switchTo().window("Message");
    WebElement label2 = driver.findElement(By.cssSelector("label[text*='Success']"));
    AssertJUnit.assertEquals("Success! You typed the right password.", label2.getText());
}
 
Example 7
Project: NoraUi   File: Step.java   Source Code and License Vote up 6 votes
/**
 * Update a html input text with a text.
 *
 * @param pageElement
 *            Is target element
 * @param textOrKey
 *            Is the new data (text or text in context (after a save))
 * @param keysToSend
 *            character to send to the element after {@link org.openqa.selenium.WebElement#sendKeys(CharSequence...) sendKeys} with textOrKey
 * @param args
 *            list of arguments to format the found selector with
 * @throws TechnicalException
 *             is thrown if you have a technical error (format, configuration, datas, ...) in NoraUi.
 *             Exception with {@value com.github.noraui.utils.Messages#FAIL_MESSAGE_ERROR_ON_INPUT} message (with screenshot, no exception)
 * @throws FailureException
 *             if the scenario encounters a functional error
 */
protected void updateText(PageElement pageElement, String textOrKey, CharSequence keysToSend, Object... args) throws TechnicalException, FailureException {
    String value = Context.getValue(textOrKey) != null ? Context.getValue(textOrKey) : textOrKey;
    if (!"".equals(value)) {
        try {
            WebElement element = Context.waitUntil(ExpectedConditions.elementToBeClickable(Utilities.getLocator(pageElement, args)));
            element.clear();
            if (DriverFactory.IE.equals(Context.getBrowser())) {
                String javascript = "arguments[0].value='" + value + "';";
                ((JavascriptExecutor) getDriver()).executeScript(javascript, element);
            } else {
                element.sendKeys(value);
            }
            if (keysToSend != null) {
                element.sendKeys(keysToSend);
            }
        } catch (Exception e) {
            new Result.Failure<>(e.getMessage(), Messages.format(Messages.getMessage(Messages.FAIL_MESSAGE_ERROR_ON_INPUT), pageElement, pageElement.getPage().getApplication()), true,
                    pageElement.getPage().getCallBack());
        }
    } else {
        logger.debug("Empty data provided. No need to update text. If you want clear data, you need use: \"I clear text in ...\"");
    }
}
 
Example 8
Project: testing_security_development_enterprise_systems   File: HomePageObject.java   Source Code and License Vote up 5 votes
public void changeData(String value){

        WebElement text = getDriver().findElement(By.id("form:text"));
        WebElement button = getDriver().findElement(By.id("form:modify"));

        text.clear();
        text.sendKeys(value);
        button.click();
        waitForPageToLoad();
    }
 
Example 9
Project: mobileAutomation   File: Page.java   Source Code and License Vote up 5 votes
public void sendText(By inputBy, String text){
	logger.log("Clear + Send text \"" + text + "\" to " + inputBy );
	WebElement input = findElement(inputBy);
	
	for (int i=1; i<=3; i++){
		input.clear();
		ThreadUtils.sleepQuiet(500);
		if(input.getText().isEmpty()){
			break;
		}
	}
			
	input.sendKeys(text);
	ThreadUtils.sleepQuiet(500);		
}
 
Example 10
Project: kheera   File: AbstractPage.java   Source Code and License Vote up 5 votes
@Override
public boolean clearText(WebElement w) throws Exception{
	boolean result = false;
	try{
		w.clear();
		result = true;
	} catch(Exception e){
		e.printStackTrace();
		result = false;
	}
	Assert.assertTrue(result, "Clear " + w.toString());
	return result;
}
 
Example 11
Project: mot-automated-testsuite   File: WebDriverWrapper.java   Source Code and License Vote up 5 votes
/**
 * Enters the specified text into the field.
 * @param text  The text to enter
 */
public void enterIntoFieldWithLabel(String labelText, String text) {
    // find the input associated with the specified label...
    WebElement labelElement = webDriver.findElement(
            By.xpath("//*[text() = '" + labelText + "']//ancestor::label"));
    WebElement textElement = webDriver.findElement(By.id(labelElement.getAttribute("for")));
    textElement.clear();
    textElement.sendKeys(text);
}
 
Example 12
Project: marathonv5   File: JTextAreaTest.java   Source Code and License Vote up 5 votes
void getAttributes() throws Throwable {
    driver = new JavaDriver();
    WebElement textArea = driver.findElement(By.cssSelector("text-area"));
    AssertJUnit.assertEquals("true", textArea.getAttribute("editable"));
    textArea.sendKeys("Systems", Keys.SPACE);
    String previousText = textArea.getText();
    textArea.clear();
    textArea.sendKeys("Jalian" + previousText);
}
 
Example 13
Project: mot-automated-testsuite   File: WebDriverWrapper.java   Source Code and License Vote up 5 votes
/**
 * Enters the specified text into the field.
 * @param text  The text to enter
 * @param label The field label
 */
public void enterIntoField(String text, String label) {
    // find the input associated with the specified label...
    WebElement labelElement = webDriver.findElement(By.xpath("//label[contains(text(),'" + label + "')]"));
    WebElement textElement = webDriver.findElement(By.id(labelElement.getAttribute("for")));
    textElement.clear();
    textElement.sendKeys(text);
}
 
Example 14
Project: opentest   File: SendKeys.java   Source Code and License Vote up 5 votes
@Override
public void run() {
    super.run();

    By locator = this.readLocatorArgument("locator");
    String text = this.readStringArgument("text", null);
    String key = this.readStringArgument("key", null);
    Boolean clearContent = this.readBooleanArgument("clearContent", Boolean.FALSE);
    Boolean sendEnter = this.readBooleanArgument("sendEnter", Boolean.FALSE);

    this.waitForAsyncCallsToFinish();

    try {
        WebElement element = this.getElement(locator);
        WebDriverWait wait = new WebDriverWait(this.driver, this.explicitWaitSec);
        wait.until(ExpectedConditions.elementToBeClickable(element));

        if (clearContent) {
            element.clear();
        }

        if (text != null) {
            element.sendKeys(text);
        } else if (key != null) {
            element.sendKeys(Keys.valueOf(key.toUpperCase()));
        } else {
            throw new RuntimeException("Neither the \"text\" argument, nor the \"key\" argument were provided.");
        }

        if (sendEnter) {
            element.sendKeys(Keys.ENTER);
        }
    } catch (Exception ex) {
        throw new RuntimeException(String.format(
                "Failed sending keys to element %s",
                locator), ex
        );
    }
}
 
Example 15
Project: marathonv5   File: JPasswordFieldTest.java   Source Code and License Vote up 5 votes
public void getText() throws Throwable {
    driver = new JavaDriver();
    WebElement passField = driver.findElement(By.cssSelector("password-field"));
    AssertJUnit.assertEquals("true", passField.getAttribute("enabled"));
    passField.clear();
    AssertJUnit.assertEquals("", passField.getText());
    passField.sendKeys("password");
    AssertJUnit.assertEquals("password", passField.getText());
    passField.clear();
    AssertJUnit.assertEquals("", passField.getText());
    passField.sendKeys("[email protected]");
    AssertJUnit.assertEquals("[email protected]", passField.getText());
}
 
Example 16
Project: marathonv5   File: JTextFieldTest.java   Source Code and License Vote up 5 votes
public void sendKeys() throws Throwable {
    driver = new JavaDriver();
    WebElement textField = driver.findElement(By.cssSelector("text-field"));
    AssertJUnit.assertEquals("true", textField.getAttribute("editable"));
    textField.clear();
    AssertJUnit.assertEquals("", textField.getText());
    textField.sendKeys("Lewis CarrollR");
    textField.sendKeys(Keys.BACK_SPACE);
    AssertJUnit.assertEquals("Lewis Carroll", textField.getText());
    textField.sendKeys(Keys.HOME + "Jhon ");
    AssertJUnit.assertEquals("Jhon Lewis Carroll", textField.getText());
    textField.sendKeys(OSUtils.getKeysFor(textField, "select-all"));
    textField.sendKeys(Keys.DELETE);
    AssertJUnit.assertEquals("", textField.getText());
}
 
Example 17
Project: mot-automated-testsuite   File: WebDriverWrapper.java   Source Code and License Vote up 4 votes
/**
 * Enter text into field with a name.
 * @param name  The name for the field
 * @param text  The text to enter into the field
 */
public void enterTextInFieldWithName(String name, String text) {
    WebElement element = webDriver.findElement(By.name(name));
    element.clear();
    element.sendKeys(text);
}
 
Example 18
Project: bromium   File: TypeTextInElementFoundByCssSelector.java   Source Code and License Vote up 4 votes
@Override
public void executeAfterJSPreconditionHasBeenSatisfied(WebDriver driver, ReplayingState replayingState) {
    WebElement element = driver.findElement(By.cssSelector(cssSelector));
    element.clear();
    element.sendKeys(text);
}
 
Example 19
Project: zucchini   File: GenericStep.java   Source Code and License Vote up 4 votes
protected void clear(WebElement webElement) {
	webElement.clear();
}
 
Example 20
Project: mot-automated-testsuite   File: WebDriverWrapper.java   Source Code and License Vote up 2 votes
/**
 * Enters the specified text into the field.
 * <p>Note: This is a low-level way to locate the field. Please only use this method if the text <code>input</code>
 * doesn't have a corresponding label, otherwise use the <code>enterIntoField(String,String)</code> method using
 * the label text to identify the field.</p>
 * @param text  The text to enter
 * @param id    The field id
 */
public void enterIntoFieldWithId(String text, String id) {
    WebElement textElement = webDriver.findElement(By.id(id));
    textElement.clear();
    textElement.sendKeys(text);
}