Saturday, January 15, 2011

Working with frames in selenium





Assume some times we need to write the test cases for frames applications.
In Such case the thing we need to follow is to check for the frame id .
In the main window the frames are being embedded.
So always to perform any action on frames content first we need to move our cursor to the frame using the selenium API. Check the below program...

package com.tests;

import junit.framework.*;

import com.thoughtworks.selenium.DefaultSelenium;

public class FramesDemo extends TestCase{
public static void main(String[] args) {
try{

DefaultSelenium selenium = new DefaultSelenium("localhost", 8888, "*firefox","http://localhost:8383");
selenium.start();
selenium.open("http://localhost:8383/testApplication/test.jsp");
selenium.setSpeed("2000");
System.out.println("Page Title "+selenium.getTitle());
assertEquals("Frames Concept", selenium.getTitle());
selenium.highlight("firstframe");
selenium.selectFrame("firstframe");
System.out.println("First frame Data "+selenium.getText("//div[@id='first']"));
selenium.selectFrame("relative=up");
selenium.highlight("secondframe");
selenium.selectFrame("secondframe");
System.out.println("Second frame Data "+selenium.getText("//div[@id='second']"));
selenium.selectFrame("relative=up");
selenium.highlight("thirdframe");
selenium.selectFrame("thirdframe");
System.out.println("Third frame Data "+selenium.getText("//div[@id='third']"));
selenium.selectFrame("relative=up");
selenium.highlight("fourthframe");
selenium.selectFrame("fourthframe");
System.out.println("Fourth frame Data "+selenium.getText("//div[@id='fourth']"));
selenium.selectFrame("relative=up");
selenium.selectFrame("secondframe");
selenium.highlight("link=Click Me to go to ThirdFrame");
selenium.click("link=Click Me to go to ThirdFrame");
selenium.waitForFrameToLoad("thirdFrame", "30000");
selenium.selectFrame("relative=up");
selenium.highlight("thirdframe");
selenium.selectFrame("thirdframe");
System.out.println("Third frame Data after Refresh "+selenium.getText("//div[@id='third']"));
System.out.println("Third frame Data in iframe is "+selenium.getText("//div[@id='fourth']"));

}catch(Exception e){
e.printStackTrace();
}
}
}

So using selectFrame we can select any frame and perform needed actions there and back to main window using selenium.selectFrame("relative=up"); .....

Hope this helps.....

3 comments:

  1. How to select a frame which is not having any name?

    ReplyDelete
    Replies
    1. You need to use javascript locators to identify those...

      Eg:- selenium.getEval("function returnIframeRef(){var docRef= window.document;
      return docRef.getElementsByTagName('iframe')[0];}returnIframeRef();");

      this is the way you need to use .. to find out the iframe that has no name... here in this example i have taken [0] index means it is the first frame in your web page... Hope it helps

      Delete
  2. hey iam able to work with selectframes on all browsers except ie 9 ... is there ant issue with selenium rc on ie 9

    ReplyDelete