You are on page 1of 2

======================================================Ajax calls/Actions

class/Mouse and keyboard


events=============================================================================
============================================
-> The Selenium WebDriver's Advanced User Interactions API allows us to perform
operations from
keyboard events and simple mouse events to complex events such as dragging-and-
dropping,
holding a key and then performing mouse operations by using the Actions class, and
building
a complex chain of events exactly like a user doing these manually.
The Actions class implements the builder pattern to create a composite action
containing a
group of other actions

-> We need to create an instance of the Actions class by passing the instance of
driver class
to the constructor in the following way:
Actions builder = new Actions(driver);

-> example : builder.movetoElement(WebElement).build().perform();


Here, build() and perform() needs to added in order to execute the action(s) before
it.
build() actually contructs the entire set of action string that may contain one or
more composite actions.
perform() actually instructs to execute the action after the action string is
built.
keyDown-> press down a key
keyUp-> release a key
contextClick() -> right click

=====================================Switching between child and prent windows


opened by
selenium===========================================================================
===================================================
Child Windows :
-> If a child window is opened, selenium by default sticks to the parent windows
only, we have to switch.
Similary if we are in a child window,selenium by default sticks to that window
only.we have to switch if we want to work with the parent window.

->driver.getWindowsHandles() : returns a set of ids of all windows opened by


selenium. It returns a set type data structure of strings i.e. Set<String>
the first window is stored in first position in the set, second window in 2nd
position nd so on.

-> Syntax :
Set<String> windowIds = driver.getWindowHandles(); //to get the ids all windows
opened by selenium at the moment.
Iterator<String> it = windowIds.iterator(); //creating an iterator instance to
iterate through the set above.
String parentId = it.next(); //gives the first element in the set i.e. the parent
window id
String childId = it.next(); //gives the second element in the set i.e. the second
window opened by selenium and so on
driver.switchTo().windows(childId); //to switch selenium driver control over the
desired window.

To open new window or tab : (from selenium 4 onwards)


driver.switchTo().newWindow(WindowType.TAB);
driver.switchTo().newWindow(WindowType.WINDOW);

====================================Iframes tag/Dealing with elements in


Frames=============================================================================
===================================================================
->HTML frames allow developers to present documents in multiple views, which may be
independent windows or subwindows. Multiple views offer developers a way to keep
certain
information visible, while other views are scrolled or replaced. For example,
within the same
window, one frame might display a static banner, the second a navigation menu, and
the third
the main document that can be scrolled through or replaced by navigating in the
second frame.

->to identify if a weblement is in a frame, check it's parent tags in html, one of
them will be "iframes".
or in firefox, top right corner, something in css with the word iframe will will be
displayed on inspecting element

-> Iframe can be located in selenium with the following options :


1. Frame id or name (id or name attribute of the iframe tag) :
driver.switchTo.frame("uniqueFrameIDOrNameID")
2. Webelement (xpath/CSS of the frame) :
driver.switchTo.frame(driver.findElement(By.cssSelector("iframe.uniqueClassname")))
;
3. By index (if there are 3 frames on the page, then index 0 will be 1st
frame, index 1 will be 2nd frame and so on) : driver.switchTo.frame(0)

-> After the tasks in the frame are complete, selenium doesn't switch out of the
frame automatically.
the following statement needs to be executed in order to move the webdriver out of
the frame and into the default webpage :
driver.switchTo().defaultContent();

->Working with multiple frames on same page


If there are multiple Iframes on single page then you can not directly navigate
from Iframe1 to IFrame2. For that, You need to select page In between as bellow.
//switch to frame1
driver.switchTo().frame("frame1");
driver.findElement(By.xpath("//td[contains(text(),'Cow')]/preceding-sibling::td/
input[@type='checkbox']")).click();

//Switch back to page content.


driver.switchTo().defaultContent();

//switch to frame2
driver.switchTo().frame("frame2");
driver.findElement(By.xpath("//input[@value='Boat']")).click();

You might also like