‐ o codigo está operacional ‐ Guia de Automação de Elementos de Formulário Web com Selenium Java JUnit - sandersbr/automacao-selenium GitHub Wiki
Fortaleza, segunda-feira, 28 de abril de 2025
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class FormularioTest {
WebDriver driver;
@Before
public void setUp() {
driver = new ChromeDriver();
driver.get("https://url-do-seu-formulario.com");
}
@Test
public void preencherFormulario() {
// Exemplos de interacoes estao listados abaixo
}
@After
public void tearDown() {
driver.quit();
}
}
Localizacao:
WebElement campoTexto = driver.findElement(By.id("campoTextoId"));
Interacao:
campoTexto.sendKeys("Texto a ser digitado");
campoTexto.clear();
Localizacao:
WebElement areaTexto = driver.findElement(By.id("areaTextoId"));
Interacao:
areaTexto.sendKeys("Texto para a area de texto");
areaTexto.clear();
Localizacao:
WebElement botao = driver.findElement(By.id("botaoId"));
Interacao:
botao.click();
Localizacao:
WebElement link = driver.findElement(By.linkText("Texto do Link"));
Interacao:
link.click();
Localizacao:
WebElement dropdown = driver.findElement(By.id("selectId"));
Select select = new Select(dropdown);
Interacao:
select.selectByVisibleText("Opcao Desejada");
select.selectByValue("valor");
select.selectByIndex(2);
Localizacao:
WebElement multiSelect = driver.findElement(By.id("multiSelectId"));
Select selectMultiple = new Select(multiSelect);
Interacao:
selectMultiple.selectByVisibleText("Opcao 1");
selectMultiple.selectByVisibleText("Opcao 2");
selectMultiple.deselectByVisibleText("Opcao 1"); // desmarca
selectMultiple.deselectAll(); // desmarca tudo
Localizacao:
WebElement checkbox = driver.findElement(By.id("checkboxId"));
Interacao:
if (!checkbox.isSelected()) {
checkbox.click(); // marca
}
if (checkbox.isSelected()) {
checkbox.click(); // desmarca
}
Localizacao:
WebElement radioButton = driver.findElement(By.id("radioButtonId"));
Interacao:
if (!radioButton.isSelected()) {
radioButton.click(); // marca
}
Localizacao:
WebElement uploadArquivo = driver.findElement(By.id("uploadId"));
Interacao:
uploadArquivo.sendKeys("/caminho/para/seu/arquivo.txt");
Localizacao:
WebElement imagem = driver.findElement(By.id("imagemId"));
Interacao:
imagem.click();
Localizacao:
WebElement modal = driver.findElement(By.id("modalId"));
Interacao:
modal.click(); // ou qualquer outra interação
Localizacao:
WebElement erro = driver.findElement(By.className("erro-class"));
Interacao:
String mensagemErro = erro.getText();
Localizacao:
List<WebElement> itens = driver.findElements(By.tagName("li"));
Interacao:
for (WebElement item : itens) {
System.out.println(item.getText());
}
Localizacao:
WebElement div = driver.findElement(By.className("classeDiv"));
Interacao:
div.click();
Localizacao:
WebElement data = driver.findElement(By.id("dataId"));
Interacao:
data.sendKeys("2025-05-01");
Localizacao:
WebElement buscar = driver.findElement(By.id("buscarId"));
Interacao:
buscar.sendKeys("Palavra chave");
Localizacao:
WebElement filtro = driver.findElement(By.className("filter-class"));
Interacao:
filtro.click(); // Marca ou desmarca filtro
Localizacao:
WebElement senha = driver.findElement(By.id("senhaId"));
Interacao:
senha.sendKeys("minhaSenha123");
Localizacao:
WebElement imagemLink = driver.findElement(By.tagName("img"));
Interacao:
imagemLink.click();
Localizacao:
WebElement dropdown = driver.findElement(By.id("dropdownId"));
Select select = new Select(dropdown);
Interacao:
select.selectByVisibleText("Escolha uma opção");
Localizacao:
WebElement tabela = driver.findElement(By.id("tabelaId"));
List<WebElement> linhas = tabela.findElements(By.tagName("tr"));
Interacao:
for (WebElement linha : linhas) {
System.out.println(linha.getText());
}
Localizacao:
WebElement invisivel = driver.findElement(By.id("invisivelId"));
Interacao:
if (invisivel.isDisplayed()) {
System.out.println("Elemento visível");
} else {
System.out.println("Elemento invisível");
}
Localizacao:
JavascriptExecutor js = (JavascriptExecutor) driver;
Interacao:
js.executeScript("window.scrollBy(0,250)");
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class FormularioTest {
WebDriver driver;
@Before
public void setUp() {
driver = new ChromeDriver();
driver.get("https://url-do-seu-formulario.com");
}
@Test
public void preencherFormulario() {
WebElement campoTexto = driver.findElement(By.id("campoTextoId"));
campoTexto.sendKeys("Preenchendo texto");
campoTexto.clear();
WebElement areaTexto = driver.findElement(By.id("areaTextoId"));
areaTexto.sendKeys("Preenchendo textarea");
WebElement checkbox = driver.findElement(By.id("checkboxId"));
if (!checkbox.isSelected()) {
checkbox.click();
}
WebElement radioButton = driver.findElement(By.id("radioButtonId"));
if (!radioButton.isSelected()) {
radioButton.click();
}
WebElement dropdown = driver.findElement(By.id("selectId"));
Select select = new Select(dropdown);
select.selectByVisibleText("Opcao 1");
WebElement multiSelect = driver.findElement(By.id("multiSelectId"));
Select selectMultiple = new Select(multiSelect);
selectMultiple.selectByVisibleText("Opcao A");
selectMultiple.selectByVisibleText("Opcao B");
WebElement botao = driver.findElement(By.id("botaoId"));
botao.click();
WebElement link = driver.findElement(By.linkText("Clique Aqui"));
link.click();
}
@After
public void tearDown() {
driver.quit();
}
}