Java ClickPage类型中ClickPage方法的重复修饰符
我正在尝试学习页面对象模型(POM)。这是我的页面类:Java ClickPage类型中ClickPage方法的重复修饰符,java,selenium,Java,Selenium,我正在尝试学习页面对象模型(POM)。这是我的页面类: import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.FindBy; import org.openqa.selenium.support.How; import org.openqa.selenium.support.PageFactory; public class
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
public class ClickPage {
@FindBy(how=How.XPATH, using="/html/body/div[1]/div/div/div/div/h3[2]/a")
public WebElement link;
public ClickPage(WebDriver driver){
PageFactory.initElements(driver, this);
}
public void navigator(){
link.click();
}
}
但在构造函数中,它抱怨
Duplicate modifier for the method ClickPage in type ClickPage
以下是你问题的答案: 从您的示例代码块可以明显看出,您正试图通过POM在工作中使用
PageFactory
。调用ClickPage
类时,页面上的元素已经初始化。接下来,您再次尝试通过PageFactory.initElements(driver,this)初始化元素代码>。因此,ClickPage类型中ClickPage方法的重复修饰符出现错误
这里需要解决以下几件事:
考虑全局声明WebDriver实例
为WebDriver实例创建构造函数
您的ClickPage
类将如下所示:
单击页面类:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
public class ClickPage {
WebDriver driver;
//constructor
public LoginPageNew(WebDriver ClickPageDriver)
{
this.driver=loginDriver;
}
@FindBy(how=How.XPATH, using="/html/body/div[1]/div/div/div/div/h3[2]/a")
public WebElement link;
public void navigator(){
link.click();
}
}
@Test (priority=0)
public void click_a_page()
{
//Created Page Object using Page Factory
ClickPage click_page = PageFactory.initElements(driver, ClickPage.class);
//Call the method
click_page.navigator();
}
现在,您需要在运行测试时在单击页面
页面上初始化变量。因此,从您的测试类VerifyValidLogin
中,我们将调用initElements()
方法,如下所示:
验证ValidLogin类别:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
public class ClickPage {
WebDriver driver;
//constructor
public LoginPageNew(WebDriver ClickPageDriver)
{
this.driver=loginDriver;
}
@FindBy(how=How.XPATH, using="/html/body/div[1]/div/div/div/div/h3[2]/a")
public WebElement link;
public void navigator(){
link.click();
}
}
@Test (priority=0)
public void click_a_page()
{
//Created Page Object using Page Factory
ClickPage click_page = PageFactory.initElements(driver, ClickPage.class);
//Call the method
click_page.navigator();
}
如果这回答了您的问题,请告诉我。Initelements方法Pagefactory类使用代理初始化用findby注释标记的变量。它不会初始化该类。