Posted: February 2nd, 2012 | Author: Christopher Vigliotti | Filed under: Java, O-O | 2 Comments »
If you are new to the strange and wonderful world of Java development, Head First Java is the essential primer.
Do you have a favorite “Intro to Java” or “Intro to Object Oriented Development” resource? Please share in the comments
Posted: January 10th, 2012 | Author: Christopher Vigliotti | Filed under: O-O, Selenium | No Comments »
This post is a part of my Learn Selenium series.
Inheritance
In object-oriented programming, a class can inherit the methods and properties from a parent class. This means that we can add code to a parent class that can be used in one or more child objects. The advantage is that one would only have to write code one time for it to be available in many places.
Lets take a look at the example in Figure 1a:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| package awesomeTests;
import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;
@SuppressWarnings("deprecation")
public class AnAwesomeTest extends SeleneseTestCase {
public void setUp() throws Exception {
setUp("http://code.christophervigliotti.com/", "*chrome");
}
public void testAwesome() throws Exception {
selenium.open("/");
selenium.click("link=Excel Entity Service");
selenium.waitForPageToLoad("30000");
selenium.type("id=comment", "Test comment.");
selenium.click("id=submit");
selenium.waitForPageToLoad("30000");
}
} |
Figure 1a. AnAwesomeTest.java
This is a basic Selenium RC test case. Lets take a closer look at line 8.
8
| public class AnAwesomeTest extends SeleneseTestCase { |
Figure 1b. Line 8 of AnAwesomeTest.java
Translated from Java-speak into English this line reads as follows. “This public class is named ‘AnAwesomeTest’ and is a child of ‘SelesenseTestCase’. AnAwesomeTest.java is a child of parent class SelesenseTestCase.java. This means that the logic found SelesenseTestCase.java and it’s ancestors is available for us to call upon. If you look at the setUp() and testAwesome() methods we are calling methods from the parent class at line 10, as well as on lines 13-18.
Lets take a look at a second Selenium test class…
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| package awesomeTests;
import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;
@SuppressWarnings("deprecation")
public class AnotherTest extends SeleneseTestCase {
public void setUp() throws Exception {
setUp("http://code.christophervigliotti.com/", "*chrome");
}
public void testSomething() throws Exception {
// your test code goes here
}
} |
Figure 2. AnotherTestCase.java
Like AwesomeTestCase, AnotherTestCase is a parent of SeleneseTestCase.
Leveraging A New Parent
Now if we were to create a new parent class for these two classes we would be able to combine some of the redundant logic into one place.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| package awesomeTests;
import com.thoughtworks.selenium.*;
// not being used! import java.util.regex.Pattern;
@SuppressWarnings("deprecation")
public class BigDaddyKane extends SeleneseTestCase {
// properties
public int pageReloadTime = 3000;
public String url = "http://code.christophervigliotti.com/";
public String browser = "*chrome";
// methods
public void setUp() throws Exception {
// was setUp("http://code.christophervigliotti.com/", "*chrome");
setUp(url, browser);
}
} |
Figure 3a. The awesomely named BigDaddyKane.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| package awesomeTests;
@SuppressWarnings("deprecation")
public class AnAwesomeTest extends BigDaddyKane {
public void testAwesome() throws Exception {
selenium.open("/");
selenium.click("link=Excel Entity Service");
// was selenium.waitForPageToLoad("3000");
selenium.waitForPageToLoad(pageReloadTime);
selenium.type("id=comment", "Test comment.");
selenium.click("id=submit");
// was selenium.waitForPageToLoad("3000");
selenium.waitForPageToLoad(pageReloadTime);
}
} |
Figure 3b. Modified AnAwesomeTestCase.java
1
2
3
4
5
6
7
8
| package awesomeTests;
@SuppressWarnings("deprecation")
public class AnotherTest extends BigDaddyKane {
public void testAwesome() throws Exception {
// your test code goes here
}
} |
Figure 3c. Modified AnotherTestCase.java
Since BigDaddyKane contains the same import statements and setUp() method we are able to remove them from AnAwesomeTestCase and AnotherTestCase. Code re-use FTW!
Leveraging Properties
If you take another look at BigDaddyKane.java you’ll notice some other new code.
9
10
11
12
| // properties
public int pageReloadTime = 3000;
public String url = "http://code.christophervigliotti.com/";
public String browser = "*chrome"; |
Figure 4, properties of BigDaddyKane
These are a few properties that I added to the class. The url and string properties are used in line 17 of BigDaddyKane.java. The pageReloadTime property is used on lines 9 and 13 of AnAwesomeTest.java. This modification allows for further code re-use, and specifies a single place where the page reload time, url and browser are defined.
Summary
Parent classes are a great way to help you both pay tribute to golden age rapper Big Daddy Kane and maximize code re-use in your Selenium RC tests.
Ready to dive deeper into the world of Java and Selenium RC? Check out my post on how your code can access test data via an Excel file.
Posted: July 6th, 2010 | Author: Christopher Vigliotti | Filed under: Design Patterns, O-O | 1 Comment »
Over the last few days I’ve been sinking my teeth into Head First Design Patterns
. I’m really enjoying both learning about Design Patterns as well as slinging some Java code.
Posted: May 12th, 2009 | Author: Christopher Vigliotti | Filed under: ColdFusion, O-O | 5 Comments »
If you don’t know what a ColdFusion component is you may want to review this before continuing.
Every system that I’ve developed over the last few years has included a Utility component. This component is where I store helper functions. The example below contains a single function that takes one argument, dumps it out on the screen then calls CFABORT.
Read the rest of this entry »
Posted: May 8th, 2009 | Author: Christopher Vigliotti | Filed under: ColdFusion, MVC, O-O | No Comments »
My first pet project is “Shell”, a simple O-O ColdFusion Sample Application. I’ve spent about 90 minutes on it at this point (translation: it’s not done), so consider it more of a “sketch” at this point.
Shell – Version 0.0.0.1b (pardon the hokey over-use of the shell metaphor in the code).