Where can I find a full test automation project with Selenium WebDriver?

You have been learning test automation with Selenium.

You went through lots of concepts and would like to see how they all work together.

Where can you find a project that uses everything you learned and more?

Here :)

What follows is a small project that I built a while ago for a job interview.

It uses many test automation concepts such as:
  • page factory
  • base classes
  • html classes
  • test listeners
  • test ng assertions and fixtures
  • annotations
  • custom locators (javascript and jquery)
  • screenshots
  • saving errors in text files


The exercise consisted in automating the following test case with Java and Selenium WebDriver:

  • Launch bestbuy url (www.bestbuy.ca)
  • Search a product and add it to cart
  • Go all the way through checkout process and place the order with invalid credit card
  • Capture the error message due to invalid credit card


Before downloading the project and checking the source code, a few details about the project.

Project details

Maven project
- all dependencies are managed through the pom.xml file

Test NG
- unit testing library

Java JDK 8
- used for lambda expressions and streams

Page Factory
- pattern for creating page object and page fragment classes
- the elements of page object/fragment classes have names and locators
- names and locators are implemented using annotations
- available locator types are id, xpath, css, name and javascript
  
   @Name("SEARCH_HEADER")
   @FindBy(className = "main-navigation-container") 
   public class SearchHeader extends HtmlElement{ 
 
   @Name("SEARCH_FIELD")
   @FindBy(id = "ctl00_MasterHeader_ctl00_uchead_GlobalSearchUC_TxtSearchKeyword") 
   private TextInput searchKeywordTxt;
 
   @Name("SEARCH_BUTTON")
   @FindBy(id = "ctl00_MasterHeader_ctl00_uchead_GlobalSearchUC_BtnSubmitSearch")
   private Button searchBtn; 
     
   public void search(String keyword) {
     searchKeywordTxt.click();  
     searchKeywordTxt.clear();
     searchKeywordTxt.sendKeys(keyword);
     searchBtn.click();  
   }
}


The project has the automation framework classes in the main folder and all test items in the test folder.

Main folder (framework classes)

annotations classes
- FindBy
- FindByJQUERY
- FindByJS
- Name
- Timeout
 
 package com.bestbuy.demo.annotations;

 import java.lang.annotation.ElementType;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;

 @Retention(RetentionPolicy.RUNTIME)
 @Target({ElementType.TYPE, ElementType.FIELD})
 public @interface Name {
   String value();
 }

html element classes
 
    package com.bestbuy.demo.element;

    import org.openqa.selenium.By;
    import org.openqa.selenium.NoSuchElementException;
    import org.openqa.selenium.WebElement;

    public class CheckBox extends TypifiedElement {
   
      public CheckBox(WebElement wrappedElement) {
        super(wrappedElement);
      }

      public WebElement getLabel() {
        try {
            return getWrappedElement().findElement(By.xpath("following-sibling::label"));
        } catch (NoSuchElementException e) {
            return null;
        }
      }

      public String getLabelText() {
        WebElement label = getLabel();
        return label == null ? null : label.getText();
      }

      public String getText() {
        return getLabelText();
      }

      public void select() {
        if (!isSelected()) 
            getWrappedElement().click();        
      }

      public void deselect() {
        if (isSelected()) 
            getWrappedElement().click();
      }

      public void set(boolean value) {
        if (value) 
           select();
        else 
           deselect();
      }
   }

  
exceptions classes

decorator and proxy classes used by the page factory

page class
- used as base class for the page object classes

page factory classes

miscellaneous classes such as
- custom driver class, screenshot class
- enumerations (used to avoid hardcoding data in the page objects)
- simple logger class
- property class
- TextFile class

Test folder (test items)

  • base test class (used as base by the test classes)

  • page objects classes (all page object and page fragment classes)

  • test listeners (class for taking a screenshot and logging exceptions in case of failures)
  •  
     package com.bestbuy.demotests.testlisteners;
    
     import java.lang.reflect.Field;
     import org.testng.ITestContext;
     import org.testng.ITestListener;
     import org.testng.ITestResult;
     import com.bestbuy.demo.exceptions.HtmlElementsException;
     import com.bestbuy.demo.utils.Driver.BrowserDriver;
     import com.bestbuy.demo.utils.Driver.Screenshot;
     import org.openqa.selenium.WebDriver;
     import static com.bestbuy.demotests.BaseTest.BaseTestClass.*;
    
     public class TestListener implements ITestListener {   
       
        @Override
        public void onTestFailure(ITestResult result) {       
           try {
             Screenshot screenshot = 
             new Screenshot(getDriverFromBaseTest(result));
       
             screenshot.capture(result.getName());
           } 
           catch (Exception ex) {
             throw new HtmlElementsException(ex.getMessage());
           }                 
       }  
    
       @SuppressWarnings("unchecked")
       private WebDriver getDriverFromBaseTest(ITestResult result) 
           throws IllegalAccessException {
       
          WebDriver driver = null;
       
          try { 
             Class< ? extends ITestResult> testClass = 
             (Class< ? extends ITestResult>) result.getInstance().getClass();
      
             Class< ?extends ITestResult> baseTestClass = 
             (Class< ? extends ITestResult>) testClass.getSuperclass();
      
             Field driverField = baseTestClass.getDeclaredField("driver");
        
             driver = (BrowserDriver)driverField.get(result.getInstance()); 
        
             return driver;
         } 
         catch (SecurityException | NoSuchFieldException | IllegalArgumentException ex) {     
             throw new HtmlElementsException("error getting the driver from base test");    
         }         
       
       }
        
       @Override
       public void onTestSuccess(ITestResult result) 
       {}  
      
       @Override
       public void onTestSkipped(ITestResult result) 
       {} 
      
       @Override
       public void onTestFailedButWithinSuccessPercentage(ITestResult result) 
       {} 
      
       @Override
       public void onStart(ITestContext context)     
       {} 
      
       @Override 
       public void onFinish(ITestContext context)    
       {}
      
       @Override
       public void onTestStart(ITestResult arg0)
       {}
        
     }
    
  • test class


Most interactions with web elements are done through the page factory classes.

Occasionally, Javascript code is used when buttons cannot be clicked through the page factory.

Some of the pages have random popups that are closed if displayed.

The test script does the following:
  1. Open the home page
  2. Execute a keyword search
  3. Select the Online Only filter
  4. Select a product with online availability
  5. Add the product to the cart
  6. Continue checkout on the basket page
  7. Select New Member
  8. Fill in the Ship To info
  9. Select credit card as payment method
  10. Fill in the payment info
  11. Submits transaction
  12. Verify that there is at least an error message
  13. Saves the error messages and the search keyword to a text file


The test script uses a keyword parameter which can take multiple values.


Download source code

Interested in learning how to build a project like this one?

Get started here


Test automation wants your manual testing job





Automation will leave many people without a job. 

I am not talking about test automation being a danger to many manual testing jobs. 

But about automation in a more general perspective. 

In Canada, in the next 10 - 15 years, up to 7.5 million jobs (out of 18) will be lost because of automation. 

This is what Huffington Post says and I agree with them. 

The key points of the article are below: 

  • Canada faces the loss of up to 7.5 million jobs to automation in the next 10 to 15 years 
  • Even people in high-income jobs won’t be spared, as automation will reduce the demand for doctors, lawyers and engineers, among others. 
  • Autonomous vehicles are already on the road, robo-advisors are dispensing financial counsel and even lawyers and reporters are starting to see automation take over routine functions. 
  • The study’s projections found a broad range in the number of jobs that could be lost — 1.5 million at the lower end, and 7.5 million at the high end. That amounts to between 8.3 per cent and 41 per cent of the 18 million jobs in existence in Canada today. 


So the demand for doctors, lawyers, engineers, reporters, etc will be reduced significantly. 

Should we hope that this will not happen to testers?

Stop being a one-trick pony! Stop focusing only on manual testing!

Manual testing will not go away. 

But it will not be anymore what it used to be. 

Like all things, testing changes and at a rapid pace. 

Companies still need manual testers but less than before. 

Normal testing is something that not only testers can do. 
A business analyst, business user or developer can do it as well. 

With so "many testers" available, companies hire outside testers if they are exceptional. 

Or multi-talented, with diverse skills. 


So do you want to continue to have a career in testing? 

Stop being a "one-trick pony" and stop focusing only on manual testing. 

Manual testing used to be sufficient for a successful career. 

You had a good "trick" and used it over and over. 

While you focused on your trick, everything around you changed and you did not notice. 

One day, you discover that the one-trick does not keep you employed. 

You rush then to learn more tricks and hope that you can do it fast.

Sadly it is too late because learning does not happen over night. 



This is something that applies to many other domains. 

Like sports. 
Take mixed martial arts, for example. 

One of the MMA women divisions had a champion for 3 years in Ronda Rousey who beat soundly (and in many cases under 1 minute) all her opponents. 

She did this with great judo skills. 

Ronda was a true "one-trick pony" since all her skills were only about judo. 

One day came when she fought a true boxer. 

Even with a lot of boxing training, she lost in a dramatic fashion. 

After her first loss, she took a year off and trained more in boxing hoping for a revenge. 

Next time she fought, she lost again but this time under 1 minute. 

What happened then?

While she was enjoying her "one pony trick", the world changed and she did not notice. 

When she realized that serious improvement was needed, it was a bit too late.

Learn automation while you don't need it

I have been a software manual tester for 10 years.

Due to the regression tests that I do every other week, it is imperative that I get this function automated to help me with the rest of my job.

I regression test over 90 custom sites for my company and really need to get this done by February 2017.

Please help.

I received this message from a tester a few weeks ago.

He wanted to learn automation and programming and use these skills successfully in 2 months.

Which is very difficult to do.

I don't know why he did not start learning earlier.

He probably thought that will not need automation and his employer will hire a developer for the automation work.

So he did what many people do in similar occasions: postpone.

We don't like change and avoid it.

Our hope is that somehow things stay the same or we figure them out.

And a moment comes when we need change fast and results too.

This is when we realize the mistake of not starting ahead of time, when we don't need the new skills yet.

Fast change happens in stories and movies only.

Real change takes time and is done in small steps.

So, stop postponing learning programming and get started.

It does not matter if you need it in your job now or not.

You will need it sooner than you think.

Next December, you will be very happy that you started learning automation the year before.

What do you need for a Selenium test automation job?

First, the obvious.





Since test automation is programming, you need to know the basics (more if possible) of a programming language such as Java.

And you need to know the basics of the Selenium WebDriver framework.


But this is not sufficient for getting an automation job.



In an interview, you have to convince other people that you know how different things work in Selenium and Java.

For example, 
  1. what is boxing in Java?
  2. what is the difference between method overwriting and overriding?
  3. what is the page object model?
  4. what are the fastest locators?
This requires practicing explaining how things work, what they are and how they should be used.



You still need more for that new automation job.




You need 500 hours of practicing Selenium and Java.

The number of hours is different from person to person.

Some people may need less, others more.

But it is essential that you put the time in for the practice.



Why so many hours?

Because companies will put you to test.



There are 2 types of tests that you may have to do:

1. JAVA coding test
This test happens before the face-to-face interview and is a condition for it.
It is usually done online on a site like codility.com and it can last around 1 hour.
You are given a problem and have to write code that solves it.

EXAMPLE


2. SELENIUM automation test
After the face-to-face interview, if the interview went well, you may be asked to do the automation exercise.
This test is about automating a scenario for a site.
You may get 1 day or more to do it.

EXAMPLE



Going back to the 500 hours practice.

It is very difficult to do well at these exercises if you are not already proficient with Java and Selenium.

Because the time for doing the exercises is limited.

And your solutions have to be very good to get the job.

Best Resource for Improving Selenium Skills? Selenium code

You learned Selenium and Java test automation basics from a book, online course or youtube. 

Page object model is understood, locators are clear, explicit waits and expected conditions are straightforward.

Java concepts such as classes, objects, inheritance, looping, arrays, lists are all covered.

With the new skills, you can write test automation scripts for a variety of websites.

You are wondering now about the next level of test automation.

There is always a next level so what is it for you?

How do you go to it?

What resource should you use for guidance?





Most available books and courses target the beginner and intermediate test automation levels.

There are not many resources available on advanced test automation skills.

There is one place however, often forgotten, that has everything you need in your quest to more knowledge.

The Selenium source code.

It is extremely helpful not only to learn how to use Selenium but also to read the source code.

Doing this provides many benefits such as:

1. it shows how Selenium classes and methods work

For example, how does Selenium find a web element using the findElement() method?
How does Selenium locate an element by css locator?

2. you can learn how a framework is built

Selenium is a framework.

You can learn many things about building an automation framework from studying how the Selenium framework is built.

3. you can learn what good programming is

Selenium was created by professional developers.

What better source of learning than the code that they wrote?

4. you find out what you dont know yet but should know

For example, by reading the Page Factory code, you will learn about annotations.

And reflection.

And design patterns.

And proxy classes.

And generics.

And interfaces.

You will need all these Java concepts (and more) to make the transition to the next test automation level.

So attach the Selenium source code to the project in Eclipse and start reading.




When do you need these new skills?

Lets say that you want to have in your automation project the ability of taking screenshots automatically when an exception happens in a test script.

You use TestNG as the unit testing framework.

TestNG listeners can help but there is a problem.

You need in the test listener the driver object created in the test class or the base test class.

To get this driver object, you will use reflection and generics. 

Another example of using the new skills is creating new locator types.

Selenium provides by default a variety of locator types such as id, class, name, xpath, css.

How about using also Jquery and Javascript locators?

You will need annotations for this.


How to interact with sliders in Selenium WebDriver



Sliders allow users to select a value by dragging and dropping a handle. 

This value can be a price, a quantity, a year. 

The web page could use a textbox for getting the same information from users. 

But with sliders, the page becomes much more interesting.

Read the full article on seleniumjava.com.

Learn Python or Java as your first language?

from http://www.thecrazyprogrammer.com/


There are so many opinions all over the place on what is the best language to learn first.

Is it Python?

Or maybe Java?

To understand better how they compare, I read multiple articles from Quora.com on this topic and selected some opinions.

We start with opinions about Python.

I added the Quora articles links as well in case you want to read more.


Why is Python a good choice for the first language to learn?


FROM https://www.quora.com/Is-Java-harder-to-learn-than-Python

Java is geared better towards applications, where as Python is geared better towards tasks.

Most programmers will eventually need C# or Java under their belt.

Python is generally a language for Sys Admins, mechanical engineers, and other adjacent career paths.

As a first language, Python is arguably easier.

Variables don't need a type, indentation is obligatory so you don't make block mistakes, syntax is very English-like, iteration is very easy, the standard library does a lot of things in very few lines of code, and so on.

Python’s syntax is much more simple and readable than that of Java. 

It also does a much better job of minimizing the amount of code needed to complete a task, which is incredibly convenient for people learning to code as they don't have enormous walls of code to deal with.




FROM https://www.quora.com/Which-is-easier-to-learn-Java-or-Python

http://cacm.acm.org/blogs/blog-cacm/176450-python-is-now-the-most-popular-introductory-teaching-language-at-top-u-s-universities/fulltext

Python has surpassed Java as the top language used to introduce U.S. students to programming and computer science, according to a recent survey posted by the Association for Computing Machinery (ACM).

Eight of the top 10 computer science departments now use Python to teach coding, as well as 27 of the top 39 schools, indicating that it is the most popular language for teaching introductory computer science courses

Python has been growing in popularity in the educational realm for at least the past few years, though this survey is the first to show it has eclipsed Java, which has been the dominant teaching language for the past decade.




FROM https://www.quora.com/If-I-had-to-choose-between-learning-Java-and-Python-what-should-I-choose-to-learn-first

Go for Python if you already have a background in computer science and want to apply that knowledge on quickly building something real, that be a web application or software tool. 

Python is very quick and easy to learn, with tons of modules that do any kind of job for you, is less verbose than Java with a great community.


Why is Java a good choice for the first language to learn?

FROM https://www.quora.com/Is-Java-harder-to-learn-than-Python

You can write VERY SIMPLE programs in Python faster than you can write programs of the same length in Java, so in that respect, you can learn Python faster.

For any serious program (longer than a few dozen lines of code), Java is much better because of various safeguards, such as static typing.

Since you spend less time debugging stupid errors in Java, you can spend more time learning the art of programming and software engineering.




FROM https://www.quora.com/Which-is-easier-to-learn-Java-or-Python

To learn, as a first language, I think it's probably Python. I think if you compare a 'hello world' in each language, Python is obviously shorter, and it obviously makes more sense to a complete beginner.

When you get into beginner programs like 100, or 200 line programs, Python is probably still easier, because you're likely using built-in types, and it's obvious what the types and methods do.

When you get into intermediate size of programs, a few thousand lines, or maybe tens of thousands of lines, I think the advantage skews very strongly in favour of Java.

Dynamic typing makes your first few hundred lines of coding very easy, and your next ten thousand lines a complete nightmare.

Learning slows down, and mystery builds. Each object type you add, further increases the mystery (more class types to remember), whereas in Java, it makes things clearer as there is a total rigidity in what everything is.

For learning, I think if you want your first week to be easy, learn Python, if you want your first 6 months to be easy, learn Java.

I've used Python for longer than I've used Java, I've probably written more Python code than I've written Java code.

I was using Python almost before Java was even released, but I strongly (very strongly) prefer Java. Java is just incredibly strict by comparison, and strict means simple, it means not vague, not flexible, not forgiving.

Unforgiving is good, it provides structure and predictability. A forgiving language means it's vague, fragile, and prone to run time errors, which would (and should) be caught at compile time.



FROM https://www.quora.com/If-I-had-to-choose-between-learning-Java-and-Python-what-should-I-choose-to-learn-first

If you've never programmed before, I'd generally recommend Java as a first language. 

Python is a great language, but in its attempt to make things easier, I think it actually makes some things harder for a new programmer. Python hides too much and that makes things confusing for a new programmer.

Python hides this stuff. It all looks like magic.

In Java, it's more explicit what the types of variables are. 
There's much less "magic" that happens.

Unfortunately, if you're relatively new to programming, I'd have to say - Java. 
I say unfortunately because I really, really, really don't like Java. 
Many people will disagree, but, again, some people like washing dishes, too.

The freedom and dynamism that characterize Python are beautiful and powerful things, but they must be earned.

If you start with Python, many a thing will be a mystery and you'll start thinking that all computers work that way. 
Well, they don't. 
It's pretty much quite the opposite. 
So, if you already know how computers work, you'll understand what's going on and know how to appreciate it. 
If you don't, what will probably happen is that you'll lay out your foundations of your programming knowledge wrong and then you're screwed. You'll struggle to find sense in anything.

Go for Java If you are a total beginner and have never learned computer science before.

This may sounds like the opposite of what you've heard, but do it. 

Java will always force you to go down to the understanding of how things really work , instead of Python which tend to hide some technical details in order to make things simpler.

My recommendation would be to learn Java first for many of the reasons others have listed. 

I assume that you have no previous programming experience for the sake of my answer.

Learning Java will force you to learn to think a little more like a programmer as you learn it.

Python is a little oversimplified and while that may seem like the ‘easy’ route, I think it will gain you some false self-confidence. 
Your appreciation for the simplicity of Python can really only be achieved by learning a language like Java first.

Something else to consider is the amount of jobs available to Java developers. Java is pretty much an industry standard for A LOT of applications. 
I think the amount of opportunities out there will be far greater for you by learning Java first. Python will come much easier to you after you know Java and it will be a nice add-on to your resume.


The final decision is yours.


Interested in a Java-vs-Python summary spreadsheet?



Which language seems to be the most predominant in Selenium script development?

60% of all Selenium downloads are for Java. 

This means that companies use mostly Java for Selenium test automation. 

Java is not only the most popular language for Selenium test automation


Source: http://blog.testproject.io/2015/12/03/worlds-most-desirable-automation-skills/

It is also more robust when used in enterprise projects. 

It has mature and powerful IDEs (Eclipse and IntelliJ) too. 

When selecting the language to learn for Selenium, keep this info in mind. 

Don't select the language based only on which is the easiest to learn. 

You may end up learning an easier language that is not really needed on your job market.