Wednesday, January 13, 2010
Selenium Basic Presentation
Saturday, December 19, 2009
5 Minute Guide To Selenium IDE and Selenium Remote Control (Java) Test Tools
There are three variants of Selenium, which can be used in isolation or in combination to create complete automation test suite for your web applications.
* Selenium Core - In Selenium Core the tests scripts (written in HTML) and the Selenium Test Runner (written in Javascript) are uploaded to the same web server that hosts the application you are trying to test. It is a simpler form of Selenium, and suitable for non-developers, but it has some inherent limitations.
* Selenium IDE - Selenium IDE is a Firefox plugin, which includes the entire Selenium Core, allows you to record, play back, edit, and debug tests in browser. It provides the simplest introduction to Selenium and is highly recommended for beginners. You can save the tests / test suite created in xml or html format. However to run them in an automated fashion you need Selenium Remote Control which is described next.
* Selenium Remote Control - The Selenium Remote Control allows you to develop test cases and test suites in Java (supports JUnit & NGUnit), PHP, Ruby, Python, Perl and even .NET. It is the most flexible setup but requires some development knowledge to set up and use.
* Selenium Grid - Selenium Grid allows several Selenium Remote Control servers to be accessed in parallel by Selenium Grid server. This is extremely useful for automated load and stress testing of web applications.
Today we will discuss on how you can easily create automatic test scripts using Selenium IDE and convert them to JUnit tests (which uses Selenium Remote Control) which can be added to your JUnit based automatic regression test suite.
How to create a test plan in Selenium IDE
Creating a test plan in Selenium IDE is very easy, so we will use it to create few simple tests to begin with.
* Install Selenium IDE 0.8.7, a Firefox plugin.
* After installing Selenium please restart your Firefox browser for the plugin to be activated.
* Now you should see a new added menu item named Selenium IDE under your Firefox Tools menu.
* Open / browse the site for which you want to prepare a test case.
* Start Selenium IDE from Firefox Tools->Selenium IDE.
* Browse some pages.
* Now click red button to stop recording.
At this point you will see Selenium automatically recording your actions. Carefully note the commands, target and value. You can create and insert your own commands/ modify or even delete them. We will show some examples below. In the next section we will see how we can modify the generated tests to suit our needs.
How to create / modify / delete Selenium commands
The default commands generated by Selenium when you are browsing the page as a normal user should be modified to make the test more robust and to add test cases to it.
* Let's replace all click commands by clickAndWait. click simply clicks the specified link and goes on to execute the next command without waiting. On the other hand clickAndWait waits for the new page to loaded before executing the next command. clickAndWait should be used to make more robust test cases.
* Insert assertTextNotPresent command after each clickAndWait command to confirm a text must not be present in the browsed page.
* Use assertTextPresent command to confirm a text must be present in the browsed page.
* Finally to test your test plan please click green arrow button to play from the begining or to play from start point.
* Export the test plan as java file by Selenium IDE File->Export Test As->Java - Selenium RC (for example the file name is SeleniumSTSanityTest.java)
* Then close your Firefox Selenium ID.
How to run above test plan (automatically generated java file from Selenium IDE) in command line?
* Download Selenium RC.
* Unzip it under the same directory where SeleniumSTSanityTest.java (exported test plan as java file from Selenium ID) was saved.
* Install junit.
* Go to directory where you unzip selenium-remote-control-1.0-beta-1-dist.zip file.
* Open a terminal and do the steps below-
o cd selenium-remote-control-1.0-beta-1/selenium-server-1.0-beta-1
o java -jar selenium-server.jar (to run the server in interactive mode execute java -jar selenium-server.jar -interactive)
o If you get an error like Error: com.thoughtworks.selenium.SeleniumException: ERROR Server Exception: sessionId should not be null; has this session been started yet? then ensure that the browser is in the PATH before running the server. For example, you want to run the test in Firefox. Then you should do next two steps.
o locate firefox-bin (for example it returns /usr/lib/firefox-1.5.0.12/firefox-bin)
o export PATH=$PATH:/usr/lib/firefox-1.5.0.12/firefox-bin;
Note: There is an alternative way to fix above error (browser is not in path). Simply replace chrome with browser PATH in SeleniumSTSanityTest.java file. For example:
line
setUp("http://blog.taragana.com", "*chrome");
becomes
setUp("http://blog.taragana.com", "*firefox /usr/lib/firefox-1.5.0.12/firefox-bin");
in SeleniumSTSanityTest.java.
To run the test in opera browser replace chrome with opera.
Now the selenium server is running and you have to run the Java client located in selenium-remote-control-1.0-beta-1/selenium-java-client-driver-1.0-beta-1.
* Open another terminal.
o export CLASSPATH=.:selenium-remote-control-1.0-beta-1/selenium-java-client-driver-1.0-beta-1/selenium-java-client-driver.jar:/usr/share/java/junit.jar
o javac SeleniumSTSanityTest.java
o java SeleniumSTSanityTest
The automatically generated java file SeleniumSTSanityTest.java is likely to have some defects. Fix it by comparing with the example below:
import com.thoughtworks.selenium.*;
import junit.framework.*;
import java.util.regex.Pattern;
public class SeleniumSTSanityTest extends SeleneseTestCase {
public void setUp() throws Exception {
setUp("http://blog.taragana.com", "*chrome"); // to run the test in opera replace chrome with opera
}
public void testSimpleThoughts() throws Exception {
selenium.open("");
assertFalse(selenium.isTextPresent("WordPress database error: ["));
assertTrue(selenium.isTextPresent("2003-2008"));
selenium.open("/index.php/category/programming/java");
selenium.waitForPageToLoad("30000");
assertFalse(selenium.isTextPresent("WordPress database error: ["));
assertTrue(selenium.isTextPresent("2003-2008"));
selenium.click("//img[@alt='Übersetzen Sie zum Deutsch/German']");
selenium.waitForPageToLoad("30000");
assertFalse(selenium.isTextPresent("WordPress database error: ["));
assertTrue(selenium.isTextPresent("2003-"));
selenium.click("//img[@alt='Přeložit do Čech/Czech']");
selenium.waitForPageToLoad("60000");
assertFalse(selenium.isTextPresent("WordPress database error: ["));
assertTrue(selenium.isTextPresent("2003"));
}
public static Test suite() {
return new TestSuite(SeleniumSTSanityTest.class);
}
public static void main(String args[]) {
junit.textui.TestRunner.run(suite());
}
}
9 Important Tips for Selenium Remote Control (Java client) - Test Tool
1. To run the Java client you need to only have selenium-java-client-driver.jar (located in selenium-remote-control-1.0-beta-1/selenium-java-client-driver-1.0-beta-1 ) in your classpath.
2. The automatically generated java file (from Selenium Core) is likely to be defective. Ensure at least you have the following import statements:
import com.thoughtworks.selenium.*;
import junit.framework.*;
import java.util.regex.Pattern;
3. Ensure that the saved file name matches the generated class file name.
4. Remove the package statement or replace it with your own package statement. Initially just remove it.
5. Error: java.lang.UnsupportedOperationException: Catch body broken: IOException from cmd=setContext&1=SeleniumSTSanityTest.testSimpleThoughts -> java.net.ConnectException: Connection refused
Have you run the Selenium server?
Run the Selenium server like this:
java -jar selenium-server.jar
The selenium server is located in:
selenium-remote-control-1.0-beta-1/selenium-server-1.0-beta-1
6. Error: com.thoughtworks.selenium.SeleniumException: ERROR Server Exception: sessionId should not be null; has this session been started yet?
Ensure that the browser is in the PATH before running the server. On my linux box I did:
export PATH=$PATH:/usr/lib/firefox-2.0.0.6/
7. Error: com.thoughtworks.selenium.SeleniumException: Permission denied to get property Location.href
This happens on Firefox when a previous page wasn't fully loaded before the next page was invoked (due to timeout or click() was used). The solution is to use *chrome instead of *firefox in setup. I use for firefox:
setUp("http://blog.taragana.com/", "*chrome");
8. Timeout error
Increase the time in selenium.waitForPageToLoad() to 60000 (1 minute) or more.
9. How to run the generated java Test file?
This is really a JUnit question but in short you can add the following code in the generated file to get it running:
public static Test suite() {
return new TestSuite(SeleniumSTSanityTest.class);
}
public static void main(String args[]) {
junit.textui.TestRunner.run(suite());
}
Note: Replace SeleniumSTSanityTest with the name of your Java source file (without the .java extension)
Thursday, December 17, 2009
Client Libraries in Selenium
The client library takes a Selenese command and passes it to the Selenium Server for processing a specific action or test against the application under test (AUT). The client library also receives the result of that command and passes it back to your program. Your program can receive the result and store it into a program variable and reporting it as a success or failure, or possibly taking corrective action if it was an unexpected error.
So to create a test program, you simply write a program that runs a set of Selenium commands using a client library API. And, optionally, if you already have a Selenese test script created in the Selenium-IDE, you can generate the Selenium-RC code. The Selenium-IDE can translate (using its Export menu item) its Selenium commands into a client-driver’s API function calls. See the Selenium-IDE chapter for specifics on exporting RC code from Selenium-IDE.
Imp Question--- "The RC server also bundles Selenium Core, and automatically loads it into the browser."
Selenium Core (Core) is a DHTML test execution framework.
It is the engine of both, Selenium IDE and Selenium RC (driven mode), but it also can be deployed on the desired application server.
Selenium Interview Questions - Part 2
the command you are using
2. looping/entering multiple data is possible in IDE/RC
3. The process followed to test in Selenium RC
4. Difference between IDE and RC?
5. What is selenium Core. How can we use that.
6. which flavor of selenium you are using
7. Diff between IDE,RC., and flavours of Selenium
8. Do you know about Selenium core?
9. Is selenium core pre loaded in RC?
10. What type of frame work you are using?
11. How u will prepare test report based on the selenium
12. How you will run test suite with java?
13. How you use selenium to prepare test the application?
14. Do you have heard about the Default selenium class?
15. What is the test engine you use to test the RC?
How Selenium recognises the objects
Locating Elements
For many Selenium commands a target is required. This target identifies an element in the content of the web application, and consists of the location strategy followed by the location in the format locatorType=location. The locator type can be omitted and one of the default strategies will be used depending on the initial characters of the location. The various locator types are explained below with examples for each.
Locating by identifier
This is probably the most common method of locating elements and is the catch- all default when no recognised locator type is used. With this strategy the element with the @id attribute value matching the location will found. If no element has a matching @id attribute then the first element with a @name attribute matching the location will be found.
- identifier=loginForm
- identifier=username
- identifier=continue
For instance, your page source could have identifier (ID) and name attributes as follows:
id="loginForm"> name="username" type="text" />
name="password" type="password" />
name="continue" type="submit" value="Login" />
name="continue" type="button" value="Continue" />
Locating by id
More limited than the identifier locator type but also more explicit. Use this when you know an element’s @id attribute.
- id=loginForm
id="loginForm">
name="username" type="text" />
name="password" type="password" />
name="continue" type="submit" value="Login" />
name="continue" type="button" value="Continue" />
Locating by name
Similar to the identifier locator type when an @id attribute is not found, the name locator type will locate the first element with a matching @name attribute. If multiple elements have the same value for a name attribute then you can use filters to further refine your location strategy. The default filter type is value (matching the @value attribute).
- name=username
- name=continue
- name=continue Continue
- name=continue value=Continue
- name=continue type=button
id="loginForm">
name="username" type="text" />
name="password" type="password" />
name="continue" type="submit" value="Login" />
name="continue" type="button" value="Continue" />
Locating by XPath
XPath is the language used for locating nodes in an XML document. As HTML can be an implementation of XML (XHTML) Selenium users can leverage this powerful language to target elements in their web applications. XPath extends beyond ( as well as supporting) the simple methods of locating by @id or @name attributes, and opens up all sorts of new possibilities such as locating the third checkbox on the page or similar.
One of the main reasons for using XPath is when you don’t have a suitable @id or @name attribute for the element you wish to locate. You can use XPath to either locate the element in absolute terms (not advised), or relative to an element that does have an @id or @name attribute.
Absolute XPaths contain the location of all elements from the root (html) and as a result are likely to fail with only the slightest adjustment to the application. By finding a nearby element with an @id or @name attribute (ideally a parent element) you can locate your target element based on the relationship. This is much less likely to change and can make your tests more robust.
- xpath=/html/body/form[1] - Absolute path (would break if the HTML was changed only slightly)
- xpath=//form[1] - First form element in the HTML
- xpath=//form[@id=’loginForm’] - The form element with @id of ‘loginForm’
- xpath=//form[input/@name=’username’] - First form element with an input child element with @name of ‘username’
- xpath=//input[@name=’username’] - First input element with @name of ‘username’
- xpath=//form[@id=’loginForm’]/input[1] - First input child element of the form element with @id of ‘loginForm’
- xpath=//input[@name=’continue’][@type=’button’] - Input with @name ‘continue’ and @type of ‘button’
- xpath=//form[@id=’loginForm’]/input[4] - Fourth input child element of the form element with @id of ‘loginForm’
id="loginForm">
name="username" type="text" />
name="password" type="password" />
name="continue" type="submit" value="Login" />
name="continue" type="button" value="Continue" />
There are also a couple of very useful Firefox Add-ons that can assist in discovering the XPath of an element:
- XPath Checker - suggests XPath and can be used to test XPath results.
- Firebug - very useful, XPath suggestions are just one of the many powerful features of this add-on.
Locating hyperlinks by link text
This is a simple method of locating a hyperlink in your web page by using the text of the link. If two links with the same text are present then the first match will be used.
- link=Continue
- link=Cancel
Are you sure you want to do this?
href="continue.html">Continue
href="cancel.html">Cancel
Locating by DOM
The Document Object Model represents a HTML document and can be accessed using JavaScript. This location strategy takes JavaScript that evaluates to an element on the page, which can be simply the element’s location using the hierarchical dotted notation.
- dom=document.getElementById(‘loginForm’)
- dom=document.forms[‘loginForm’]
- dom=document.forms[0]
- dom=document.forms[0].username
- dom=document.forms[0].elements[‘username’]
- dom=document.forms[0].elements[0]
- dom=document.forms[0].elements[3]
id="loginForm">
name="username" type="text" />
name="password" type="password" />
name="continue" type="submit" value="Login" />
name="continue" type="button" value="Continue" />
You can use Selenium itself as well as other sites and extensions to explore the DOM of your web application. A good reference exists on W3Schools.
Locating by CSS
The “AndWait” commands
The difference that any user should see between a command and it’s AndWaitclick) will do the action and continue with the following command as fast as it can. While the AndWaitclickAndWait) tells Selenium to wait for the page to load after the action has been done. alternative is that the regular command (e.g. alternative (e.g.
The andWait alternative is always used when an action causes the browser to navigate to another page or reload the present one.
Selenium Interview Questions - Part 1
• What is the difference between an assert and a verify with Selenium commands?
• What Selenese commands can be used to help debug a regexp?
• What is one big difference between SilkTest and Selenium, excluding the price?
• Which browsers can Selenium IDE be run in?
• If a Selenium function requires a script argument, what would that argument look like in general terms?
• If a Selenium function requires a pattern argument, what five prefixes might that argument have?
• What is the regular expression sequence that loosely translates to "anything or nothing?"
• What is the globbing sequence that loosely translates to "anything or nothing?
• What does a character class for all alphabetic characters and digits look like in regular expressions?
• What does a character class for all alphabetic characters and digits look like in globbing?
• What must one set within SIDE in order to run a test from the beginning to a certain point within the test?
• What does a right-pointing green triangle at the beginning of a command in SIDE indicate?
• How does one get rid of the right-pointing green triangle?
• How can one add vertical white space between sections of a single test?
• What Selenium functionality uses wildcards?
• Which wildcards does SIDE support?
• What are the four types of regular expression quantifiers which we've studied?
• What regular expression special character(s) means "any character?"
• What distinguishes between an absolute and relative URL in SIDE?
• How would one access a Selenium variable named "count" from within a JavaScript snippet?
• What Selenese command can be used to display the value of a variable in the log file, which can be very valuable for debugging?
• 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?
• Where did the name "Selenium" come from?
• Which Selenium command(s) simulates selecting a link?
• Which two commands can be used to check that an alert with a particular message popped up?
• What does a comment look like in Column view?
• What does a comment look like in Source view?
• What are Selenium tests normally named (as displayed at the top of each test when viewed from within a browser)?
• What command simulates selecting the browser's Back button?
• If the Test Case frame contains several test cases, how can one execute just the selected one of those test cases?
• What globbing functionality is NOT supported by SIDE?
• What is wrong with this character class range? [A-z]
• What are four ways of specifying an uppercase or lowercase M in a Selenese pattern?
• What does this regular expression match?
• What are two ways to match an asterisk within a Selenese regexp?
• What is the generic name for an argument (to a Selenese command) which starts with //?
• What Selenese command is used to choose an item from a list?
• How many matches exist for this pattern?
• What is the oddity associated with testing an alert?
• How can one get SIDE to always record an absolute URL for the open command's argument?
• What Selenese command and argument can be used to transfer the value of a JavaScript variable into a SIDE variable?
• 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?
• What is the name of the type of JavaScript entity represented by the last answer?
• What string(s) does this regular expression match?
• What Selenium regular expression pattern can be used instead of the glob below to produce the same results?
• What Selenium globbing pattern can be used instead of the regexp below to produce the same results?