Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ClickPage类型中ClickPage方法的重复修饰符_Java_Selenium - Fatal编程技术网

Java ClickPage类型中ClickPage方法的重复修饰符

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

我正在尝试学习页面对象模型(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 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注释标记的变量。它不会初始化该类。