-
Notifications
You must be signed in to change notification settings - Fork 40
Frames
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.
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
}
}
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.
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
}
}
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;
- Configuring Bobcat
- Selenium enhancements
- Cucumber enhancements
- Traffic analyzer
- Email support
- Reporting
- Cloud integration
- Mobile integration
- Executing tests on different environments
- Working with multiple threads
- Tips and tricks
- Authoring tutorial - Classic
- AEM Classic Authoring Advanced usage
- Siteadmin
- Sidekick
- Aem Component
- Working with author pages
- Working with Publish pages
- Advanced component interactions
- Working with Context Menu
- Using Aem Content Tree
- Aem Content Finder
- Storing component configurations
- Working with packages
- Jcr Support
- Authoring tutorial - Touch UI
- Adding and editing a component
- Sites management tutorial