【发布时间】:2014-07-14 17:44:46
【问题描述】:
为了实现 Selenium 测试的拖放,我参考了http://elementalselenium.com/tips/39-drag-and-drop 那里提到使用javascript(来自https://gist.github.com/rcorreia/2362544)来处理拖放。
我按原样实现了它并且它有效。但就我而言,我有源元素和目标元素的动态 xpath。为了实现这一点,我尝试了以下代码:
package org.test.selenium;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class HTML5DragAndDrop {
WebDriver driver = null;
@BeforeClass
public void setUp(){
System.out.println(System.getProperty("user.dir"));
String chromeDriver = System.getProperty("user.dir")+ File.separator + "drivers" + File.separator + "chromedriver.exe";
System.setProperty("webdriver.chrome.driver", chromeDriver);
driver = new ChromeDriver();
driver.get("http://the-internet.herokuapp.com/drag_and_drop");
}
@AfterClass
public void tearDown(){
driver.quit();
}
@Test
public void testDragAndDrop() throws IOException, InterruptedException {
String filePath = "C://dnd.js";
String source = "//div[@id='column-a']";
String target = "//div[@id='column-b']";
StringBuffer buffer = new StringBuffer();
String line;
BufferedReader br = new BufferedReader(new FileReader(filePath));
while((line = br.readLine())!=null)
buffer.append(line);
String javaScript = buffer.toString();
javaScript = javaScript + "$('" + source + "').simulateDragDrop({ dropTarget: '" + target + "'});";
((JavascriptExecutor)driver).executeScript(javaScript);
}
}
但它给出了错误:
org.openqa.selenium.WebDriverException: unknown error: Runtime.evaluate threw exception: SyntaxError: Unexpected identifier
(会话信息:chrome=35.0.1916.153)
但是,如果像下面这样使用源和目标作为 css,它工作得非常好:
String source = "#column-a";
String target = "#column-b";
有人可以建议我需要进行哪些更改,以便上面可以使用 xpaths 的源元素和目标元素吗?就我而言,我只能使用 xpath,而只能使用 xpath。
【问题讨论】:
标签: javascript jquery xpath selenium-webdriver