Selenium Cucumber Java BDD Framework 7 - Page Factory | Step by Step

preview_player
Показать описание
Today we will learn:
1 - What is Page Factory
2 - How to implement Page Factory
3 - Useful Tips

What is Page Factory
A simple and easier implementation of Page Object Model in Selenium
Selenium’s inbuilt and optimized Page Object Model concept
As POM, has separation of objects and tests
Uses annotation @FindBy to find WebElements
@FindBy can use id, name, css, xpath, tagName, linkText, partialLinkText etc
Uses method initElements to initialize web elements
on calling initElements method all objects on that page gets initialized

Demo How to implement Page Factory Model
Step 1 - Create a class for each page
Step 2 - Create locators of all objects to be used in that page using @FindBy
Step 3 - Create methods or actions to be performed on the objects
Step 4 - Create constructor to get driver instance and
initialize Elements using method initElements
public LoginPage_PF(WebDriver driver) {
}
Step 5 - Update Test Scripts to refer methods from PageFactory class
Step 6 - Run and validate

Useful Tips 1
@CacheLookup
CacheLookup can be used to instruct the InitElements() method to cache the element once its located and so that it will not be searched over and over again whenever calling it from any method

@FindBy(id = "name")
@CacheLookup
WebElement txt_username;

This works well with a basic web application, but not recommended if you have Ajax applications where DOM changes on user actions.
In case you get StaleElementExceptions, avoid using this.

Useful Tips 2
In Ajax applications to handle loading time for element and to avoid
‘No Element Exception’, we can use

AjaxElementLocatorFactory Class
timeout for a WebElement can be assigned to the Object page class with the help of AjaxElementLocatorFactory

AjaxElementLocatorFactory factory = new AjaxElementLocatorFactory(driver, 30);

The above code will wait for maximum of 30 seconds until the elements specified by annotations is loaded.
If the element is not found in the given time interval, it will throw ‘NoSuchElementException' exception.

Useful Tips 3

With PageFactory, you can also locate a list of Elements

@FindBy(partialLinkText = "raghav")
List<WebElement> myLinks;

References

Hit Like and. Subscribe button, if you like this video. It gives me great motivation.

You can support my mission for education by sharing this knowledge and helping as many people as you can

________ ONLINE COURSES TO LEARN ________
Рекомендации по теме
Комментарии
Автор

I really worried about how I missed your videos for these days. You are great teacher.

aruneisen
Автор

You are a star man .. have been trying to understand the driver thing for so long and only to see your videos.. very good way of explanation..

Keerthirapolu
Автор

Thank you so much, Raghav !! Learned a lot from this series. keep sharing the knowledge.

Shashisingh-zgnc
Автор

Wish I discovered this video sooner, Great work thanks a mill Raghav🥳

JamesBrown-lqnn
Автор

Thanks, Raghav, for one more great session. Very useful.

srikanthbolisetty
Автор

Hi Raghav, this was a really great informative video, for some reason the steps i took were not working as the username and password steps didn't work and i had followed exactly your steps but I managed to solve the problem by modifying the constructor to

public LoginDemo_PF(WebDriver driver) {

PageFactory.initElements(driver, this);

}

Also i made all the WebElements in the PageFactory "public" and it seemed to work.

Anyways thanks for your help, these videos are the best on youtube right now!

Muhaimin
Автор

Thank you for your great job, Raghav!

svetlanamazhaykina
Автор

superb... learnt alot in this series... i started from scratch...

sonalikhairnar
Автор

Thank you so much, Raghav. Awsome Session!!

myWayIn
Автор

Awesome sir. Salute for making this video.

suchishah
Автор

Thank you,
It was a great Session, amazing content.

mahmoudiwissem
Автор

I came as a stranger to this channel and become as a student to you sir🥺🙏

AjayKumar-gugr
Автор

Thanks Raghav this is very useful session.

firstlooks
Автор

This is really great, I learned a lot. Thank you so much!

I have 2 small questions:
at 21:44, you forgot to change LoginPage_PF on line 18 to HomePage_PF.
at 25:26, what's the difference between using "LoginPage_PS.class" and "this"? I thought you mentioned earlier that both should work?

glenxoseph
Автор

very nicely explain keep uploading bro some more topic

amitbhardwaj
Автор

thank you Raghav really helped me understand

jungleetoka
Автор

You are just AMAZING
thanks a lot ...

Ashutoshkumar-ixqr
Автор

Hi Raghav,
All your sessions are great !!
Do you have any videos for webrtc quality testing like network impairment testing in automation?

avnibansal
Автор

Thank you so much, Raghav. Great Session. :)

rohit-
Автор

Thanks Raghav for such a wonderful session. If possible please do a video on steps definition reusability

pallavkrishna