Monday, 18 April 2011

Hello World Example

Aim:
Detailed Example to be presented to the delegates on the workshop. Covers many Selenium IDE features.

    Pre-requisites:
    Tips

    1. once copied and pasted , locate the CaptureEntireScreenshot Command correct the path.

    Key Steps

    1. Save all the scripts on the PC first in same folder.
    2. Go with the embedded video file and view the execution
    3. Many commands demonstrated.

    What you will learn?
    • Test Suites
    • Test Cases
    • User extension
    • echo command
    • asserts
    • xpaths
    • javascript functions
    • captureEntirePageScreenshot command
    • variable usage
    • random variables
    • verify command

    Related Entries
    • none

    Test suite script, save it with name 'Solution2Suite.html'
    <?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 content="text/html; charset=UTF-8" http-equiv="content-type" />
     <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>





    first test case, save it with name 'testcase1.html'
    <?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 profile="http://selenium-ide.openqa.org/profiles/test-case">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="selenium.base" href="http://foundation.time2test.co.uk/" />
    <title>TestCase1</title>
    </head>
    <body>
    <table cellpadding="1" cellspacing="1" border="1">
    <thead>
    <tr><td rowspan="1" colspan="3">TestCase1</td></tr>
    </thead><tbody>
    <tr>
    <td>open</td>
    <td>/</td>
    <td></td>
    </tr>
    <tr>
    <td>clickAndWait</td>
    <td>link=Log in</td>
    <td></td>
    </tr>
    <tr>
    <td>assertTextPresent</td>
    <td>Username</td>
    <td></td>
    </tr>
    <tr>
    <td>assertTextPresent</td>
    <td>Password</td>
    <td></td>
    </tr>
    <tr>
    <td>type</td>
    <td>id=user_login</td>
    <td>admin</td>
    </tr>
    <tr>
    <td>storeValue</td>
    <td>id=user_login</td>
    <td>loginVariable</td>
    </tr>
    <tr>
    <td>type</td>
    <td>id=user_pass</td>
    <td>time4test</td>
    </tr>
    <tr>
    <td>storeValue</td>
    <td>id=user_pass</td>
    <td>passwordVariable</td>
    </tr>
    <tr>
    <td>echo</td>
    <td>&quot;Print the Creadential:&quot;</td>
    <td></td>
    </tr>
    <tr>
    <td>echo</td>
    <td>${loginVariable}</td>
    <td></td>
    </tr>
    <tr>
    <td>echo</td>
    <td>${passwordVariable}</td>
    <td></td>
    </tr>
    <tr>
    <td>clickAndWait</td>
    <td>id=wp-submit</td>
    <td></td>
    </tr>
    <tr>
    <td>waitForElementPresent</td>
    <td>//div[@id='user_info']/p/a[2]</td>
    <td></td>
    </tr>
    </tbody></table>
    </body>
    </html>




    second test case, save it with name 'testcase2.html'
    <?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 profile="http://selenium-ide.openqa.org/profiles/test-case">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="selenium.base" href="http://foundation.time2test.co.uk/" />
    <title>TestCase2</title>
    </head>
    <body>
    <table cellpadding="1" cellspacing="1" border="1">
    <thead>
    <tr><td rowspan="1" colspan="3">TestCase2</td></tr>
    </thead><tbody>
    <tr>
    <td>clickAndWait</td>
    <td>link=Posts</td>
    <td></td>
    </tr>
    <tr>
    <td>verifyTextPresent</td>
    <td>Posts</td>
    <td></td>
    </tr>
    <tr>
    <td>waitForElementPresent</td>
    <td>//div[@id='wpbody-content']/div[3]/h2</td>
    <td></td>
    </tr>
    <tr>
    <td>clickAndWait</td>
    <td>link=Add New</td>
    <td></td>
    </tr>
    <tr>
    <td>type</td>
    <td>id=title</td>
    <td>MyPost</td>
    </tr>
    <tr>
    <td>clickAndWait</td>
    <td>id=publish</td>
    <td></td>
    </tr>
    <tr>
    <td>waitForElementPresent</td>
    <td>//div[@id='wpbody-content']/div[3]/h2</td>
    <td></td>
    </tr>
    </tbody></table>
    </body>
    </html>





    Third test case, save it with name 'testcase3.html'
    <?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 profile="http://selenium-ide.openqa.org/profiles/test-case">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="selenium.base" href="http://foundation.time2test.co.uk/" />
    <title>TestCase3</title>
    </head>
    <body>
    <table cellpadding="1" cellspacing="1" border="1">
    <thead>
    <tr><td rowspan="1" colspan="3">TestCase3</td></tr>
    </thead><tbody>
    <tr>
    <td>clickAndWait</td>
    <td>link=Users</td>
    <td></td>
    </tr>
    <tr>
    <td>assertTextPresent</td>
    <td>Users</td>
    <td></td>
    </tr>
    <tr>
    <td>clickAndWait</td>
    <td>//li[@id='menu-users']/div[3]/ul/li[2]/a</td>
    <td></td>
    </tr>
    <tr>
    <td>type</td>
    <td>id=user_login</td>
    <td>javascript{&quot;Test&quot; + new Date().getMilliseconds();}</td>
    </tr>
    <tr>
    <td>type</td>
    <td>id=email</td>
    <td>javascript{&quot;Test&quot; + new Date().getMilliseconds()+&quot;@gmail.com&quot;;}</td>
    </tr>
    <tr>
    <td>randomString</td>
    <td>10</td>
    <td>alphanumeric</td>
    </tr>
    <tr>
    <td>type</td>
    <td>id=first_name</td>
    <td>javascript{storedVars['alphanumeric'] }</td>
    </tr>
    <tr>
    <td>randomString</td>
    <td>10</td>
    <td>numeric</td>
    </tr>
    <tr>
    <td>type</td>
    <td>id=last_name</td>
    <td>javascript{&quot;Test&quot;+storedVars['alphanumeric'] }</td>
    </tr>
    <tr>
    <td>type</td>
    <td>id=url</td>
    <td>http://www.google.com/</td>
    </tr>
    <tr>
    <td>type</td>
    <td>id=pass1</td>
    <td>time2test</td>
    </tr>
    <tr>
    <td>type</td>
    <td>id=pass2</td>
    <td>time2test</td>
    </tr>
    <tr>
    <td>verifyTextPresent</td>
    <td>Users</td>
    <td></td>
    </tr>
    <tr>
    <td>captureEntirePageScreenshot</td>
    <td>C:Image.jpg</td>
    <td></td>
    </tr>
    <tr>
    <td>clickAndWait</td>
    <td>id=addusersub</td>
    <td></td>
    </tr>
    <tr>
    <td>clickAndWait</td>
    <td>link=Log Out</td>
    <td></td>
    </tr>
    </tbody></table>
    </body>
    </html>







    Here is the Random.js file which we need to store in IDE first
    Selenium.prototype.doRandomString = function( options, varName ) {
    
    var length = 8;
    var type   = 'alphanumeric';
    var o = options.split( '|' );
    for ( var i = 0 ; i < 2 ; i ++ ) {
    if ( o[i] && o[i].match( /^d+$/ ) )
    length = o[i];
    
    if ( o[i] && o[i].match( /^(?:alpha)?(?:numeric)?$/ ) )
    type = o[i];
    }
    
    switch( type ) {
    case 'alpha'        : storedVars[ varName ] = randomAlpha( length ); break;
    case 'numeric'      : storedVars[ varName ] = randomNumeric( length ); break;
    case 'alphanumeric' : storedVars[ varName ] = randomAlphaNumeric( length ); break;
    default             : storedVars[ varName ] = randomAlphaNumeric( length );
    };
    };
    
    function randomNumeric ( length ) {
    return generateRandomString( length, '0123456789'.split( '' ) );
    }
    
    function randomAlpha ( length ) {
    var alpha = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split( '' );
    return generateRandomString( length, alpha );
    }
    
    function randomAlphaNumeric ( length ) {
    var alphanumeric = '01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split( '' );
    return generateRandomString( length, alphanumeric );
    }
    
    function generateRandomString( length, chars ) {
    var string = '';
    for ( var i = 0 ; i < length ; i++ )
    string += chars[ Math.floor( Math.random() * chars.length ) ];
    return string;
    }
    Selenium.prototype.doRandomString = function( options, varName ) {
    
    var length = 8;
    var type   = 'alphanumeric';
    var o = options.split( '|' );
    for ( var i = 0 ; i < 2 ; i ++ ) {
    if ( o[i] && o[i].match( /^d+$/ ) )
    length = o[i];
    
    if ( o[i] && o[i].match( /^(?:alpha)?(?:numeric)?$/ ) )
    type = o[i];
    }
    
    switch( type ) {
    case 'alpha'        : storedVars[ varName ] = randomAlpha( length ); break;
    case 'numeric'      : storedVars[ varName ] = randomNumeric( length ); break;
    case 'alphanumeric' : storedVars[ varName ] = randomAlphaNumeric( length ); break;
    default             : storedVars[ varName ] = randomAlphaNumeric( length );
    };
    };
    
    function randomNumeric ( length ) {
    return generateRandomString( length, '0123456789'.split( '' ) );
    }
    
    function randomAlpha ( length ) {
    var alpha = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split( '' );
    return generateRandomString( length, alpha );
    }
    
    function randomAlphaNumeric ( length ) {
    var alphanumeric = '01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split( '' );
    return generateRandomString( length, alphanumeric );
    }
    
    function generateRandomString( length, chars ) {
    var string = '';
    for ( var i = 0 ; i < length ; i++ )
    string += chars[ Math.floor( Math.random() * chars.length ) ];
    return string;
    }
    




    Helpful Video File


    11 comments:

    1. I am not getting the purpose of this exercise, can you please explain?

      ReplyDelete
    2. viresh comments:
      1. in this blog entry , we want to demonstate the example located at My dropbox\vipin\helloWolrdExample.
      2. this example will be discussed in the course in detail.
      3. the example needs to be placed into this blog entry
      4. the example covers lots of things e.g. suite, javascript, asserts, verifys, e.t.c
      5. the exapmple needs to run against the vm ( so you need to modify the example)


      Aim of example:
      to cover in detail a first example and introducde the many different types of commands/code/structure e.t.c

      ReplyDelete
    3. this example needs to cover:
      screenshot capture?
      echo command?
      javascript in a command?
      variable usage?
      random value?
      suite
      test cases
      assert command
      verify command


      can you confirm the above? if not, then please modify

      ReplyDelete
    4. it's done, I have covered all the points into the attached HTML scripts.

      ReplyDelete
    5. Echo command does not appear to work

      ReplyDelete
    6. I have uploaded the created video on dailymotion and embedded it with the blog, please look into this and let me know if you still see the same problem

      ReplyDelete
    7. Checked this exercise and it seems there are issues to be rectified for the script "testcase3.html"

      ReplyDelete
    8. I have rectified the script "testcase3.html".

      ReplyDelete
    9. Add regular expression to this example

      ReplyDelete

    Thank you for your comment
    - Time2test Team