In order to use Webdriver, you will need to have Maven installed and set up. Fortunately, the helpful people at Apache Software have created an excellent tutorial about how to set up Maven:
http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
It probably won't take you five minutes as advertised, but it will take you less than a day. Don't worry too much about what you are doing as you are running these commands; once Maven is set up, we only have to think about the commands we need to run our automated tests.
Now let's return to Eclipse. The last time we were in Eclipse we created a project called MyFirstJavaProject. Right-click on this project and select "Configure-> Convert to Maven project". A popup window will appear. Click "Finish" on the window, and wait for the configuration to complete. When it's done, you'll notice that your project icon now has a little M next to the J. You'll also notice that you have a pom.xml file in your project folder. We'll learn more about the POM in a future blog post.
Let's create a JUnit test. First we'll run it from Eclipse, then we'll run it from the command line using Maven.
1. Right-click on the MyFirstJavaProject and select New->Source Folder
2. Give the new folder a name of src/test/java (click the "Update exclusion filters" checkbox if needed)
3 Right-click on the new folder and select New->Package
4. Give the package a name of myFirstTests
5. Now right-click on the myFirstTests package and select New->Class
6. Give the class a name of FirstTest
7. The FirstTest class should open in the center pane, and should look like this:
package myFirstTests;
public class FirstTest {
}
8. In between the brackets, add the following:
@Test
public void test() {
int x = 2;
int y = 3;
int total = x+y;
assertEquals(total, 5);
}
9. Right-click on the Project name (MyFirstJavaProject) and choose Build Path->Add Libraries
10. Select JUnit
11. Choose JUnit 4 and click Finish
12. Look again at the lines of code you added to the FirstTest class. There will be two lightbulb icons on the left side of the code.
13. Right-click on the one next to "@Test" and choose Add JUnit4 to Build Path
14. Right-click on the one next to "assertEquals(total,5)" and choose Import org.junit.Assert;
15. Now near the top of the page, you will see
import static org.junit.Assert.*;
import org.junit.Test;
16. Save your changes by clicking on the disk icon in the toolbar
17. Right-click on the FirstTest.java file in the left pane, and choose Run As-> JUnit Test
18. Your test should run, and a new tab named JUnit should appear in the left pane
19. Click on the left tab and notice that the window displays what test has been run, and that there is a green bar across the pane indicating that the test has passed
Now let's try running the test from the command line. First we'll need to make a change to the pom.xml file:
1. Double-click on the pom.xml file to open it in the center pane
2. Click the tab at the bottom of the pane that says pom.xml
3. You should now be viewing the file in xml format
4. Add this text to the xml file, right underneath </build>:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
</dependency>
</dependencies>
5. Save your changes
6. Now open the command window
7. Change directories until you are in your project folder (MyFirstJavaProject)
8. Run this command:
mvn clean test
9. Your project should build, your test should run from the command line, and you should be notified that the build is successful
Congratulations! You have run your first test from Maven!
Email Subscription Form
Tuesday, June 11, 2013
Wednesday, May 8, 2013
Introduction to the Command Line
In order to run Maven (which we will discuss in the next post), it is necessary to know how to run commands from the command line. If you are not familiar with using the command window, this post will explain the two most important commands. My instructions will refer to the Windows command line, but they can be adapted without too much difficulty to Linux or Mac.
To bring up the command line, go to the Start menu and type "cmd" in the search field. Click the return key and a command window should open.
cd:
cd stands for "change directory". You will use this to navigate through your file system. When you opened your command window, you probably saw something like this: C:\Users\YourName>. This is your user folder. If you were to navigate to it in the File Explorer, you would go to Computer->Windows7_OS (C:)
->Users->YourName. (The OS will vary based on your computer, and the YourName will be your user name.)
Let's try navigating to the Users folder. In the command window, type "cd Users" and click return key. Note that the command prompt now says C:\Users>.
There is another way to navigate up one folder. First, let's return to your folder. Type "cd YourName" (replacing the words YourName with your actual user name) and click Return. Now the command prompt should say C:\Users\YourName. Now type "cd .." and click Return. Notice that you have returned to the Users folder. The two dots send the instruction to go up one level. If you type "cd.." and Return again, you will see C:\> in the command prompt.
You can also navigate through more than one folder at a time. Type "cd Users/YourName" (replacing the YourName with your user name) and click Return. Now the command prompt will say C:\Users\YourName. You've navigated down two levels, from the C: level to the YourName level.
dir:
dir is the command used to list all the files in a particular location. This really comes in handy when you can't remember what you named a file.
Let's try using the dir command. In the command window, verify that the command prompt currently reads C:\Users\YourName. If it doesn't, use the cd command to navigate there. Now type "dir" and click Return. You should get a list of all the files in your personal folder.
Let's add a new file to this location. It's possible to do this through the command line, but that is beyond the scope of this tutorial. Instead, create a simple Word or Notepad file called MyCommandFile, and save it in your folder. Now return to the command window and type the "dir" command again. You should see your new file in the directory.
Another shortcut to navigation when you have forgotten a file name is the tab key. In the command line, type "cd My" and then click the tab key. You should see the file name auto-complete for you.
Bonus command:
One more helpful command to use is the up arrow key. This key will go back through your most recent commands. This really comes in handy if you have typed a particularly long command and would rather not type it again.
The command window is very helpful, and there are a number of good tutorials available if you would like to learn more about what it can do for you.
To bring up the command line, go to the Start menu and type "cmd" in the search field. Click the return key and a command window should open.
cd:
cd stands for "change directory". You will use this to navigate through your file system. When you opened your command window, you probably saw something like this: C:\Users\YourName>. This is your user folder. If you were to navigate to it in the File Explorer, you would go to Computer->Windows7_OS (C:)
->Users->YourName. (The OS will vary based on your computer, and the YourName will be your user name.)
Let's try navigating to the Users folder. In the command window, type "cd Users" and click return key. Note that the command prompt now says C:\Users>.
There is another way to navigate up one folder. First, let's return to your folder. Type "cd YourName" (replacing the words YourName with your actual user name) and click Return. Now the command prompt should say C:\Users\YourName. Now type "cd .." and click Return. Notice that you have returned to the Users folder. The two dots send the instruction to go up one level. If you type "cd.." and Return again, you will see C:\> in the command prompt.
You can also navigate through more than one folder at a time. Type "cd Users/YourName" (replacing the YourName with your user name) and click Return. Now the command prompt will say C:\Users\YourName. You've navigated down two levels, from the C: level to the YourName level.
dir:
dir is the command used to list all the files in a particular location. This really comes in handy when you can't remember what you named a file.
Let's try using the dir command. In the command window, verify that the command prompt currently reads C:\Users\YourName. If it doesn't, use the cd command to navigate there. Now type "dir" and click Return. You should get a list of all the files in your personal folder.
Let's add a new file to this location. It's possible to do this through the command line, but that is beyond the scope of this tutorial. Instead, create a simple Word or Notepad file called MyCommandFile, and save it in your folder. Now return to the command window and type the "dir" command again. You should see your new file in the directory.
Another shortcut to navigation when you have forgotten a file name is the tab key. In the command line, type "cd My" and then click the tab key. You should see the file name auto-complete for you.
Bonus command:
One more helpful command to use is the up arrow key. This key will go back through your most recent commands. This really comes in handy if you have typed a particularly long command and would rather not type it again.
The command window is very helpful, and there are a number of good tutorials available if you would like to learn more about what it can do for you.
Friday, May 3, 2013
Setting Up Your Java Environment
In order to write your automated tests in Java, you will need the Java Development Kit, or JDK. Here are instructions for downloading and installing the JDK:
1. Navigate to http://www.oracle.com/technetwork/java/javase/downloads/index.html
2. Click on the download button for the JDK
3. Click the radio button that signifies that you have accepted the license agreement
4. Find your operating system and click on the download link. If you are a Windows user and trying to determine whether you should use Windows x86 or Windows x64, go to the start menu, right-click on "Computer" and click "Properties". A popup window should appear. Look at the entry next to "System type". A 32-bit operating system means you should use x86; a 64-bit operating system means you should use x64.
5. Follow the installation prompts, and if prompted to do so, restart your computer
Next, you will need an Integrated Development Environment (IDE), like Eclipse. Here's how to download and install it:
1. Navigate to http://www.eclipse.org/downloads/
2. Use the dropdown at the top of the page to select your operating system
3. Find the entry for Eclipse IDE for Java Developers, and click on the link for either 32-bit or 64-bit, depending on your operating system
4. Follow the installation prompts
Now you are all ready to begin coding in Java! Let's write a program:
1. Open Eclipse
2. You will be prompted to name your workspace; give this space whatever file name and filepath you prefer, such as C:\Users\YourName\workspace.
3. The Eclipse window should open
4. Select File-> New-> Java Project
5. Give your project a name, such as MyFirstJavaProject, and click Finish
6. Look in the leftmost window, in the section called Package Explorer. You should see a folder icon with the letter J above it, and your project name.
7. Right-click on the project name and choose New-> Package
8. Give your package a name, such as myFirstPackage, and click Finish
9. In the Package Explorer section you should now see a folder called src under the project name, with an icon that shows a folder with a package inside it. You should also see a package icon under the src folder with your package name. You also should see that the JRE System Library has been added to the project file.
10. Right-click on the package name and choose New->Class
11. Give the class a name, such as MyFirstClass, and click Finish
12. In the Package Explorer section you should now see a file called MyFirstClass.java. The file will have an icon that looks like a sheet of paper with a J on it.
13. Look in the center section of the Eclipse window. You should see that the class file you created is open, and has this text in it:
package myFirstPackage;
public class MyFirstClass {
}
14. Paste the following code in between the curly braces found after the class name:
public static void main(String[ ] args){
System.out.println("Hello world!");
}
15. If you have done step 14 correctly, the whole class file should look like this:
package myFirstPackage;
public class MyFirstClass {
public static void main(String[ ] args){
System.out.println("Hello world!");
}
}
16. Save the changes you made by clicking on the disk icon in the toolbar
17. Right-click on the class name in the Package Explorer and choose Run As-> Java Application
18. Look in the the bottommost pane of the Eclipse window- you should see a section called Console. In this section, you should now see the message Hello world!
19. Congratulations! You have successfully set up Eclipse and run your first Java program!
Wednesday, April 17, 2013
Introduction to Java: Passing Parameters
When I first started learning Java, I was fairly perplexed by the parentheses found after every method. Sometimes they were empty, and sometimes they had values in them, and I didn't understand why. Plus, in the declaration of a method, there was a variable mentioned before the method name, and I didn't know why that was there. I've found it's easiest to think about parameters and methods in terms of input and output. Here's an example:
output Square (input) {
}
The green output describes what kind of result you are going to return from the method. The green input describes what kind of input I put into the method. This method is going to take a number, square it, and return the result of the squaring. I would like the input number to be an integer, so I will declare that here:
output Square (int input) {
}
The word "input" is what I'm calling the variable that I'm going to pass into the method. Because I know that if I square an integer, I will get an integer as a result, I'm going to declare that the output will be an integer:
int Square (int input) {
}
Note that I don't need to create a variable name for the output when I'm declaring the method. Now let's add in the steps for the method:
int Square (int input) {
int output = input * input;
return output;
}
So what this function does is take the variable called input, squares it, sets it to equal an int variable called output, and then returns the output.
It's also possible to have more than one input parameter:
String Square (int input, String name) {
int output = input * input;
String result = "Hi " + name + "! Your result is " + " output;
return result;
}
This is a little more confusing, so I will explain it in more detail. The first line of the method is taking the input variable and squaring it, and setting it to a variable called output, just as it did before. But in the second line we're creating a String. The characters in between the quote marks are entered into the string. These are concatenated (added) with our int output and the String name that we entered in the parameters, using plus signs to add the characters:
"Hi " + name + "! Your result is " + " output
So if I called the function like this:
Square(2, Kristin);
The result String would be set to:
Hi Kristin! Your result is 4
Then if my main program wanted to output the result, I would use the instruction:
System.out.println (result);
And my result would be printed to the console.
Now, what about methods that don't pass in any parameters? It would seem as if parameters would be needed to run a method, but they aren't. Here's an example:
void MyParameterlessMethod () {
System.out.println ("I can print something out without even passing in a parameter!");
}
All this method is going to do is run the print instruction inside, which doesn't need any parameter. You may have noticed the void before my method name. This says that the method isn't going to return anything to the main program.
Is it possible to have a method that doesn't pass in any parameters, but still returns something? Sure!
int MyParameterlessMethod () {
int result = 4;
return result;
}
This method assigns the number 4 to the variable called result, and then returns it to the main program.
In my next post, I will discuss setting up Eclipse and writing a simple program.
output Square (input) {
}
The green output describes what kind of result you are going to return from the method. The green input describes what kind of input I put into the method. This method is going to take a number, square it, and return the result of the squaring. I would like the input number to be an integer, so I will declare that here:
output Square (int input) {
}
The word "input" is what I'm calling the variable that I'm going to pass into the method. Because I know that if I square an integer, I will get an integer as a result, I'm going to declare that the output will be an integer:
int Square (int input) {
}
Note that I don't need to create a variable name for the output when I'm declaring the method. Now let's add in the steps for the method:
int Square (int input) {
int output = input * input;
return output;
}
So what this function does is take the variable called input, squares it, sets it to equal an int variable called output, and then returns the output.
It's also possible to have more than one input parameter:
String Square (int input, String name) {
int output = input * input;
String result = "Hi " + name + "! Your result is " + " output;
return result;
}
This is a little more confusing, so I will explain it in more detail. The first line of the method is taking the input variable and squaring it, and setting it to a variable called output, just as it did before. But in the second line we're creating a String. The characters in between the quote marks are entered into the string. These are concatenated (added) with our int output and the String name that we entered in the parameters, using plus signs to add the characters:
"Hi " + name + "! Your result is " + " output
So if I called the function like this:
Square(2, Kristin);
The result String would be set to:
Hi Kristin! Your result is 4
Then if my main program wanted to output the result, I would use the instruction:
System.out.println (result);
And my result would be printed to the console.
Now, what about methods that don't pass in any parameters? It would seem as if parameters would be needed to run a method, but they aren't. Here's an example:
void MyParameterlessMethod () {
System.out.println ("I can print something out without even passing in a parameter!");
}
All this method is going to do is run the print instruction inside, which doesn't need any parameter. You may have noticed the void before my method name. This says that the method isn't going to return anything to the main program.
Is it possible to have a method that doesn't pass in any parameters, but still returns something? Sure!
int MyParameterlessMethod () {
int result = 4;
return result;
}
This method assigns the number 4 to the variable called result, and then returns it to the main program.
In my next post, I will discuss setting up Eclipse and writing a simple program.
Monday, April 15, 2013
Introduction to Java: Definitions
Another thing I struggled with when learning Java was what the difference was between a Class, an Object, and a Method. Through personal experience and web searches, I have come up with this explanation:
A class is the basic building block in Java. It consists of variables and methods. Here's an example:
public class Cat { //this is the class
//these are the variables in the Cat class
String name;
int age;
int weight;
String color;
//these are the methods in the Cat class
public void eat() {
}
public void sleep() {
}
public void meow() {
}
}
A variable is a little bit of memory that stores a value to be used in a program. A String is a bit of text, like "Hello!" or "rainbow". In the Cat class, the String variables we have are name (the name of the cat), and color (the color of the cat). An int is an integer, like 7 or 13. In the Cat class, the int variables we have are age (the age of the cat) and weight (the weight of the cat).
An Object is an instance of a class. If we want our program to create and use a cat, we need to create a Cat object:
public void main() { //this is the main method of our program
//this is where we create the new Cat Object
Cat Fluffy = new Cat; //Fluffy is the variable name given to the Cat Object
}
A method is a task or group of tasks. If you are familiar with mathematical functions, you might want to think of a method as a function. In the Cat class, the methods are eat(), sleep(), and meow(). The parentheses after the method name indicate what kind of parameter will be passed into the method. In this case, eat(), sleep(), and meow() do not require a parameter. In the next blog post I will give some examples of methods with parameters and how they work.
A class is the basic building block in Java. It consists of variables and methods. Here's an example:
public class Cat { //this is the class
//these are the variables in the Cat class
String name;
int age;
int weight;
String color;
//these are the methods in the Cat class
public void eat() {
}
public void sleep() {
}
public void meow() {
}
}
A variable is a little bit of memory that stores a value to be used in a program. A String is a bit of text, like "Hello!" or "rainbow". In the Cat class, the String variables we have are name (the name of the cat), and color (the color of the cat). An int is an integer, like 7 or 13. In the Cat class, the int variables we have are age (the age of the cat) and weight (the weight of the cat).
An Object is an instance of a class. If we want our program to create and use a cat, we need to create a Cat object:
public void main() { //this is the main method of our program
//this is where we create the new Cat Object
Cat Fluffy = new Cat; //Fluffy is the variable name given to the Cat Object
}
A method is a task or group of tasks. If you are familiar with mathematical functions, you might want to think of a method as a function. In the Cat class, the methods are eat(), sleep(), and meow(). The parentheses after the method name indicate what kind of parameter will be passed into the method. In this case, eat(), sleep(), and meow() do not require a parameter. In the next blog post I will give some examples of methods with parameters and how they work.
Tuesday, April 9, 2013
Introduction to Java: Object-Oriented Programming
Java is one of the most commonly used programming languages, and it works well with Webdriver and JQuery, which we will be using in our automated tests. Java is an Object-Oriented Programming (OOP) Language. You may have had exposure to OOP languages in the past, but if not, the concept may seem a little foreign at first. Having grown up before OOP, I had a particularly tough time with it. I hope that the explanation below will make it easier on you!
Think about the tasks that you do when you get ready in the morning. You might: exercise, shower, and get dressed. If you were writing a program, or class, to tell a robot to do these things, you would probably write a little subroutine, or method, for each task. For example:
public class getReady { //this is the class
exercise(); //this calls the exercise method
shower();
getDressed();
public void exercise() { //this is the exercise method
pushups;
situps;
squats;
}
public void shower() { //this is the shower method
get in shower;
lather;
rinse;
repeat;
}
public void getDressed() { //this is the get dressed method
put on shirt;
put on pants;
put on shoes;
}
}
Now consider your friend, who exercises and showers at night instead of in the morning. His nighttime routine is: exercise, shower, put on pajamas. If you were programming a robot to do your friend's nighttime activities, it might look like this:
public class getReadyForBed { //this is the class
exercise();
shower();
putOnPajamas(); //this calls the put on pajamas method
public void exercise() {
pushups;
situps;
squats;
}
public void shower() {
get in shower;
lather;
rinse;
repeat;
}
public void putOnPajamas() { //this is the put on pajamas method
put on pajama top;
put on pajama bottoms;
}
}
Notice that the two classes have two methods that are exactly the same. Why not share the code instead of copying and pasting it into each class? That way if you ever need to make a change to your exercise method, you will only have to change it once. Let's make a base class that contains all the methods we might need:
public class Ready { //this is our base class
public void exercise() {
pushups;
situps;
squats;
}
public void shower() {
get in shower;
lather;
rinse;
repeat;
}
public void getDressed() {
put on shirt;
put on pants;
put on shoes;
}
public void putOnPajamas() {
put on pajama top;
put on pajama bottoms;
}
}
Now, we can change our two programs to call upon the existing methods instead of copying and pasting them:
public class getReady extends Ready { //this is your routine
exercise();
shower();
getDressed();
public class getReadyForBed extends Ready { //this is your friend's routine
exercise();
shower();
putOnPajamas();
Think about the tasks that you do when you get ready in the morning. You might: exercise, shower, and get dressed. If you were writing a program, or class, to tell a robot to do these things, you would probably write a little subroutine, or method, for each task. For example:
public class getReady { //this is the class
exercise(); //this calls the exercise method
shower();
getDressed();
public void exercise() { //this is the exercise method
pushups;
situps;
squats;
}
public void shower() { //this is the shower method
get in shower;
lather;
rinse;
repeat;
}
public void getDressed() { //this is the get dressed method
put on shirt;
put on pants;
put on shoes;
}
}
Now consider your friend, who exercises and showers at night instead of in the morning. His nighttime routine is: exercise, shower, put on pajamas. If you were programming a robot to do your friend's nighttime activities, it might look like this:
public class getReadyForBed { //this is the class
exercise();
shower();
putOnPajamas(); //this calls the put on pajamas method
public void exercise() {
pushups;
situps;
squats;
}
public void shower() {
get in shower;
lather;
rinse;
repeat;
}
public void putOnPajamas() { //this is the put on pajamas method
put on pajama top;
put on pajama bottoms;
}
}
Notice that the two classes have two methods that are exactly the same. Why not share the code instead of copying and pasting it into each class? That way if you ever need to make a change to your exercise method, you will only have to change it once. Let's make a base class that contains all the methods we might need:
public class Ready { //this is our base class
public void exercise() {
pushups;
situps;
squats;
}
public void shower() {
get in shower;
lather;
rinse;
repeat;
}
public void getDressed() {
put on shirt;
put on pants;
put on shoes;
}
public void putOnPajamas() {
put on pajama top;
put on pajama bottoms;
}
}
Now, we can change our two programs to call upon the existing methods instead of copying and pasting them:
public class getReady extends Ready { //this is your routine
exercise();
shower();
getDressed();
}
public class getReadyForBed extends Ready { //this is your friend's routine
exercise();
shower();
putOnPajamas();
}
See how much space this saves? There are other reasons why OOP is a good idea, but the main thing to understand, in my opinion, is that OOP saves you from having to copy and paste code.
Wednesday, April 3, 2013
When to Automate
The first step in automation success is knowing when to automate and when not to automate. Here are some guidelines:
- DO automate repetitive tasks.
- DON'T automate brand-new features.
- DO automate basic smoke-level tests.
- DON'T automate fragile features or works in progress.
- DO automate things users will do every day.
- DON'T automate weird edge cases.
Monday, April 1, 2013
Why Automate?
For many of you, your initial answer to the question "Why automate?" may be "Because management wants me to." This is, of course, a valid reason. But after you have a few automated tests written, you will discover other, more important reasons:
- It saves you time.
- It frees you from some repetitive tasks.
- It helps you to think more critically about your testing.
When I'm manually testing, I tend to follow a written plan and add in a few ad hoc tests as well. But when I'm writing an automated test, I need to think about why I'm running the test. The test engine will not be exploring, so I'm going to need to tell it to do something very specific. What are the minimum steps required to ensure that the feature works? Will I need to vary those steps for broader coverage, and what will be the most efficient way to do that?
- It helps you to understand your development team.
Before I started writing automated tests, I knew a bit about coding from my college courses, but I'd never had real hands-on experience. Using an IDE and writing in Java helped me have more sympathy for what developers go through in order to code, and it also taught me how programs are structured and how the build process works. This has helped me become a better tester.
The path to learning automation won't be easy, but it will teach you important skills, streamline your testing process, help you understand the development life-cycle, and improve your thinking about your tests.
Friday, March 29, 2013
Welcome!
This blog is aimed at Software QA Engineers who know nothing about test automation. I was just like you six months ago. After much trial and error, I have successfully automated many tests, and I would like to share what I learned with you and hopefully save you some time and trouble!
In the next few weeks I will be covering:
In the next few weeks I will be covering:
- Why automate?
- When to use automated tests and when to use manual tests
- Introduction to Java
- Setting up Eclipse
- Setting up JUnit
- Introduction to WebDriver
- Setting up Maven
- Simple Maven commands
- Setting up Sauce
- Setting up Bamboo
- Creating your Base Test
- Introduction to JQuery
- Troubleshooting navigation issues
Subscribe to:
Posts (Atom)
New Blog Location!
I've moved! I've really enjoyed using Blogger for my blog, but it didn't integrate with my website in the way I wanted. So I...
-
It's never fun to start your work day and discover that some or all of your nightly automated tests failed. It's especially frustra...
-
It's book review time once again, and this month I read Unit Testing Principles, Practices, and Patterns by Vladimir Khorikov. I thoug...
-
I've moved! I've really enjoyed using Blogger for my blog, but it didn't integrate with my website in the way I wanted. So I...