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 WebDriverおりTestNG、ページ内のいくつかのエントリをテストしようとしましたJSF

4

1 に答える 1

10

@BeforeClass の定義があります。

@BeforeClass
Run before all the tests in a class

そして@FindBy、クラスを呼び出すたびに「実行」されます。

実際にはあなた@FindByの前に呼び出される@BeforeClassので、動作しません。

私があなたに提案できるのは、保持することですが、 PageObject パターン@FindByの使用を開始しましょう。

テストのページを保持し、次のようなオブジェクトの別のクラスを作成します。

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'll は次のようになります。

@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 に答える