Showing posts with label Selenium. Show all posts
Showing posts with label Selenium. Show all posts

Sunday, April 8, 2012

Selenium usage tips- cookbook for identifying elements in webpage.

Selenium usage tips- cookbook for identifying elements in webpage.
XPath, CSS, DOM and Selenium: The Rosetta Stone
http://www.simple-talk.com/content/article.aspx?article=1269

Wednesday, March 14, 2012

How to get the Element type in Selenium

Below command can be used to get the Element type i.e input,textarea,select..etc
selenium.getEval(String.format("this.browserbot.getCurrentWindow().document.getElementById('%s').type", ElementId)

Friday, March 2, 2012

Session Data loss popup problem

I want to share one of my experiences in how easy to maintain scripts when we follow Page Object pattern with a recent change in one of the application.

To avoid data loss while navigating or refreshing to a page, a popup window appears with message "Application session / Data will be loss" with OK and Cancel button. (And which is not a JavaScript popup :( )
Problem:

There are too many flows where the application opens dialog boxes, frames, popup etc.
Everywhere we need to handle the Session popup.

Solution: When we view the source code of the page, we got to know the JavaScript for Session popup written as below

&lt script language="JavaScript" &gt

var allowPrompt = true;

window.onbeforeunload = confirmExit;
.....
So, to avoid this we used  ”this.browserbot.getCurrentWindow().allowPrompt=false"  command in our base page constructor which solved the problem.
Now we are happily running our tests without any change in our test script(s).

Wednesday, January 4, 2012

Selenium2 webelement.click is not working in Internet Explorer

There is some issue with few versions of IE (includes OS).
we can use webelement.sendKeys("\n") instead of webelement.click();

Tuesday, December 20, 2011

Monday, December 19, 2011

Selenium Click and ClickAt

Click - to click on a Locator
ClickAt - to click on a Locator w.r.t coordinates

Basically ClickAt can be used as an alternate to Click for rich ui frameworks like DOJO,Wickets etc.

Tuesday, November 15, 2011

Selenium - Autosuggest edit field

keyPressNative method will be used to handle autosuggestion.
keyPressNative method -Simulates a user pressing and releasing a key by sending a native operating system keystroke. This function uses the java.awt.Robot class to send a keystroke; this more accurately simulates typing a key on the keyboard. It does not honor settings from the shiftKeyDown, controlKeyDown, altKeyDown and metaKeyDown commands, and does not target any particular HTML element. To send a keystroke to a particular element, focus on the element first before running this command.

public void selectAutosuggest(String webElementLocator,String inputValue){
char[] stringArray = inputValue.toUpperCase().toCharArray();
selenium.click(webElementLocator);
for(int i=0;i &lt stringArray.length ; i + +)
selenium.keyPress(webElementLocator,""+(int)stringArray[i]);
//Press downarrow and click EnterKey
selenium.keyPressNative(String.valueOf(KeyEvent.VK_DOWN));
selenium.keyPressNative(String.valueOf(KeyEvent.VK_ENTER));
Thread.sleep(1000);

}

Sunday, November 13, 2011

Selenium - XPATH - Location Of text...


If we want to click on some text which we dont know the Id,Name
we can use selenium.click("xpath=//*[text()=\"Search\"]")
To be more specific we can use
selenium.click("xpath=//*[@id='ParentDivId']//*[text()='Search']");



Thursday, August 18, 2011

Selenium Window target = "_blank" issue

For example if you have a link like



Upon click mylink , the url loads into a new window ,  to do some operations in newly opened window it is painful to identify the windowName ..etc

And the solution for that is
selenium.open("/test.html");
selenium.getEval("this.page().findElement(\"link=mylink with target _blank\").target='maddys_window'");
selenium..getEval("selenium.browserbot.getCurrentWindow().open('', 'maddys_window')");
selenium.click("link=mylink with target _blank");
Thread.sleep(2000);
selenium.selectWindow("maddys_window");
selenium.windowFocus();
......
......
selenium.close(); //to close maddys_window
selenium.selectWindow("null");  //to get focus to main window
....
...

Monday, July 11, 2011

Selenium - Ajax , Selenium - Dojo, ...

Ref: http://www.ibm.com/developerworks/opensource/library/os-webautoselenium/index.html?ca=drs-

Friday, June 3, 2011

how-to-locate-element-which-have-same.html

http://automationtricks.blogspot.com/2010/09/how-to-locate-element-which-have-same.html

Selenium Regular Expression

Selenium supports a few methods that help match text patterns. However, selenium locators don’t accept regular expressions. Only patterns or values accept them.
Globbing:
selenium.click("link=glob:*Gifts"); // Clicks on any link with text suffixed with 'Gifts'
selenium.verifyTextPresent("glob:*Gifts*");
Regular Expressions:[regexp, regexpi]
selenium.click("link=regexpi:^Over \\$[0-9]+$");  //matches links such as 'Over $75', 'Over $85' etc
Contains:
selenium.highlight("//div[contains(@class,'cnn_sectbin')]");  //highlights the first div with class attribute that contains 'cnn_sectbin'
selenium.highlight("css=div#cat_description:contains(\"to last\")");  //locating a div containing the text 'to last' using css selector
Starts-with:
selenium.click("//img[starts-with(@id,'cat_prod_image')]");  //clicks on the first image that has an id attribute that starts with 'cat_prod_image'
selenium.click("//div[starts-with(@id,'tab_dropdown')]/a[last()]");  //clicks on the last link within the div that has a class attribute starting with 'tab_dropdown'
selenium.click("//div[starts-with(@id,'tab_dropdown')]/a[position()=2]"); //clicks on the second link within the div that has a class attribute starting with 'tab_dropdown'
selenium.highlight("css=div[class^='samples']"); //highlights div with class that starts with 'samples'
Ends-with:
selenium.highlight("css=div[class$='fabrics']"); //highlights div with class that ends with 'fabrics'
selenium.click("//img[ends-with(@id,'cat_prod_image')]"); //clicks on the first image that has an id attribute that ends with 'cat_prod_image'
[Note: ends-with is supported only by Xpath 2.0. FF 3 might throw an error for this.]
Ref: http://blog.browsermob.com/2011/02/regular-expressions-and-pattern-matching-with-browsermob-and-selenium/

Way to Get XPATH in IE

Are you stuck with an application that only works in IE?
Do you want the XPATH of an element that can be seen only IE? Unable to replicate the scenario in firefox. There are many firefox addons like xpather, xpath-checker and firebug that will give you the xpath of an element in a second. But sadly there is no addon or tool avaialable that will do this for IE. For most cases you can get the xpath of the elements that fall in your script using the above tools in firefox and tweak them a little (if required) to make them work in IE. But if you are testing an application that will work only in IE or the specific scenario or page that has this element will open-up/play-out only in IE then you cannot use any of the above mentione tools to find the XPATH.
Well the only thing that works in this case is the Bookmarklets that were coded just for this purpose. Bookmarklets are JavaScript code that you will add in IE as bookmarks and later use to get the XPATH of the element you desire. Using these you can get the XPATH as easily as you get using xpather or any other firefox addon.

STEPS TO INSTAL BOOKMARKLETS
1)Open IE
2)Type about:blank in the address bar and hit enter
3)From Favorites main menu select--->Add favorites
4) In the Add a favorite popup window enter name GetXPATH1.
5)Click add button in the add a favorite popup window.
6)Open the Favorites menu and right click the newly added favorite and select properties option.
7)GetXPATH1 Properties will open up. Select the web Document Tab.
8)Enter the following in the URL field.

javascript:function getNode(node){var nodeExpr=node.tagName;if(!nodeExpr)return null;if(node.id!=''){nodeExpr+="[@id='"+node.id+"']";return "/"+nodeExpr;}var rank=1;var ps=node.previousSibling;while(ps){if(ps.tagName==node.tagName){rank++;}ps=ps.previousSibling;}if(rank>1){nodeExpr+='['+rank+']';}else{var ns=node.nextSibling;while(ns){if(ns.tagName==node.tagName){nodeExpr+='[1]';break;}ns=ns.nextSibling;}}return nodeExpr;}

9)Click Ok. Click YES on the popup alert.
10)Add another favorite by following steps 3 to 5, Name this favorite GetXPATH2 (step4)
11)Repeat steps 6 and 7 for GetXPATH2 that you just created.
12)Enter the following in the URL field for GetXPATH2

javascript:function o__o(){var currentNode=document.selection.createRange().parentElement();var path=[];while(currentNode){var pe=getNode(currentNode);if(pe){path.push(pe);if(pe.indexOf('@id')!=-1)break;}currentNode=currentNode.parentNode;}var xpath="/"+path.reverse().join('/');clipboardData.setData("Text", xpath);}o__o();

13)Repeat Step 9.

You are all done!!

Now to get the XPATH of elements just select the element with your mouse. This would involve clicking the left mouse button just before the element (link, button, image, checkbox, text etc) begins and dragging it till the element ends. Once you do this first select the favorite GetXPATH1 from the favorites menu and then select the second favorite GetXPATH2. At his point you will get a confirmation, hit allow access button. Now open up a notepad file, right click and select paste option. This will give you the XPATH of the element you seek.


*I got this bookmarklets from some other site but can't remember which one. Credit goes to the guy who created these JS.
 
Ref: http://deepintoselenium.blogspot.com/2011/05/way-to-get-xpath-in-ie.html
 

Thursday, February 17, 2011

Selenium RC supporting for Https certificate issue

Download latest selenium1.0.x and include the following code while starting selenium server from code.

RemoteControlConfiguration rcc = new RemoteControlConfiguration();
rcc.setTrustAllSSLCertificates(true);
seleniumServer = new SeleniumServer(rcc);
seleniumServer.start();

Wednesday, February 9, 2011

Selenium Tutorial

This tutorial explains how to use write selenium tests using frameworks like Junit , TestNG with Ant. Hope by the end of tutorial you will get a base framework to start your tests for your application under test.
First of all take your own time and go through the entire document available at http://seleniumhq.org/docs/index.html.
Now starts with Installation of required artifacts.



  1. Installing eclipse
    Your computer should have a working Java Runtime Environment (JRE) v6 to run Eclipse. If you do not already have it, follow the instructions for installing Java 6 before installing Eclipse. Regardless of whether or not you had an existing, prior version of Eclipse, click to open the local zip archive of the Eclipse program so that you can view the folder. Once downloaded zip file , extract the zip contents into a folder (eg: c:\eclipse ). Open eclipse editor by doubleclick eclipse.exe.

Thursday, December 16, 2010

Automation-Selenium Ajax Application

For ajax applications waitForPageToLoad wouldn’t work since the page is not actually loaded to refresh the Ajax element. Instead of using Thread.sleep(xxx) we can wait for a specified period and then continue once we found the element.

waittime=10;
while(waittime!=0){
try { if (selenium.isElementPresent("webelement")) break; } catch (Exception e) {}
Thread.sleep(1000);
waittime--;
}



Wednesday, October 8, 2008

Automation-Selenium

Good Tools To Know, and Work Easily With Selenium and Other OpenSource Tools.

Chris Pederick’s Web Developer toolbar for Firefox and Internet Explorer
• http://chrispederick.com/work/webdeveloper/

XPath Helpers
XPather
https://addons.mozilla.org/extensions/moreinfo.php?id=1192

XPath Checker
https://addons.mozilla.org/extensions/moreinfo.php?id=1095

Selenium is supporting UI-Element concept.
To know more about what is UI-Element and how can we use this etc,
first we need to understand the concepts of JSON (JavaScript Object Notation).
http://en.wikipedia.org/wiki/JSON ,
and Playback of scripts depends on JavaScript we need to have any idea about JScript
http://www.w3schools.com/js/default.asp


Many Opensource tools dont support Regular Expression to verify the content of HTML page (eg:Shai,Selenium,Watir...) , before knowing how to verifyhtmltext (placing a checkpoint on a text) with the tools, we need to know about XPATH. Lets have a look its easy and fun.
http://www.zvon.org/xxl/XPathTutorial/General/examples.html