Monday, April 30, 2012

Selenium Syntax


11)      Working With Ajax : Whenever ajax is loaded. Always use a
       Command: WaitForElementPresent
       Target:  //div[@id=’ajaxid’]
22)      To Store Element that are on the page to use later in test. For eg. Test needs to pick a date and use it later so that you do not need to hardcode values into your test. 
       Command: Type
       Target: name of the textbox
       Value:  ${variableName} . (This variable name will come from : Right-click on the text, go to the storeText command in the context menu, and click on it. A dialog will appear just as below. Enter the name of a variable that you want to use. )
3)      Using element attributes in XPath queries:
      Target: xpath=//div[@class='classname'].
4)      Do partial match on attribute content: For example, if you wanted to access the element that has the text "This element has an ID that changes every time the page is loaded" in it, you will use //div[contains(@id,'time_')]. This is due to the first part of the ID always being static. Or use //div[starts-with(@id,’time_)]
5)      Find element that text it contains: To do this, your query will need to have the text() method call in the query. It will match the entire contents of the node if it has the format //element[text()='inner text'].  Example : //div[contains(text(),’element has a ID’)] or //div[text()=’This element has a ID’]
6)      We are going to find a button and then find its sibling. In this, the query that we will generate is equivalent to xpath=//div[@class='leftdiv']/ input[2]. We will start by finding the first element for our query, which is //input[@ value='Button with ID']. Place that into the Selenium IDE Target box and see which element it highlights. There is another button below the one that is highlighted, and that is the element that we need to work with in this section. The button is the next input item in the HTML, so it is elements following-sibling that we need. Our locator will look like //input[@value='Button with ID']/following-sibling::input[@ value='Sibling Button'] . Similarly, use preceding instead of following.
7)      Javascript : Let's imagine you are working against an auction site. The script will need to get the value of the last highest bid off the page and then add 2 to it. Let's try this in the Selenium IDE.
Command: storeText
Target: NAME OF Id
Value: Any name
Command: type
Target: Previous Value
Value: javascript{+storedVars[‘Name Of the id’]+2}
8)      Javascript within Verify or Assert : Let's say that you have stored a value from a different part of the test and now you need to verify or assert that the value is correct. (Create a step to verify that it is equal to 5*10 using the verifyEval command)
Command: storeText
Target: NAME OF Id
Value: Any name
Command: verifyEval
Target: javascript{5*10}
Value: ${Value} //Previous Value that we used
9)      Accessing Page with BrowserBot:
BrowserBot is the JavaScript object that allows Selenium to control the browser. It overrides access to the window, document, and other key browser JavaScript objects that we can normally access.
    
        Syntax is: var window = this.browserbot.getUserWindow();

 Imagine that you need to call a JavaScript function on the page. This could be because you are working against elements that Selenium can't interact with and your test needs to call into the API to put something on the page. Or you might have to write some tests to exercise the JavaScript functions on the page.
Syntax for Remote Control Server:
1)      To test testcase or test suite on any browser:   Open a Command Prompt and write.
Where jar file is place>java –jar selenium-server-standalone-2.20.0.jar –htmlsuite *firefox http://book.theautomatedtester.co.uk "c:\book\chapter 6\testsuite. html" "c:\book\chapter 6\result.html"
(Where jar file is place>java –jar selenium-server-standalone-2.20.0.jar –htmlsuite *name of the browser, name of the site “where test suite or test case is kept” “where result has to save”)
2)      Number of Browser to test Code :
*firefox
*mock
*firefoxproxy
*pifirefox
*chrome
*iexploreproxy
*iexplore
*firefox3
*safariproxy
*googlechrome
*conqueror
*firefox2
*safari
*piiexplore
*firefoxchrome
*opera
*iehta
*custom
3) Running Selenium IDE tests with User Extensions:
java –jar selenium-server-standalone.jar –userExtensions \\ path\to\extensions.js -htmlsuite *firefox http://book. theautomatedtester.co.uk c:\path\to\testsuite.html c:\path\to\ results.html

4) Running Selenium IDE tests withport:

Since, RC acts as a proxy between your tests and the application being tested; it has to use a port to listen for commands. There will be instances where you will not be able to use the standard 4444 port. When this happens, adding –port allows you to use a different port number without conflicts.
5) Running Selenium IDE tests with -firefoxProfileTemplate
If you require a special profile, or if you need to make sure that a specific Firefox Add-on is installed, use –firefoxProfileTemplate /path/to/firefox/profile. This command will take the profile that you want and then clone it to be used in your test.

6) Converting Selenium IDE tests to a C# :

*) Phase One: Record a Test using Selenium IDE
  •  All green, it passed.  Good.
  • Now we want to convert this test to C#.
  • Select “File ->Export Test Case as C# -> Save on Desktop as HTML File ” from the Selenium IDE window.
  • Next select all the C# code and copy it.
*) Phase Two: Setup the C# Test Project
  • Open up Visual Studio
  • Select “File ->New ->Project”
  • In the New Project window under project types, select “Visual C# ->Windows”
  • Select the “Class Library” Template and name it something like “TestingBlogSeleniumRc” and click “Ok”.
  • It will create the project along with a file called “Class1.cs”.  Paste your SeleniumIDE test into that file.
  • Rename “Class1.cs” to “FirstTest.cs”.
  • In the code, change the namespace from “SeleniumTests” to “TestingBlogSeleniumRc”:
  • Modify the class name from “Untitled” to “FirstTest”.  It’s good to change this so that if you add other tests, there won’t be any duplicated class names in the namespace (This has gotten me before, causing compile errors.).
  • We also have to modify the Selenium instantiation to launch Internet Explorer.  Where it has “*chrome”, change it to “*firefox”.
  • Where it has “http://change-this-to-the-site-you-are-testing/”, change it to “http://www.google.com”.
  • Next we need to add all the references so this thing will run.
  • Go to the solution explorer, and right click on the “References” and select “Add Reference…”
  • Hit the “Browse” tab and navigate to where you unzipped your SeleniumRC files.  Look in the dotnet client driver folder.  
  • Once you navigate to where the proper *.dlls are, select all of them and click “OK” to add them to the project.  (I know you can probably get away with adding less, but let’s keep it simple.)
  • Build the project to see if it works.  (Mine worked.  Hopefully yours did too.)
*) Phase Three: Running the Selenium RC test
  • In order to run the Selenium RC test, you first have to have the Selenium RC server running. Make a batch file.
(How to make .bat file:
Open Notepad and write this :
 @Echo Off
Echo starting Selenium Server
java -jar E:\Ridihima\Software\Selenium\selenium-server-standalone-2.20.0.jar
And save this as .bat File )
  • Next you need to launch the NUnit GUI to run the test from.  Since you installed NUnit, you should be able to navigate to it “Start -> All Programs -> NUnit 2.4.7 -> NUnit GUI (.NET 2.0)”
  • Once you open the NUnit runner, Click “File -> Open Project…” and navigate to the compiled dll from your test project.  For me, the path looks like this:
  • C:\Documents and Settings\dbrown1\My Documents\Visual Studio 2008\Projects\TestingBlogSeleniumRc\TestingBlogSeleniumRc\bin\Debug\TestingBlogSeleniumRc.dll
  • Once it loads, click on “Run” to start the test.
You must get rid of the try-catch exception block.  As long as you have that block in your test, your test will never fail.  Nunit counts on an exception being thrown when an assert statement fails.  If that exception gets caught, then Nunit thinks that the test passed.


No comments: