本文介绍了如何在Selenium WebDriver中使用@FindBy注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to know what wrong with my code, because when I try to test my code, I didn´t get anything.

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 ,我试图在<$ c中测试一些条目$ c> JSF page。

I´m using Selenium WebDriver and TestNG, and I tried to test some entries in JSF page.

推荐答案

@BeforeClass有一个定义:

There's a difinition for @BeforeClass :

@BeforeClass
Run before all the tests in a class

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

And @FindBy is "executed" each time that you'll call the class.

实际上你的 @FindBy @BeforeClass 之前调用,所以它不起作用。

Actually your @FindBy is called before the @BeforeClass so it won't work.

我建议你保持 @FindBy 但让我们开始使用

What i can suggest to you is to keep the @FindBy but let's start to use the PageObject pattern.

你保持您的测试页面,您为对象创建另一个类,如:

You keep your test's page and you create another class for your objects like :

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看起来像这样:

Your SeleniumTest'll looks like that :

@Page
PageObject testpage;

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

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

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

如果你打算用这个告诉我怎么了。

If you're going to use this tell me what's up.

这篇关于如何在Selenium WebDriver中使用@FindBy注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 07:40