简体   繁体   English

使用PageFactory的Page类

[英]Page Class using PageFactory

I am new to PageFactory and referring this tutorial https://www.toptal.com/selenium/test-automation-in-selenium-using-page-object-model-and-page-factory 我是PageFactory新手,请PageFactory本教程https://www.toptal.com/selenium/test-automation-in-selenium-using-page-object-model-and-page-factory

An example from this page: 此页面上的示例:

public class HomePage {
   private WebDriver driver;

   //Page URL
   private static String PAGE_URL="https://www.toptal.com";

   //Locators

   //Apply as Developer Button
   @FindBy(how = How.LINK_TEXT, using = "APPLY AS A DEVELOPER")
   private WebElement developerApplyButton;

   //Constructor
   public HomePage(WebDriver driver){
       this.driver=driver;
       driver.get(PAGE_URL);
       //Initialise Elements
       PageFactory.initElements(driver, this);
   }

   public void clickOnDeveloperApplyButton(){

       developerApplyButton.click();

   }
}
  1. Why create a private instance of WebDriver ? 为什么要创建WebDriver的私有实例? It is appearing with yellow line for me. 对我来说它以黄线出现。
  2. When to use how = HOW and when we can straight away use xpath= //id.. ? 何时使用how = HOW ,何时可以使用xpath= //id..
  3. In constructor we are again passing WebDriver parameters? 在构造函数中,我们再次传递WebDriver参数?
  1. The yellow line is because you are not using the driver variable declared with the class but the one you are passing as the variable to the constructor. 黄线是因为您没有使用在类中声明的驱动程序变量,而是将您作为变量传递给构造函数的变量。 Try using : 尝试使用:

    this.driver.get(PAGE_URL); //Initialise Elements PageFactory.initElements(this.driver, this);

  2. You can use xpath = "//div" straight away when you are not using how.LINK_TEXT as you also have to provide the text for searching the link if you want to locate an element via LINK_TEXT. 不使用how..LINK_TEXT时,可以立即使用xpath = "//div"如果要通过LINK_TEXT查找元素,还必须提供用于搜索链接的文本。

  3. In the constructor, you are passing the driver instance so when the page object is created, it has a driver instance as it is not the page instantiating the driver. 在构造函数中,您要传递驱动程序实例,因此在创建页面对象时,它具有驱动程序实例,因为它不是实例化驱动程序的页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM