Thursday, 31 January 2013

From Selenese to a Program | Selenium Tutorial pdf

From Selenese to a Program

The primary task for using Selenium-RC is to convert your Selenese into a programming language. In this section, we provide several different language-specific examples.

=> Sample Test Script

Let’s start with an example Selenese test script. Imagine recording the following test with Selenium- IDE.
Note: This example would work with the Google search page http://www.google.com

=> Selenese as Programming Code

Here is the test script exported (via Selenium-IDE) to each of the supported programming languages. If you have at least basic knowledge of an object- oriented programming language, you will understand how Selenium runs Selenese commands by reading one of these examples. To see an example in a specific language, select one of these buttons.
In C#:
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, "*firefox" , "http://www.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()
{
selenium.Open( "/" );
selenium.Type( "q" , "selenium rc" );
selenium.Click( "btnG" );
selenium.WaitForPageToLoad( "30000" );
Assert.IsTrue(selenium.IsTextPresent( "Results * for selenium rc" ));
}
}
}

In Java:

package com.example.tests;
import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;
public class NewTest extends SeleneseTestCase {
public void setUp() throws Exception {
setUp( "http://www.google.com/" , "*firefox" );
}
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" ));
}
}

In Perl:

use strict;
use warnings;
use Time::HiRes qw( sleep ) ;
use Test::WWW::Selenium;
use Test::More "no_plan" ;
use Test::Exception;
my $sel = Test::WWW::Selenium->new( host => "localhost" ,
port => 4444,
browser => "*firefox" ,
browser_url => "http://www.google.com/" );
$sel->open_ok( "/" );
$sel->type_ok( "q" , "selenium rc" );
$sel->click_ok( "btnG" );
$sel->wait_for_page_to_load_ok( "30000" );
$sel->is_text_present_ok( "Results * for selenium rc" );

In PHP:

<?php
require_once ’PHPUnit/Extensions/SeleniumTestCase.php’ ;
class Example extends PHPUnit_Extensions_SeleniumTestCase
{
function setUp()
{
$this->setBrowser( " *firefox " );
$this->setBrowserUrl( " http://www.google.com/ " );
}
function testMyTestCase()
{
$this->open( " / " );
$this->type( " q " , " selenium rc " );
$this->click( " btnG " );
$this->waitForPageToLoad( " 30000 " );
$this->assertTrue($this->isTextPresent( " Results * for selenium rc " ));
}
}
?>

in Python:

from selenium import selenium
import unittest, time, re
class NewTest(unittest.TestCase):
def setUp(self):
self.verificationErrors = []
self.selenium = selenium( " localhost " , 4444, " *firefox " ,
" http://www.google.com/ " )
self.selenium.start()
def test_new(self):
sel = self.selenium
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 " ))
def tearDown(self):
self.selenium.stop()
self.assertEqual([], self.verificationErrors)

in Ruby:

require " selenium "
require " test/unit "
class NewTest < Test::Unit::TestCase
def setup
@verification_errors = []
if $selenium
@selenium = $selenium
else
@selenium = Selenium::SeleniumDriver.new( " localhost " , 4444, " *firefox " , " http://@selenium.start
end
@selenium.set_context( " test_new " )
end
def teardown
@selenium.stop unless $selenium
assert_equal [], @verification_errors
end
def test_new
@selenium.open " / "
@selenium.type " q " , " selenium rc "
@selenium.click " btnG "
@selenium.wait_for_page_to_load " 30000 "
assert @selenium.is_text_present( " Results * for selenium rc " )
end
end

In the next section we’ll explain how to build a test program using the generated code.

No comments: