Mock tests, Interview questions, Tutorials and Tech news
 
 
Home > Programming / tutorials > Unit testing with Selenium

Unit testing with Selenium

September 7th, 2010 Leave a comment Go to comments

In our last Selenium – Web application testing tool, we had introduced selenium and explained different components of selenium. In this post we will look at code examples.

Selenium runs in Internet Explorer, Mozilla and Firefox on Windows, Linux, and Macintosh, Safari on the Mac, with plans to support Safari on iPhone soon. Selenium test scripts are portable, can be run from browsers (using Selenium IDE) or from JUnit(using Selenium RC) etc. For example, test scripts written using Selenium IDE in Firefox on Windows can run on Firefox in Mac or Linux, without changing any code. Selenium tests run directly in browsers and so match the end-user experience closely.

We will discuss here on how you can easily create automatic test scripts using Selenium IDE and convert them to JUnit tests (which uses Selenium Remote Control) which can be added to your JUnit based automatic regression test suite.

How to create a test plan in Selenium IDE

Creating a test plan in Selenium IDE is very easy, so we will use it to create few simple tests to begin with.

  • Install Selenium IDE 1.0.6, a Firefox plugin.
  • After installing Selenium please restart your Firefox browser for the plugin to be activated.
  • Now you should see a new added menu item named Selenium IDE under your Firefox Tools menu.
  • Open / browse the site for which you want to prepare a test case.
  • Start Selenium IDE from Firefox Tools->Selenium IDE.
  • Browse some pages.
  • Now click red button to stop recording.

At this point you will see Selenium automatically recording your actions. Carefully note the commands, target and value. You can create and insert your own commands/ modify or even delete them. We will show some examples below. In the next section we will see how we can modify the generated tests to suit our needs.

How to create / modify / delete Selenium commands

The default commands generated by Selenium when you are browsing the page as a normal user should be modified to make the test more robust and to add test cases to it.

  • Let’s replace all click commands by clickAndWait. click simply clicks the specified link and goes on to execute the next command without waiting. On the other hand clickAndWait waits for the new page to loaded before executing the next command. clickAndWait should be used to make more robust test cases.
  • Insert assertTextNotPresent command after each clickAndWait command to confirm a text must not be present in the browsed page.
  • Use assertTextPresent command to confirm a text must be present in the browsed page.
  • Finally to test your test plan please click green arrow button to play from the begining or to play from start point.
  • Export the test plan as java file by Selenium IDE File->Export Test As->Java – Selenium RC (for example the file name is SeleniumFirstTest.java)
  • Then close your Firefox Selenium ID.

How to run above test plan (automatically generated java file from Selenium IDE) in command line?

  • Download Selenium RC.
  • Unzip it under the same directory where SeleniumFirstTest.java (exported test plan as java file from Selenium ID) was saved.
  • Install junit.
  • Go to directory where you unzip selenium-remote-control-1.0-beta-1-dist.zip file.
  • Open a terminal and do the steps below-
    • cd selenium-remote-control-1.0-beta-1/selenium-server-1.0-beta-1
    • java -jar selenium-server.jar (to run the server in interactive mode execute java -jar selenium-server.jar -interactive)
    • If you get an error like Error: com.thoughtworks.selenium.SeleniumException: ERROR Server Exception: sessionId should not be null; has this session been started yet? then ensure that the browser is in the PATH before running the server. For example, you want to run the test in Firefox. Then you should do next two steps.
    • locate firefox-bin (for example it returns /usr/lib/firefox-1.5.0.12/firefox-bin)
    • export PATH=$PATH:/usr/lib/firefox-1.5.0.12/firefox-bin;
      Note: There is an alternative way to fix above error (browser is not in path). Simply replace chrome with browser PATH in SeleniumFirstTest.java file. For example:
      line
      setUp(“http://blog.taragana.com”, “*chrome”);
      becomes
      setUp(“http://blog.taragana.com”, “*firefox /usr/lib/firefox-1.5.0.12/firefox-bin”);
      in SeleniumFirstTest.java.
      To run the test in opera browser replace chrome with opera.

Now the selenium server is running and you have to run the Java client located in selenium-remote-control-1.0-beta-1/selenium-java-client-driver-1.0-beta-1.

  • Open another terminal.
    • export CLASSPATH=.:selenium-remote-control-1.0-beta-1/selenium-java-client-driver-1.0-beta-1/selenium-java-client-driver.jar:/usr/share/java/junit.jar
    • javac SeleniumSTSanityTest.java
    • java SeleniumSTSanityTest

The automatically generated java file SeleniumFirstTest.java is likely to have some defects. Fix it by comparing with the example below:

 
import com.thoughtworks.selenium.*;
import junit.framework.*;
import java.util.regex.Pattern;
public class SeleniumFirstTest extends SeleneseTestCase {
    public void setUp() throws Exception {
        setUp("http://blog.taragana.com", "*chrome"); // to run the test in opera replace chrome with opera
    }
    public void testSimpleThoughts() throws Exception {
        selenium.open("");
        assertFalse(selenium.isTextPresent("WordPress database error: ["));
        assertTrue(selenium.isTextPresent("2003-2008"));
        selenium.open("/index.php/category/programming/java");
        selenium.waitForPageToLoad("30000");
        assertFalse(selenium.isTextPresent("WordPress database error: ["));
        assertTrue(selenium.isTextPresent("2003-2008"));
        selenium.click("//img[@alt='Übersetzen Sie zum Deutsch/German']");
        selenium.waitForPageToLoad("30000");
        assertFalse(selenium.isTextPresent("WordPress database error: ["));
        assertTrue(selenium.isTextPresent("2003-"));
        selenium.click("//img[@alt='Přeložit do Čech/Czech']");
        selenium.waitForPageToLoad("60000");
        assertFalse(selenium.isTextPresent("WordPress database error: ["));
        assertTrue(selenium.isTextPresent("2003"));
    }
    public static Test suite() {
        return new TestSuite(SeleniumSTSanityTest.class);
    }
    public static void main(String args[]) {
        junit.textui.TestRunner.run(suite());
    }
}

Note: This code is not written by author.

Be Sociable, Share!
  1. AJEET
    March 24th, 2011 at 05:43 | #1

    @AJEET YADAV
    YOU SHOULD DESCRIBE SELENIUM AUTOMATION FRAMEWORK WITH TESTLINK

  2. AJEET
    March 24th, 2011 at 05:35 | #2

    ALL RARE GOOD.BUT GIVE ABOUT SELENIUM WITH TESTLINK

  3. Ruchika
    September 27th, 2010 at 10:06 | #3

    Classpath is not properly set that is why you are getting this compilation errors. Try setting classpath in hidden file bashrc in Linux. I hope this helps.

  4. srini
    September 20th, 2010 at 10:11 | #4

    hi,
    I have added following in environmental variables
    SELENINUM_HOME=H:\pf\selenium
    classpath=H:\pf\selenium\selenium-java-client-driver-1.0.1
    path=%SELENIUM_HOME%\selenium-java-client-driver-1.0.1

    ——————————————
    but getting the same above error…..any ideas?

    Thanks,
    Srini

  5. September 16th, 2010 at 12:29 | #5

    @srini
    Srini , look like jar path are missing from your classpath.
    On windows, you can set classpath
    set classpath=%classpath%;path_to_jar

    Or through My Computer/Advance icon.

  6. srini
    September 16th, 2010 at 11:01 | #6

    File: seleniumTest.java

    package com.example.tests;

    import com.thoughtworks.selenium.*;
    import java.util.regex.Pattern;

    public class seleniumTest extends SeleneseTestCase {
    public void setUp() throws Exception {
    setUp(“http://change-this-to-the-site-you-are-testing/”, “*chrome”);
    }
    public void testUntitled() throws Exception {
    selenium.open(“/vceptpl/csr/auth/login.do”);
    selenium.type(“username”, “test”);
    selenium.type(“password”, “pass”);
    selenium.clickAndWait(“//input[@value='Login >>']“);
    selenium.waitForPageToLoad(“30000″);
    selenium.selectFrame(“topBar”);
    selenium.clickAndWait(“logout”);
    selenium.waitForPageToLoad(“30000″);
    selenium.selectWindow(“null”);
    selenium.waitForPopUp(“”, “30000″);
    }
    }

  7. srini
    September 16th, 2010 at 10:50 | #7

    Hi,
    I am getting the following error after following your article.
    Any ideas?

    Thanks,
    Srini

    $ javac seleniumTest.java
    seleniumTest.java:3: package com.thoughtworks.selenium does not exist
    import com.thoughtworks.selenium.*;
    ^
    seleniumTest.java:6: cannot find symbol
    symbol: class SeleneseTestCase
    public class seleniumTest extends SeleneseTestCase {
    ^
    seleniumTest.java:8: setUp() in com.example.tests.seleniumTest cannot be applied
    to (java.lang.String,java.lang.String)
    setUp(“http://change-this-to-the-site-you-are-testing/”, “*chrom
    e”);
    ^
    seleniumTest.java:11: cannot find symbol
    symbol : variable selenium
    location: class com.example.tests.seleniumTest
    selenium.open(“/vceptpl/csr/auth/login.do”);
    ^
    seleniumTest.java:12: cannot find symbol
    symbol : variable selenium
    location: class com.example.tests.seleniumTest
    selenium.type(“username”, “test”);
    ^
    seleniumTest.java:13: cannot find symbol
    symbol : variable selenium
    location: class com.example.tests.seleniumTest
    selenium.type(“password”, “pass”);
    ^
    seleniumTest.java:14: cannot find symbol
    symbol : variable selenium
    location: class com.example.tests.seleniumTest
    selenium.clickAndWait(“//input[@value='Login >>']“);
    ^
    seleniumTest.java:15: cannot find symbol
    symbol : variable selenium
    location: class com.example.tests.seleniumTest
    selenium.waitForPageToLoad(“30000″);
    ^
    seleniumTest.java:16: cannot find symbol
    symbol : variable selenium
    location: class com.example.tests.seleniumTest
    selenium.selectFrame(“topBar”);
    ^
    seleniumTest.java:17: cannot find symbol
    symbol : variable selenium
    location: class com.example.tests.seleniumTest
    selenium.clickAndWait(“logout”);
    ^
    seleniumTest.java:18: cannot find symbol
    symbol : variable selenium
    location: class com.example.tests.seleniumTest
    selenium.waitForPageToLoad(“30000″);
    ^
    seleniumTest.java:19: cannot find symbol
    symbol : variable selenium
    location: class com.example.tests.seleniumTest
    selenium.selectWindow(“null”);
    ^
    seleniumTest.java:20: cannot find symbol
    symbol : variable selenium
    location: class com.example.tests.seleniumTest
    selenium.waitForPopUp(“”, “30000″);
    ^
    13 errors

  8. September 14th, 2010 at 10:34 | #8

    That sounds interesting. Need to check out. Thanks for specifying the major features.

  9. September 14th, 2010 at 07:34 | #9

    Testatoo is based on Selenium so if you have a problem that you cannot resolve with Testatoo you have in all case the possibility to use the hidden Selenium ;)

    – Testatoo have semantic => radio is nor a checkbox
    – Testatoo offer a good fluent api to have a clear test
    – Testatoo offer a native browser event (mouse and keyboard !)
    – Testatoo offer a good abstraction so you can run the same test on Html4 technology as for exemple on Flex3 technology ;)

    Don’t forget Testatoo is only a wrapper on Selenium ;) is a Selenium++ ;)

    Enjoy

  10. September 13th, 2010 at 10:37 | #10

    Not sure how much complex UI testatoo can handle.. Selenium does a pretty good job in that sense.

  11. September 13th, 2010 at 08:13 | #11

    Hummm

    I don’ t like recording. Look at http://www.testatoo.org it’s a better usage of selenium than this approach.

  12. September 11th, 2010 at 23:16 | #12

    i still don’t know the scene in the development will be used. of course, it’s interesting.

  13. chaohuang.hong
    September 9th, 2010 at 20:37 | #13

    i still don’t know the scene in the development will be used. of course, it’s interesting.

  14. Simon
    September 9th, 2010 at 18:24 | #14

    Strictly, Selenium tests are not “unit tests”, even if they’re driven through JUnit. The term properly refers to testing small pieces of code (units) in isolation – things like this would generally be distinguished as “acceptance tests”, “integration tests”, or some similar term.

    You might also want to have a look at WebDriver (which I think is being merged into Selenium) as an alternative – I’ve not used it for a while, but have previously found it’s API a lot nicer to work with than the classic Selenium-RC one.

  15. John Doe
    September 9th, 2010 at 15:30 | #15

    As of today, Selenium RC is at version 1.0.3. Also, your post describes only very basic concepts, and it does so in an absolutely deasterous way. Please delete this post. Thanks

  1. September 9th, 2010 at 13:24 | #1

Get Adobe Flash playerPlugin by wpburn.com wordpress themes