[go: up one dir, main page]

0% found this document useful (0 votes)
16 views6 pages

Keyboard Action

Uploaded by

chaitravittal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views6 pages

Keyboard Action

Uploaded by

chaitravittal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

⌨️

Selenium with Java | Keyboard


Actions | Sliders | Tabs &
Windows
In this detailed guide, we will explore various Selenium WebDriver concepts
with Java, such as keyboard actions, interacting with sliders, and managing
tabs and windows. For each section, I'll provide explanations and sample code
to demonstrate how to perform these tasks using Selenium WebDriver.

1. Keyboard Actions in Selenium with Java


Selenium provides the Actions class to simulate keyboard and mouse events.
You can simulate pressing keys, typing text, and combinations of keys.
Commonly used keys include:

Keys.ENTER

Keys.TAB

Keys.ARROW_UP

Keys.ARROW_DOWN

Keys.SHIFT

Keys.CONTROL

Code Example (Keyboard Actions):

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class KeyboardActionsExample {

Selenium with Java | Keyboard Actions | Sliders | Tabs & Windows 1


public static void main(String[] args) {
// Set up the WebDriver
WebDriver driver = new ChromeDriver();

// Open a website
driver.get("https://www.google.com");

// Locate the search input field


driver.findElement(By.name("q")).sendKeys("Selenium
WebDriver");

// Create an Actions object


Actions actions = new Actions(driver);

// Perform keyboard action (pressing Enter key)


actions.sendKeys(Keys.ENTER).perform();

// Optionally, you can also combine actions like SH


IFT + TAB or CTRL + A
actions.sendKeys(Keys.chord(Keys.CONTROL, "a")).per
form();

// Close the browser


driver.quit();
}
}

In this example, we use the Actions class to send keyboard events. The method
chord() can be used for key combinations (like Ctrl+A).

2. Interacting with Sliders in Selenium with Java


Sliders are commonly used for selecting numeric values within a specific range
(e.g., setting volume or brightness). Selenium allows you to move a slider using
the Actions class.

Code Example (Slider Interaction):

Selenium with Java | Keyboard Actions | Sliders | Tabs & Windows 2


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class SliderExample {


public static void main(String[] args) {
// Set up the WebDriver
WebDriver driver = new ChromeDriver();

// Navigate to a page with a slider


driver.get("https://www.jqueryui.com/slider/");

// Switch to the frame containing the slider


driver.switchTo().frame(0);

// Locate the slider element


WebElement slider = driver.findElement(By.xpath("//
div[@id='slider']/span"));

// Create Actions object to move the slider


Actions actions = new Actions(driver);

// Move the slider to a specific position (e.g., 5


0% of its range)
actions.dragAndDropBy(slider, 50, 0).perform(); //
x: 50, y: 0

// Close the browser


driver.quit();
}
}

This example demonstrates how to interact with a slider on a webpage. We


locate the slider element and use dragAndDropBy() to move it horizontally by 50
pixels.

Selenium with Java | Keyboard Actions | Sliders | Tabs & Windows 3


3. Handling Tabs and Windows in Selenium with Java
Selenium allows you to switch between multiple browser tabs or windows using
the window handles . Each browser window or tab has a unique identifier, which
you can use to switch between them.

Code Example (Switch Between Tabs/Windows):

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.Set;

public class TabAndWindowHandlingExample {


public static void main(String[] args) {
// Set up the WebDriver
WebDriver driver = new ChromeDriver();

// Open the first URL


driver.get("https://www.google.com");

// Store the current window handle


String originalWindow = driver.getWindowHandle();

// Open a new tab/window by clicking a link or usin


g JavaScript
driver.findElement(By.linkText("Gmail")).click();
// Assuming this link opens a new window

// Get all window handles


Set<String> allWindows = driver.getWindowHandles();

// Switch to the new window


for (String windowHandle : allWindows) {
if (!windowHandle.equals(originalWindow)) {
driver.switchTo().window(windowHandle);
break;
}

Selenium with Java | Keyboard Actions | Sliders | Tabs & Windows 4


}

// Perform actions in the new window (for example,


print title)
System.out.println("New window title: " + driver.ge
tTitle());

// Switch back to the original window


driver.switchTo().window(originalWindow);
System.out.println("Original window title: " + driv
er.getTitle());

// Close all windows and end the session


driver.quit();
}
}

In this example:

1. We first store the current window handle.

2. After clicking a link that opens a new window or tab, we retrieve all window
handles.

3. We loop through the handles, switch to the new window, and perform
actions there.

4. Finally, we switch back to the original window.

Key Points:
1. Keyboard Actions: The Actions class in Selenium can simulate keyboard
interactions, including special keys and combinations.

2. Sliders: Sliders are moved using the dragAndDropBy() method within the
Actions class to simulate dragging.

3. Tabs & Windows: Use window handles to manage and switch between
multiple browser windows or tabs in Selenium.

This covers the essentials for dealing with keyboard actions, sliders, and
tabs/windows in Selenium with Java. For a real-world scenario, you might need

Selenium with Java | Keyboard Actions | Sliders | Tabs & Windows 5


to add additional wait mechanisms (e.g., WebDriverWait) to ensure elements
are interactable.

Selenium with Java | Keyboard Actions | Sliders | Tabs & Windows 6

You might also like