0

我想知道我的代码有什么问题,因为当我尝试测试我的代码时,我什么也没得到。

public class SeleniumTest {

private WebDriver driver;
private String nome;
private String idade;

@FindBy(id = "j_idt5:nome")
private WebElement inputNome;

@FindBy(id = "j_idt5:idade")
private WebElement inputIdade;

@BeforeClass
public void criarDriver() throws InterruptedException {

    driver = new FirefoxDriver();
    driver.get("http://localhost:8080/SeleniumWeb/index.xhtml");
    PageFactory.initElements(driver, this);

}

@Test(priority = 0)
public void digitarTexto() {

    inputNome.sendKeys("Diego");
    inputIdade.sendKeys("29");

}

@Test(priority = 1)
public void verificaPreenchimento() {

    nome = inputNome.getAttribute("value");
    assertTrue(nome.length() > 0);

    idade = inputIdade.getAttribute("value");
    assertTrue(idade.length() > 0);
}

@AfterClass
public void fecharDriver() {

    driver.close();

}

}

我正在使用Selenium WebDriverand TestNG,我尝试测试JSF页面中的一些条目。

4

1 回答 1

10

@BeforeClass 有一个定义:

@BeforeClass
Run before all the tests in a class

@FindBy每次你打电话给班级时都会“执行” 。

实际上,您@FindBy在 the 之前被调用,@BeforeClass所以它不起作用。

我可以向您建议的是保留@FindBy但让我们开始使用PageObject 模式。

您保留测试页面并为您的对象创建另一个类,例如:

public class PageObject{
  @FindBy(id = "j_idt5:nome")
  private WebElement inputNome;

  @FindBy(id = "j_idt5:idade")
  private WebElement inputIdade;

  // getters
  public WebElement getInputNome(){
    return inputNome;
  }

  public WebElement getInputIdade(){
    return inputIdade;
  }

  // add some tools for your objects like wait etc
} 

你的 SeleniumTest 看起来像这样:

@Page
PageObject testpage;

@Test(priority = 0)
public void digitarTexto() {

  WebElement inputNome = testpage.getInputNome();
  WebElement inputIdade = testpage.getInputIdade();

  inputNome.sendKeys("Diego");
  inputIdade.sendKeys("29");
}
// etc

如果你要使用这个,告诉我发生了什么。

于 2013-05-12T21:54:55.280 回答