Java Code Examples for org.openqa.selenium.WebElement#sendKeys()

The following examples show how to use org.openqa.selenium.WebElement#sendKeys() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HtmlPasswordInputTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@Alerts({"HtmlUnit", "HtmlUnit"})
public void typeDoesNotChangeValueAttributeWithInitialValue() throws Exception {
    final String html = "<html>\n"
            + "<head></head>\n"
            + "<body>\n"
            + "  <input type='password' id='p' value='HtmlUnit'/>\n"
            + "  <button id='check' onclick='alert(document.getElementById(\"p\").getAttribute(\"value\"));'>"
                    + "DoIt</button>\n"
            + "</body></html>";

    final WebDriver driver = loadPage2(html);
    final WebElement p = driver.findElement(By.id("p"));

    final WebElement check = driver.findElement(By.id("check"));
    check.click();
    verifyAlerts(driver, getExpectedAlerts()[0]);

    p.sendKeys("abc");
    check.click();
    verifyAlerts(driver, getExpectedAlerts()[1]);
}
 
Example 2
Source File: EditQuestionPage.java    From mamute with Apache License 2.0 6 votes vote down vote up
public QuestionPage edit(String title, String description, String tags) {
	WebElement editQuestionForm = byClassName("question-form");
	
	WebElement questionTitle = editQuestionForm.findElement(By.name("title"));
	questionTitle.clear();
	questionTitle.sendKeys(title);
	
	WebElement questionDescription = editQuestionForm.findElement(By.name("description"));
	questionDescription.clear();
	questionDescription.sendKeys(description);
	
	WebElement questionTags = editQuestionForm.findElement(By.name("tagNames"));
	questionTags.clear();
	questionTags.sendKeys(tags);
	
	WebElement editComment = editQuestionForm.findElement(By.name("comment"));
	editComment.clear();
	editComment.sendKeys("my comment");
	
	editQuestionForm.submit();
	return new QuestionPage(driver);
}
 
Example 3
Source File: EvernoteAndroidTest.java    From candybean with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void newNote() throws CandybeanException {
	openUsersMenu();
	WebElement newNoteButton = iface.wd.findElement(By
			.id("com.evernote:id/btn_new_note"));
	newNoteButton.click();
	iface.pause(2000);
	WebElement noteTitleField = iface.wd.findElement(By
			.id("com.evernote:id/note_title"));
	noteTitleField.click();
	iface.pause(1000);
	noteTitleField.sendKeys(Calendar.getInstance().getTime().toString());
	WebElement noteField = iface.wd
			.findElement(By.id("com.evernote:id/text"));
	noteField.click();
	iface.pause(1000);
	noteField
			.sendKeys("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor "
					+ "incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud "
					+ "exercitation ullamco laboris nis");
	iface.wd.findElement(By.className("android.widget.ImageButton")).click();
	iface.pause(2000);
}
 
Example 4
Source File: BmiCalculatorTest.java    From Selenium-WebDriver-3-Practical-Guide-Second-Edition with MIT License 6 votes vote down vote up
@Test
public void testBmiCalc() {
    WebElement height = driver.findElement(By.name("heightCMS"));
    height.sendKeys("181");

    WebElement weight = driver.findElement(By.name("weightKg"));
    weight.sendKeys("80");

    WebElement calculateButton = driver.findElement(By.id("Calculate"));
    calculateButton.click();

    WebElement bmi = driver.findElement(By.name("bmi"));
    assertEquals(bmi.getAttribute("value"), "24.4");

    WebElement bmi_category = driver.findElement(By.name("bmi_category"));
    assertEquals(bmi_category.getAttribute("value"), "Normal");
}
 
Example 5
Source File: SimpleTestDriver.java    From portals-pluto with Apache License 2.0 6 votes vote down vote up
/**
 * Called to login to the portal if necessary.
 */
protected static void login() {

   driver.get(loginUrl);

   List<WebElement> uels = driver.findElements(By.id(usernameId));
   List<WebElement> pwels = driver.findElements(By.id(passwordId));

   // If there is no login or password fields, don't need to login.
   if (!uels.isEmpty() && !pwels.isEmpty()) {

      System.out.println("login: found userid and password fields");
      WebElement userEl = uels.get(0);
      WebElement pwEl = pwels.get(0);

      // perform login
      userEl.clear();
      userEl.sendKeys(username);
      pwEl.clear();
      pwEl.sendKeys(password);
      pwEl.submit();

   }
}
 
Example 6
Source File: SugarIosTest.java    From candybean with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void testLogin() throws CandybeanException {
    WebElement username = iface.wd.findElement(By.xpath("//window[1]/scrollview[1]/webview[1]/textfield[1]"));
    assertTrue(username.isDisplayed());
    Set<String> handles = iface.wd.getWindowHandles();
    for (String s : handles) {
        System.out.println(s);
    }
    if (!username.getText().equals("")) {
        username.clear();
    }
    username.sendKeys("admin");
    WebElement password = iface.wd.findElement(By.xpath("//window[1]/scrollview[1]/webview[1]/secure[1]"));
    assertTrue(password.isDisplayed());
    password.click();
    password.sendKeys("asdf");
    WebElement login = iface.wd.findElement(By.xpath("//window[1]/scrollview[1]/webview[1]/link[1]"));
    login.click();
    iface.pause(1000000);
}
 
Example 7
Source File: SugarAndroidTest.java    From candybean with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void testLogin() throws Exception {
    WebElement username = driver.findElement(By.xpath("//window[1]/scrollview[1]/webview[1]/textfield[1]"));
    assertTrue(username.isDisplayed());
    Set<String> handles = driver.getWindowHandles();
    for (String s : handles) {
        System.out.println(s);
    }
    if (!username.getText().equals("")) {
        username.clear();
    }
    username.sendKeys("admin");
    WebElement password = driver.findElement(By.xpath("//window[1]/scrollview[1]/webview[1]/secure[1]"));
    assertTrue(password.isDisplayed());
    password.click();
    password.sendKeys("asdf");
    WebElement login = driver.findElement(By.xpath("//window[1]/scrollview[1]/webview[1]/link[1]"));
    login.click();
    Thread.sleep(1000000);
}
 
Example 8
Source File: JiraAwareAftBase.java    From rice with Educational Community License v2.0 5 votes vote down vote up
private WebElement type(By by, String text) {
    WebElement element = findElement(by);
    String name = element.getAttribute("name");
    WebDriverUtils.jGrowl(getDriver(), "Type", false, "Type into " + name + " the text: " + text);
    WebDriverUtils.highlightElement(getDriver(), element);
    element.sendKeys(text);
    return element;
}
 
Example 9
Source File: SimpleTests.java    From testing-cin with MIT License 5 votes vote down vote up
@Test
public void testNavigateAmazon() {
    String baseUrl = "http://amazon.com";        
    // launch browser
    driver.get(baseUrl);
    // look up today's deals
    WebElement element = driver.findElement(By.linkText("Today's Deals"));
    element.click();
    // search for some item
    element = driver.findElement(By.name("field-keywords"));
    element.sendKeys("MacBook Pro");
    element = driver.findElement(By.cssSelector(".nav-search-submit > input:nth-child(2)"));
    element.click();
    // The oracle is that this test should not crash. There is no **explicit** assertion.
}
 
Example 10
Source File: LibraryClientResponsivenessAjaxFieldQueryAft.java    From rice with Educational Community License v2.0 5 votes vote down vote up
/**
 * focus, blur seem real flaky on xvfb, maybe clear, enter value, and tab will be better
 *
 * @param fieldName name of the field that needs to be focused on.
 * @param fieldValue value to be placed in field
 *
 * @throws InterruptedException
 */
private void clearTypeAndFocus(String fieldName, String fieldValue) throws InterruptedException {
    clearTextByName(fieldName);
    WebElement element = WebDriverUtils.waitFor(driver, WebDriverUtils.configuredImplicityWait(), By.name(
            fieldName), this.getClass().toString());
    element.sendKeys("", fieldValue);
    fireEvent(fieldName, "focus");
    fireEvent(fieldName, "blur");
    assertTextPresent(fieldValue);
}
 
Example 11
Source File: SmartWebElement.java    From blueocean-plugin with MIT License 5 votes vote down vote up
/**
 * Sets the matched input to the given text, if setting to empty string
 * there is some special handling to clear the input such that events are
 * properly handled across platforms by sending an additional 'oninput' event
 * @param text text to use
 */
public void setText(CharSequence... text) {
    WebElement e = getElement();
    validateTextElement(e);
    e.clear();
    e.sendKeys(text);
    // If setting the text empty, also send input event
    if (text.length == 1 && "".equals(text[0])) {
        // b'cuz React, see: https://github.com/facebook/react/issues/8004
        sendInputEvent(e);
    }
}
 
Example 12
Source File: DashboardUtil.java    From Insights with Apache License 2.0 5 votes vote down vote up
/**
 * Login to Grafana and fetch current {dashUrl} dashboard 
 * Css Applicable only to Grafana v6.1.6 and firefox browser.
 * 
 * @param driver
 * @param dashUrl
 */
private void loginGrafana(WebDriver driver, String dashUrl) {
	driver.get(ApplicationConfigProvider.getInstance().getGrafana().getGrafanaEndpoint());
	WebElement username = driver.findElement(By.name("username"));
	username.sendKeys(ApplicationConfigProvider.getInstance().getGrafana().getAdminUserName());
	WebElement pwd = driver.findElement(By.name("password"));
	pwd.sendKeys(ApplicationConfigProvider.getInstance().getGrafana().getAdminUserPassword());
	driver.findElement(By.cssSelector("button.btn:nth-child(1)")).click();
	driver.get(dashUrl);
}
 
Example 13
Source File: WebDriverWebController.java    From stevia with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void getFocus(String locator) {
	WebElement element = waitForElement(locator);
	if ("input".equals(element.getTagName())) {
		element.sendKeys("");
	} else {
		new Actions(driver).moveToElement(element).perform();

	}
}
 
Example 14
Source File: DeployGroupTest.java    From packagedrone with Eclipse Public License 1.0 5 votes vote down vote up
private void addKey ( final String id )
{
    final WebDriver driver = getWebContext ().getResolved ( "/deploy/auth/group/" + id + "/createKey" );
    final WebElement nameEle = driver.findElement ( By.id ( "name" ) );
    nameEle.sendKeys ( "key1" );
    nameEle.submit ();
}
 
Example 15
Source File: ClientAddPage.java    From oxTrust with MIT License 5 votes vote down vote up
public void changePassword(String pwd) {
	webDriver.findElement(By.className("changeClientSecretButton")).click();
	WebElement newField = webDriver.findElement(By.id("clientPassword:changePasswordForm:pass"));
	newField.sendKeys(pwd);
	WebElement confirm = webDriver.findElement(By.id("clientPassword:changePasswordForm:conf"));
	confirm.sendKeys(pwd);
	WebElement main = webDriver.findElement(By.id("clientPassword:changePasswordModalPanel_content"));
	Assert.assertNotNull(main);
	WebElement button = main.findElement(By.className("savePasswordButton"));
	button.click();
	button.click();
	fluentWait(ONE_SEC);
}
 
Example 16
Source File: JRadioButtonTest.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
public void sendKeys() throws Throwable {
    driver = new JavaDriver();
    List<WebElement> radioButtons = driver.findElements(By.cssSelector("radio-button"));
    AssertJUnit.assertEquals(5, radioButtons.size());
    WebElement rbutton1 = radioButtons.get(0);
    AssertJUnit.assertEquals("Bird", rbutton1.getAttribute("buttonText"));
    WebElement rbutton2 = radioButtons.get(1);
    AssertJUnit.assertEquals("Cat", rbutton2.getAttribute("buttonText"));
    WebElement rbutton3 = radioButtons.get(2);
    AssertJUnit.assertEquals("Dog", rbutton3.getAttribute("buttonText"));
    WebElement rbutton4 = radioButtons.get(3);
    AssertJUnit.assertEquals("Rabbit", rbutton4.getAttribute("buttonText"));
    WebElement rbutton5 = radioButtons.get(4);
    AssertJUnit.assertEquals("Pig", rbutton5.getAttribute("buttonText"));

    AssertJUnit.assertEquals("true", rbutton1.getAttribute("selected"));
    AssertJUnit.assertEquals("false", rbutton2.getAttribute("selected"));
    AssertJUnit.assertEquals("false", rbutton3.getAttribute("selected"));
    AssertJUnit.assertEquals("false", rbutton4.getAttribute("selected"));
    AssertJUnit.assertEquals("false", rbutton5.getAttribute("selected"));
    rbutton3.sendKeys(Keys.SPACE);
    AssertJUnit.assertEquals("false", rbutton1.getAttribute("selected"));
    AssertJUnit.assertEquals("false", rbutton2.getAttribute("selected"));
    AssertJUnit.assertEquals("true", rbutton3.getAttribute("selected"));
    AssertJUnit.assertEquals("false", rbutton4.getAttribute("selected"));
    AssertJUnit.assertEquals("false", rbutton5.getAttribute("selected"));
}
 
Example 17
Source File: HTMLFormElementTest.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
private void requiredFileInput(final String req) throws Exception {
    final String html = "<html>\n"
        + "<head><title>first</title></head>\n"
        + "<body>\n"
        + "  <form name='testForm' action='\" + URL_SECOND + \"'>\n"
        + "    <input type='submit' id='submit'>\n"
        + "    <input type='file' name='test' value='' " + req + " >"
        + "  </form>\n"
        + "</body></html>";

    final String html2 = "<?xml version='1.0'?>\n"
        + "<html>\n"
        + "<head><title>second</title></head>\n"
        + "<body>OK</body></html>";
    getMockWebConnection().setDefaultResponse(html2);

    final WebDriver driver = loadPage2(html);
    driver.findElement(By.id("submit")).click();
    assertEquals(getExpectedAlerts()[0], driver.getTitle());

    loadPage2(html);
    final WebElement e = driver.findElement(By.name("test"));
    final String absolutePath = new File("pom.xml").getAbsolutePath();
    e.sendKeys(absolutePath);
    driver.findElement(By.id("submit")).click();
    assertEquals(getExpectedAlerts()[1], driver.getTitle());
}
 
Example 18
Source File: AuthorizationCodeTest.java    From ebay-oauth-java-client with Apache License 2.0 5 votes vote down vote up
private String getAuthorizationResponseUrl() throws InterruptedException {
    // Optional, if not specified, WebDriver will search your path for chromedriver.
    System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");

    WebDriver driver = new ChromeDriver();
    OAuth2Api auth2Api = new OAuth2Api();
    String authorizeUrl = auth2Api.generateUserAuthorizationUrl(EXECUTION_ENV, SCOPE_LIST, Optional.of("current-page"));

    driver.get(authorizeUrl);
    Thread.sleep(5000);

    WebElement userId = (new WebDriverWait(driver, 10))
            .until(ExpectedConditions.visibilityOf(driver.findElement(By.cssSelector("input[type='text']"))));
    WebElement password = driver.findElement(By.cssSelector("input[type='password']"));

    userId.sendKeys(CRED_USERNAME);
    password.sendKeys(CRED_PASSWORD);
    driver.findElement(By.name("sgnBt")).submit();

    Thread.sleep(5000);

    String url = null;
    if (driver.getCurrentUrl().contains("code=")) {
        printDetailedLog("Code Obtained");
        url = driver.getCurrentUrl();
    } else {
        WebElement agreeBtn = (new WebDriverWait(driver, 10))
                .until(ExpectedConditions.visibilityOf(driver.findElement(By.id("submit"))));

        agreeBtn.submit();
        Thread.sleep(5000);
        url = driver.getCurrentUrl();
    }
    driver.quit();
    return url;
}
 
Example 19
Source File: ProfileSteps.java    From attic-rave with Apache License 2.0 4 votes vote down vote up
private void changeFieldValue(String fieldId, String value) {
    final WebElement field = portal.findElement(By.id(fieldId));
    field.clear();
    field.sendKeys(value);
}
 
Example 20
Source File: OxAuthSettingPage.java    From oxTrust with MIT License 4 votes vote down vote up
public void setServerIp(String ip) {
	WebElement element = webDriver.findElement(By.className("serverIpTextBox"));
	element.clear();
	element.sendKeys(ip);
}