If you Google “Selenium RC user-extension” ten times you will find ten different approaches to using this feature. Below, is the official Selenium suggested approach.
9.6.1 Example
C#
1. Place your user extension in the same directory as your Selenium Server.
2. If you are using client code generated by the Selenium-IDE you will need to make a couple small edits. First, you will need to create an HttpCommandProcessor object with class scope (outside the SetupTest method, just below private StringBuilder verificationErrors;)
HttpCommandProcessor proc;
1. Next, instantiate that HttpCommandProcessor object as you would the DefaultSeleniumobject. This can be done in the test setup.
proc = new HttpCommandProcessor( "localhost" , 4444, "*iexplore" , "http://google.ca/" );
1. Instantiate the DefaultSelenium object using the HttpCommandProcessor object you created.
selenium = new DefaultSelenium(proc);
1. Within your test code, execute your user-extension by calling it with the DoCommand() method of HttpCommandProcessor. This method takes two arguments: a string to identify the user- extension method you want to use and string array to pass arguments. Notice that the first letter of your function is lower case, regardless of the capitalization in your user-extension. Selenium automatically does this to keep common JavaScript naming conventions. Because JavaScript is case sensitive, your test will fail if you begin this command with a capital. inputParams is the array of arguments you want to pass to the JavaScript user-extension. In this case there is only one string in the array because there is only one parameter for our user extension, but a longer array will map each index to the corresponding user-extension parameter. Remember that user extensions designed for Selenium-IDE will only take two arguments.
string[] inputParams = {"Hello World"}; proc.DoCommand( "alertWrapper" , inputParams);
1. Start the test server using the -userExtensions argument and pass in your user-extensinos.jsfile.
java -jar selenium-server.jar -userExtensions user-extensions.js
using System; using System.Text; using System.Text.RegularExpressions; using System.Threading; using NUnit.Framework; using Selenium; namespace SeleniumTests { [TestFixture]
public class NewTest
{
private ISelenium selenium;
private StringBuilder verificationErrors;
private HttpCommandProcessor proc; [SetUp]
public void SetupTest()
{
proc = new HttpCommandProcessor( "localhost" , 4444, "*iexplore" , "http://google.ca/" ); selenium = new DefaultSelenium(proc);
//selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://google.ca/"); selenium.Start();
verificationErrors = new StringBuilder();
[TearDown]
public void TeardownTest()
{ try { selenium.Stop(); } catch (Exception) {
// Ignore errors if unable to close the browser
}
Assert.AreEqual( "" , verificationErrors.ToString());
}
[Test]
public void TheNewTest()
{
selenium.Open( "/" );
string[] inputParams = {"Hello World" ,}; proc.DoCommand( "alertWrapper" , inputParams);
} }
}
Appendixes:
TEN
.NET CLIENT DRIVER
CONFIGURATION
.NET client Driver can be used with Microsoft Visual Studio. To Configure it with Visual Studio do as Following.• Launch Visual Studio and navigate to File > New > Project.
• Select Visual C# > Class Library > Name your project > Click on OK button.
• A Class (.cs) is created. Rename it as appropriate.
• Select following dll files - nmock.dll, nunit.core.dll, nunit.framework.dll,ThoughtWorks. Selenium.Core.dll, ThoughtWorks.Selenium.IntegrationTests.dll, Thought- Works.Selenium.UnitTests.dll and click on Ok button
ELEVEN
JAVA CLIENT DRIVER
CONFIGURATION
In General configuration of Selenium-RC with any java IDE would have following steps:• Download Selenium-RC from the SeleniumHQdownloads page
• Start any java IDE • Create new project
• Add “selenium-java-<version-number>.jar” to your project classpath
• Record your test from Selenium-IDE and translate it to java code (Selenium IDE has automatic translation feature to generate tests in variety of languages)
• Run selenium server from console • Run your test in the IDE
These points have been delineated below with reference to Eclipse and IntelliJ:
11.1 Configuring Selenium-RC With Eclipse
Eclipse is a multi-language software development platform comprising an IDE and a plug-in system to extend it. It is written primarily in Java and is used to develop applications in this language and, by means of the various plug-ins, in other languages as well as C/C++, Cobol, Python, Perl, PHP and more. Following lines describes configuration of Selenium-RC with Eclipse - Version: 3.3.0. (Europa Release). It should not be too different for higher versions of Eclipse
• Launch Eclipse.
• Select File > New > Other.
• Provide Name to your project, Select JDK in ‘Use a project Specific JRE’ option (JDK 1.5 selected in this example) > click Next
• Keep ‘JAVA Settings’ intact in next window. Project specific libraries can be added here. (This described in detail in later part of document.)
• Click Finish > Click on Yes in Open Associated Perspective pop up window.
• Right click on src folder and click on New > Folder
Name this folder as com and click on Finish button. • This should get com package insider src folder.
• Following the same steps create core folder inside com
SelTestCase class can be kept inside core package.
Create one more package inside src folder named testscripts. This is a place holder for test scripts. Please notice this is about the organization of project and it entirely depends on individual’s choice / organization’s standards. Test scripts package can further be segregated depending upon the project requirements.
• Create a folder called lib inside project Google. Right click on Project name > New > Folder. This is a place holder for jar files to project (i.e. Selenium client driver, selenium server etc)
• Right click on lib folder > Build Path > Configure build Path
• Under Library tab click on Add External Jars to navigate to directory where jar files are saved. Select the jar files which are to be added and click on Open button.
After having added jar files click on OK button.