What is the significance of using Constructors in WebDriver Automation project?

2,404 views
Skip to first unread message

Paul Levesque

unread,
Apr 7, 2014, 9:31:32 AM4/7/14
to webd...@googlegroups.com
I am new to Selenium WebDriver. I can do bit of coding in JAVA but never feel confident that my code is optimum.
If any one experience person can put some light here, on How one can use JAVA constructors in building WebDriver Automation project.

- Thanks

Krishnan Mahadevan

unread,
Apr 7, 2014, 11:05:12 PM4/7/14
to webd...@googlegroups.com
Paul,
I didn’t quite get your question. What do you mean “Use Java constructors” in building web driver automation ? Can you please elaborate ?

Constructors are essentially mechanisms for creating objects. What would that have anything to do with WebDriver automation ?



Thanks and Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"


--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webdriver+...@googlegroups.com.
To post to this group, send email to webd...@googlegroups.com.
Visit this group at http://groups.google.com/group/webdriver.
For more options, visit https://groups.google.com/d/optout.

Paul Levesque

unread,
Apr 8, 2014, 1:38:33 AM4/8/14
to webd...@googlegroups.com
May be I can better explain using an example:

1. When I am not using a constructor
public class FindElementin60 extends AllElements {
   
    // wait upto 60 secs and click  -  for text
    public  void cliktxt(String locator){
        new WebDriverWait(driver, 60).until(
                ExpectedConditions.presenceOfElementLocated(By
                        .linkText(locator))).click();
    }
}
 
public class SignUp extends AllElements{

public void parentsignup(){

driver = new FirefoxDriver();
driver.get("http://myschool.com);
FindElementin60.cliktxt("Sign up");

}

}

2. When I am using a constructor

public class FindElementin60 extends AllElements {   
/*
* using constructor of same name
*
*/
public FindElementin60() {  

        class ClearIt{
           
            void txt(String locator) {
                new WebDriverWait(driver, 60).until(
                        ExpectedConditions.presenceOfElementLocated(By
                                .linkText(locator))).clear();
            }
           
        }
       
       
    }
   
public class SignUp extends AllElements{

public void parentsignup(){

driver = new FirefoxDriver();
driver.get("http://myschool.com);
FindElementin60.ClearIt.txt("career_id");
}

}
So question here, whats the difference in above two. Only difference I see is the different ways of calling a common function, thats it. Secondly, is it the best practice (most optimum approach) using constructor i.e. every WebDriver aspirant should make a habit of using constructors.
For example: Using Page Factory, creating common functions class, setting up @After and @Before Methods, using ".properties" file etc etc , this is something every experienced Automation developer advises to follow. Similarly, using  JAVA constructors always is it the best approach everyone should follow.

Thanks

Chris Merrill

unread,
Apr 8, 2014, 9:53:53 AM4/8/14
to webd...@googlegroups.com
I'm still confused. This code won't compile and nature of the errors make it hard to understand the
intention.

Constructors are integral to the Java language. You can't write anything beyond the most trivial
program without using constructors. Concepts related the use of constructors have little relevance
to discussions of WebDriver, beyond the requirement of understanding the language you are using to
access the WebDriver APIs.

Neither of the two lines you emphasized are using constructors. Well, sort-of, but not in a way that
would compile.

The phrasing of your question(s) and invalid code indicates that you have not yet mastered the
basics of the Java language....and perhaps that are having difficulty communicating the nature of
the question. I could be wrong, and I don't intend to offend, but that's just what I feel after
reading your messages.


So, I'm not quite sure how to help you here. My only suggestion is to study the Java language a bit
more, particularly constructors. Then write an example of valid Java code (i.e. make sure that it
compiles) and then come back with the question again. Hopefully I can better assist you at that point.

Chris


On 4/8/2014 1:38 AM, Paul Levesque wrote:
> May be I can better explain using an example:
>
> *1. When I am not using a constructor*
> public class FindElementin60 extends AllElements {
>
> // wait upto 60 secs and click - for text
> public void cliktxt(String locator){
> new WebDriverWait(driver, 60).until(
> ExpectedConditions.presenceOfElementLocated(By
> .linkText(locator))).click();
> }
> }
>
> public class SignUp extends AllElements{
>
> public void parentsignup(){
>
> driver = new FirefoxDriver();
> driver.get("http://myschool.com);
> *FindElementin60.cliktxt("Sign up");*
>
> }
>
> }
>
> *2. When I am using a constructor *
>
> public class FindElementin60 extends AllElements {
> /*
> * using constructor of same name
> *
> */
> public FindElementin60() {
>
> class ClearIt{
>
> void txt(String locator) {
> new WebDriverWait(driver, 60).until(
> ExpectedConditions.presenceOfElementLocated(By
> .linkText(locator))).clear();
> }
>
> }
>
>
> }
>
> public class SignUp extends AllElements{
>
> public void parentsignup(){
>
> driver = new FirefoxDriver();
> driver.get("http://myschool.com);
> *FindElementin60.ClearIt.txt("career_id");*
> webdriver+...@googlegroups.com <javascript:>.
> To post to this group, send email to webd...@googlegroups.com <javascript:>.
> <http://groups.google.com/group/webdriver>.
> For more options, visit https://groups.google.com/d/optout <https://groups.google.com/d/optout>.
>
> --
> You received this message because you are subscribed to the Google Groups "webdriver" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> webdriver+...@googlegroups.com <mailto:webdriver+...@googlegroups.com>.
> To post to this group, send email to webd...@googlegroups.com <mailto:webd...@googlegroups.com>.
> Visit this group at http://groups.google.com/group/webdriver.
> For more options, visit https://groups.google.com/d/optout.


--
------------------------------------------------------------------------ -
Chris Merrill | Web Performance, Inc.
ch...@webperformance.com | http://webperformance.com
919-433-1762 | 919-845-7601

Web Performance: Website Load Testing Software & Services
------------------------------------------------------------------------ -

darrell

unread,
Apr 8, 2014, 10:04:52 AM4/8/14
to webd...@googlegroups.com
Your question is less about WebDriver and more about how to create a test framework in Java. As always, I think about how would I test an application; not about whether to automate it or test it manually but more about what steps would I take to test it.

If I'm manually testing something and things go wrong, do I just keep blindly following the test plan? Or do I close the browser, start the browser and move to the next test? Probably the latter is true. So if I write my test as a Java application and something goes wrong in the middle of the test, how do I abort everything, close the browser and start the next test? Is there a framework out there I can use or build on?

Most people see Selenium testing to be a similar flow as unit testing. So unit test frameworks like JUnit or TestNG can be adapted to Selenium testing. Understanding how to do this assumes you understand Java and unit test frameworks. If you are trying to learn Java, unit testing, Selenium and how to properly test an application then you have four things to master. The more things you already have a handle on the easier it will be to incorporate the other concepts. Trying to incorporate all of them with little or no understanding of any would be a VERY daunting task. I suspect you are somewhere in the middle of these two extremes but the closer you are to knowing none of these things the harder it will be.

The most important thing is to question why you are doing anything. Why use Java application rather than JUnit or vice versa? Why extend a parent class? Do I create a new browser for each test or do I create one browser and run all the tests on it? What are the pros and cons of doing this?

In additionally to getting the right answers will be asking the right questions. Many companies want to hire someone with hands on experience because as you build a test framework and work with it you will find issues and limitations. You will have to figure out how to work around or eliminate those issues and limitations. As you build your list of skills it will become easier. However without actual hands on experience it will be difficult to predict what issues will come up for your environment.

Pushpraj Singh

unread,
Apr 8, 2014, 10:07:50 AM4/8/14
to webd...@googlegroups.com
@Chris
I know that I have only beginner's knowledge on both JAVA and WebDriver. I also got to know that QA don't need to become a master of JAVA but knowledge of Oops concept is must. While reading Oops, I have come across "Constructors". I am on my own in learning JAVA, so I need to pick what I can learn and what I can pursue later.
So the purpose of raising this question in forum is to get experienced Automation Developer's opinion on :

--
You received this message because you are subscribed to a topic in the Google Groups "webdriver" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/webdriver/OlHJ7VR5udE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to webdriver+...@googlegroups.com.
To post to this group, send email to webd...@googlegroups.com.

Chris Merrill

unread,
Apr 8, 2014, 10:27:02 AM4/8/14
to webd...@googlegroups.com
On 4/8/2014 10:07 AM, Pushpraj Singh wrote:
> @Chris
> I know that I have only beginner's knowledge on both JAVA and WebDriver. I also got to know that QA
> don't need to become a master of JAVA but knowledge of Oops concept is must.

I must respectfully disagree - if you are going to use WebDriver as part of your QA tooling, then
you must be very well versed in Java (or whatever language you are using with WebDriver). WebDriver
is not a testing tool for end-users, it is a programming API and you must be a good programmer to
use it effectively.

> While reading Oops, I
> have come across "Constructors". I am on my own in learning JAVA, so I need to pick what I can learn
> and what I can pursue later.
> So the purpose of raising this question in forum is to get experienced Automation Developer's
> opinion on :
>
> * is it the best practice (most optimum approach) using
>> constructor i.e. every WebDriver aspirant should make a habit of using constructors.
>> For example: Using Page Factory, creating common functions class, setting up @After and @Before
>> Methods, using ".properties" file etc etc , this is something every experienced Automation developer
>> advises to follow. Similarly, using JAVA constructors always is it the best approach everyone
>> should follow.

Well, here is my opinion on that: anyone writing Java code MUST use constructors. Period. There is
no need to "make a habit of it" because you can't get anything done without them.


Chris

Paul Levesque

unread,
Apr 8, 2014, 10:36:00 AM4/8/14
to webd...@googlegroups.com
@Darell, very insightful reply.

But guys, as I said that I am a beginner/aspirant and I am in phase of mastering it JAVA and WebDriver.
And I am not expecting that you guys would teach me or recommend be a book to learn constructors. What I wanted to know it this the best habit to use, if yes, could you please quote an example here.

Thanks

darrell

unread,
Apr 9, 2014, 9:56:30 AM4/9/14
to webd...@googlegroups.com
Paul,

I think the problem is that a Google Group is a great place to get focused help on something but when I read your question it raises numerous questions I have before I can help you. Essentially, if we worked together and you could sit with me I could pair with you and show you a lot of what you need to know in hours or days. Trying to do this in a forum like this would be difficult, slow and frustrating.

I can point out little things like, I seldom extend classes. I like using Java because the JUnit or TestNG framework solves challenges I have with creating a test suite framework. The fact that Java is an OOP is not why I picked Java. Realistically, the current project I'm using WebDriver is Java but the previous one was C# and before that was Python. Essentially, use what works for you. Do not let the tools you use drive how you create your framework. 

Additionally, there are some incredibly important rules for using JUnit to do unit testing. If you follow all these 'best practices' religiously you will in some cases be doing yourself more harm then good. JUnit has features in it which make it ideal for doing unit testing. It was designed to make it easy to follow the best practices of unit testing. WebDriver is NOT unit testing. So I do not follow all the best practices for JUnit and unit testing.

I NEVER think what features does a tool (e.g. Java) have and how can I use that feature. Instead I think about how I'm going to test an application. I don't think about how I'm going to automate it. I think about how to testing it, manually or automated. Once I know how I NEED to test it I think about how tools (Java, JUnit, etc.) will help me achieve my goal. I then use the tools as I need them and not necessary how they were originally intended.

Bottom line, if you ask the wrong questions, we can answer the questions you are asking but it is not going to help you. It will most likely confuse you even more.

SANTHOSH BABY

unread,
Apr 9, 2014, 12:40:49 PM4/9/14
to webd...@googlegroups.com
Please learn JAVA and ask questions related to java in seperate java
forum . Dont spam .Dont ask next what is variable , abstract class,
interface etc,,,,
Thank you
Cheers.

Santhosh Baby
Mob No +91-9611437246

Santhosh| Software Automation Engineer




Think GREEN. Please consider the environment before printing this email

"When The Winds of Change Blow..... Some people Build Walls & Others
Windmills.... Attitude Matters...."

Shiva Kumar

unread,
Apr 29, 2017, 4:42:32 AM4/29/17
to webdriver
Hi Krishnan,


     My question is  how constructors are useful in Selenium WebDriver, pleasee give me any one example.

sandee...@highq.com

unread,
May 10, 2017, 10:34:06 AM5/10/17
to webdriver
I think, I got your question.

see, you will get your answer in the Page Object Model (POM) design pattern.
once you design the POM pattern, you will be using the webdriver interface object 'driver', multiple time for passing the same 'driver' object to the different page factory classes.

at that time you will be creating the webdriver interface one time and using it at every class in the form of constructor by passing the driver in constructor.

Note: may this explanation will fill-up your requirement.
Thank you.
Sandeep Duve

sandee...@highq.com

unread,
May 10, 2017, 10:34:18 AM5/10/17
to webdriver
I think, I got your question.

see, you will get your answer in the Page Object Model (POM) design pattern.
once you design the POM pattern, you will be using the webdriver interface object 'driver', multiple time for passing the same 'driver' object to the different page factory classes.

at that time you will be creating the webdriver interface one time and using it at every class in the form of constructor by passing the driver in constructor.

Note: may this explanation will fill-up your requirement.
Thank you.
Sandeep Duve

Reply all
Reply to author
Forward
0 new messages