Wednesday, 9 March 2011

Exercise 13 - Advanced JavaScript function

Aim:
In the given Selenium Script that adds a new user to the wordpress.com, replace the text with the advanced javascript provided.

Difficulty level: Easy

What you will learn?
  • Javascript function usage
  • random variables

Tips:
  • run the script first to appreciate what is required and identify the steps where the javascript function will be used.
Pre-requisites:
Exercise Steps:
1. use the existing test case below in the IDE
2. Run the script without any modifications
3. copy and paste the javascript function, provided below
4. re-run the script


Conclusions

  • example shows how to use complex javascript to improve the test script

Related Entries

Zip file location: ( download from here)
C:\FoundationSelenium\Ex13\ExerciseScript.html, C:\FoundationSelenium\Ex13\AdvancedExerciseScript.html, C:\FoundationSelenium\Ex13\AdvancedJavascriptFunction.txt.


Solution:

Here is the screen of HTML script:




















Here is the script in text format:

<?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>ExerciseScriptt</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">ExerciseScript</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>type</td>
<td>id=user_pass</td>
<td>time4test</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>
<tr>
<td>clickAndWait</td>
<td>link=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>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;Testuser&quot; + new Date().getMilliseconds();}</td>
</tr>
<tr>
<td>type</td>
<td>id=email</td>
<td>javascript{&quot;Testemail&quot; + new Date().getMilliseconds()+&quot;@gmail.com&quot;;}</td>
</tr>
<tr>
<td>type</td>
<td>id=first_name</td>
<td>TestFirst</td>
</tr>
<tr>
<td>type</td>
<td>id=last_name</td>
<td>TestLast</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>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 java script code used into the file javascript_text.txt:
-------------------------------------------------------------------

javascript{var d=new Date();var mymonth=new Array(12);mymonth[0]='jan';mymonth[1]='feb';mymonth[2]='mar';mymonth[3]='apr';mymonth[4]='may';mymonth[5]='jun';mymonth[6]='jul';mymonth[7]='aug';mymonth[8]='sep';mymonth[9]='oct';mymonth[10]='nov';mymonth[11]='dec'; [d.getDate()] + mymonth[d.getMonth()] + d.getFullYear()+ '-' + d.getHours() + d.getMinutes() + d.getSeconds() + '@gmail.com';}




-------------------------------------------------------------------
'After' script with the Java Script

<?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>AdvancedExerciseScript</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">AdvancedExerciseScript</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>type</td>
<td>id=user_pass</td>
<td>time4test</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>
<tr>
<td>clickAndWait</td>
<td>link=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=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{var d=new Date();var mymonth=new Array(12);mymonth[0]='jan';mymonth[1]='feb';mymonth[2]='mar';mymonth[3]='apr';mymonth[4]='may';mymonth[5]='jun';mymonth[6]='jul';mymonth[7]='aug';mymonth[8]='sep';mymonth[9]='oct';mymonth[10]='nov';mymonth[11]='dec'; [d.getDate()] + mymonth[d.getMonth()] + d.getFullYear()+ '-' + d.getHours() + d.getMinutes() + d.getSeconds() + 'username';}</td>
</tr>
<tr>
<td>type</td>
<td>id=email</td>
<td>javascript{var d=new Date();var mymonth=new Array(12);mymonth[0]='jan';mymonth[1]='feb';mymonth[2]='mar';mymonth[3]='apr';mymonth[4]='may';mymonth[5]='jun';mymonth[6]='jul';mymonth[7]='aug';mymonth[8]='sep';mymonth[9]='oct';mymonth[10]='nov';mymonth[11]='dec'; [d.getDate()] + mymonth[d.getMonth()] + d.getFullYear()+ '-' + d.getHours() + d.getMinutes() + d.getSeconds() + '@gmail.com';}</td>
</tr>
<tr>
<td>type</td>
<td>id=first_name</td>
<td>TestFirst</td>
</tr>
<tr>
<td>type</td>
<td>id=last_name</td>
<td>TestLast</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>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>



Helpful video file


9 comments:

  1. notes
    show the before script html
    show the after script html
    video needs to reflect this

    ReplyDelete
  2. Can you please explain this in detail as I am not getting exact work around for same?

    ReplyDelete
  3. viresh>>the aim of the exercise is to use the more advanced javascript function via copy and paste. So, the script before should not have any javascript function and then the steps are to create modify the script to add the command with the advanced javascript function. So, the video should reflect this. Also, the user should be able to copy and paste the original script from the blog and also the advanced javascript function and then also the final script with modifications.

    ReplyDelete
  4. I have made changes, can you please review and let me know if still you see any issue.

    ReplyDelete
  5. Notes:
    you still need to put the "after" script with the javascript.

    ReplyDelete
  6. notes for viresh/vipin:
    make this into a proper exercise with aim and steps. At the moment it does not read like an exercise.

    ReplyDelete
  7. Hi Viresh, I see the aim and other details in the solution, so it should be done.

    ReplyDelete
  8. hi don't think this video is loading properly.

    ReplyDelete
  9. What is the problem jgeorge? I have tried it on different machines and video loads up just fine.

    ReplyDelete

Thank you for your comment
- Time2test Team