[英]How to use @FindBy annotation in Selenium WebDriver
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();
}
} }
I´m using Selenium WebDriver
and TestNG
, and I tried to test some entries in JSF
page. 我正在使用Selenium WebDriver
和TestNG
,并且尝试测试JSF
页面中的某些条目。
There's a difinition for @BeforeClass : @BeforeClass有一个定义:
@BeforeClass
Run before all the tests in a class
And @FindBy
is "executed" each time that you'll call the class. 每次调用该类时,都会“执行” @FindBy
。
Actually your @FindBy
is called before the @BeforeClass
so it won't work. 实际上,您的@FindBy
在@BeforeClass
之前被调用,因此它将不起作用。
What i can suggest to you is to keep the @FindBy
but let's start to use the PageObject pattern. 我可以向您建议的是保留@FindBy
但让我们开始使用PageObject模式。
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 : 您的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
If you're going to use this tell me what's up. 如果您要使用此功能,请告诉我怎么了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.