Total Pageviews

Monday, January 2, 2012

How to use JQuery instead of Xpath in Selenium.


To identify objects, generally we use Xpath, CSS and DOM. If we are going with FF, we will use Xpath. It will works tremendously slowly in IE 6 and IE 7.
To solve the speed in IE, we can use css or JQuery also.

see the below mentioned code:
<table class="style">
    <tr class="somestyle">
        <td>SomeName1</td>
        <td>
            <input type="submit" value="Edit" class="btn" />
        </td>
    </tr>
    <tr class="gvAlternatingRowH35">
        <td>SomeName2</td>
        <td>
            <input type="submit" value="Edit" class="btn" />
        </td>
    </tr>
</table>

Here all the buttons are having the same name,  If you want to click the “Edit” button next to SomeName2, you can easily complete this task in selenium by using XPath and its parent feature:

selenium.click("//td[text()='SomeName2']/../td/input[@value='Edit']")

As we mentioned above, this won’t be that easy to do with CSS because it doesn’t have the appropriate feature. So, we used the JQuery parent() function to have the ability to select the parent of the current node. It works really fast in all browsers, since JQuery uses CSS.

by adding the query's library to the selenium server and execute JQuery script directly from selenium and find the objects easily. To execute JQuery script, you can use selenium.getEval(someJS) if you need the return value, or selenium.runScript(someJS) if you don’t.


1. Add Jquery’s library to Selenium server.

Add JQuery’s library (‘jquery.min.js’) to your selenium-server.jar (‘selenium-server.jar\core\scripts\’ folder). To do that, open ‘selenium-server.jar’ by using one of the archives — for example, 7-Zip — and drag-and-drop ‘jquery.min.js’ in the mentioned folder.


2. Add script reference to JQuery library.

Add script reference to ‘jquery.min.js’ to the ‘<head>’ section of the RemoteRunner.html (‘selenium-server.jar\core\’).

<head>
...
<script language="JavaScript" type="text/javascript" src="scripts/jquery.min.js" />
...
<\head>

3. Add new JQuery locator to Selenium core.

You can add a new JQuery locator to the Selenium core in the runSeleniumTest() function of the selenium-remoterunner.js file. All you need to do is to add a call of selenium.doAddLocationStrategy() method before Selenium property initialization of the window object. See the sample below for more details.

function runSeleniumTest() {
    ...
    selenium.doAddLocationStrategy("jquery", "
var loc = locator;
var attr = null;
var isattr = false;
var inx = locator.lastIndexOf('@');

if (inx != -1) {
    loc = locator.substring(0, inx);
    attr = locator.substring(inx + 1);
    isattr = true
}

var selectors = loc.split('<');
var found = $(inDocument);

for (var i = 0; i < selectors.length; i++) {
    if (i > 0) {found = $(found.parents()[0]);
}

if (jQuery.trim(selectors[i]) != '')
    found = found.find(selectors[i]);
}

if (found.length > 0) {
    if (isattr) {
        return found[0].getAttributeNode(attr);
    }
    else {
        return found[0];
    }
}
else {
    return null;
}
    ");
    ...
}

The first parameter of the selenium.doAddLocationStrategy method (“JQuery”) is the new Selenium locator’s name. The second one (“var loc = locator…”) is the new custom Selenium locator. If you want to, you can write your own implementation for the locator. To call the JQuery parent() function, we used the '<' custom symbol .

Finally, let’s take a look at how you can use the new JQuery locators, instead of the XPath ones.

XPath locators:
•    xpath=//a[contains(@href,'#id1')]
•    xpath=(//table[@class='stylee'])//th[text()='theHeaderText']/../td
•    xpath=//input[@name='name2' and @value='yes']

JQuery locators:
•    jquery= a[href*='#id1']
•    jquery= table[class='stylee'] th:contains('theHeaderText')<td
•    jquery= input[name='name2'][value='yes']

Monday, December 19, 2011

Different ways to verify pop-up/Window using selenium


SelectWindow:

If the window name/title is null, then we can focus the window/pop-up as below mentioned manner:


For this we can use, selectWindow | windowName command and to go back to main window from other window, we can use  selectWindow | null


Arguments:
  • by windowid - the java Script window id of the window to select. - Selects pop-up window id using window locator; Once a window selected, all the commands will execute on the pop-up window until main window selected.  To select the main window, use null as a target.
We can select the window by providing different objects:
  • by title, by internal java script "name" or by java script "variable".
    • title= MyWindow: This text appears in the title bar. some times two windows can have the same title, in that scenario, this locator will just pick one. 
      •  name=myWindow: Finds the window using its internal JavaScript "name" property. This is the second parameter "windowName" passed to the JavaScript method window.open(url, windowName, windowFeatures, replaceFlag) (which Selenium intercepts). 

      • var=variableName: Some pop-up windows are unnamed (anonymous), but are associated with a JavaScript variable name in the current application window, e.g. "window.foo = window.open(url);". In those cases, you can open the window using "var=foo".


selectPopup:

If it is a popup then do selectPopUp | windowId and then to go back to the main window do selectWindow | null

selectPopUp(windowID) Arguments:

    * windowID - an identifier for the popup window, which can take on a number of different meanings

Simplifies the process of selecting a popup window (and does not offer functionality beyond what selectWindow() already provides).

    * If windowID is either not specified, or specified as "null", the first non-top window is selected. The top window is the one that would be selected by selectWindow() without providing a windowID . This should not be used when more than one popup window is in play.

    * Otherwise, the window will be looked up considering windowID as the following in order: 1) the "name" of the window, as specified to window.open(); 2) a javascript variable which is a reference to a window; and 3) the title of the window. This is the same ordered lookup performed by selectWindow . 

Sunday, December 18, 2011

Verify the color of an object using selenium


The HTML code is
//<input name="input" class="form-text required error" id="edit-input" type="text" size="20" maxLength="255"/>

//<input name="op" class="form-submit" id="edit-submit-1" type="submit" value="Submit"/>

The Code example to check the above behavior is
@Test
  public void iscolorchanged() {
        org.openqa.selenium.ie.InternetExplorerDriver driver = new org.openqa.selenium.ie.InternetExplorerDriver();
        driver.get("the url to test");
        WebElement submitbutton = (new WebDriverWait(driver, 10)).until(new ExpectedCondition<WebElement>(){
            @Override
            public WebElement apply(WebDriver d) {
                  return d.findElement(By.xpath("//*[@id='edit-submit-1' and @name='op']"));
            }}); 
        submitbutton.submit();
        WebElement inputbox = (new WebDriverWait(driver, 10)).until(new ExpectedCondition<WebElement>(){
            @Override
            public WebElement apply(WebDriver d) {
                  return d.findElement(By.xpath("//*[@id='edit-input'and @class='form-text required error']"));
            }}); 
        Boolean colorchanged;
        String color= inputbox.getCssValue("border-color");
        if(color.equals("#ff0000"))
                    {
                          colorchanged=true;
                    }
                    else
                    {
                          colorchanged=false;
                    }
                   
        System.out.println( colorchanged);
       
        }

Gathered from Net

Monday, December 12, 2011

Selenium Locaters - Hand Book

Hi all,

Here I added a new hand book which I got it from Net. This is very useful for selenium testers to construct Objects.

You can download this from here

Selenium Locaters - HandBook

Wednesday, November 30, 2011

Free Load testing Tools webApplications


Pylot - Open Source Load Testing Tool
Pylot is an open source tool which runs HTTP load tests for testing performance and scalability of web services.
It generates concurrent load (HTTP Requests), verifies server responses, and produces reports with metrics. Tests suites are executed and monitored from a GUI or shell/console.

Tsung

Tsung
Tsung can be used to stress almost any kind of server including:
  • HTTP
  • WebDAV
  • SOAP
  • PostgreSQL, MySQL
  • LDAP
  • Jabber/XMPP servers.
Several IP addresses can be used on a single machine using the underlying OS IP Aliasing& HTTP reports are generated during the tests.

Apache JMeter

Apache JMeter
Apache JMeter may be used to test performance both on static and dynamic resources. Besides HTTP, it can connect to databases, POP3 & more.

Siege

Siege - Load Testing Tool
Siege is an http regression testing and benchmarking utility. It was designed to let web developers measure the performance of their code under duress, to see how it will stand up to load on the internet.
It supports basic authentication, cookies, HTTP and HTTPS protocols. It allows the user hit a web server with a configurable number of concurrent simulated users.
Siege was written on GNU/Linux and has been successfully ported to AIX, BSD, HP-UX and Solaris.

The Grinder

Java Load Testing
The Grinder is a Java load testing framework that makes it easy to run a distributed test using many load injector machines.
Tests are written in Jython scripting language. It has an automatic management of client connections and cookies.