Thursday, December 23, 2010

Selenium Fitnesse First Script

Follow the below Steps to get create your script.


1.       Start Selenium and  Fitnesse servers by clicking respective Selenium.vbs and Fitnesse.vbs files
2.       It opens two cmd windows which shows.
3.       Go to http://localhost:8000/ and click on Edit button on Top left corner of the frontPage
4.       Paste the following code at the end of the existing data and click on save button

//*** this code is for creating text and link for the test case ***//

| !c '''Test Suite''' |
| !c !style_pass([[Web Test Suites][FitNesse.SuiteWebTests]])| !style_pass(''Simple Test Case'')|
Or simply paste the following if we don’t need any tabular format.
|[[Web Test Suites][FitNesse.SuiteWebTests]]|FitNesse's suite of Web Tests|

5.       On clicking save button it shows the page with tabular format of web test suite, click on that link, again click on Edit button and paste the following code.

//*** this code is for declaration of variables and functions needed for testing***//

!*> Class Path & Global Defines
!path C:\Program Files\Fitnesse\selenium-java-client-driver.jar     
!path C:\Program Files\Fitnesse \genericfixture.jar                
!define IE {*piiexplore}                                                          
!define FF {*pifirefox}                                                
!define sel {selenium=}
!define ds {com.thoughtworks.selenium.DefaultSelenium}
!define host {localhost}
!define port {4444}
!define COLLAPSE_SETUP {true}                                    
!define COLLAPSE_TEARDOWN {true}
*!
----
SuiteWebTests.SetUp                      
SuiteWebTests.TearDown                
SuiteWebTests.GoogleTest


6.       Click on SuiteWebTests.SetUp ? link and copy paste the following code

//** this code is for declaration of the page related predefined variables and methods***//
!| DSL Adapter |
| user types | % | into | % | field | type | {2}, {1} |
| user starts the browser | start | |
| users opens the URL | % | open | {1} |
| page has the title | getTitle | |
| user sees the text on the page | % | isTextPresent | {1} |
| page has an element named | % | isElementPresent | {1} |
| page loads in less than | % | seconds | waitForPageToLoad | {1}000 |
| page has URL | getLocation | |
| user clicks on submit button | click | //button[@type='submit'] |
| user clicks on the link named | % | click | link={1} |
| user clicks on the button named | % | clickAt | {1},"" |
| get text from element named | % | getText | xpath=id('{1}') |
| user stops the browser | stop | |
| add a key-value with key | % | and value | % | put | {1}, {2} |
| get value for the key | % | get | {1} |
| show | toString | |

!|Generic Fixture|${sel}${ds}|${host}|${port}|${FF}|http://${host}|
| user starts the browser |


7.       Go to SuiteWebTests.TearDown link and paste the following code and click on save

!| Generic Fixture | ${sel} |
| user stops the browser |


8.       Go to SuiteWebTests.GoogleTests link and paste the following code and click on save

//** this code is for declaration of the page related methods***//
!define q {fitnesse generic fixture}
!| Generic Fixture | ${sel} |
| users opens the URL | http://www.google.com |
| page has the title | | Google |
| page has an element named | btnG | | true |
| user types | ${q} | into | q | field |
| user clicks on the button named | btnG |
| page loads in less than | 5 | seconds |
| page has the title | | ${q} - Google Search |
| user clicks on the link named | Next |
| page loads in less than | 5 | seconds |
| user clicks on the link named | Next |
| page loads in less than | 5 | seconds |
| user sees the text on the page | Results 21 - 30 | | true |

Thursday, December 16, 2010

FitNesse with Selenium using Generic Fixture

Selenium-RC is the solution for tests that need more than simple browser actions and linear execution. Selenium-RC uses the full power of programming languages to create more complex tests like reading and writing files, querying a database, and emailing test results.
You’ll want to use Selenium-RC whenever your test requires logic not supported by Selenium-IDE. What logic could this be? For example, Selenium-IDE does not directly support


  • condition statements
  • iteration
  • logging and reporting of test results
  • error handling, particularly unexpected errors
  • database testing
  • test case grouping
  • re-execution of failed tests
  • test case dependency
  • screenshot capture of test failures

FitNesse with Selenium using Generic Fixture

You can download latest fitnesse.jar file from http://fitnesse.org/FitNesseDevelopment.DownLoad 
And then Create a folder under C:/Program Files/Fitnesse/
Copy fitnesse.jar file and paste into C:/Program Files/Fitnesse/    folder.
Double click on " fitnesse.jar " file it will install itself.

Download Selenium Remote Control

Selenium-RC can be downloaded from http://seleniumhq.org/download/
Unzip the downloaded Selenium-RC into C:/Porgram Files/Selenium/ folder.


Create a start script for Selenium and Fitnesse


Open text file and Copy the following code into it and save the text file as Selenium.vbs

Function IsProcessRunning( strServer, strProcess )
   Dim Process, strObject
   IsProcessRunning = False
   strObject   = "winmgmts://" & strServer
   For Each Process in GetObject( strObject ).InstancesOf( "win32_process" )
       If UCase( Process.name ) = UCase( strProcess ) Then
           IsProcessRunning = True
           Exit Function
       End If
   Next
End Function


sServer = "."      ' "." for local machine
Set objShell = WScript.CreateObject("WScript.Shell")
Set objEnv = objShell.Environment("Process")



Selenium = "java -jar selenium-server.jar -browserSessionReuse -proxyInjectionMode"
If( IsProcessRunning( sServer, Selenium ) = False ) Then
   objshell.CurrentDirectory =_
"C:\Program Files\Selenium\selenium_1_0_3\selenium-server-1.0.3"
   result = objshell.Run  ( Selenium, 5, true )
End If

Wscript.Quit




Open text file and Copy the following code into it and save the text file as Fitnesse.vbs


Function IsProcessRunning( strServer, strProcess )
   Dim Process, strObject
   IsProcessRunning = False
   strObject   = "winmgmts://" & strServer
   For Each Process in GetObject( strObject ).InstancesOf( "win32_process" )
       If UCase( Process.name ) = UCase( strProcess ) Then
           IsProcessRunning = True
           Exit Function
       End If
   Next
End Function


sServer = "."      ' "." for local machine
Set objShell = WScript.CreateObject("WScript.Shell")
Set objEnv = objShell.Environment("Process")

FitNesse = "java -cp fitnesse.jar fitnesse.FitNesse -p 8000"
If( IsProcessRunning( sServer, FitNesse ) = False ) Then
   objshell.CurrentDirectory  = "C:\Program Files\Fitnesse"
   f = objshell.Run  ( FitNesse, 6, false )
End If

Wscript.Quit




Download and Install Generic Fixture


You must have FitNesse framework installed on your computer and FitNesse server must be running on the computer where Generic Fixture is being installed.

You can download 

Download the jar file genericfixture.jar containing full source code and compiled classes from Generic Fixture Sourceforge Site. Included classes have been generated using Sun’s JDK 1.5 Java compiler. Should you need to build Generic Fixture with any other JVM then make sure to put fitnesse.jar and fitlibrary.jar (provided in all the Fitnesse releases) in your CLASSPATH.

Install the Generic Fixture

Save genericfixture.jar somewhere on your computer where FitNesse server is running. Then simply add genericfixture.jar in your classpath on FitNesse Wiki. eg:
!path /path/to/download/dir/genericfixture.jar

Run startup script


Run the startup scripts Selenium.vbs and Fitnesse.vbs files which will start the both the servers.


Now you are about to run your own fitnesse scripts.......



Selenium IDE Useful Commands

Selenium IDE is an integrated development environment for Selenium scripts. It is implemented as a Firefox extension.
Selenium IDE allows you to record, edit, and debug tests. 


Selenium IDE can be downloaded from http://seleniumhq.org/download/ 

Recording any action on a page will be done using commands known as Selenese Commands.

Most common Commands used as follows:

verifyTextPresent  
      Verifies that text appears on the page.  It can be dynamic text that was loaded with ajax.  If it’s on the page in the active window at the time this command is called, it’ll find it.

verifyValue
          Verifies the value of an input field.  Provide the id of the input as the target and the value is the text you’re expecting.

click 
         Tells the test to click something on the page.  The target can be referenced with the element ID or xPath as with the other commands.

clickAndWait
      This command is useful for form submissions.  Many times when you’re in record mode and click a submit button, selenium will only record a “click” command.  This will break your test commands following the form submission where you verify the presence of a success message or form values because the next page hasn’t loaded yet.  The “wait” portion will wait to continue the testing when the next page has fully loaded. 

select
      select an option in a select box by providing the id of the select box and the option value.
   
verifyAttribute
           Useful for checking the attributes (attributes such as class, style, href and anchor tags)   for specific url.
           To verifyAttribute first thing you need to do is viewXpath by right clicking on particular attribute.

            Xpath catcher is an add-on for firefox to viewXpath. You can download Xpath catcher from https://addons.mozilla.org/en-US/firefox/addon/1095/?id=1095 


Steps to find Xpath
        1.Right click on attribute and select viewXpath 
        2.Copy the Xpath in target field of the Selenium IDE
        3.Edit the xpath copied as follows
                     For example you have xpath as  id('p_13872472-main_story')/x:div/x:div/x:h3/x:a
                    You have to edit in the following manner for verifying class attribute
                     //div[@id='p_13872472-main_story']/div/div/h3/a/@class