Saturday, December 19, 2009

5 Minute Guide To Selenium IDE and Selenium Remote Control (Java) Test Tools

Selenium is a high quality open source test automation tool for web application testing. Selenium runs in Internet Explorer, Mozilla and Firefox on Windows, Linux, and Macintosh, Safari on the Mac, with plans to support Safari on iPhone soon. Selenium test scripts are portable, can be run from browsers (using Selenium IDE) or from JUNit or NGUnit (using Selenium RC) etc.. For example, test scripts written using Selenium IDE in Firefox on Windows can run on Firefox in Mac or Linux, without changing any code. Selenium tests run directly in browsers and so matches the end-user experience closely.

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

Selenium Remote Control (RC) is a test tool that allows you to write automated web application UI tests in many programming languages against any HTTP website using any mainstream JavaScript-enabled browser. Selenium RC is a powerful and simple framework for running (scheduled or manually) automated UI centric regression tests for web applications / services. Here are few simple tips for Selenium RC java client. I used JUnit for tests. You can use NGUnit too.

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 libraries provide the programming support that allows you to run Selenium commands from a program of your own design. There is a different client library for each supported language. A Selenium client library provides a programming interface (API), i.e., a set of functions, which run Selenium commands from your own program. Within each interface, there is a programming function that supports each Selenese command.

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

1. Is it possible to connect to db using Selenium, how you are doing and what is
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 does SIDE stand for?
• 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?

Selenium Interview Questions with answers

Q1. What is Selenium?

Ans. Selenium is a set of tools that supports rapid development of test automation scripts for web based applications

. Selenium testing tools provides a rich set of testing functions specifically designed to fulfill needs of testing of a web based application.

Q2. What are the main components of Selenium testing tools?
Ans. Selenium IDE, Selenium RC and Selenium Grid

Q3. What is Selenium IDE?
Ans.
Selenium IDE is for building Selenium test cases. It operates as a Mozilla Firefox add on and provides an easy to use interface for developing and running individual test cases or entire test suites. Selenium-IDE has a recording feature, which will keep account of user actions as they are performed and store them as a reusable script to play back.

Q4. What is the use of context menu in Selenium IDE?
Ans. It allows the user to pick from a list of assertions and verifications for the selected location.

Q5. Can tests recorded using Selenium IDE be run in other browsers?
Ans.
Yes. Although Selenium IDE is a Firefox add on, however, tests created in it can also be run in other browsers by using Selenium RC (Selenium Remote Control) and specifying the name of the test suite in command line.

Q6. What are the advantage and features of Selenium IDE?

Ans. 1. Intelligent field selection will use IDs, names, or XPath as needed
2. It is a record & playback tool and the script format can be written in various languages including C#, Java, PERL, Python, PHP, HTML
3. Auto complete for all common Selenium commands
4. Debug and set breakpoints
5. Option to automatically assert the title of every page
6. Support for Selenium user-extensions.js file

Q7. What are the disadvantage of Selenium IDE tool?
Ans.
1. Selenium IDE tool can only be used in Mozilla Firefox browser.
2. It is not playing multiple
windows when we record it.

Q8. What is Selenium RC (Remote Control)?
Ans.
Selenium RC allows the test automation expert to use a programming language for maximum flexibility and extensibility in developing test logic. For example, if the application under test returns a result set and the automated test program needs to run tests on each element in the result set, the iteration / loop support of programming language’s can be used to iterate through the result set, calling Selenium commands to run tests on each item. Selenium RC provides an API and library for each of its supported languages. This ability to use Selenium RC with a high level programming language to develop test cases also allows the automated testing to be integrated with the project’s automated build environment.

Q9. What is Selenium Grid?

Ans. Selenium Grid in the selenium testing suit allows the Selenium RC solution to scale for test suites that must be run in multiple environments. Selenium Grid can be used to run multiple instances of Selenium RC on various operating system and browser configurations.

Q10. How Selenium Grid works?

Ans. Selenium Grid sent the tests to the hub. Then tests are redirected to an available Selenium RC, which launch the browser and run the test. Thus, it allows for running tests in parallel with the entire test suite.

Q 11. What you say about the flexibility of Selenium test suite?
Ans.
Selenium testing suite is highly flexible. There are multiple ways to add functionality to Selenium framework to customize test automation. As compared to other test automation tools, it is Selenium’s strongest characteristic. Selenium Remote Control support for multiple programming and scripting languages allows the test automation engineer to build any logic they need into their automated testing and to use a preferred programming or scripting language of one’s choice.

Also, the Selenium testing suite is an open source project where code can be modified and enhancements can be submitted for contribution.

Q12. What test can Selenium do?
Ans.
Selenium is basically used for the functional testing of web based applications. It can be used for testing in the continuous integration environment. It is also useful for agile testing

Q13. What is the cost of Selenium test suite?
Ans.
Selenium test suite a set of open source software tool, it is free of cost.

Q14. What browsers are supported by Selenium Remote Control?
Ans.
The test automation expert can use Firefox, IE 7/8, Safari and Opera browsers to run tests in Selenium Remote Control.

Q15. What programming languages can you use in Selenium RC?
Ans.
C#, Java, Perl, PHP, Python, Ruby

Q16. What are the advantages and disadvantages of using Selenium as testing tool?
Ans.
Advantages: Free, Simple and powerful DOM (document object model) level testing, can be used for continuous integration; great fit with Agile projects.

Disadvantages: Tricky setup; dreary errors diagnosis; can not test client server applications.

Q17. What is difference between QTP and Selenium?
Ans.
Only web applications can be testing using Selenium testing suite. However, QTP can be used for testing client server applications. Selenium supports following web browsers: Internet Explorer,

Firefox, Safari, Opera or Konqueror on Windows, Mac OS X and Linux. However, QTP is limited to Internet Explorer on Windows.

QTP uses scripting language implemented on top of VB Script. However, Selenium test suite has the flexibility to use many languages like Java, .Net, Perl, PHP, Python, and Ruby.

Q18. What is difference between Borland Silk test and Selenium?
Ans.
Selenium is completely free test automation tool, while Silk Test is not. Only web applications can be testing using Selenium testing suite. However, Silk Test can be used for testing client server applications. Selenium supports following web browsers: Internet Explorer, Firefox, Safari, Opera or Konqueror on Windows, Mac OS X and Linux. However, Silk Test is limited to Internet Explorer and Firefox.

Silk Test uses 4Test scripting language. However, Selenium test suite has the flexibility to use many languages like Java, .Net, Perl, PHP, Python, and Ruby.

Cross Browser Testing using Selenium

1. Record the entire script and then stop it.
2. Either you can go to File --> Export Test Case as -->Select your preferred language (here i'll take an example of java i.e. Java (Junit) - Selenium RC )
or
After recording the script just go to Options --> Format --> Java (Junit) - Selenium RC then copy the entire script
3. Then open your IDE for ex: Eclipse and then paste the script
4. then you can start the server which comes with the Selenium server
Go to Selenium-server folder using command prompt and then use the command java - jar selenium-server.jar
then the server will start
5. Now in eclipse you can run the script by default it will run in firefox

if you want to run in different browser then you have to change *chrome in the below mentioned code in you script to iexplore for running it in Internet Explorer

setUp("http://change-this-to-the-site-you-are-testing/", "*chrome");


Below have mentioned the codes for different browsers

firefoxproxy = FirefoxCustomProfileLauncher
firefox = FirefoxLauncher
chrome = FirefoxChromeLauncher
firefoxchrome = FirefoxChromeLauncher
firefox2 = Firefox2Launcher
firefox3 = Firefox3Launcher
iexploreproxy = InternetExplorerCustomProxyLauncher
safari = SafariLauncher
safariproxy = SafariCustomProfileLauncher
iehta = HTABrowserLauncher
iexplore = InternetExplorerLauncher
opera = OperaCustomProfileLauncher
piiexplore = ProxyInjectionInternetExplorerCustomProxyLauncher
pifirefox = ProxyInjectionFirefoxCustomProfileLauncher
// DGF pisafari isn't working yet
//pisafari = ProxyInjectionSafariCustomProfileLauncher
konqueror = KonquerorLauncher
mock = MockBrowserLauncher
googlechrome = GoogleChromeLauncher



then your test will launch the browser and starts running.......in the browser you have chosen

Selenium Important Stuffs

1. Some of our UI fields are automatically filled in by the system based on Javascript. But these things don't work in our Selenium test case. What to do?
When your page has onblur events to populate some of the data, it may not be triggered by Selenium's type command. In such cases, the blur event should be manually fired in our test case as follows

selenium.fireEvent("fieldname","blur");



2. To check the values displayed in the drop down field

selenium.isTextPresent("Values displayed in the field");



3. To check the values displayed in drop down coming from tables

List x = Methods();
StringBuilder stringbringprofilo_fisico = new StringBuilder( "default value");
for (int i = 0; i < class="code-object">String
element = (String) x.get(i);
stringbringprofilo_fisico.append(" ");
stringbringprofilo_fisico.append(element);
}
String profilo_fisico = stringbringprofilo_fisico.toString();
System.out.println(profilo_fisico);
assertTrue(selenium.isTextPresent(profilo_fisico));
}
public List Methods()
{
Connection con = null;
String driver = "connection string";
// String s[] = null;
List al = new ArrayList();
try { Class.forName(driver);
con = DriverManager.getConnection("thin string", "user name", "password");
try { String query = "query";
PreparedStatement st = con.prepareStatement(query);
ResultSet res = st.executeQuery();
while (res.next())
{
al.add(res.getString("field name"));
con.close();
}
catch (SQLException sq)
{
e.printStackTrace();
}
}
catch (Exception e)
{
e.printStackTrace();
}
return al;
}

4. How to check the presence of the fields in the page that are displayed or hidden depending on other fields? When fields are hidden/shown dynamically, "assertEquals" function gives the same result whether the check box is enable or not. We can use "assertVisible" function to resolve this issue. But we have to check the presence of every field seperately. If you try to check more than one field using the above function, then you will get "ERROR: Element (names of the fields) not found"

5. How to check whether a button is disabled or enabled?
We can use the command To check for the presence of an enabled button

assertEquals("false", selenium.getAttribute("sm-event.ABCCliente-ABCSearchBase.StampaLettera@disabled"));

OR

verifyTrue(selenium.isEditable("
sm-event.ABCCliente-ABCSearchBase.StampaLettera"));

To check for the presence of a disabled button

assertEquals("true", selenium.getAttribute("sm-event.ABCCliente-SearchBase.StampaLettera@disabled"));

OR

VerifyFalse(selenium.isEditable("
sm-event.ABCCliente-ABCSearchBase.StampaLettera"));

Note:
in html format it is like

Command : assertAttribute

Target : ButtonName@disabled

Value : true or false



Please note that the parameter is the button name and not the button caption

input type="submit" name="sm-event.ABCCliente-ABCSearchBase.StampaLettera""Stampa Lettera" style="cursor:hand" class="Bottone"/ value=


6. How to run more than one test file?
Use the concept of JUnit TestSuite where you can group individual test cases into related Test suite.

import junit.framework.Test;
import junit.framework.TestSuite;
public class TestAll {
public static Test suite() {
TestSuite suite = new TestSuite("test all the scripts");
suite.addTestSuite(Testfile1.class);
suite.addTestSuite(Testfile2.class);
return suite;
}
}


We can add N no of Test files similarly. Run this file alone the remaining things selenium server will take care



7. How to ensure that the user session expires?
Sometimes, even after the window gets closed at the end of a test case, the user session still remains. So any subsequent operations starts from the previous logged on user. To avoid this, use

selenium.deleteAllVisibleCookies();

Sometimes, when the window is not closing properly, we can use


selenium.deleteAllVisibleCookies();
selenium.close();


The above fragment ensures that the session expires and the window closes.


8.How to get the read only field's value in a variable when the field has the jsp tag?
we can get the text or value displayed in jsp by

selenium.getTable("xpath of the field");

for example if you want to get a value displayed in a field otto cifre, in IDE record a field with the command "verify table" which will produce java command like

verifyEquals("54376383",selenium.getTable("//form/table\[1\]/tbody/tr/td/table.3.1"));

then change this command like

String Accountno = selenium.getTable("//form/table\[1\]/tbody/tr/td/table.3.1");

so that we can have the jsp value in ottoCifre variable.

Data Driven Testing is possible in Selenium RC

Data driven testing is possible in selenium RC by having a separate class, that have methods to provide the data needed to do the field validation. call the methods in that class for doing the field validation for the required fields in your test. Here is a example for number validation.

public class fieldValidations {
public String[] getNumberValidation() {
String[] number =
{ "as", "2a", "a2", "2@", "@2", " ", "@#", "-23"};
return number;
}
}


provide your field with the values in the method getNumberValidation (For example)

fieldValidations object=new fieldValidations();
String value = object.getNumberValidation();
for (int i = 0; i < value.length; i++) {
selenium.type("Field Name", value[i]);
}