What is Page Object Model And Why We Use PageFactory Class in Selenium?

What is Page Object Model And Why We Use PageFactory Class in Selenium?

Page Object Model (POM) is a Framework Design Pattern in Selenium. When tests increase in our test suites, typing code becomes more complex. So we need reusable, understandable, maintainable and faster framework design. PageFactory class help us in impelementing Page Object Model in Selenium Framework.

We create a class under pages package(mostly named “websitename”page) for object repository to storing all web elements. Then we create a constructer and in this we call initElements methods from PageFactory class. We initialize the page element so we can work directly on the element without getting the NullPointerException.

We use @FindBy annotation to locate web elements in page class. We make access modifiers public of web elements to be accessible from anywhere. Now we write once and we can use whenever we need this locators. Here is a sample page class.

No alt text provided for this image


If you use cucumber, call this page from stepdefiniton class or you use TestNG call it your test class. To call page first we create an object from web elements page class. Then with instance variable we can access all web element locators.?In this sample I created an object from FaceBookPage and I accessed all web elements locators with instance variable. So when I need this locators I can use from this page repeatedly.

No alt text provided for this image

要查看或添加评论,请登录

社区洞察

其他会员也浏览了