Thursday, 31 January 2013

Programming Your Test | Selenium Tutorial pdf

Programming Your Test

Now we’ll illustrate how to program your own tests using examples in each of the supported programminglanguages. There are essemtially two tasks. * Generate your script into a programming language
from Selenium-IDE, optionally modifying the result. * And two, write a very simple main program that executes the generated code. Optionally, you can adopt a test engine platform like JUnit or TestNG for Java, or NUnit for .NET if you are using one of those languages.
Here, we show language-specific examples. The language-specific APIs tend to differ from one to another, so you’ll find a separate explanation for each.
=> Java
=> C#
=> Python
=> Perl, PHP, Ruby

=> Java

For Java, people use either Junit or TestNG as the test engine. Some development environments like Eclipse have direct support for these via plug-ins. This makes it even easier. Teaching JUnit or TestNG is beyond the scope of this document however materials may be found online and there are publications available. If you are already a “java-shop” chances are your developers will already have some experience with one of these test frameworks.
You will probably want to rename the test class from “NewTest” to something of your own choosing.
Also, you will need to change the browser-open parameters in the statement:
selenium = new DefaultSelenium("localhost", 4444, "*iehta", "http://www.google.com/");
The Selenium-IDE generated code will look like this. This example has coments added manually for additional clarity.
package com.example.tests;
// We specify the package of our tess
import com.thoughtworks.selenium.*;
// This is the driver’s import. You’ll use this for instantiating a
// browser and making it do what you need.
import java.util.regex.Pattern;
// Selenium-IDE add the Pattern module because it’s sometimes used for
// regex validations. You can remove the module if it’s not used in your
// script.
public class NewTest extends SeleneseTestCase {
// We create our Selenium test case
public void setUp() throws Exception {
setUp( "http://www.google.com/" , "*firefox" );
// We instantiate and start the browser
}
public void testNew() throws Exception {
selenium.open( "/" );
selenium.type( "q" , "selenium rc" );
selenium.click( "btnG" );
selenium.waitForPageToLoad( "30000" );
assertTrue(selenium.isTextPresent( "Results * for selenium rc" ));
// These are the real test steps
}
}

=> C#

The .NET Client Driver works with Microsoft.NET. It can be used with any .NET testing framework like NUnit or the Visual Studio 2005 Team System.
Selenium-IDE assumes you will use NUnit as your testing framework. You can see this in the generated code below. It includes the using statement for NUnit along with corresponding NUnit attributes identifying the role for each member function of the test class.
You will probably have to rename the test class from “NewTest” to something of your own choosing.
Also, you will need to change the browser-open parameters in the statement:
selenium = new DefaultSelenium("localhost", 4444, "*iehta", "http://www.google.com/");
The generated code will look similar to this.
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using NUnit.Framework;
using Selenium;
namespace SeleniumTests
{
[TestFixture]
public class NewTest
{
private ISelenium selenium;
private StringBuilder verificationErrors;
[SetUp]
public void SetupTest()
{
selenium = new DefaultSelenium( "localhost" , 4444, "*iehta" ,
"http://www.google.com/" );
selenium.Start();
verificationErrors = new StringBuilder();
}
[TearDown]
public void TeardownTest()
{
try
{
selenium.Stop();
}
catch (Exception)
{
// Ignore errors if unable to close the browser
}
Assert.AreEqual( "" , verificationErrors.ToString());
}
[Test]
public void TheNewTest()
{
// Open Google search engine.
selenium.Open( "http://www.google.com/" );
// Assert Title of page.
Assert.AreEqual( "Google" , selenium.GetTitle());
// Provide search term as "Selenium OpenQA"
selenium.Type( "q" , "Selenium OpenQA" );
// Read the keyed search term and assert it.
Assert.AreEqual( "Selenium OpenQA" , selenium.GetValue( "q" ));
// Click on Search button.
selenium.Click( "btnG" );
// Wait for page to load.
selenium.WaitForPageToLoad( "5000" );
// Assert that "www.openqa.org" is available in search results.
Assert.IsTrue(selenium.IsTextPresent( "www.openqa.org" ));
// Assert that page title is - "Selenium OpenQA - Google Search"
Assert.AreEqual( "Selenium OpenQA - Google Search" ,
selenium.GetTitle());
}
}
}
You can allow NUnit to manage the execution of your tests. Or alternatively, you can write a simple main() program that instantiates the test object and runs each of the three methods, SetupTest(), The- NewTest(), and TeardownTest() in turn.

=> Python

Pyunit is the test framework to use for Python. To learn pyunit refer to its official documentation <http://docs.python.org/library/unittest.html>_.
The basic test structure is:
from selenium import selenium
# This is the driver’s import. You’ll use this class for instantiating a
# browser and making it do what you need.
import unittest, time, re
# This are the basic imports added by Selenium-IDE by default.
# You can remove the modules if they are not used in your script.
class NewTest(unittest.TestCase):
# We create our unittest test case
def setUp(self):
self.verificationErrors = []
# This is an empty array where we will store any verification errors
# we find in our tests
self.selenium = selenium( " localhost " , 4444, " *firefox " ,
" http://www.google.com/ " )
self.selenium.start()
# We instantiate and start the browser
def test_new(self):
# This is the test code. Here you should put the actions you need
# the browser to do during your test.
sel = self.selenium
# We assign the browser to the variable "sel" (just to save us from
# typing "self.selenium" each time we want to call the browser).
sel.open( " / " )
sel.type( " q " , " selenium rc " )
sel.click( " btnG " )
sel.wait_for_page_to_load( " 30000 " )
self.failUnless(sel.is_text_present( " Results * for selenium rc " ))
# These are the real test steps
def tearDown(self):
self.selenium.stop()
# we close the browser (I’d recommend you to comment this line while
# you are creating and debugging your tests)
self.assertEqual([], self.verificationErrors)
# And make the test fail if we found that any verification errors
# were found

=> Perl, PHP, Ruby

The members of the documentation team have not used Sel-RC with Perl, PHP or Ruby. If you are using Selenium-RC with either of these two languages please contact the Documentation Team (see the chapter on contributing). We would love to include some examples from you and your experiences support Perl and PHP users.

No comments: