Automatic translation

Archives

November 2008
L My Me J V S D
"October December "
A 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

Contributors

Test your web graphics

In large applications, changes to web pages and the need for compatibility with different browsers render web tests tedious. There are many tools to facilitate this task:

  • StrutsTestCase , an extension of JUnit class TestCase to test the controller layer of the Struts application. It allows to test the forwards, the error messages generated by what is entered in the ActionForm.
  • HTMLUnit
  • JWebUnit
  • Selenium

and many more (Fitness, Jqunit ...)

  HTMLUnit, and Selenium JWebUnit unlike StrutsTestCase directly simulates user actions (button clicks, links, radio button, combo ... the input text entry, form submission, call javascript code ...) in real browsers or virtual.

HTMLUnit (Apache License 2) simulates a browser in memory and provides a rich API in Java to describe a usage scenario of an application in a   browser. Junit or TestNG can be used to make assertions about the content of the HTML page. HTMLUnit also offers good support javascript and ajax. Note simply that its use often requires a framework encapsulation in a "house" to facilitate the use of its API.

Web Writing tests takes time and may request a modification of your pages if you do not uniquely identified your DOM elements, a good practice is to add id to the elements to be tested. In any case it does not hurt. Feel free to post to determine how much you want to test your pages because the slightest change in the code may fail your test. Given the time of writing these tests it is reasonable not to test everything.

See more detail JWebUnit and selenium:

JWebUnit

JWebUnit is a Java framework for writing tests for web application and it is against one of the highest level since the qu'HTMLUnit wraps! The code is more concise JWebUnit qu'HTMLUnit and allows in theory to use two engines tests: HTMLUnit and Selenium.

Sample test case:

  • it connects to the site http://www.google.fr/
  • you enter the search input field the word "HTMLUnit"
  • you click on search
  • Clicking on a link that contains the text "HTMLUnit"
  • it is verified that the title of the current page is "HTMLUnit - Welcome to HTMLUnit"
  • it checks for the text "Get started"

With the plugins HTMLUnit JWebUnit-HTMLUnit-plugin-2.0.jar in your build path:

 junit.framework.TestCase import, import net.sourceforge.jwebunit.htmlunit.HtmlUnitTestingEngineImpl; import net.sourceforge.jwebunit.junit.WebTester public class extends TestCase {private TestSearchExample WebTest testing; public TestSearchExample (String name) {super (name) ; test = new WebTest (); HtmlUnitTestingEngineImpl HtmlUnitTestingEngineImpl engine = new (); tester.setDialog (engine); tester.getTestContext (). setBaseUrl ("http://www.google.fr/");} public void testSearch ( ) throws Exception {tester.beginAt ("/"); tester.setTextField ("q", "HTMLUnit"); tester.submit ("btnG"); tester.clickLinkWithText ("HTMLUnit"); tester.assertTitleEquals ("HTMLUnit - Welcome to HTMLUnit "); tester.assertLinkPresentWithText (" Get started ");}} 

If you want to use the plugins JWebUnit selenium-selenium-plugin-2.0.jar, you must first start the server selenium remote control:

  java-jar C: \ cheminVers \ selenium-server.jar 
  junit.framework.TestCase import;
 net.sourceforge.jwebunit.junit.WebTester import;
 net.sourceforge.jwebunit.selenium.SeleniumTestingEngineImpl import;

 public class extends TestCase {TestSearchGoogleSelenium
       WebTest private testing;
       public TestSearchGoogleSelenium (String name) {
         super (name);
         test = new WebTest ();
         SeleniumTestingEngineImpl SeleniumTestingEngineImpl engine = new ();
         tester.setDialog (engine);
         tester.getTestContext (). setBaseUrl ("http://www.google.fr/ig?hl=fr");
     }

     public void testSearch () throws Exception {
       tester.beginAt ("/");
       tester.setTextField ("q", "HTMLUnit");
       tester.submit ("btnG");
       tester.clickLinkWithText ("HTMLUnit");
       tester.assertTitleEquals ("HTMLUnit - Welcome to HTMLUnit");
       tester.assertLinkPresentWithText ("Get started");
      }
 } 

This code is a way of doing it is another way to initialize the test engine.

Behind the scenes, implementations engine tests and Selenium HTMLUnit do not work the same way: SeleniumTestingEngineImpl uses XPath.

The launch of this test, a FireFox browser is opened with the Google search page and the rendering of the scenario. If all goes well, the JUnit test in Eclipse turns green. As you see the API is JWebUnit very explicit in its method names.

If your application code is already written, using FireBug for id elements you are testing is a way. Another solution is to use Selenium IDE.

Reported that after doing some tests with HTMLUnit 2.3 It seems that the method does not Ajax.Updater: = (

Selenium

Note first that a new version of the site of selenium was released: http://selenium.openqa.org/

What is Selenium? A suite of community OpenQA to test your Web applications in real browsers. Your tests will be more faithful to the behavior of users with emulations of browsers.

Selenium IDE is

  • ease of use,
  • multi-language,
  • multi-platform,
  • parallelization tests

Selenium IDE

This is a FireFox plugin that lets you record a test case for your application. Clicks, data entry, verification of the presence of text are saved as a script to be replayed later in selenium IDE or exported in your favorite language: Java, Ruby, PHP, perl, C # or Python.

In FireFox:

  • Go to the test,
  • click Tools,
  • select selenium IDE.

A window opens in "recording" save your scenario.

To test the title of the page: select the text, right click and choose "assertTextPresent." For a simple test to use the wording "verifyTextPresent." The difference between the two commands is that "verifyTextPresent" if it fails, does not block after the test. At the end of your script click the red button to stop recording.

To save the script and be able to play again click on "File", "save in the test." For export in a more evolved a script example java format, select "export as the test"

You will sometimes also need to redefine the base URL.

An example of java:

  springapp.domain package;

 import com.thoughtworks.selenium .*;

 public class extends TestSpringApp1 SeleneseTestCase {

       public void setUp () throws Exception {
             setUp ("http://localhost:8060/springapp/hello.htm", "* chrome");
       }

       public void testNew () throws Exception {
             selenium.open ("/ springapp / hello.htm");
             assertTrue (selenium.isTextPresent ("Increase Prices"));
             selenium.click ("link = Increase Prices");
             selenium.waitForPageToLoad ("30000");
             selenium.type ("percentage", "30");
             selenium.click ("/ / input [@ value = 'Execute']");
             selenium.waitForPageToLoad ("30000");
             verifyTrue (selenium.isTextPresent ("Products"));
       }
 } 

This test will be run java with seleniumRC.

Before starting the test from Eclipse it is necessary to start the server seleniumRC:

  java-jar 

Now you can run it, a window 'selenium Functional Testing for Web Apps "appears with the progress of your script in the bottom of your browser.

If you have in your code in an onload a window.open, it appears that selenium does not intercept the open this new window (salt-339). A workaround is to launch yourself to open your window without leaving this burden to the window that contains an onload: selenium.openWindow ("URLInOnload", "nameOfWindow") is a selectWindow | | nameOfWindow in the selenium IDE and selenium.selectWindow ("nameOfWindow"). The focus is now on the new window.

Some tips for entering your script:

  • use as a first order assertTextPresent a label that guarantees that the page is loaded then a series of verifyTextPresent
  • if at a click on a link selenium IDE generates a click and the loading takes a little time using a clickAndWait to wait for the next page is busy and thus avoid the selenium test fails because it would already be passed to the next command. You can also use a click and a waitForTextPresent.
  • it may also happen that FDI in the first open is not operational. Simply reopen and you can enter your test.

SeleniumRC

SeleniumRC is a test tool that allows you to start testing graphical web application in several programming languages ​​for http sites in different browsers (IE, FF, Safari) and operating systems (Windows, Linux, Mac) . You can test all these combinations in real conditions of use. This is still a big plus!

We have seen how to run a test use case. But how many launch and group them? The solution is the creation of TestSuite.

Made in Selenium IDE

  • new test follows
  • pull down your first scenario
  • Save the test as (in html)
  • Test after save as (in html)

To add a second test in your test do the following:

  • New Test (either from the menu or from the left that lists the tests)
  • Roll out your second scenario
  • Save the test as (in html)
  • Save the following test (in html)

If you close Selenium IDE and you are open you will see your test following two tests.

For example, here is an example containing three tests:

  <? Xml version = "1.0" encoding = "UTF-8"?>

 <! DOCTYPE html PUBLIC "- / / W3C / / DTD XHTML 1.0 Strict / / EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 <head>
   <meta http-equiv="content-type" content="text/html; charset=utf-8" />
   <title> Test Suite </ title>
 </ Head>
 <body>
 <table id="suiteTable" cellpadding="1" cellspacing="1" border="1" class="selenium"> <tbody>
 <tr> <td> <b> Test Suite </ b> </ td> </ tr>
 <tr> <td> <a href="testCase1.html"> testCase1 </ a> </ td> </ tr>
 <tr> <td> <a href="testCase2.html"> testCase2 </ a> </ td> </ tr>
 <tr> <td> <a href="testCase3.html"> testCase3 </ a> </ td> </ tr>
 </ Tbody> </ table>
 </ Body>
 </ Html> 

Selenium IDE as it is possible to run these tests by clicking on the button "start with Selenium TestRunner" or on the button "Play Entire test follows"

Parallelization testing with Selenium Grid

If you are in continuous integration in your project, you may want to be that each set of tests do not take an hour. You can have multiple "selenium remote control" on different machines that offer different configurations of browsers and operating systems. But what can we do if the machinery running the tests have no browsers?

It is possible to use a VNC plugins (eg http://hudson.gotdns.com/wiki//display/HUDSON/Xvnc+Plugin ) or Xvfb is a simulator x Linux server.

Operation of Selenium Grid: http://selenium-grid.openqa.org/how_it_works.html

Selenium Grid makes it possible to run different instances of "selenium remote control" in parallel. According to this website, each housing five powerful machines' 10 remote control "sufficient to divide the time of functional testing by 50! Is a linear progression.   If you want to see what happens: http://selenium-grid.openqa.org/run_the_demo.html

Some interesting links:

  • http://www.testatoo.org Altus34

    Hi,

    We conducted a test UI API that bridges the gaps in HTMLUnit, JWebUnit and Selenium: Testatoo ;)

    http://www.testatoo.org