下面列出了org.openqa.selenium.interactions.Actions#perform ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void shouldPerformAction() {
driver.get("http://guidebook.seleniumacademy.com/Selectable.html");
WebElement one = driver.findElement(By.name("one"));
WebElement three = driver.findElement(By.name("three"));
WebElement five = driver.findElement(By.name("five"));
// Add all the actions into the Actions actions.
Actions actions = new Actions(driver);
actions.keyDown(Keys.CONTROL)
.click(one)
.click(three)
.click(five)
.keyUp(Keys.CONTROL);
// Perform the action
actions.perform();
}
private void clickOnACell() throws InterruptedException {
zoomIntoHelsinore();
waitForCellsToBeDisplayed();
WebElement map = getMap();
Actions actions = new Actions(browser);
actions.moveToElement(map, map.getSize().getWidth() / 2, map.getSize().getHeight() / 2);
actions.click();
actions.perform();
// Cursor position is exactly in the center of the map
browser.findElement(By.id("tab-map")).click();
assertEquals("(56°02'05.7\"N, 12°38'59.7\"E)", browser.findElement(By.cssSelector("div#cursorpos.useroutput p")).getText());
// Assert statistic data for correct cell displayed
final String expectedCellInfo = "Cell id 6249302540 (56°02'06.5\"N,12°38'53.8\"E) - (56°02'00\"N,12°39'00.3\"E)";
By actualCellInfoElement = By.cssSelector("div.cell-data-contents > h5");
wait.until(ExpectedConditions.textToBePresentInElement(actualCellInfoElement, expectedCellInfo));
}
@Test
public void shouldMoveByOffSet() {
driver.get("http://guidebook.seleniumacademy.com/Selectable.html");
WebElement three = driver.findElement(By.name("three"));
System.out.println("X coordinate: " + three.getLocation().getX()
+ ", Y coordinate: " + three.getLocation().getY());
Actions actions = new Actions(driver);
actions.moveByOffset(three.getLocation().getX() + 1, three.
getLocation().getY() + 1);
actions.perform();
}
/**
* @throws Exception if an error occurs
*/
@Test
@Alerts(DEFAULT = {"[object Event]", "undefined", "[object MouseEvent]", "1", "[object MouseEvent]", "2"},
IE = {"[object Event]", "undefined", "[object PointerEvent]", "0", "[object PointerEvent]", "0"})
public void detail() throws Exception {
final String html =
"<html><head><script>\n"
+ " function alertDetail(e) {\n"
+ " alert(e);\n"
+ " alert(e.detail);\n"
+ " }\n"
+ "</script></head>\n"
+ "<body onload='alertDetail(event)'>\n"
+ " <div id='a' onclick='alertDetail(event)'>abc</div>\n"
+ " <div id='b' ondblclick='alertDetail(event)'>xyz</div>\n"
+ "</body></html>";
final String[] alerts = getExpectedAlerts();
int i = 0;
final WebDriver driver = loadPage2(html);
verifyAlerts(driver, alerts[i++], alerts[i++]);
driver.findElement(By.id("a")).click();
verifyAlerts(driver, alerts[i++], alerts[i++]);
final Actions action = new Actions(driver);
action.doubleClick(driver.findElement(By.id("b")));
action.perform();
verifyAlerts(driver, alerts[i++], alerts[i++]);
}
/**
* @throws Exception if an error occurs
*/
@Test
@Alerts("mouse over")
@BuggyWebDriver(FF = "mouse overmouse overmouse over",
FF60 = "mouse overmouse overmouse overmouse over",
FF68 = "mouse overmouse overmouse over")
public void mouseOver() throws Exception {
final String html =
"<html>\n"
+ " <head>\n"
+ " <script>\n"
+ " function doTest() {\n"
+ " document.title += 'mouse over';\n"
+ " }\n"
+ " </script>\n"
+ " </head>\n"
+ "<body>\n"
+ " <form id='form1'>\n"
+ " <select name='select1' id='select1' size='4' onmouseover='doTest()'>\n"
+ " <option value='option1' id='option1' >Option1</option>\n"
+ " <option value='option2' id='option2'>Option2</option>\n"
+ " </select>\n"
+ " </form>\n"
+ "</body></html>";
final WebDriver driver = loadPage2(html);
final Actions actions = new Actions(driver);
actions.moveToElement(driver.findElement(By.id("select1")));
actions.perform();
Thread.sleep(400);
assertTitle(driver, getExpectedAlerts()[0]);
}
/**
* @throws Exception if an error occurs
*/
@Test
@Alerts(DEFAULT = "",
FF = "mouse over",
FF68 = "mouse over",
FF60 = "mouse over")
@BuggyWebDriver(FF = "mouse overmouse overmouse over",
FF68 = "mouse overmouse overmouse over",
FF60 = "mouse overmouse overmouse over")
public void mouseOverDisabledSelect() throws Exception {
final String html =
"<html>\n"
+ " <head>\n"
+ " <script>\n"
+ " function doTest() {\n"
+ " document.title += 'mouse over';\n"
+ " }\n"
+ " </script>\n"
+ " </head>\n"
+ "<body>\n"
+ " <form id='form1'>\n"
+ " <select name='select1' id='select1' size='4' onmouseover='doTest()' disabled='disabled'>\n"
+ " <option value='option1' id='option1'>Option1</option>\n"
+ " <option value='option2' id='option2'>Option2</option>\n"
+ " </select>\n"
+ " </form>\n"
+ "</body></html>";
final WebDriver driver = loadPage2(html);
final Actions actions = new Actions(driver);
actions.moveToElement(driver.findElement(By.id("select1")));
actions.perform();
Thread.sleep(400);
assertTitle(driver, getExpectedAlerts()[0]);
}
/**
* @throws Exception if an error occurs
*/
@Test
@Alerts({"clicked", "fireEvent not available"})
public void fireEvent_WithoutTemplate() throws Exception {
final String html =
"<html>\n"
+ " <head>\n"
+ " <title>Test</title>\n"
+ " <script>\n"
+ " function doTest() {\n"
+ " var elem = document.getElementById('a');\n"
+ " if (!elem.fireEvent) { alert('fireEvent not available'); return }\n"
+ " elem.fireEvent('onclick');\n"
+ " }\n"
+ " </script>\n"
+ " </head>\n"
+ "<body>\n"
+ " <div id='a' onclick='alert(\"clicked\")'>foo</div>\n"
+ " <div id='b' onmouseover='doTest()'>bar</div>\n"
+ "</body></html>";
final WebDriver driver = loadPage2(html);
driver.findElement(By.id("a")).click();
verifyAlerts(driver, getExpectedAlerts()[0]);
final Actions actions = new Actions(driver);
actions.moveToElement(driver.findElement(By.id("b")));
actions.perform();
verifyAlerts(driver, getExpectedAlerts()[1]);
}
/**
* FF behaves is different.
* @throws Exception if an error occurs
*/
@Test
@Alerts(DEFAULT = "click href click doubleClick href ",
IE = "click href click doubleClick ")
@BuggyWebDriver(
FF = "click doubleClick click href href ",
FF68 = "click doubleClick click href href ",
FF60 = "click doubleClick click href href ")
@NotYetImplemented
public void doubleClick() throws Exception {
final String html =
"<html>\n"
+ "<body>\n"
+ " <a id='myAnchor' "
+ "href=\"javascript:document.getElementById('myTextarea').value+='href ';void(0);\" "
+ "onClick=\"document.getElementById('myTextarea').value+='click ';\" "
+ "onDblClick=\"document.getElementById('myTextarea').value+='doubleClick ';\">foo</a>\n"
+ " <textarea id='myTextarea'></textarea>\n"
+ "</body></html>";
final WebDriver driver = loadPage2(html);
final Actions action = new Actions(driver);
action.doubleClick(driver.findElement(By.id("myAnchor")));
action.perform();
assertEquals(getExpectedAlerts()[0], driver.findElement(By.id("myTextarea")).getAttribute("value"));
}
/**
* @throws Exception if the test fails
*/
@Test
@Alerts("click click dblclick ")
public void dblClick() throws Exception {
final String content = "<html>\n"
+ "<head>\n"
+ "<script>\n"
+ " function clickMe() {\n"
+ " document.getElementById('myTextarea').value+='click ';\n"
+ " }\n"
+ " function dblClickMe() {\n"
+ " document.getElementById('myTextarea').value+='dblclick ';\n"
+ " }\n"
+ "</script>\n"
+ "</head>\n"
+ "<body id='myBody' onclick='clickMe()' ondblclick='dblClickMe()'>\n"
+ "<textarea id='myTextarea'></textarea>\n"
+ "</body></html>";
final WebDriver driver = loadPage2(content);
final Actions action = new Actions(driver);
action.doubleClick(driver.findElement(By.id("myBody")));
action.perform();
assertEquals(getExpectedAlerts()[0], driver.findElement(By.id("myTextarea")).getAttribute("value"));
}
/**
* Hovers the mouse over the given element
* @param locator The element locator
*/
public void mouseHoverOverElement(By locator) {
SynchronisationService synchronisationService = new SynchronisationService();
synchronisationService.waitAndAssertForExpectedCondition(ExpectedConditions.visibilityOfElementLocated(locator));
WebElement element = getWebDriver().findElement(locator);
Actions builder = new Actions(getWebDriver());
Actions hoverOverRegistrar = builder.moveToElement(element);
hoverOverRegistrar.perform();
}
private void mouseOver(final String element) throws Exception {
final String html =
HtmlPageTest.STANDARDS_MODE_PREFIX_
+ "<html>\n"
+ " <head>\n"
+ " <script>\n"
+ " function dumpEvent(event) {\n"
+ " // target\n"
+ " var eTarget;\n"
+ " if (event.target) {\n"
+ " eTarget = event.target;\n"
+ " } else if (event.srcElement) {\n"
+ " eTarget = event.srcElement;\n"
+ " }\n"
+ " // defeat Safari bug\n"
+ " if (eTarget.nodeType == 3) {\n"
+ " eTarget = eTarget.parentNode;\n"
+ " }\n"
+ " var msg = 'mouse over';\n"
+ " if (eTarget.name) {\n"
+ " msg = msg + ' [' + eTarget.name + ']';\n"
+ " } else {\n"
+ " msg = msg + ' [' + eTarget.id + ']';\n"
+ " }\n"
+ " document.title+= msg;\n"
+ " }\n"
+ " </script>\n"
+ " </head>\n"
+ "<body>\n"
+ " <form id='form1'>\n"
+ " " + element + "\n"
+ " </form>\n"
+ "</body></html>";
final WebDriver driver = loadPage2(html);
final Actions actions = new Actions(driver);
actions.moveToElement(driver.findElement(By.id("tester")));
actions.perform();
assertTitle(driver, getExpectedAlerts()[0]);
}
private void mouseOver(final String element) throws Exception {
final String html =
HtmlPageTest.STANDARDS_MODE_PREFIX_
+ "<html>\n"
+ " <head>\n"
+ " <script>\n"
+ " function dumpEvent(event) {\n"
+ " // target\n"
+ " var eTarget;\n"
+ " if (event.target) {\n"
+ " eTarget = event.target;\n"
+ " } else if (event.srcElement) {\n"
+ " eTarget = event.srcElement;\n"
+ " }\n"
+ " // defeat Safari bug\n"
+ " if (eTarget.nodeType == 3) {\n"
+ " eTarget = eTarget.parentNode;\n"
+ " }\n"
+ " var msg = 'mouse over';\n"
+ " if (eTarget.name) {\n"
+ " msg = msg + ' [' + eTarget.name + ']';\n"
+ " } else {\n"
+ " msg = msg + ' [' + eTarget.id + ']';\n"
+ " }\n"
+ " document.title += msg;\n"
+ " }\n"
+ " </script>\n"
+ " </head>\n"
+ "<body>\n"
+ " <form id='form1'>\n"
+ " " + element + "\n"
+ " </form>\n"
+ "</body></html>";
final WebDriver driver = loadPage2(html);
final Actions actions = new Actions(driver);
actions.moveToElement(driver.findElement(By.id("tester")));
actions.perform();
assertTitle(driver, getExpectedAlerts()[0]);
}
/**
* @throws Exception if an error occurs
*/
@Test
@Alerts(DEFAULT = {"o-mouse over [option1]", "s-mouse over [option1]"},
IE = {})
public void mouseOver() throws Exception {
shutDownRealIE();
final String html =
HtmlPageTest.STANDARDS_MODE_PREFIX_
+ "<html>\n"
+ " <head>\n"
+ " <title>Test</title>\n"
+ " <script>\n"
+ " function dumpEvent(event, pre) {\n"
+ " // target\n"
+ " var eTarget;\n"
+ " if (event.target) {\n"
+ " eTarget = event.target;\n"
+ " } else if (event.srcElement) {\n"
+ " eTarget = event.srcElement;\n"
+ " }\n"
+ " // defeat Safari bug\n"
+ " if (eTarget.nodeType == 3) {\n"
+ " eTarget = eTarget.parentNode;\n"
+ " }\n"
+ " var msg = pre + '-mouse over';\n"
+ " if (eTarget.name) {\n"
+ " msg = msg + ' [' + eTarget.name + ']';\n"
+ " } else {\n"
+ " msg = msg + ' [' + eTarget.id + ']';\n"
+ " }\n"
+ " alert(msg);\n"
+ " }\n"
+ " </script>\n"
+ " </head>\n"
+ "<body>\n"
+ " <form id='form1'>\n"
+ " <select name='select1' id='select1' size='2' onmouseover='dumpEvent(event, \"s\");' >\n"
+ " <option value='option1' id='option1' onmouseover='dumpEvent(event, \"o\");' >Option1</option>\n"
+ " <option value='option2' id='option2'>Option2</option>\n"
+ " </select>\n"
+ " </form>\n"
+ "</body></html>";
final WebDriver driver = loadPage2(html);
final Actions actions = new Actions(driver);
actions.moveToElement(driver.findElement(By.id("option1")));
actions.perform();
verifyAlerts(driver, getExpectedAlerts());
}
/**
* @throws Exception if an error occurs
*/
@Test
@Alerts(DEFAULT = "o-mouse over [option1]",
FF = "o-mouse over [option1] s-mouse over [option1]",
FF68 = "o-mouse over [option1] s-mouse over [option1]",
FF60 = "o-mouse over [option1] s-mouse over [option1]",
IE = "")
public void mouseOverDisabledSelect() throws Exception {
final String html =
HtmlPageTest.STANDARDS_MODE_PREFIX_
+ "<html>\n"
+ " <head>\n"
+ " <script>\n"
+ " function dumpEvent(event, pre) {\n"
+ " // target\n"
+ " var eTarget;\n"
+ " if (event.target) {\n"
+ " eTarget = event.target;\n"
+ " } else if (event.srcElement) {\n"
+ " eTarget = event.srcElement;\n"
+ " }\n"
+ " // defeat Safari bug\n"
+ " if (eTarget.nodeType == 3) {\n"
+ " eTarget = eTarget.parentNode;\n"
+ " }\n"
+ " var msg = pre + '-mouse over';\n"
+ " if (eTarget.name) {\n"
+ " msg = msg + ' [' + eTarget.name + ']';\n"
+ " } else {\n"
+ " msg = msg + ' [' + eTarget.id + ']';\n"
+ " }\n"
+ " document.title += ' ' + msg;\n"
+ " }\n"
+ " </script>\n"
+ " </head>\n"
+ "<body>\n"
+ " <form id='form1'>\n"
+ " <select name='select1' id='select1' size='2' disabled='disabled' "
+ "onmouseover='dumpEvent(event, \"s\");' >\n"
+ " <option value='option1' id='option1' onmouseover='dumpEvent(event, \"o\");'>Option1</option>\n"
+ " <option value='option2' id='option2'>Option2</option>\n"
+ " </select>\n"
+ " </form>\n"
+ "</body></html>";
final WebDriver driver = loadPage2(html);
final Actions actions = new Actions(driver);
actions.moveToElement(driver.findElement(By.id("option1")));
actions.perform();
assertTitle(driver, getExpectedAlerts()[0]);
}
/**
* @throws Exception if an error occurs
*/
@Test
@Alerts(DEFAULT = "",
FF = "s-mouse over [select1] o-mouse over [option1] s-mouse over [option1]",
FF68 = "s-mouse over [select1] o-mouse over [option1] s-mouse over [option1]",
FF60 = "s-mouse over [select1] o-mouse over [option1] s-mouse over [option1]")
public void mouseOverDisabledOption() throws Exception {
final String html =
HtmlPageTest.STANDARDS_MODE_PREFIX_
+ "<html>\n"
+ " <head>\n"
+ " <title></title>\n"
+ " <script>\n"
+ " function dumpEvent(event, pre) {\n"
+ " // target\n"
+ " var eTarget;\n"
+ " if (event.target) {\n"
+ " eTarget = event.target;\n"
+ " } else if (event.srcElement) {\n"
+ " eTarget = event.srcElement;\n"
+ " }\n"
+ " // defeat Safari bug\n"
+ " if (eTarget.nodeType == 3) {\n"
+ " eTarget = eTarget.parentNode;\n"
+ " }\n"
+ " var msg = pre + '-mouse over';\n"
+ " if (eTarget.name) {\n"
+ " msg = msg + ' [' + eTarget.name + ']';\n"
+ " } else {\n"
+ " msg = msg + ' [' + eTarget.id + ']';\n"
+ " }\n"
+ " if (msg.length == 0) { msg = '-' };\n"
+ " document.title += ' ' + msg;\n"
+ " }\n"
+ " </script>\n"
+ " </head>\n"
+ "<body>\n"
+ " <form id='form1'>\n"
+ " <select name='select1' id='select1' size='2' onmouseover='dumpEvent(event, \"s\");' >\n"
+ " <option value='option1' id='option1' onmouseover='dumpEvent(event, \"o\");' "
+ "disabled='disabled'>Option1</option>\n"
+ " <option value='option2' id='option2'>Option2</option>\n"
+ " </select>\n"
+ " </form>\n"
+ "</body></html>";
final WebDriver driver = loadPage2(html);
final Actions actions = new Actions(driver);
actions.moveToElement(driver.findElement(By.id("option1")));
actions.perform();
assertTitle(driver, getExpectedAlerts()[0]);
}
/**
* @throws Exception if an error occurs
*/
@Test
@Alerts("mouse over [btn]")
public void mouseOver() throws Exception {
final String html =
HtmlPageTest.STANDARDS_MODE_PREFIX_
+ "<html>\n"
+ " <head>\n"
+ " <title>Test</title>\n"
+ " <script>\n"
+ " function dumpEvent(event) {\n"
+ " // target\n"
+ " var eTarget;\n"
+ " if (event.target) {\n"
+ " eTarget = event.target;\n"
+ " } else if (event.srcElement) {\n"
+ " eTarget = event.srcElement;\n"
+ " }\n"
+ " // defeat Safari bug\n"
+ " if (eTarget.nodeType == 3) {\n"
+ " eTarget = eTarget.parentNode;\n"
+ " }\n"
+ " var msg = 'mouse over';\n"
+ " if (eTarget.name) {\n"
+ " msg = msg + ' [' + eTarget.name + ']';\n"
+ " } else {\n"
+ " msg = msg + ' [' + eTarget.id + ']';\n"
+ " }\n"
+ " alert(msg);\n"
+ " }\n"
+ " </script>\n"
+ " </head>\n"
+ "<body>\n"
+ " <form id='form1'>\n"
+ " <button id='btn' onmouseover='dumpEvent(event);'>button</button><br>\n"
+ " </form>\n"
+ "</body></html>";
final WebDriver driver = loadPage2(html);
final Actions actions = new Actions(driver);
actions.moveToElement(driver.findElement(By.id("btn")));
actions.perform();
verifyAlerts(driver, getExpectedAlerts());
}
/**
* @throws Exception if an error occurs
*/
@Test
@Alerts(DEFAULT = "Test:",
FF = "Test:mouse over [disabledBtn]",
FF68 = "Test:mouse over [disabledBtn]",
FF60 = "Test:mouse over [disabledBtn]")
public void mouseOverDiabled() throws Exception {
final String html =
HtmlPageTest.STANDARDS_MODE_PREFIX_
+ "<html>\n"
+ " <head>\n"
+ " <title>Test:</title>\n"
+ " <script>\n"
+ " function dumpEvent(event) {\n"
+ " // target\n"
+ " var eTarget;\n"
+ " if (event.target) {\n"
+ " eTarget = event.target;\n"
+ " } else if (event.srcElement) {\n"
+ " eTarget = event.srcElement;\n"
+ " }\n"
+ " // defeat Safari bug\n"
+ " if (eTarget.nodeType == 3) {\n"
+ " eTarget = eTarget.parentNode;\n"
+ " }\n"
+ " var msg = 'mouse over';\n"
+ " if (eTarget.name) {\n"
+ " msg = msg + ' [' + eTarget.name + ']';\n"
+ " } else {\n"
+ " msg = msg + ' [' + eTarget.id + ']';\n"
+ " }\n"
+ " document.title += msg;\n"
+ " }\n"
+ " </script>\n"
+ " </head>\n"
+ "<body>\n"
+ " <form id='form1'>\n"
+ " <button id='disabledBtn' onmouseover='dumpEvent(event);' disabled>disabled button</button><br>\n"
+ " </form>\n"
+ "</body></html>";
final WebDriver driver = loadPage2(html);
final Actions actions = new Actions(driver);
actions.moveToElement(driver.findElement(By.id("disabledBtn")));
actions.perform();
assertTitle(driver, getExpectedAlerts()[0]);
}
public void processKeyDownKeyUpEvents(WebDriver wd, JSONObject event) throws UnsupportedEncodingException
{
WebElement element = findTargetWebElement(wd, event, UserScenario.getTargetForEvent(event));
if (isNoOp(event, element))
{
return;
}
//TODO remove this when recording of cursor in text box is implemented
if (skipKeyboardForElement(element))
{
LOG.warn("Keyboard processing for non empty Date is disabled");
return;
}
if (!event.has(EventConstants.KEY_CODE) && !event.has(EventConstants.CHAR_CODE))
{
throw new IllegalStateException("Keydown/Keyup event don't have keyCode/charCode property");
}
Actions actions = new Actions(wd);
SPECIAL_KEYS_MAPPING.keySet().forEach(property -> {
if (event.getBoolean(property))
{
actions.keyDown(element, SPECIAL_KEYS_MAPPING.get(property));
}
});
// Backward compatibility
int code = event.getInt(EventConstants.CHAR_CODE);
if (code == 0)
{
code = event.getInt(EventConstants.KEY_CODE);
}
switch (code)
{
case 8:
actions.sendKeys(element, Keys.BACK_SPACE);
break;
case 27:
actions.sendKeys(element, Keys.ESCAPE);
break;
case 46:
actions.sendKeys(element, Keys.DELETE);
break;
case 13:
actions.sendKeys(element, Keys.ENTER);
break;
case 37:
actions.sendKeys(element, Keys.ARROW_LEFT);
break;
case 38:
actions.sendKeys(element, Keys.ARROW_UP);
break;
case 39:
actions.sendKeys(element, Keys.ARROW_RIGHT);
break;
case 40:
actions.sendKeys(element, Keys.ARROW_DOWN);
break;
}
SPECIAL_KEYS_MAPPING.keySet().forEach(property -> {
if (event.getBoolean(property))
{
actions.keyUp(element, SPECIAL_KEYS_MAPPING.get(property));
}
});
try
{
actions.perform();
}
catch (Exception ex)
{
// TODO Fix correctly
LOG.error("Sending keys to and invisible element. must have JS workaround: " + ex.toString(), ex);
}
}
/**
* Drags an element some place else
*
* @param draggable
* The element to drag
* @param droppable
* The drop aim
* @throws InterruptedException
*/
public void dragElementTo(By draggable, By droppable) throws InterruptedException {
WebDriver driver = getWebDriver();
Actions clickAndDrag = new Actions(getWebDriver());
clickAndDrag.dragAndDrop(driver.findElement(draggable), driver.findElement(droppable));
clickAndDrag.perform();
}
/**
* Drags an element some place else
*
* @param draggable
* The element to drag
* @param x
* Offset
* @param y
* Offset
* @throws InterruptedException
*/
public void dragElementTo(By draggable, int x, int y) throws InterruptedException {
WebDriver driver = getWebDriver();
Actions clickAndDrag = new Actions(getWebDriver());
clickAndDrag.dragAndDropBy(driver.findElement(draggable), x, y);
clickAndDrag.perform();
}