Clear() and Sendkeys() in a single method

Asked

Viewed 718 times

-1

I have the methods clear and sendKeys and wanted to "colapsar" both in one,

WebElement user = webdriver.findElement(By.id("username"));
user.clear();
user.sendKeys(username);       
WebElement passw = webdriver.findElement(By.id("password"));
passw.clear();
passw.sendKeys(password);

2 answers

1

You can create a new method:

public void setNewInputValue(WebElement elem, String value) {
    elem.clear();
    elem.sendKeys(value);
}

0

There are 2 options I know, but I believe that the best alternative really is to create a method for action, as the examples below:

@Test
public void clearSendTest() throws InterruptedException {
    driver.findElement(By.cssSelector("input[title='pesquisar']")).sendKeys(Keys.CLEAR, "php");
    Thread.sleep(3000);
}

public void clearAfterSend() {
    driver.findElement(By.cssSelector("input[title='pesquisar']")).clear();
    driver.findElement(By.cssSelector("input[title='pesquisar']")).sendKeys("java");
}

@Test
public void clearAfterSendTest() throws InterruptedException {
    clearAfterSend();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.