Created
December 19, 2020 17:56
-
-
Save eliasnogueira/67da81c62cab3de2383052148e7ecafa to your computer and use it in GitHub Desktop.
Comparing two different ways to clean an input field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io.github.bonigarcia.wdm.WebDriverManager; | |
import org.junit.jupiter.api.*; | |
import org.openqa.selenium.By; | |
import org.openqa.selenium.JavascriptExecutor; | |
import org.openqa.selenium.WebDriver; | |
import org.openqa.selenium.WebElement; | |
import org.openqa.selenium.chrome.ChromeDriver; | |
class CleanInputsTest { | |
private static WebDriver driver; | |
@BeforeAll | |
static void before() { | |
WebDriverManager.chromedriver().setup(); | |
driver = new ChromeDriver(); | |
driver.get("https://output.jsbin.com/waqojuz/1"); | |
} | |
@BeforeEach | |
void each() { | |
driver.navigate().refresh(); | |
} | |
@Test | |
@DisplayName("Clean fields using Selenium") | |
void cleanUsingSelenium() { | |
WebElement email = driver.findElement(By.id("email")); | |
email.clear(); | |
email.sendKeys("new@email.com"); | |
WebElement password = driver.findElement(By.id("password")); | |
password.clear(); | |
password.sendKeys("987654321"); | |
WebElement address = driver.findElement(By.id("address")); | |
address.clear(); | |
address.sendKeys("Viebrug"); | |
WebElement address2 = driver.findElement(By.id("address2")); | |
address2.clear(); | |
address2.sendKeys("1"); | |
WebElement city = driver.findElement(By.id("city")); | |
city.clear(); | |
city.sendKeys("Utrecht"); | |
WebElement zipCode = driver.findElement(By.id("zipcode")); | |
zipCode.clear(); | |
zipCode.sendKeys("3511 AJ"); | |
} | |
@Test | |
@DisplayName("Clean fields using Javascript") | |
void cleanByJSExecutor() { | |
((JavascriptExecutor) driver).executeScript("document.getElementById('my-form').reset()"); | |
fillInInputFields(); | |
} | |
@Test | |
@DisplayName("Clean fields using another Javascript way") | |
void cleanByJSExecutorForEach() { | |
// this code use the Text Block feature from Java 15 | |
// if you don't have Java 15 installed you can add it as a regular String | |
String script = """ | |
Array.from(document.getElementsByTagName('input')).forEach(\s | |
function(element) { | |
element.value = ''; | |
} | |
); | |
"""; | |
((JavascriptExecutor) driver).executeScript(script); | |
fillInInputFields(); | |
} | |
@Test | |
@DisplayName("Clean fields using another Selenium way") | |
void cleanAllInputsUsingSelenium() { | |
// this will thrown an InvalidElementStateException | |
driver.findElements(By.tagName("input")).forEach(WebElement::clear); | |
fillInInputFields(); | |
} | |
@AfterAll | |
static void after() { | |
driver.quit(); | |
} | |
private void fillInInputFields() { | |
driver.findElement(By.id("email")).sendKeys("new@email.com"); | |
driver.findElement(By.id("password")).sendKeys("987654321"); | |
driver.findElement(By.id("address")).sendKeys("Viebrug"); | |
driver.findElement(By.id("address2")).sendKeys("1"); | |
driver.findElement(By.id("city")).sendKeys("Utrecht"); | |
driver.findElement(By.id("zipcode")).sendKeys("3511 AJ"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment