Os métodos de teste que envolvem submit e verificação de mensagem após a resposta do servidor sempre estavam falhando. Tive que colocar um Thread.sleep(300) para aguardar a resposta da requisição e em seguida fazer o teste.
package br.com.caelum.selenium;
import static org.junit.Assert.*;
import io.github.bonigarcia.wdm.MarionetteDriverManager;
import java.io.IOException;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.MarionetteDriver;
public class UsuarioSystemTest {
private WebDriver driver;
@BeforeClass
public static void createAndStartService() throws IOException {
MarionetteDriverManager.getInstance().setup();
}
@Before
public void setUp(){
this.driver = new MarionetteDriver();
}
@Test
public void deveAdicionarUmUsuario () throws InterruptedException{
driver.get("http://localhost:8080/usuarios/new");
WebElement nome = driver.findElement(By.name("usuario.nome"));
WebElement email = driver.findElement(By.name("usuario.email"));
WebElement botaoSalvar = driver.findElement(By.id("btnSalvar"));
String nomeUsuario = "Adriano Xavier";
String emailUsuario = "axavier@empresa.com.br";
nome.sendKeys(nomeUsuario);
email.sendKeys(emailUsuario);
botaoSalvar.click();
Thread.sleep(300);
driver.get("http://localhost:8080/usuarios");
assertTrue("Não encontrou o nome cadastrado", driver.getPageSource().contains(nomeUsuario));
assertTrue("Não encontrou o email cadastrado", driver.getPageSource().contains(emailUsuario));
}
@Test
public void deveAvisarQueNomeEhObrigatorioAoAdicionarUsuario () throws InterruptedException{
driver.get("http://localhost:8080/usuarios/new");
WebElement email = driver.findElement(By.name("usuario.email"));
WebElement botaoSalvar = driver.findElement(By.id("btnSalvar"));
String emailUsuario = "axavier@empresa.com.br";
email.sendKeys(emailUsuario);
botaoSalvar.click();
Thread.sleep(300);
assertTrue("Não encontrou aviso de que o nome é obrigatório", driver.getPageSource().contains("Nome obrigatorio!"));
}
@Test
public void deveAvisarQueEmailEhObrigatorioAoAdicionarUsuario () throws InterruptedException{
driver.get("http://localhost:8080/usuarios/new");
WebElement nome = driver.findElement(By.name("usuario.nome"));
WebElement botaoSalvar = driver.findElement(By.id("btnSalvar"));
String nomeUsuario = "axavier@empresa.com.br";
nome.sendKeys(nomeUsuario);
botaoSalvar.click();
Thread.sleep(300);
assertTrue("Não encontrou aviso de que o email é obrigatório", driver.getPageSource().contains("E-mail obrigatorio!"));
}
@Test
public void deveAvisarQueNomeEmailSaoObrigatorioAoAdicionarUsuario () throws InterruptedException{
driver.get("http://localhost:8080/usuarios/new");
WebElement botaoSalvar = driver.findElement(By.id("btnSalvar"));
botaoSalvar.click();
Thread.sleep(300);
assertTrue("Não encontrou aviso de que o nome é obrigatório", driver.getPageSource().contains("Nome obrigatorio!"));
assertTrue("Não encontrou aviso de que o email é obrigatório", driver.getPageSource().contains("E-mail obrigatorio!"));
}
@After
public void tearDown() throws InterruptedException{
if (this.driver != null) {
this.driver.close();
}
}
}
Essa é a maneira mais correta de fazer? Achei estranha...
Também não consegui executar vários testes em sequência, pois o seguinte erro é apresentado:
org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
Build info: version: '2.53.1', revision: 'a36b8b1cd5757287168e54b817830adce9b0158d', time: '2016-06-30 19:26:09'
System info: host: 'notezim', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.4.0-31-generic', java.version: '1.8.0_91'
Driver info: driver.version: MarionetteDriver
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:665)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:249)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:234)
at org.openqa.selenium.firefox.MarionetteDriver.run(MarionetteDriver.java:79)
at org.openqa.selenium.firefox.MarionetteDriver.<init>(MarionetteDriver.java:73)
at org.openqa.selenium.firefox.MarionetteDriver.<init>(MarionetteDriver.java:45)
at br.com.caelum.selenium.UsuarioSystemTest.setUp(UsuarioSystemTest.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.openqa.selenium.WebDriverException: org.apache.http.conn.HttpHostConnectException: Connect to localhost:30643 [localhost/127.0.0.1] failed: Conexão recusada
Build info: version: '2.53.1', revision: 'a36b8b1cd5757287168e54b817830adce9b0158d', time: '2016-06-30 19:26:09'
System info: host: 'notezim', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.4.0-31-generic', java.version: '1.8.0_91'
Driver info: driver.version: MarionetteDriver
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:91)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:644)
... 31 more
Caused by: org.apache.http.conn.HttpHostConnectException: Connect to localhost:30643 [localhost/127.0.0.1] failed: Conexão recusada
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:151)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:71)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)
at org.openqa.selenium.remote.internal.ApacheHttpClient.fallBackExecute(ApacheHttpClient.java:144)
at org.openqa.selenium.remote.internal.ApacheHttpClient.execute(ApacheHttpClient.java:90)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:142)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
... 32 more
Caused by: java.net.ConnectException: Conexão recusada
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:74)
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:134)
... 45 more
Há alguma forma de contorno para isso?