How to use @FindBy annotation in Selenium WebDriver

一笑奈何 提交于 2019-12-04 21:13:28
e1che

There's a difinition for @BeforeClass :

@BeforeClass
Run before all the tests in a class

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

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

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
} 

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!