Showing posts with label language for test automation. Show all posts
Showing posts with label language for test automation. Show all posts

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.

How to create classes and objects in Java

The focus of this blog is test automation for manual testers.

Test automation is not only about the Selenium WebDriver framework but also about a programming language (Java in our case) and unit testing (JUNIT).

Actually, the most important test automation skill is Java programming. 

This article is about how classes and objects are created in Java.

The code that follows is about working with triangles.

Any triangle has 3 sides.

For a triangle, one of the measures is the perimeter which is calculated by summing the values of the triangle sides.

The easiest way of calculating a triangle perimeter is

public class mainTriangle {
public static void main(String[] args) {

int x = 3, y = 4, z = 5;

int perimeter = x + y + z;

System.out.println("perimeter value = " + perimeter);

}
}

The code declares 3 numeric variables (using the int data type), one for each triangle side. It assigns a value to each side.

It then declares a numeric variable for the perimeter and it assigns to it the sum of the sides variables.

The last line displays the perimeter value to the console. If you run this code in Eclipse, the result is




Let's assume now that we want to do the same thing for one more triangle. The code changes just a bit:

public class mainTriangle 

public static void main(String[] args)

int x1 = 3, y1 = 4, z1 = 5;

int perimeter1 = x1 + y1 + z1;

System.out.println("perimeter1 value = " + perimeter1);;

int x2 = 6, y2 = 7, z2 = 8;

int perimeter2 = x2 + y2 + z2;

System.out.println("perimeter2 value = " + perimeter2);

}


The result of executing the code is:

perimeter1 value = 12
perimeter2 value = 18




The problems with this code are that

  • it is not clear that x1, y1, z1 (and x2, y2, z2) are the sides of a triangle
  • the code is repeated twice for calculating the perimeter for both triangles

To correct these deficiencies, we will create a class that describes a triangle.

A class is simply a container that defines the data and behaviors of a triangle:

public class Triangle{

int side1;
int side2;
int side3;

public Triangle(int x, int y, int z)

{
side1 = x;
side2 = y;
side3 = z;
}
}

What are all these things?

Lets take them one by one:

  • Triangle is the name of the class 
  • The class content is included between { }
  • side1, side2, side3 are called class members:
  1. The class members store information about the class. 
  2. In our case, the class members will store the triangle side values. 
  • public Triangle(int x, int y, int z) is called the class constructor:
  1. When an object for the class is created, the class constructor is executed first. 
  2. The class constructor can have parameters (x, y, z). 
  3. It stores the parameter values in the class members so that they can be used at a later time 
  4. The constructor provides the way of supplying information about specific triangle objects and storing it inside of the object.

Each class has objects that are concrete instances of the class.

An object is created as follows:


Triangle triangle1 = new Triangle(3, 4, 5);

Triangle triangle2 = new Triangle(7, 8, 9);



The code for creating the triangle1 object does the following:

  • the class contructor gets the following values for the x, y, z parameters: 3, 4, 5
  • the constructor stores the parameter values in the class members: side1, side2, side3
  • the values for class members for the triangle1 object are 3, 4, 5

So, our code becomes



public class mainTriangle { 


public static void main(String[] args) {

Triangle triangle1 = new Triangle(3, 4, 5);

Triangle triangle2 = new Triangle(7, 8, 9);

}



Executing the code will show no results.

We have added so far definitions of the data that any triangle has (members for the triangle sides).

We need to add now definitions for the behavior of the Triangle class.



Lets add a method to the class.

A method is a way of implementing a behavior that the class objects have.

The method will do something very simple.

It will get the value of one of the sides and return it.

public class Triangle{

int side1;
int side2;
int side3;

public Triangle(int x, int y, int z)

{
    side1 = x;
    side2 = y;
    side3 = z;
}

public int getSide1()
{
    return side1;
}

}


The method has the following components:

  • getSide1() is the method name
  • the code of the method is included in {}
  • the method returns a numeric value (int)
  • the method will get the value of the side1 class member and provide it (return side1;)
Similar methods can be implemented for the other 2 sides:


public class Triangle{

int side1;
int side2;
int side3;

public Triangle(int x, int y, int z)

{
    side1 = x;
    side2 = y;
    side3 = z;
}

public int getSide1()
{
    return side1;
}

public int getSide2()
{
    return side2;
}

public int getSide3()
{
    return side3;
}

}


We can add more code to our application:

public class mainTriangle {

public static void main(String[] args) {


Triangle triangle1 = new Triangle(3, 4, 5);
Triangle triangle2 = new Triangle(7, 8, 9);


System.out.println("side1 = " + triangle1.getSide1());
System.out.println("side2 = " + triangle1.getSide2());
System.out.println("side3 = " + triangle1.getSide3());

}


The result of executing the code is

side1 = 3
side2 = 4
side3 = 5

We can also calculate the perimeter of triangle1:

public class mainTriangle {

public static void main(String[] args) {


Triangle triangle1 = new Triangle(3, 4, 5);
Triangle triangle2 = new Triangle(7, 8, 9);



int perimeter = triangle1.getSide1() + triangle1.getSide2() + triangle1.getSide3();
System.out.println("perimeter = " + perimeter);


}


The result of executing the code is

side1 = 3
side2 = 4
side3 = 5
perimeter = 12




It is very nice and interesting that you can create classes and objects.

But how is this code better than the original one?

It seems that more code is needed when using classes and objects!

For the perimeter calculation, we can create a new method inside of the Triangle class:

public class Triangle{

int side1;
int side2;
int side3;

public Triangle(int x, int y, int z)

{
    side1 = x;
    side2 = y;
    side3 = z;
}

public int getSide1()
{
    return side1;
}

public int getSide2()
{
    return side2;
}

public int getSide3()
{
    return side3;
}

public int getPerimeter()
{
    return side1 + side2 + side3;
}

}

The getPerimeter() method works as follows:

- it gets the values of the side1, side2, side3 members
- it adds them up
- it returns the value of the sum

With the new method, the code gets simpler:

public class mainTriangle {

public static void main(String[] args) {

Triangle triangle1 = new Triangle(3, 4, 5);
Triangle triangle2 = new Triangle(7, 8, 9);

System.out.println("perimeter = " + triangle1.getPerimeter());

}


I did not use a variable to store the result of getPerimeter and then display its value.

Instead, I display directly the getPerimeter value.

If the perimeter of the second triangle is needed, just one more line is added:

public class mainTriangle {

public static void main(String[] args) {

Triangle triangle1 = new Triangle(3, 4, 5);
Triangle triangle2 = new Triangle(7, 8, 9);

System.out.println("perimeter1 = " + triangle1.getPerimeter());
System.out.println("perimeter2 = " + triangle2.getPerimeter()); 


}

}


I am hoping that the advantages of creating classes and objects start becoming obvious:

- it is clear in the final code that 3, 4, 5 are side values of a triangle

- the Triangle class defines how the triangle perimeter is calculated; this is done only once and then used by any of the objects of the class; imagine that the we need not one line of code for calculating the perimeter but 5 lines

- there is no code duplication any longer

I hope that this short example showed that programming can be learned and it is not as difficult as you may think.


How about some exercises using your new knowledge? :)

1. Create a rectangle class with methods for calculating the rectangle perimeter and area

2. Add a method to the Triangle class that checks if the triangle is scalene (all sides are different), isosceles (2 sides are equal) or equilateral (all sides are equal)?


If you liked this article and want to provide feedback or want to submit your solutions to the exercises, please use the comments section.

Is Test Automation For Testers?

Who does test automation?

Is test automation for testers or for developers?



Who writes the test automation code?

Do testers have to write code?


A few years ago, my answer was that testers do the test automation.

My impression at that time was that testers can learn programming and a test automation framework and improve their development skills from there.

Test automation was getting more and more prominence as Selenium and other tools were created in addition to QTP.

Today, I am not so sure.

I attended this afternoon the VAN Q testing meetup organized monthly in Vancouver, BC.

The meetup topic was test automation of mobile apps and games.

There were 2 parts to the meetup.

The first was about Test Droid cloud service and how it allows running automated tests for mobile apps and games on hundreds of real devices.



This exposure was more about how Test Droid works and less on test automation.

If you have not heard of Test Droid before, it is similar to how Sauce Labs works for web and mobile test automation.

It uses different automation frameworks like Appium, UI Automator, Selendroid, Robotium and lots of real devices.

The second part covered how test automation is done at Electronic Arts.

Electronic Arts is using 2 different approaches to test automation:
  1. based on image comparison
  2. based on the application instrumentation 
Even if both approaches are still used in projects, the instrumentation approach becomes the preferred way.

For the test automation based on application instrumentation, Electronic Arts has built its own test automation framework called Unity. 

With the help of the framework, the testers can create test automation scripts using Java and C#. 

Before creating a test automation framework, EA tried many approaches and tools.

All of them worked to a certain degree only. 

So, the test automation process for EA includes
  1. using their own test automation framework
  2. creating test automation scripts with Java and C#
  3. integrating the test automation scripts with the build process using Jenkins

Very good meetup with lots of interesting details. 

So many technologies mentioned in one afternoon: 
  • programming languages: C#, Java
  • multi device cloud services: TestDroid
  • test automation frameworks: UI Automator, Appium, Selendroid, custom frameworks
  • Jenkins

But I wonder how many testers from the audience have any experience with them. 

My impression is that not many. 

So, going back to the initial question, is test automation for testers? 

Or test automation is for developers? 

Some people say that test automation is for software developers in test. 

This is a confusing name which essentially means just software developer. 



To make the answer to the main question more difficult, see below some questions from an interview I had a while ago for a test automation position: 

  • what is new in Java 8?
  • what is a stack?
  • what is a queue?
  • considering an array of numbers that can have the same number twice, how do you find the duplicated numbers?
  • what is a hash table?
  • can a static method be overloaded? 

 How many manual testers can answer these questions? 

Also, there seems to exist a trend where big companies have less and less testers or no testers at all. 

They rely on developers to do the development and testing. 

Because it is obviously easier for developers to learn testing than for testers to learn development.



Based on all this information, I think that unless testers start thinking seriously about learning programming and learning test automation frameworks, they should leave the test automation to developers.



It is not easy to go from manual testing to test automation but not impossible either. 

The issue with this is that the distance between manual testers and developers becomes wider and wider every year.

So what are you waiting for?


Make the first step and learn a programming language.



Add another step and learn the test automation framework. 

Add another step and another and another ...

Test automation is waiting for you ....

How much Java do I need to learn for mastering Selenium?

I see these questions from time to time on testing forums:

"How much Java do I need to learn for mastering Selenium?"
"How much programming do I need to learn for test automation?"

My initial response is "as much Java as you can learn".

What I mean is that since the programming language is the most important skill for test automation, everyone should learn it as much as possible.

While I still like my response, it is not helpful for other testers that want to learn a programming language for test automation.


First, I want to say something about the question.

It implies that mastering Selenium happens after learning some Java programming.



A similar question could be:

I don't know how to swim but how much swimming do I need to learn to compete for Olympics?

To go from no swimming skills to just decent swimming is very difficult.

Going from decent swimming to mastery of swimming is just not realistic for most of people.

I picked swimming as a skill example as the process of learning test automation is similar to learning any other skill.


Mastering a skill usually means that you are at the highest level possible for it.

This does not happen obviously through just learning as a lot of practice and self-improvement are needed.

And time, perseverance and patience.


Going from a manual tester to a test automation engineer that masters Selenium is a very difficult goal.


Test automation engineers that master Selenium are usually software developers in test.


Meaning professional developers.


Still, how much Java do you need to master Selenium?

What concepts of Java do you need to learn to use Selenium?

Between manual tester with no programming skills and test automation engineer that masters Selenium, there are a few different levels.






1. Learn Java basics for Selenium test automation scripts

Java basic knowledge is sufficient for creating simple Selenium test automation scripts.

You will need to learn about
  • variables
  • fundamental data types (String, Integer, Boolean) and their operators
  • conditional operators
  • string methods
  • conditional statements (if/else, switch)
  • looping statements (for, while)
  • arrays
  • packages
  • exceptions
  • lists
  • read and write files
  • use Java API


After learning the basic concepts, you should be able to create basic test automation scripts with Selenium.

These scripts will include WebDriver API which is ok while learning Selenium.

But as soon as you start having more and more scripts, the scripts maintenance will become an issue.



2. Create maintainable scripts with page object model


Having a large number of test automation scripts that include WebDriver APIs poses an issue from the maintenance point of view.

Since each script is independent and some scripts may just repeat the same lines of code, changing the test scripts for improvements or because of bugs is difficult to do.

The more complex test scripts are, the harder to maintain.

And we should remember that the biggest failure point in test automation efforts is test automation scripts complexity.


So we need to reduce the test scripts complexity and make scripts maintenance easier.

For these purposes, you need to start learning the Java object oriented programming concepts.

This includes learning about

  • creating classes and objects
  • class fields
  • methods and constructors
  • method overloading
  • set/get methods
  • class/field/method modifiers


The object oriented programming concepts will help implementing the page object model for your test automation scripts.

This means that you will create classes that correspond to site's pages (or components of site pages) and move the WebDriver API code from the test scripts to the page object model classes.

By doing this, you keep your test scripts free of any direct interaction with the site.

The interaction with the site is being done in the page object classes.


3. Create an automated testing framework with Selenium





After having page object classes, you will want to start creating your test automation framework.


The framework should be useful not only for one site but any number of sites.


And possibly for mobile applications as well.




You need to learn the Java advanced concepts like

  • class inheritance
  • base classes
  • method overriding
  • polymorphism
  • interfaces
  • static classes
  • inner classes
  • abstract classes


4. Improve the test automation framework design



You need to learn code refactoring and design patterns for this purpose.

Code refactoring will help making your test automation framework simpler and more efficient.

Design patterns will provide proven ways of solving common development problems.






Are there more levels?


Yes. Learning a skill never ends. 


There is a long way from manual testing to test automation with Selenium.

But taking it a step at a time will eventually get you there.

With perseverance and lots of patience.

What Programming Language To Learn For Test Automation



A fellow tester asked me recently what programming language would be best to start out learning for test automation.

She tried learning C++ awhile back without success.


She was also asking if , for a tester, knowing a programming language is a "must learn" to succeed and grow in testing.


In other words, do testers have to write code?

I agree that learning a programming language is intimidating and difficult for manual testers.

But it should not be so.




What is the best programming language for test automation?


My suggestion is Java or C#.

Let's take Java for example since C# is very similar to Java.


People say that Python and Ruby are easier to learn for a novice and they are probably right.


I don't know Python and used Ruby for just a month to see how it is (very interesting).



I suggest Java because


- Java is available for a long time compared with Ruby and Python which are rather new



- Java is very popular; it is the language with the highest popularity increase (5.94%) in 2015







- a lot of documentation is available on the internet for Java, many online Java courses and many Java books


- learning the first language is difficult regardless of the chosen language; some people recommend learning first Python and Ruby and then learning Java; but if you will put the effort and time into learning, why not learn Java from the start?


- learning Java may be more difficult than Python and Ruby, especially the object oriented part of it; but as soon as you learn it, Java can be applied to many testing types like test automation with Selenium for web and mobile apps, API testing, performance testing (with JMETER)



- while learning the new language, you will have a lot of questions and may get stuck; if your learning is through a Java course, you can ask your instructor for answers; otherwise, it is important to know people that have experience with the language that you are learning; most of developers know Java or C#; Ruby and Python developers are rather rare; I still haven't met a developer on the local market (Vancouver, Canada) that uses these languages for work projects; most of developers use either Java or C#



- Java has a very good IDE (Eclipse)



- if you are planning on using Java for test automation, 65% from all Selenium downloads are for Java







Is learning a programming language a "must-do" for a tester?


More and more companies are moving away from hiring pure manual testers and prefer testers with test automation skills.

Learning a programming language has additional benefits to get you started with test automation.


A tester that knows a programming language will speak the same "language" as developers.


He will understand better what developers do and get a better appreciation of how complicated development is.


He can do unit testing and participate in code reviews.