Various scenarios for the actions class

In this section, we will take a look at two scenarios for the actions class: double click and hover.

For the double click scenario, the following code creates an HTML page. This HTML page uses JQuery to show and hide the contents of a paragraph:

<!DOCTYPE html>
<html>
<head>
<title>JQuery Demo</title>
<script
src="https://code.jquery.com/jquery-3.3.1.js"
integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
crossorigin="anonymous"></script>
<script type="text/javascript">
$(document).ready(function(){
$('h2').dblclick(function(){
$('p').show();
})
});
</script>
<style>
p {
display:none;
}
</style>
</head>
<body>
<h2>Double click Here!</h2>
<p>This is some sample text</p>
</body>
</html>

Next, we put in a Java class with Selenium code. The following code uses the actions class instance to double click the h2 tag. On double clicking, the paragraph tag is displayed and the text in the paragraph tag is captured:

public class JQueryDBLClick {
public static void main(String[] args) {
String userDirectory = System.getProperty("user.dir");
System.setProperty("webdriver.chrome.driver",
userDirectory+"\src\main\resources\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.navigate().to(
"C:\Users\Bhagyashree\Documents\DoubleClick.html");
Actions actions = new Actions(driver);
actions.doubleClick(driver.findElement(By.tagName("h2"))).perform();
System.out.println("Text in para: "
+ driver.findElement(By.tagName("p")).getText());
}
}
You can also set the path of the binary executable in the path variable. Hence, you can avoid using System.setProperty() to set the path explicitly.

Next, we will explore the hover scenario, and for this, we will make use of https://jqueryui.com/tooltip/, which has a ready-made tooltip that we can make use of. It can be seen that the aria-describedby HTML attribute is only inserted into the HTML when a mouse hover is done on this element. 

This code extracts the aria-describedby attribute and prints it to the console:

public class HoverDemo {
public static void main(String[] args) {
String userDirectory = System.getProperty("user.dir");
System.setProperty("webdriver.chrome.driver",
userDirectory+"\src\main\resources\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(15,
TimeUnit.SECONDS);
driver.navigate().to("https://jqueryui.com/tooltip/");
driver.manage().window().maximize();
Actions actions = new Actions(driver);
driver.switchTo().frame(0);
actions.moveToElement(driver.findElement(By.id("age"))).build()
.perform();
System.out.println("Attribute is: "
+ driver.findElement(By.id("age")).getAttribute(
"aria-describedby"));
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset