Monday, May 24, 2010

Selenium Framework

Index:
  1. Folder Structure.
  2. Creating the folder structure through eclipse.
  3. Creating Package and Java file.
  4. Create Build.xml file
  5. Create Testing.xml file.
  6. Set Build.Properties value
  7. Run the Test Suite.
  8. How it works?
1.    Folder Structure.

     The folder structure or contents of this framework is as follows:
    |----------SeleniumTestFramework
    |-------config
    |-------lib
    |-------test-src
    |-------build.xml
    |-------config.properties
    |-------test-output
    |-------build
  • The config directory contains testng.xml file, and is a place holder for configuration files.
  • The lib directory contains required jar files, and is a place holder for other library jars/zips.
  • The test directory contains test java files and is a place holder for other test files. Once the tests are run, build and test-output directories will be created.Test-output directory contains result files, and is generated by TestNG.
  • The build.properties is properties file.
  • The build.xml file is taking care of starting selenium server, compiling testcases and running the testsuite.
  • The build directory contains compiled classes, and is a volatile directory that is deleted by the target clean in build.xml file.
2. Creating the folder structure through eclipse.
     1.  Create a Java Project by name “SeleniumTestFramework” (Ex: New>Java Project)

     2.   Rename the src folder to “test-src”. This folder will contain all the java source file and packages.
     3.   Create a folder “config” (it contains all the configuration for your each test case) and “lib” (Copy all the jar file mentioned below to this folder)

     4.  Create a “Build.xml” file in base directory of the project. (Which is basically ANT file)
     5.  Create a “build.properties” file in base directory of the project. (Which contains the global variable declaration required for the test.
     6.  Create “testing.xml” file under “config” folder.(Which is basically a TestNG file).


     7.  Right Click on Project “SeleniumTestFramework” and select “Build Path>Configure Build Path”.Add jar file to the project as specified in below snapshot.

3. Creating Package and Java file.

1. Right Click on “Test-src” folder and select “New>Package”
2. Create a package by name “com.example.selenese.tests” as shown below:


3. Create a java file: Right click on Package created above and select “New>Class”.
4. Create a class by name “Demo” as shown below:


5. Replace the content of Demo.java with below content.  















4. Create Build.xml file
•    Replace the content of build.xml file with below content


















NOTE: For more info please visit http://ant.apache.org/manual/using.html

5.    Create Testing.xml file.
•    Replace the content of testing.xml file with below content





NOTE: For more info please visit http://testng.org/doc/documentation-main.html#testng-xml

6.    Set Build.Properties value





7.    Run the test suite.
•    Run the Project using ANT. OR Select Build.xml and Run this file using ANT Build
•    Eclipse will take care of starting selenium server and running each test case mentioned in testing.xml.
•    The Report will be generated in Project Base directory with name “test-output” as shown below.















8. How it works?
As a test suite starts in your favorite language, the following happens:
  1. The client/driver establishes a connection with the selenium-server.
  2. Selenium-Server launches a browser (or reuses an old one) with a URL that will load Selenium core web page.
  3. Selenium-Core gets the first instruction from the client/driver (via the HTTP Proxy built into the Selenium-RC Server).
  4. Selenim-Core acts on that first instruction, typically opening a page of the AUT.
  5. The web server is asked for that page, and it renders in the frame/window reserved for it.

How Selenium RC works?

Below image will give you overall view of selenium, its components and how they work together.(Please correct me if i am wrong)

Selenium RC Setup and Configuration

Index
  1. Introduction
  2. Download and Configure Selenium RC JAVA, TestNG and Eclipse
  3. Test the Setup.
  4. Conclusion
1.Introduction:  
Selenium RC 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
Although these tasks are not supported by Selenium directly, all of them can be achieved by using programming techniques with a language-specific Selenium-RC client library.  

Selenium RC Java client libraries Selenium RC Java client libraries communicate with the Server passing each Selenium command for execution. Then the server passes the Selenium command to the browser using Selenium-Core JavaScript commands. The browser, using its JavaScript interpreter, executes the Selenium command. This runs the Selenese action or verification you specified in your test script.  

Selenium Server Selenium Server receives Selenium commands from your test program, interprets them, and reports back to your program the results of running those tests.  

TestNG http://testng.org/doc/ TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionality that make it more powerful and easier to use, such as:
  • JDK 5 Annotations (JDK 1.4 is also supported with JavaDoc annotations).
  • Flexible test configuration.
  • Support for data-driven testing (with @DataProvider).
  • Support for parameters.
  • Allows distribution of tests on slave machines.
  • Powerful execution model (no more TestSuite).
  • Supported by a variety of tools and plug-ins (Eclipse, IDEA, Maven, etc...).
  • Embeds BeanShell for further flexibility.
  • Default JDK functions for runtime and logging (no dependencies).
  • Dependent methods for application server testing.
TestNG is designed to cover all categories of tests: unit, functional, end-to-end, integration, etc...  

Why use TestNG with Selenium RC ? Selenium IDE by itself is great for creating test cases and executing single tests. TestRunner is ok at running a test suite. But if you want full automation against different server and client platforms, you need a way to invoke the tests from a command line process, flexibility in how you create your test suites, and reports that tell you what happened. TestNG provides these features. You can use Selenium RC with other languages as well, but since Sakai is written in Java it makes sense to stick with the language most of us are familiar with already.  

Eclipse Eclipse is a multi-language software development environment comprising an integrated development environment (IDE) and an extensible plug-in system. It is written primarily in Java and can be used to develop applications in Java and, by means of the various plug-ins, in other languages as well, including C, C++, COBOL, Python, Perl, PHP, and others.  

ANT Ant is a Java library and command-line tool. Ant's mission is to drive processes described in build files as targets and extension points dependent upon each other. The main known usage of Ant is the build of Java applications. Ant supplies a number of built-in tasks allowing to compile, assemble, test and run Java applications. Ant can also be used effectively to build non Java applications, for instance C or C++ applications. More generally, Ant can be used to pilot any type of process which can be described in terms of targets and tasks. 
 
2. Download Files (Always download Latest version)  

1. Selenium RC Package. • Download Selenium RC http://seleniumhq.org/download/ • Unzip the download file. • Copy “selenium-java-client-driver-1.0.1” and “selenium-server-1.0.3” to a location in your local system. Ex: C:\MyWorks\Live_Selenium_Files\selenium-java-client-driver-1.0.1 C:\MyWorks\Live_Selenium_Files\selenium-server-1.0.3  
2. Download TestNG • Download TestNG from http://testng.org/download.html • Unzip the download file. Ex: C:\MyWorks\Live_Selenium_Files\testng-5.11 
3. Download Eclipse (Eclipse IDE for Java Developers) • Download eclipse from http://www.eclipse.org/downloads/ • Unzip the package. Ex: C:\MyWorks\eclipse-ganymede 
4. Download ANT • Download ANT from http://ant.apache.org/bindownload.cgi • Unzip the ANT. Ex: C:\ant 
 
Note: 
1. Make sure that you have Installed Java environment in your systems and classpath are configured respectively. 
2. Install TestNG plug-in for Eclipse 
• Open Eclipse. 
• Go to “Help>Software Updates…” and select “Available Software” tab. 
• Click on “Add site” and enter TestNG plugin download URL depending on your eclipse version. http://testng.org/doc/download.html Ex: http://beust.com/eclipse 
• Follow the instruction and complete the TestNG plugin installation. 
3. Make sure you have configured all the path (Classpath): Ex: 
• ANT_HOME C:\ant\;C:\ant\lib;C:\ant\bin;C:\ant\bin\ant.bat; 
• CLASSPATH %CLASSPATH%;C:\MyWorks\Live_Selenium_Files\junit-4.8.1.jar;C:\MyWorks\Live_Selenium_Files\selenium-java-client-driver-1.0.1\selenium-java-client-driver.jar;C:\MyWorks\Live_Selenium_Files\selenium-server-1.0.3\selenium-server.jar;C:\Program Files\Java\jdk1.6.0_10\bin;C:\Program Files\Java\jdk1.6.0_10\lib\tools.jar; 
• JAVA_HOME C:\Program Files\Java\jdk1.6.0_10\lib;C:\Program Files\Java\jdk1.6.0_10\bin;C:\Program Files\Java\jdk1.6.0_10;C:\Program Files\Java\jre6\bin;C:\Program Files\Java\jre6;C:\Program Files\Java\jdk1.6.0_10\bin\java.exe;C:\ant\;C:\ant\lib;C:\ant\bin;C:\ant\bin\ant.bat; 

3. Test the Setup 
1. Open command Prompt and Type “Java – version” Above screen shows the java is installed and Java classpath is set.  
2. Open command Prompt and Type “ant” Above screen shows the ANT is working fine.  
3. Open command Prompt • Navigate to selenium-server folder (Ex: C:\MyWorks\Live_Selenium_Files\selenium-server-1.0.3) • Type “java -jar selenium-server.jar” 4. Conculsion: By Using Eclispe IDE you can create Selenese script

Selenium Interview Questions

1) What does SIDE stand for?
Ans: Selenium Integrated Development Environment.

2) What is the difference between an assert and a verify with Selenium commands?
Ans: Assert: Will fail and abort the current test execution.Verify: Will fail and continue to run the test execution.

3) What Selenese commands can be used to help debug a regexp?
Ans:

4) What is one big difference between SilkTest and Selenium, excluding the price?
Ans: I am not sure about Silk Test. But general difference what i found from other Automation Testing tools is that, selenium does not work on Object Repository concept for identifying the objects in the application

5) Which browsers can Selenium IDE be run in?
Ans: FireFox

6) If a Selenium function requires a script argument, what would that argument look like in general terms?
Ans: StoreEval(script, variable) and storeExpression(expression, variableName)

7) If a Selenium function requires a pattern argument, what five prefixes might that argument have?
Ans: glob, regexp, exact, regexpi

8)    What is the regular expression sequence that loosely translates to "anything or nothing?"
Ans:     glob: Real* or regexp:Real.*

9)    What is the globbing sequence that loosely translates to "anything or nothing?
Ans:    *

10)    What does a character class for all alphabetic characters and digits look like in regular  expressions?
Ans:

POSIX
Perl
ASCII
Description
[:alnum:]

[A-Za-z0-9]
Alphanumeric characters
[:word:]
\w
[A-Za-z0-9_]
Alphanumeric characters plus "_"
[:alpha:]

[A-Za-z]
Alphabetic characters
[:digit:]
\d
[0-9]
Digits
[:lower:]

[a-z]
Lowercase letters
[:upper:]

[A-Z]
Uppercase letters








11)    What does a character class for all alphabetic characters and digits look like in globbing?
Ans:     [0-9] matches any digit
    [a-zA-Z0-9] matches any alphanumeric character
    [a-zA-Z] matches any alphabet character

12)    What must one set within SIDE in order to run a test from the beginning to a certain point within the test?
Ans:     Breakpoint (Keyword: b OR in SIDE, Right click and select “Toggle Breakpoint”)

13)    What does a right-pointing green triangle at the beginning of a command in SIDE indicate?
Ans:    Play entire Test suite

14)    How does one get rid of the right-pointing green triangle?
Ans:

15)    How can one add vertical white space between sections of a single test?
Ans:

16)    What Selenium functionality uses wildcards?
Ans:    

17)    Which wildcards does SIDE support?
Ans:     *, [ ]


NOTE: PLEASE  correct me if i am wrong

18)    What are the four types of regular expression quantifiers which we've studied?
Ans:

19)    What regular expression special character(s) means "any character?"
Ans:    “.*”

20)    What distinguishes between an absolute and relative URL in SIDE?
Ans:    Absolute URL: Its is base url and this represent domain address.
           Relative URL: (Absolute URL + Page Path).
          Open command uses Base URL (Absolute URL) to navigate web page.

21)    How would one access a Selenium variable named "count" from within a JavaScript snippet?
Ans:    ${count}

22)    What Selenese command can be used to display the value of a variable in the log file, which can  be very valuable for debugging?
Ans:    store | Chidambaram | var
          echo  | ${var}

23)    If one wanted to display the value of a variable named answer in the log file, what would the first argument to the previous command look like?
Ans:    echo | ${answer}

24)    Where did the name "Selenium" come from?
Ans:   

25)    Which Selenium command(s) simulates selecting a link?
Ans:    click, clickandWait, ClickAt, ClickAtandWait, DoubleClick, DoubleClickandWait, doubleClickAt, doubleClickAtandWait

26)    Which two commands can be used to check that an alert with a particular message popped up?
Ans:    The following commands are available within Selenium for processing Alerts:
•    getAlert()
•    assertAlert()
•    assertAlertNotPresent()
•    assertAlertPresent()
•    storeAlert()
•    storeAlertPresent()
•    verifyAlert()
•    verifyAlertNotPresent()
•    verifyAlertPresent()
•    waitForAlert()
•    waitForAlertNotPresent()
•    waitForAlertPresent()
The …AlertPresent() and …AlertNotPresent() functions check for the existence or not of an alert – regardless of it’s content.
The …Alert() functions allow the caller to specify a pattern which should be matched.
The getAlert() method also exists in Selenium RC, and returns the text from the previous Alert displayed.

27)    What does a comment look like in Column view?
Ans:     Pink color (Appears only in command column)

28)    What does a comment look like in Source view?
Ans:     Normal 0 false false false MicrosoftInternetExplorer4
Command
Target
Value
store
10
hits
storeXpathCount
//blockquote
blockquotes
storeEval
storedVars[‘hits’]-storedVars[‘blockquotes’]
paragraphs

This next example illustrates how a JavaScript snippet can include calls to methods, in this case the JavaScript String object’s toUpperCase method and toLowerCase method.

Command
Target
Value
store
Edith Wharton
name
storeEval
storedVars[‘name’].toUpperCase()
uc
storeEval
storedVars[‘name’].toLowerCase()
lc

JavaScript Usage with Non-Script Parameters
JavaScript can also be used to help generate values for parameters, even when the parameter is not specified to be of type script. However, in this case, special syntax is required–the JavaScript snippet must be enclosed inside curly braces and preceded by the label javascript, as in javascript {*yourCodeHere*}. Below is an example in which the type command’s second parameter value is generated via JavaScript code using this special syntax:

Command
Target
Value
store
league of nations
searchString
type
q
javascript{storedVars[‘searchString’].toUpperCase()}
    Ans is StoredVars[]

43)    How would one access the value of a SIDE variable named name from within a JavaScript snippet used as the argument to a Selenese command? 
Ans:    ${name}

44)    What is the name of the type of JavaScript entity represented by the last answer?
Ans:   

45)    What string(s) does this regular expression match? regexp:August|April 5, 1908
Ans:    August 5,1980 or April 5, 1980

46)    What Selenium regular expression pattern can be used instead of the glob below to produce the same results? verifyTextPresent | glob:9512?
Ans:    Glob uses two class pattern : * and []. I am not sure whether question is valid or not.

47)    What Selenium globbing pattern can be used instead of the regexp below to produce the same results? verifyTextPresent | regexp:Hush.*Charlotte
Ans:     glob: Hush*Charlotte

48)    I am using the selenium ide for testing my web application i recorded some script and played back while playing the recorded script the “alert msg is not getting closed”.
Ans:    If the alert message is in different browser you have to handle it after recording. You have to select that popup window and close it.Then transfer the control back to main window.
                 waitForPopUp | winId | 30000
                 selectWindow | winId
                 …
                 close
                 selectWindow

Thursday, May 20, 2010

Selenium... Element and Testing Tool!

Selenium Element
Selenium (pronounced /sɨˈleːniəm/ or pronounced /sɨˈliːniəm/ sə-LEE-nee-əm) is a chemical element with the atomic number 34, represented by the chemical symbol Se, an atomic mass of 78.96. It is a nonmetal, chemically related to sulfur and tellurium, and rarely occurs in its elemental state in nature.

Selenium is found in economic quantities in sulfide ores such as pyrite, partially replacing the sulfur in the ore matrix. Minerals that are selenide or selenate compounds are also known, but are rare. The chief commercial uses for selenium today are in glassmaking and in chemicals and pigments. Uses in electronics, once important, have been supplanted by silicon semiconductor devices.

For more info on selenium Element, click here

Selenium ... the OpenSource Testing Tool for web application.
Selenium is a portable software testing framework for web applications. Selenium provides a record/playback tool for authoring tests without learning a test scripting language. Selenium provides a test domain specific language (DSL) to write tests in a number of popular programming languages, including Java, Ruby, Groovy, Python, PHP, and Perl. Test playback is possible in most modern web browsers. Selenium deploys on Windows, Linux, and Macintosh platforms.

Selenium was originally developed by Jason Huggins, who was later joined by other programmers and testers at ThoughtWorks. It is open source software, released under the Apache 2.0 license and can be downloaded and used without charge. The latest side project is Selenium Grid, which provides a hub allowing the running of multiple Selenium tests concurrently on any number of local or remote systems, thus minimizing test execution time.

For more info on selenium Testing Tool, click here