Example usage for org.openqa.selenium WebElement sendKeys

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

Introduction

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

Prototype

void sendKeys(CharSequence... keysToSend);

Source Link

Document

Use this method to simulate typing into an element, which may set its value.

Usage

From source file:TestWithSelenium.java

@Test
public void vTestClearFilter() {
    //searching for filter & sending key
    WebElement filter = (new WebDriverWait(driver, MAX))
            .until((ExpectedCondition<WebElement>) (WebDriver d) -> {
                return d.findElement(By.id("filter"));
            });//from  w  w  w  . j  a v  a 2 s  .  c  o  m

    filter.sendKeys(Keys.BACK_SPACE);

    //searching for tbody
    WebElement table = (new WebDriverWait(driver, MAX)).until((ExpectedCondition<WebElement>) (WebDriver d) -> {
        return d.findElement(By.tagName("tbody"));
    });

    //checking whether number of rows is 5
    Assert.assertThat(table.findElements(By.tagName("tr")).size(), is(5));
}

From source file:TestWithSelenium.java

@Test
public void xTestEdit() {
    //searching for tbody
    List<WebElement> tableRows = (new WebDriverWait(driver, MAX))
            .until((ExpectedCondition<WebElement>) (WebDriver d) -> {
                return d.findElement(By.tagName("tbody"));
            }).findElements(By.tagName("tr"));

    WebElement editButton = null;/*from  ww w. ja va  2  s  .c o m*/
    for (int i = 0; i < tableRows.size(); i++) {
        if (tableRows.get(i).findElements(By.tagName("td")).get(0).getText().equalsIgnoreCase("938")) {
            editButton = tableRows.get(i);
            break;
        }
    }

    editButton.findElements(By.tagName("td")).get(7).findElements(By.tagName("a")).get(0).click();
    WebElement element = driver.findElement(By.id("description"));
    element.clear();
    element.sendKeys("Cool car");
    driver.findElement(By.id("save")).click();

    //searching for new tbody
    List<WebElement> tableRowsUpdated = (new WebDriverWait(driver, MAX))
            .until((ExpectedCondition<WebElement>) (WebDriver d) -> {
                return d.findElement(By.tagName("tbody"));
            }).findElements(By.tagName("tr"));

    String descriptionNew = "";
    for (int i = 0; i < tableRowsUpdated.size(); i++) {
        if (tableRowsUpdated.get(i).findElements(By.tagName("td")).get(0).getText().equalsIgnoreCase("938")) {
            descriptionNew = tableRowsUpdated.get(i).findElements(By.tagName("td")).get(5).getText();
            break;
        }
    }
    assertThat(descriptionNew, is("Cool car"));
}

From source file:$.SimpleTest.java

License:Apache License

@Test
    public void testSimple() throws Exception {
        this.driver.get(this.serverUrl + "index.html");

        String whoToSend = "foo";

        WebElement who = this.driver.findElement(By.id("who"));
        who.sendKeys(whoToSend);

        WebElement sendBtn = this.driver.findElement(By.id("send-btn"));
        sendBtn.click();/*from  w  ww .  j a  v a 2 s . com*/

        // wait 5 secs for ajax response
        new WebDriverWait(this.driver, 5)
                .until(ExpectedConditions.textToBePresentInElement(By.id("response"), whoToSend));

        WebElement response = this.driver.findElement(By.id("response"));
        String text = response.getText();

        Assert.assertEquals("Hello " + whoToSend, text);

    }

From source file:adminPanel.scenarios.GeneratorInstances.java

private void setPhoto() {
    WebElement fileInput = webDriver.findElement(By.xpath("//*[@id=\"file_ajax\"]"));
    fileInput.sendKeys("/Users/oleg/IdeaProjects/NGC_Tests/src/test/resources/images/doctor_avatar.png");
}

From source file:adminPanel.scenarios.GeneratorInstances.java

private void setPhoto(String path) {
    WebElement fileInput = webDriver.findElement(By.xpath("//*[@id=\"file_ajax\"]"));
    fileInput.sendKeys("/Users/oleg/IdeaProjects/NGC_Tests/src/test/resources/images/" + path);
}

From source file:alwaysonline.Access.java

License:Open Source License

/**
 * Method for the access./*from   w w  w.  j  ava  2 s .c  o m*/
 * @param user String that contains the username
 * @param pass String that contains the password
 * @return Access result: 0 failed, 1 success.
 */
public int doAccess(String user, String pass) {
    WebDriver driver = new HtmlUnitDriver();
    WebElement username, password;

    // access page
    driver.get("https://auth3.unipi.it/auth/perfigo_cm_validate.jsp");
    // looking for the credential fields
    username = driver.findElement(By.name("username"));
    username.clear();
    username.sendKeys(user);
    password = driver.findElement(By.name("password"));
    password.clear();
    password.sendKeys(pass);
    // Executing the access, submiting values
    driver.findElement(By.name("username")).submit();
    // If the session was already opened
    if (driver.getPageSource().contains("Too many users using this account")) {
        // put again credentials
        username = driver.findElement(By.name("username"));
        username.clear();
        username.sendKeys(user);
        password = driver.findElement(By.name("password"));
        password.clear();
        password.sendKeys(pass);
        // remove the old session
        driver.findElement(By.name("remove_old")).click();
        // submit again access data
        driver.findElement(By.name("username")).submit();
    }
    // controll if the access has been done successful
    if (driver.getPageSource().contains("You have been successfully logged on the network"))
        return 1;
    return 0;
}

From source file:app.Stepdefs.java

private void updateField(WebElement element, String data) {
    element.sendKeys("");
    element.sendKeys(data);
}

From source file:application.Main.java

License:Open Source License

public static void appSingleThread(Globals GLOBALS) {
    LinkedList<String> list = Input.readFacebookPostIds(GLOBALS.FILENAME);
    if (GLOBALS.RANDOM) {
        Collections.shuffle(list);
    }/*w  w  w.ja va  2  s .c o m*/
    String email = GLOBALS.fb_eamil;
    String pass = GLOBALS.password;
    WebDriver driver = new FirefoxDriver(); // The Firefox driver supports javascript 
    Autenticate.perform(driver, email, pass);
    try {
        Thread.sleep(Globals.TIME_TO_LOGIN_IN_FB_MS);
    } catch (Exception ex) {
        System.out.println("InterruptedException in crawl.run()");
        ex.printStackTrace();
        System.exit(-1);
    }
    for (String post_id : list) {
        System.out.println("\n****** STARTING NEW TREE: " + post_id + "******");
        try {
            //OPEN NEW TAB
            WebElement body = driver.findElement(By.tagName("body"));
            body.sendKeys(Keys.CONTROL + "t");
            Tree post = new Tree(post_id, GLOBALS);
            boolean result = post.crawl(driver);
            if (result) {
                post.prune();
                post.print();
                post.printEdgeList();
                System.out.println("****** TREE " + post_id + " ACCOMPLISHED ******");
            } else {
                body.sendKeys(Keys.CONTROL + "w");
                System.out.println("****** TREE " + post_id + " FAILED ******");
            }
            //SWITCH TAB
            ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
            driver.switchTo().window(tabs.get(tabs.size() - 1));
        } catch (Exception e) {
            System.out.println("****** TREE " + post_id + " FAILED ******");
            System.out.println("EXCEPTION in MAIN " + e);
            System.out.println("...RESTARTING APPLICATION AFTER 1 minute APPLICATION...");
            driver.quit();
            try {
                Thread.sleep(Globals.ONE_MINUTE);
            } catch (Exception ex) {
                System.out.println("InterruptedException in crawl.run()");
                ex.printStackTrace();
                System.exit(-1);
            }
            driver = new FirefoxDriver(); // The Firefox driver supports javascript 
            Autenticate.perform(driver, email, pass);
        }
    }
    driver.quit();
}

From source file:at.ac.tuwien.big.testsuite.impl.selenium.BaseSeleniumTest.java

License:Apache License

protected static void writeText(WebDriver driver, String id, String text) {
    final WebElement elem = driver.findElement(By.name(id));
    elem.clear();//from   w w  w. ja  va 2  s.  c om
    elem.sendKeys(text);
}

From source file:at.tugraz.ist.cucumber.SeleniumStepdefs.java

License:Open Source License

@When("^I use the top search box to search for a project called \"([^\"]*)\"$")
public void I_use_the_top_search_box_to_search_for_a_project_called(String projectName) throws Throwable {
    WebElement largeTopSearchBox = driver().findElement(By.xpath("//*[@id='largeMenu']/div[4]/input"));
    WebElement mobileSearchBox = driver().findElement(By.xpath("//*[@id='smallSearchBar']/input"));
    WebElement mobileSearchButton = driver().findElement(By.id("mobileSearchButton"));

    if (largeTopSearchBox.isDisplayed()) {
        largeTopSearchBox.clear();/*from   w w  w  .  j av a2 s.  com*/
        largeTopSearchBox.sendKeys(projectName);
        largeTopSearchBox.sendKeys(Keys.RETURN);
        jqueryWait();
    } else if (mobileSearchButton.isDisplayed()) {
        mobileSearchButton.click();
        mobileSearchBox.clear();
        mobileSearchBox.sendKeys(projectName);
        mobileSearchBox.sendKeys(Keys.RETURN);
        jqueryWait();
    }
}