A 'Context Menu' is a pop-up menu that provides shortcuts for actions the software developer anticipates the user might want to take.
In a Windows environment, the context menu is accessed with a right mouse click.
EXAMPLE:
package com.keysCommands;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;
public class ContextMenu {
@Test
public void contextMenuExample() {
WebDriver driver = new FirefoxDriver();
try {
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(50, TimeUnit.SECONDS);
driver.get("http://www.google.com/");
Thread.sleep(3000);
WebElement element = driver.findElement(By.id("hplogo"));
Actions builder = new Actions(driver);
// build the action chain.
Action rightclick = builder.contextClick(element).build();
// perform the action
rightclick.perform();
Thread.sleep(8000);
}
catch (Exception e) {
System.out.println("Exception - > " + e.toString());
} finally {
driver.close();
driver.quit();
}
} // main function ends
}// class ends
Executing test via TestNG.xml
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Context Menu Example in Selenium WebDriver">
<test name="Context Menu Test">
<classes>
<class name="com.keysCommands.ContextMenuExample" />
</classes>
</test>
</suite>
In a Windows environment, the context menu is accessed with a right mouse click.
EXAMPLE:
package com.keysCommands;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;
public class ContextMenu {
@Test
public void contextMenuExample() {
WebDriver driver = new FirefoxDriver();
try {
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(50, TimeUnit.SECONDS);
driver.get("http://www.google.com/");
Thread.sleep(3000);
WebElement element = driver.findElement(By.id("hplogo"));
Actions builder = new Actions(driver);
// build the action chain.
Action rightclick = builder.contextClick(element).build();
// perform the action
rightclick.perform();
Thread.sleep(8000);
}
catch (Exception e) {
System.out.println("Exception - > " + e.toString());
} finally {
driver.close();
driver.quit();
}
} // main function ends
}// class ends
Executing test via TestNG.xml
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Context Menu Example in Selenium WebDriver">
<test name="Context Menu Test">
<classes>
<class name="com.keysCommands.ContextMenuExample" />
</classes>
</test>
</suite>
No comments:
Post a Comment