SingletonDesign Pattern In Java || How to achieve Singleton In Selenium WebDriver
ฝัง
- เผยแพร่เมื่อ 26 ก.ค. 2024
- Singleton Pattern In Java || How to achieve Singleton In Selenium WebDriver.
~~Singleton Class in Java:
In object-oriented programming, a singleton class is a class that can have only one object (an instance of the class) at a time.
After first time, if we try to instantiate the Singleton class, the new variable also points to the first instance created.
So whatever modifications we do to any variable inside the class through any instance, it affects the variable of the single instance created and is visible if we access that variable through any variable of that class type defined.
~~~To design a singleton class:
1. Make constructor as private.
2. Write a static method that has return type object of this singleton class. Here, the concept of Lazy initialization in used to write this static method.
Normal class vs Singleton class: Difference in normal and singleton class in terms of instantiation is that, For normal class we use constructor, whereas for singleton class we use getInstance() method.
~~~Subscribe to this channel, and press bell icon to get some interesting videos on Selenium and Automation:
th-cam.com/users/Naveen%20Au...
Follow me on my Facebook Page:
/ naveenqtpexpert
Let's join our Automation community for some amazing knowledge sharing and group discussion:
t.me/joinchat/COJqZQ4enmEt4JA... Subscribe to this channel, and press bell icon to get some interesting videos on Selenium and Automation:
th-cam.com/users/Naveen%20Au...
Follow me on my Facebook Page:
/ naveenqtpexpert
Let's join our Automation community for some amazing knowledge sharing and group discussion on Telegram:
t.me/joinchat/COJqZUPB02r5sB7...
Paid courses (Recorded) videos:
Java & Selenium Course: www.naveenautomationlabs.com/p...
API Course: www.naveenautomationlabs.com/p... ➡️Get Our Courses✔️
📗 Get My Paid Courses at
Paid courses (Recorded) videos:
Java & Selenium Course: www.naveenautomationlabs.com/p...
API Course: www.naveenautomationlabs.com/p...
-------------------------------
✔️SOCIAL NETWORKS
Facebook: / naveenqtpexpert
Twitter: / naveenkhunteta
Blog: www.naveenautomationlabs.com
--------------------------------
Support My Channel✔️Or Buy Me A Coffee
Paypal: paypal.me/naveenkhunteta
Google Pay: naveenanimation20@gmail.com
--------------------------------
✔️Thanks for watching!
देखने के लिए धन्यवाद
Благодаря за гледането
感谢您观看
Merci d'avoir regardé
Grazie per la visione
Gracias por ver
شكرا للمشاهدة - วิทยาศาสตร์และเทคโนโลยี
This is important interview question👍
Awesome, very nice explanation
Hey Naveen, Man this is so helpful i have implemented this and also was asked in an interview about this! As always very informative and concise!!
Hi Naveen
Thanks for very informative video on singleton class.
Thanks. God bless Naveen.
Hi naveen , great video. one small thing - regarding lines 25,26 I'm not sure it's correct to say that nothing return ( 14:15 , 14:47 ) , since what's actually return is the 'singleton_instance' , which means y,z will point to the same address as x , that's why when you print y.str and z.str you get the same upper case string as x.str
Always the best
Hi naveen..very informative and useful videos..you are really doing a great job and very good explanation.
Could you please make a video on about Ant build tool and how to implement Ant in TestNG framework.
Thanks in advance!!!
Awesome :)
Hello Sir,
I have one question
If Two threads are accessing the Singleton class object at time then what will happen or what problem will come and how we resolve such problem.
just want to know what will happen when we use this code for doing parallel execution in testNG
sir while changing x.str valve other 2 reference value changed. And same thing is repeated in z.str value is it corroct sir.And where we can use this like any application where we use this.
instead of using string to check new object is created or not u should use hashCode()
If you call the openBrowser method from another class, then it will create anothe webdriver ??
What if for next teat case we need To invoke IE browser.. then hiw to proceed??
Hey, It looks like there is problem in your example. If we call openBrowser() method twice - than it opened two chrome browsers. So I think we should move this line "driver = new ChromeDriver" or in private constructor or in if block of getInstance method. So after that we avoid of possibility to open two browsers at the same time.
Correct
public WebDriver driverinit(){
WebDriverManager.chromedriver().setup();
if (driver==null){
driver = new ChromeDriver();
}
return driver;
Hello naveen sir i have a one quetion how to implement thread safe webDriver instance using threadlocal class please create on video for me please ...
hello, sir your huge fan. I have one doubt on time 10.38 why you call a static method with classname "Singleton x =Singleton.getinstance();" we can call a method directly "Singleton x = getinstance();" its worked and according to me there no need to call through the class name.
please explain it Sir !!
thank you !
Yes that's also fine
Hi naveen, When will u start Mobile App Testing using Appium.
what is a use of singleton class?
@Naveen Automation Labs : Please Create Video for "Serialization and Deserialization in Java" and its Implementation in Automation Framework.
Thanks
~PS
Hi Naveen, I had one query, Let say in TestFolder, My project contains 40 Test cases, and i m using Page object model, Do i need to use Local Webdriver instance for every test case
in singleton concept, yes!!
Thank you for the great video, I have a comment:
What if you have multiple test methods in your Test Class???
let's say we have another test method called
public void test02() {}
before this method you're trying to get an instance of WebDriver, it will never create a new instance of our Singleton class (Driverinit), but simultaneously the instance of WebDriver is not null, moreover try to handle any element in the second test method, it will throw (SessionNotCreatedException) as you've already quitted or closed the driver!!!
i guess I'm quite off topic but does anybody know of a good site to watch new movies online?
@Bodhi Derrick flixportal =)
@Major Malachi Thanks, signed up and it seems like a nice service :D I appreciate it !
@Bodhi Derrick glad I could help :)
Nice Video, but why Constructor is declared private? Pls explain
Because ..only 1 object call krna hai...single tone me ..other isme se acces nahi hona chahiye constructor...so private ..
Output is one-time only