Se eu colocar essas 2 linhas de código: System.setProperty("webdriver.gecko.driver", "c:\geckodriver.exe"); driver = new FirefoxDriver(); em cada teste sem o before e o after funciona, após implementar esses métodos meu código ficou como abaixo e parou de funcionar.
package br.com.caelum.teste;
import static org.junit.Assert.assertTrue;
import org.junit.After;
import org.junit.Before;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class UsuariosSystemTest {
private FirefoxDriver driver;
@Before
public void inicializa() {
System.setProperty("webdriver.gecko.driver", "c:\\geckodriver.exe");
driver = new FirefoxDriver();
}
@Test
public void deveAdicionarUmUsuario() {
driver.get("http://localhost:8080/usuarios/new");
WebElement nome = driver.findElement(By.name("usuario.nome"));
WebElement email = driver.findElement(By.name("usuario.email"));
nome.sendKeys("Ronaldo Luiz de Albuquerque");
email.sendKeys("ronaldo2009@terra.com.br");
WebElement botaoSalvar = driver.findElement(By.id("btnSalvar"));
botaoSalvar.click();
boolean achouNome = driver.getPageSource().contains("Ronaldo Luiz de Albuquerque");
boolean achouEmail = driver.getPageSource().contains("ronaldo2009@terra.com.br");
assertTrue(achouNome);
assertTrue(achouEmail);
}
@Test
public void naoDeveAdicionarUmUsuario() {
driver.get("http://localhost:8080/usuarios/new");
WebElement email = driver.findElement(By.name("usuario.email"));
email.sendKeys("ronaldo2009@terra.com.br");
WebElement botaoSalvar = driver.findElement(By.id("btnSalvar"));
botaoSalvar.click();
assertTrue(driver.getPageSource().contains("Nome obrigatorio!"));
}
@Test
public void naoDeveAdicionarUmUsuarioSemNomeOuEmail() {
driver.get("http://localhost:8080/usuarios/new");
WebElement botaoSalvar = driver.findElement(By.id("btnSalvar"));
botaoSalvar.click();
boolean semNome = driver.getPageSource().contains("Nome obrigatorio!");
boolean semEmail = driver.getPageSource().contains("E-mail obrigatorio!");
assertTrue(semNome);
assertTrue(semEmail);
}
@After
public void fecha() {
driver.close();
}
}