Skip to content
This repository has been archived by the owner on Nov 23, 2021. It is now read-only.
Mikołaj Mański edited this page Jul 12, 2016 · 3 revisions

Frames

Introduction

Switching frames in Selenium can be done via the switchTo() method and TargetLocator object:

webDriver.switchTo().defaultContent(); // default frame
webDriver.switchTo().frame("my-frame"); // selecting frame by name
webDriver.switchTo().frame(1); // selecting frame by index

Bobcat provides a set of annotations that store information about frames. You can decorate your page object classes and their methods with these annotations. Bobcat will read this information and perform the switching for you.

Frame annotation

Consider the following code:

@PageObject
@Frame("/my-frame")
public class MyPageObject {
 
    @Inject
    private WebDriver webDriver;
 
    public void sampleMethod() {
        webDriver.findElement(By.className("xyz"));
    }
}

The @Frame annotation causes all methods to run in the given frame. You may also annotate a single method, to switch it:

@PageObject
@Frame("/my-frame")
public class MyPageObject {
 
    public void sampleMethod() {
        // I'm running inside the my-frame
    }
 
    @Frame("other-frame")
    public void otherMethod() {
        // I'm running inside other-frame
    }
}

If you nest one page object into another the @Frame annotation will be inherited.

@PageObject
@Frame("/my-frame")
public class MyPageObject {
    @FindBy(id = "xyz")
    private OtherPageObject otherPageObject;
}
 
@PageObject
public class OtherPageObject {
    public void myMethod() {
        // this method is run inside the my-frame
    }
}

Frame path

If one frame is embedded into another, you may specify a full path to the desired frame. Let's assume that you have an <iframe name="outer-frame"> and inside it you have <iframe name="nested">. You may access the nested frame using following path: /outer-frame/nested. Other examples:

  • / - this is the default content (main window),
  • /frame123 - frame named frame123 placed inside the default content,
  • /$0 - first frame in the default content ($1 will be the second, etc.)
  • /outer-frame/nested/$1 - second frame inside the nested inside the outer-frame.

Inheritance

If a frame path doesn't start with slash, it's treated as a relative path. Method annotations are relative to the class annotation, which are relative to the parent page object:

@PageObject
@Frame("/my-frame")
public class MyPageObject {
    @FindBy(id = "xyz")
    private OtherPageObject otherPageObject;
}
 
@PageObject
@Frame("other-frame")
public class OtherPageObject {
 
    @Frame("$1")
    public void myMethod() {
        // this method is run inside /my-frame/other-frame/$1
    }
}

Manual switching

However it's not recommended and should be avoided, there is a possibility of manual switch between frames using FrameSwitcher service. It has two useful methods: switchTo(String path) and switchBack() that restores the previous frame:

@Inject
private FrameSwitcher switcher;
 
@Frame("/my-frame")
public void myMethod() {
 
    webDriver.findElement(...); // this is run inside /my-frame
 
    switcher.switchTo("other-frame"); // path is relative to the method annotation
    webDriver.findElement(...); // this is run inside /my-frame/other-frame
    switcher.switchBack(); // we are going back to the /my-frame
 
    switcher.switchTo("/other-frame");
    webDriver.findElement(...); // this is run inside /other-frame
    switcher.switchBack(); // we are going back to the /my-frame
}

If you need to know the frame you are currently in, use:

@CurrentFrame
private String framePath;
 
// or:
 
@CurrentFrame
private FramePath framePath;

Getting started with Bobcat

  1. Getting started

AEM Related Features

  1. Authoring tutorial - Classic
  1. AEM Classic Authoring Advanced usage
  1. Authoring tutorial - Touch UI
Clone this wiki locally