Handle windows with Selenium:

 There are test cases in which we need to switch between multiple windows, to automate these test cases using selenium we need to use below functions:
1.getWindowHandle(): This function return parent window reference.
2.getWindowHandles() : This function return all windows reference.
3.switchTo().window(Window) : This function used to switch between windows.
Example:
String Parent_Window = driver.getWindowHandle();
for (String Child_Window : driver.getWindowHandles())
{
driver.switchTo().window(Child_Window);
driver.findElement(By.id("appointment")).click();
driver.findElement(By.id("closeAppointment")).click();
driver.close();
}

driver.switchTo().window(Parent_Window);

Handling Frames in Selenium:

Selenium has multiple methods to handle Frames.
  1. driver.switchTo().frame(name) 
  2. driver.switchTo().frame(id) 
  3. driver.switchTo().frame(index) 
  4. driver.switchTo().frame(iframe_element) 
Example:
<iframe name=”content” id=”contentSection”></iframe>
Multiple options:

driver.switchTo.frame(“content”);
driver.switchTo.frame(“contentSection”);
driver.switchTo.frame(0);
driver.switchTo.frame(driver.findElement(By.cssSelector("iframe[id='contentSection']")
)


To get out of Frame:
driver.SwitchTo().DefaultContent()
Switch to Parent Frame:
driver.switchTo().parentFrame();



Handling keyboard events and mouse actions:

Action class is used to handle keyboard events and mouse actions. Further this class is also used to chain actions or perform multiple actions.

Action name Parameters Description
click WebElement This is used to click in the middle of the element passed as parameter. Like action.click(webelement)
click No Parameter This is used to click at the current location. It is used with combination of actions.
Like action.moveToElement(org.openqa.selenium.WebElement, int, int).click() or action.moveByOffset(int, int).click()
doubleClick WebElement This is used to double click in the middle of the element passed as parameter. Like action.doubleClick(webelement)
doubleClick No Parameter This is used to double click at the current location. It is used with combination of actions.
Like action.moveToElement(org.openqa.selenium.WebElement, int, int).doubleClick() or action.moveByOffset(int, int).doubleClick()
moveToElement WebElement This is used to move mouse pointer to the middle of webelement passed as parameter. The element is scrolled into view. Like action.moveToElement(webelement)
moveToElement WebElement, int, int This is used to move the mouse pointer to offsets from the top left corner of element.The element is scrolled into view. Example: action.moveToElement(webelement, int, int)
moveByOffset int, int This is used to move mouse pointer from current position to the provided offsets. Example: action.moveByOffset(int, int)
keyDown java.lang.CharSequence This is used to perform key press. Remember key is not released automatically, Need to call keyUp() or sendKeys() function. Example: action.keyDown(keys.CONTROL)
keyDown WebElement, java.lang.CharSequence This is used to press key at web element. Example: action.keyDown(webelement, keys.CONTROL)
keyUp Java.lang.CharSequence This is used to keup or release key at current location. Example: action.keyUp(keys.CONTROL)
KeyUp WebElement, Java.lang.CharSequence This is used to press up at web element. Example: action.keyUp(webelement, keys.CONTROL)
sendKeys java.lang.CharSequence This is used to send keys on active element. Example: action.sendKeys(Keys.TAB)
clickAndHold WebElement Click on current mouse location without releasing.
contextClick WebElement Right click on web element. Example: action.contextClick(webelement)
dragAndDrop WebElement, WebElement Perform drag and drop from one element to other.
dragAndDropBy WebElement, int, int Perform drag and drop from one element to co-ordinates.
build No Parameter Used to combine multiple actions into single step. Example:
Actions oAction=new Actions(driver);
Actions moreActions = oAction
.moveToElement(element)
.click()
.keyDown(element,Keys.SHIFT)
.sendKeys(element,"selenium");

Action chaining= moreActions.build();
perform No Parameter For execution of Action object, this method is used. Example:
chaining.perform();