Google Analytics

Search

To search for specific articles you can use advanced Google features. Go to www.google.com and enter "site:darrellgrainger.blogspot.com" before your search terms, e.g.

site:darrellgrainger.blogspot.com CSS selectors

will search for "CSS selectors" but only on my site.


Monday, February 15, 2010

Creating a Selenium Test Case in Java from scratch

I you want to create a Selenium test case from scratch, i.e. not using the record method listed earlier in my blog, here is how you do it:


  1. I assume you have Eclipse installed
  2. I assume you have downloaded the Selenium RC
    1. The files selenium-server.jar and selenium-java-client-driver.jar will be needed
  3. In Eclipse create a new Java Project
    1. Pick a name for the project, set anything else you think you'll need on the first page
    2. On the next page, go to the Libraries tab and add the two selenium jar files to the project
    3. Finish
  4. Right click on the src folder and create a new JUnit Test Case
    1. Select JUnit 3, pick a package if you want (can be changed later), pick a name, tick setUp and tearDown, click Finish
    2. It should ask you if you want to add the JUnit 3 library to the project. Answer yes.
  5. You should now be looking at the JUnit test case class in the editor
  6. Change the class so it 'extends SeleneseTestCase
  7. You should get a warning on the SeleneseTestCase
  8. Hover over it and you should get the option to import com.thoughtworks.selenium.SeleneseTestCase which you should do
  9. In the setUp() method, the body should be:
    1. super.setUp(url, browser); where url is the URL of you web site being tested and browser is something like "*firefox", "*iehta" or "*safari"
    2. For example, super.setUp("http://www.google.ca", "*safari");
    3. The setUp method should now look like:



      public void setUp() throws Exception {
              super.setUp("http://www.google.ca", "*safari");
          }
      
      
  10. In the tearDown() method the body should be:
    1. super.tearDown();
    2. The tearDown method should now look like:



      public void tearDown() throws Exception {
              super.tearDown();
          }
      
      
  11. Now we can add a test case. It might look like:
  12. public void testAGoodDescriptionOfWhatWeAreTesting() throws Exception {
            selenium.open("/");
            System.out.println(selenium.getHtmlSource());
        }
    
    
  13. To run this, you'll need to start the Selenium Server.
  14. Go to a command line and enter:
  15. java -jar selenium-server.jar
    
  16. You might need some more command line switches like -firefoxProfileTemplate or -trustAllSSLCertificates. To see help on the server use:
  17. java -jar selenium-server.jar -help
    
  18. Once you have the server running, in Eclipse you want to run the test case as a JUnit Test
NOTE: the SeleneseTestCase is a JUnit 3 TestCase. It assumes the names of the methods are fixed and does not use annotations. You have to use setUp, tearDown and all test cases need to start with 'test'. If you want to create the same thing as JUnit 4 you can use:

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;

public class Whatever {

 Selenium selenium;

 @Before
 public void setUp() throws Exception {
  selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.com");
 }

 @Test
 public void whatever() throws Exception {
  selenium.open("/");
  System.out.println(selenium.getHtmlSource());
 }

 @After
 public void tearDown() throws Exception {
  selenium.close();
  selenium.stop();
 }
}

And there is a simple test case create in Eclipse.

4 comments:

Anonymous said...

nicely explained..thank uuuuuuu

Dung said...

Hi bro, I'm a new commer to this one, I still haven't know what is Junit 3 .
I tried to do as you post, with the extends Testcase.
All I got after "run as" is run1/1 and failure 1/1 (your example).
Is it right ?
And why can't I use Extends SeleneseTestCase ?
Could you explain me a little bit here ?
My basic is just Java and a little html.

Darrell said...

Dung,

Creating test automation with Selenium is programming and requires a certain amount of programming knowledge. The larger you expect the project to become, the more development skill you will need to exhibit.

You need to learn Java, Eclipse, JUnit and then learn Selenium.

If you want to learn Java, check out http://www.oracle.com/technetwork/java/index.html.

If you want to learn about Eclipse, check out http://eclipse.org/.

If you want to learn about JUnit, check out http://junit.sourceforge.net/.

The SeleneseTestCase class was written to extend the JUnit 3 TestCase class. When they release JUnit 4 they made some significant changes. The SeleneseTestCase was never updated to take advantage of those changes. If you extend SeleneseTestCase with a JUnit 4 test case, you lose all the new features of JUnit 4, i.e. it goes into backward compatibility mode.

Additionally, all these examples assume Selenium 1.0. Selenium 2.0 is very different. Look for posts from 2011 for examples of Selenium 2.0.

Dung said...

Thanks for your answer, I would start from Junit .
Could I ask you some more question here ?
I orginal want to make an addon for my Firefox with my basic to improve my programming skill, so I go around around and end up at meeting Selenium , Am I going wrong cause I look closely to your Blog and looklike you are doing something else, more difficult than I image .
BTW love your blog .