简体   繁体   English

页面对象模型中需要页面工厂

[英]Need of page factory in page object model

Can anyone please explain what is the need of pagefactory in page object model. 谁能解释一下页面对象模型中pagefactory的需求。

Eg. 例如。 We use below code for initializing the page object class. 我们使用下面的代码初始化页面对象类。

LoginPage loginPage = PageFactory.initElements(driver, LoginPage.class);

Why can't we use 为什么我们不能使用

LoginPage loginPage = new LoginPage(driver);

Similarly while returning new page in each page object method we use 同样,在每个页面对象方法中返回新页面时,我们使用

return new PageFactory.initElements(driver, HomePage.class);

why should it not be 为什么不应该

return new HomePage(driver);

Please help me in understanding PageFactory, as I am new to page object pattern. 请帮助我理解PageFactory,因为我是页面对象模式的新手。 I am thinking we can still manage without using PageFactory. 我认为我们仍然可以在不使用PageFactory的情况下进行管理。

You can, in fact, use the page object pattern without using the PageFactory class. 实际上,您可以使用页面对象模式而不使用PageFactory类。 The page object pattern simply abstracts your business logic away from the physical structure of your pages. 页面对象模式只是将您的业务逻辑从页面的物理结构中抽象出来。 What the PageFactory class gives you is the ability to use annotations to automatically find the elements on the page without resorting to explicit findElement calls. PageFactory类为您提供了使用批注自动在页面上查找元素的能力,而无需诉诸明确的findElement调用。 It lets you write code like this: 它使您可以编写如下代码:

public class LoginPage {
  @FindBy(how = How.ID, using = "user")
  private WebElement userNameTextBox;

  @FindBy(how = How.ID, using = "password")
  private WebElement passwordTextBox;

  public void fillLoginDetails(String userName, String password) {
    userNameTextBox.sendKeys(userName);
    passwordTextBox.sendKeys(password);
  }
}

Note that there are no explicit findElement calls here. 请注意,这里没有显式的findElement调用。 Using the PageFactory allows you to write this code more cleanly and eliminates some boilerplate code. 使用PageFactory可使您更清晰地编写此代码,并消除一些样板代码。 The same thing could be accomplished by using findElement in your page objects to locate the appropriate elements. 通过在页面对象中使用findElement来找到适当的元素,可以完成相同的操作。 It's a stylistic choice. 这是一种风格选择。

The PageFactory injects WebElement s defined in your page object using the WebDriver you provide. PageFactory使用提供的WebDriver注入在页面对象中定义的WebElement

The documentation of PageFactory says: PageFactory的文档说:

The PageFactory relies on using sensible defaults: the name of the field in the Java class is assumed to be the "id" or "name" of the element on the HTML page. PageFactory依赖于使用合理的默认值:Java类中字段的名称假定为HTML页面上元素的“ id”或“ name”。

So you can just define a field of type WebElement with the id or name of an WebElement and the PageFactory takes care that it will be available. 因此,您可以使用ID或WebElement名称定义WebElement类型的字段,而PageFactory会确保该PageFactory将可用。

I am thinking we can still manage without using PageFactory. 我认为我们仍然可以在不使用PageFactory的情况下进行管理。

Yes you can. 是的你可以。 If you don't use the PageFactory you must lookup the WebElement s using the WebDriver api, eg 如果不使用PageFactory ,则必须使用WebDriver api查找WebElement ,例如

WebElement searchBox = driver.findElement(By.id("q"));

But you can also annotate a field with @FindBy , @FindBys or @FindAll , eg 但是您也可以使用@FindBy@FindBys@FindAll来注释字段,例如

@FindBy(how = How.ID, using = "q")
private WebElement searchBox;

I am new to page object pattern 我是页面对象模式的新手

A page object is an encapsulation of some web page. 页面对象是某些网页的封装。 It's api provides a user-like access to the web page and hides the implementation details of a page (web elements, ids, etc.). 它的api提供了类似于用户访问网页的功能,并隐藏了网页的实现细节(Web元素,ID等)。 Thus you can write your tests in a way a user would describe the tests. 因此,您可以以用户描述测试的方式编写测试。

For example 例如

 @Test
 public void googleSearch(){
     WebDriver driver = ....;

     GooglePage google = GooglePage.open(driver);
     SearchResultPage searchResult = google.search("stackoverflow");
     SearchResult firstResult = searchResult.getResult(0); // first result

     Assert.assertEquals("Stack Overflow", firstResult.getTitle());
     ...
 }

I see that you use java for selenium. 我看到您将Java用于硒。 You should take a look at the Arquillian-Graphene framework. 您应该看看Arquillian-Graphene框架。 It is an extesnion. 这是一个绝妙的选择。 So it will not mess up with your existing framework. 因此,它不会弄乱您现有的框架。

Main aim of using Arquillian framework is - you do not need Page Factory. 使用Arquillian框架的主要目的是-您不需要Page Factory。 It has nice set of annotations to inject the page object model at run time. 它具有一组不错的注释,可在运行时注入页面对象模型。

For ex: I create a page object for Google as shown here. 例如:我为Google创建一个页面对象,如下所示。

public class Google {

    @Drone
    private WebDriver driver;

    public void goTo(){
        driver.get("https://www.google.com");
    }

    public boolean isAt(){
        return driver.getTitle().equals("Google");
    }
}

In my testng/junit, 用我的话说

@RunAsClient
public class GoogleSearchTest extends Arquillian{

    @Page
    Google google;


    @Test(priority = 1)
    public void launchGoogle(){

        google.goTo();
        Assert.assertTrue(google.isAt());

    }

}

Did you notice? 你注意到了吗? @Drone - automatically injects the browser instance. @Drone-自动注入浏览器实例。 @Page automatically creates a Google page object instance. @Page自动创建一个Google页面对象实例。

Check here for more info: 在此处查看更多信息:

http://www.testautomationguru.com/arquillian-graphene-page-fragments/ http://www.testautomationguru.com/arquillian-graphene-page-fragments/

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

相关问题 Selenium Grid与页面对象模型页面工厂 - Selenium Grid With Page Object Model Page Factory 网页对象模型和网页工厂(如果是网页元素列表) - Page Object Model and Page Factory in case of list of web elements 硒中的页面对象模型和页面工厂有什么区别? - What is the differnce between page object model and page factory in selenium? 页面对象工厂-NullPointerException - Page Object Factory - NullPointerException Webdriver-使用页面工厂的网页对象模型,网页链接过多-最佳做法 - Webdriver - Page Object model using page factory, web page withh too many links - Best Practice Page对象工厂中的硒声明 - Assertions in selenium with Page Object Factory 使用 Page Factory Design 和 Page Object Model 进行测试,使用 Selenium 和 ZD5238788375ZA21833187880E1EA218 打开浏览器的两个实例 - Tests using Page Factory Design and Page Object Model opens two instances of the browser using Selenium and Java 如何从网站获取数据并在 excel Page object model 中打印 - How to fetch data from website and print in excel Page object model, Data driven and page factory this is hybrid framework 定位器和页面 Object Model - Locators and Page Object Model 在Java页面对象模型中支持两个版本的网页的工厂设计模式 - Factory design pattern for supporting two version of web pages in java page object model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM