简体   繁体   English

Selenium 页面对象模型框架

[英]Selenium Page Object Model Framework

I am learning page object model using selenium webdriver and can someone advise me on these?我正在使用 selenium webdriver 学习页面对象模型,有人可以就这些给我建议吗?

1 - Why does each page in the POM have constructors to initialize the webdriver object? 1 - 为什么POM中的每个页面都有构造函数来初始化webdriver对象? (like the one below) What will happen if webdriver is not initialized? (如下图所示)如果 webdriver 没有初始化会怎样?

public class New_Register 
{
    WebDriver driver;

    public New_Register(WebDriver driver)
    {
        this.driver=driver;
    }

    @FindBy(xpath=".//input[@id='emailAdd']") WebElement EmailID;
}

2 - Is POM a framework or design pattern in Selenium Webdriver? 2 - POM 是 Selenium Webdriver 中的框架或设计模式吗?

If your Page Object inherits from LoadableComponent class forcing you to override load and isloaded methods and you use the PageFactory.initElements to initialize proxies for your webelements(@Find...) I think you are using it as a framework.如果您的 Page Object 继承自 LoadableComponent 类,迫使您覆盖 load 和 isloaded 方法,并且您使用 PageFactory.initElements 为您的 webelements 初始化代理(@Find...),我认为您将其用作框架。 If you handle all this stuff taking care of checking whether your page is loaded, getting webelements, passing the pageobject etc but you using a class to model a page or part of its functionality it may be regarded as a design pattern.如果您处理所有这些事情并检查您的页面是否已加载、获取 web 元素、传递 pageobject 等,但是您使用类来对页面或其部分功能进行建模,则它可能被视为一种设计模式。

If you do not pass in the driver you will have to initialize driver yourself.如果您不传入驱动程序,则必须自己初始化驱动程序。 Else you will get the favorite Null Pointer Exception when you call any method on it.否则,当您在其上调用任何方法时,您将获得最喜欢的 Null 指针异常。 Also when you call PageFactory.initElements it first looks for a constructor on the pageobject with driver as the sole argument.此外,当您调用 PageFactory.initElements 时,它首先会在页面对象上查找以驱动程序作为唯一参数的构造函数。 Then only it looks for the default one if it does not get the previous one, then you will have to handle driver initialization yourself.然后如果它没有得到前一个,它只会寻找默认的,然后你将不得不自己处理驱动程序初始化。

a) I will answer your 2nd question first - Page Object Model in UI Test Automation is a design pattern NOT a Framework. a)我将首先回答您的第二个问题 - UI 测试自动化中的页面对象模型是一种设计模式而不是框架。 A Framework provides you easy access to generic methods/configs etc. on top of which you can build your application (let it be a Web App or Test Automation Suite) and reduces much of our boilerplate code.框架让您可以轻松访问通用方法/配置等,您可以在其上构建应用程序(让它成为 Web 应用程序或测试自动化套件)并减少我们的大部分样板代码。 A good framework should make use of existing libraries and user-written code and should do most of the heavy lifting.一个好的框架应该利用现有的库和用户编写的代码,并且应该完成大部分繁重的工作。 For example, the TestNG library can be considered as a Testing Framework that provides us testing related features (like running tests, supplying data to our tests, defining rules for running the tests like priority etc.).例如,TestNG 库可以被视为一个测试框架,它为我们提供测试相关的功能(如运行测试、为我们的测试提供数据、定义运行测试的规则,如优先级等)。

Whereas a design pattern provides a solution to commonly occurring or repeating design problems in your project.而设计模式为项目中常见或重复的设计问题提供了解决方案。
For example, you may need to instantiate a single instance of Webdriver or any Database reading object to control its action throughout its execution.例如,您可能需要实例化 Webdriver 的单个实例或任何数据库读取对象,以控制其在整个执行过程中的操作。 Now to solve this you may use a Singleton Pattern in your code.现在要解决这个问题,您可以在代码中使用单例模式。 Reading about other design patterns like Facade and Builder will provide more clarity on this.阅读其他设计模式,如 Facade 和 Builder 会更清楚地说明这一点。 Now similarly you have POM which is useful in reducing code duplication and improving test case maintenance since we segregate respective Page classes from their tests.现在类似地,您拥有 POM,它在减少代码重复和改进测试用例维护方面很有用,因为我们将各自的 Page 类与其测试分开。

Again do not get confuse Design Pattern with Design Principles like DRY and SOLID etc.再次不要将设计模式与 DRY 和 SOLID 等设计原则混淆。

So in a nutshell all answers different problems :)所以简而言之,所有答案都不同:)

b) for your first question kindly refer to my already existing answer provided below - And as @Grasshopper rightly mentioned, you will get a null pointer exception if you do not implement the constructors. b)对于您的第一个问题,请参阅下面提供的我已经存在的答案-正如@Grasshopper 正确提到的那样,如果您不实现构造函数,您将获得空指针异常。

https://stackoverflow.com/a/70078344/6053575 https://stackoverflow.com/a/70078344/6053575

Do let me know if you have any further questions.如果您还有其他问题,请告诉我。

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

相关问题 Selenium TestNG 页面对象模型框架中的问题 - Issue in Selenium TestNG Page Object Model framework 为 Selenium 页面编写通用方法 Object Model 框架 - Writing generic methods for Selenium Page Object Model Framework Selenium Grid与页面对象模型页面工厂 - Selenium Grid With Page Object Model Page Factory 如何使用不同的程序包管理Selenium Project代码(如页面对象模型/关键字驱动程序框架) - How to manage Selenium Project Code using different package (Like page object model/Keyword driver framework) 我正在 selenium 自动化中创建页面 object model 框架,我正面临 java null 指针异常 - I am creating Page object model framework in selenium automation and I am facing a java null pointer exception 使用页面对象模型创建框架Selenium Webdriver Java TestNG和Maven需要访问一个网页上的多个页面 - Framework creating Using Page Object Model Selenium Webdriver Java TestNG and Maven need to access multiple pages on a web page NelePointerException在硒中使用页面对象模型 - NullPointerException using Page Object Model in Selenium Selenium中的页面对象模型最佳实践 - Page Object Model Best Practices in Selenium Selenium WebDriver Page对象模型描述 - Selenium WebDriver Page object model description Selenium Java-页面对象模型查询 - Selenium Java - Page Object Model Query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM