Boa tarde!
Ao executar meu teste "LanceSystemTeste" foi exibido a seguinte mensagem de erro:
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at br.com.caelum.teste.LeiloesPage.detalhes(LeiloesPage.java:37)
at br.com.caelum.teste.LanceSystemTest.deveFazerUmLance(LanceSystemTest.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
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.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
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.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
apareceu no eclipse como se o erro estivesse no deveFazerUmLance.
package br.com.caelum.teste;
import static org.junit.Assert.assertTrue;
import org.junit.Before; import org.junit.Test; import org.openqa.selenium.firefox.FirefoxDriver;
public class LanceSystemTest {
private FirefoxDriver driver;
private LeiloesPage leiloes;
@Before
public void inicializa() {
driver = new FirefoxDriver();
leiloes = new LeiloesPage(driver);
driver.get("http://localhost:8080/apenas-teste/limpa");
UsuariosPage usuarios = new UsuariosPage(driver);
usuarios.visita();
usuarios.novo().cadastra("Jessica Rodrigues", "jessica@teste.com");
LeiloesPage leiloes = new LeiloesPage(driver);
leiloes.visita();
leiloes.novo().preenche("Mesa", 150, "Jessica Rodrigues", false);
}
@Test
public void deveFazerUmLance() {
DetalhesDoLeilaoPage lances = leiloes.detalhes(1);
lances.lance("Jessica Rodrigues", 150);
assertTrue(lances.existeLance("Jessica Rodrigues", 150));
}
}
Código com implementação DetalhesDoLeilaoPage
package br.com.caelum.teste;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class LeiloesPage {
private WebDriver driver;
public LeiloesPage(WebDriver driver) {
this.driver = driver;
}
public void visita() {
driver.get("http://localhost:8080/leiloes");
}
public NovoLeilaoPage novo() {
driver.findElement(By.linkText("Novo Leilão")).click();
return new NovoLeilaoPage(driver);
}
public boolean existe (String produto, double valor, String usuario, boolean usado) {
return driver.getPageSource().contains(produto) &&
driver.getPageSource().contains(String.valueOf(valor)) &&
driver.getPageSource().contains(usado ? "Sim":"Não");
}
public DetalhesDoLeilaoPage detalhes(int posicao) {
List<WebElement> elementos = driver.findElements(By.linkText("exibir"));
elementos.get(posicao -1).click();
return new DetalhesDoLeilaoPage(driver);
}
}