Some useful selenium tips

When I was trying to automate using Selenium there were couple of times that I got stuck. And google.com was there to help always but at times I would end up searching lot many sites for solutions. So I decided to come up with this post.

Following is a list of tips that would be helpful to you while using Selenium.

*Identifying a dynamic element with Selenium:
Lets say you have an element "input type="text" id="mail__ID197:_ID05_test". The next time you check the id its input type="text" id="mail__ID820:_ID95_test". Now you have a dynamic element. There are multiple ways of identifying this element. You can use "contains" in the locator. You can give the locator as
                    xpath=//input[contains(@id,"mail__") and contains(@id,"_test")]



*Identifying multiple dynamic elements (with similiar xpaths)with Selenium:
Lets say you have a textfield in a page which is dynamic. Every time the number of textfields that appear differ and every time you need to enter some value for every text field that appears. You can use "storeXpathCount" command of IDE (or getXpathCount for Selenium RC)in this case.Lets say the xpaths for the textfields look like input type="text" id="mail__ID97:_ID95_test and input type="text" id="mail__ID26:_ID87_test.
Your commands would look like(Selenium RC and Java):
                     int k = selenium.getXpathCount("xpath=//input[contains(@id,\"mail__\") and contains(@id,\"_test\")]");
                    for (int i=0;i<=k;i++){
                    selenium.type("xpath=//input[contains(@id,\"mail__\") and contains(@id,\"_test\")]","Testing");
                    }


*Identifying multiple dynamic elements (with different xpaths) with Selenium:
Lets take the above case itself but now say you have buttons instead of textfields and you want to perform clicking action for every button. You can use "storeAllButtons" command of IDE in this case.
Your commands would look like(Selenium RC and Java):
String[] g = selenium.getAllButtons();
for (int i=0;i<=g.length();g++){
selenium.click(g[i]);
}
The id's of all the buttons would be stored in the string array. If a button doesnt have an id it will be stored as null. Hence this would work only if you have id's specified for your elements. There are other selenium commands which work the same way- storeAllFields, storeAllLinks.


*Identifying a pop-up window with Selenium
There are multiple ways of identifying a pop-up window. Some are given below:
* storeAllWindowIds, storeAllWindowNames, storeAllWindowTitles: You can use any of these commands to get all the windows. You can then select your pop up window based on its id, name or title. This never really worked for me so I always used the next
solution.

* You forcefully open a window with a name that you specify and then select it. Here I am opening a window with name 'mynewwin' and then selecting it.
selenium.getEval("this.browserbot.getCurrentWindow().open('','mynewwin')");
selenium.click(buttonxpath);
Thread.sleep(5000);
selenium.windowFocus();
selenium.waitForPopUp("mynewwin", "50000");
selenium.selectWindow("mynewwin");
This doesnt work in IE. So you can use the below solution.

* Identifying a pop-up window in IE using selenium:
selenium.openWindow('','mynewwin');
selenium.click(xpath);
selenium.windowFocus();
selenium.waitForPopUp("mynewwin", "50000");
selenium.selectWindow("mynewwin");

3 comments:

  1. thanks a lot for your valuable information

    ReplyDelete
  2. Nice post. Useful infrmation. This is always happy to know about Salesforce training in Gurgaon whose certification program helps many to get certificed. I strongly urge you to join now in Salesforce Training in Hyderabad | Course Price | Training institute and enroll for free demo at Salesforce Training in Bangalore | Course Certification Cost and Job Placement Assistance . I have gone through this Salesforce Certification Training. Why done you enroll for a free demo at Salesforce training institutes in Chennai [updated] | Course Price

    ReplyDelete
  3. Such an excellent and interesting information in your blog, it is awesome to read and do post like this with more informations. Salesforce Training Dallas

    ReplyDelete