0

I have tried to simulate drag and drop for Selenium WebDriver test using the following JavaScript call, but I get a WebDriver error. I tried this way, as with Actions class didn't work on any browser (Firefox, Chrome).
Can someone tell what I am doing wrong?

String filePath = "C://Work//Spica import files//drag_and_drop_helper.js";
String source = "li[draggable='true']";
String target = "#fieldCc";
StringBuffer buffer = new StringBuffer();
String line;
BufferedReader br = null;
try {
  br = new BufferedReader(new FileReader(filePath));
} catch (FileNotFoundException e) {
  e.printStackTrace();
}

try {
  while((line = br.readLine())!=null)
    buffer.append(line);
} catch (IOException e) {
    e.printStackTrace();
}

String javaScript = buffer.toString();
javaScript = javaScript + "$('" + source + "').simulateDragDrop({ dropTarget: '" + target + "'});";
((JavascriptExecutor)getDriver()).executeScript(javaScript);

The error that I get is:

org.openqa.selenium.WebDriverException: unknown error: Runtime.evaluate threw exception: SyntaxError: missing ) after argument list (Session info: chrome=53.0.2785.116) (Driver info: chromedriver=2.9.248315,platform=Windows NT 6.3 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 17 milliseconds Build info: version: '2.53.1', revision: 'a36b8b1cd5757287168e54b817830adce9b0158d', time: '2016-06-30 19:26:09' System info: host: 'EN610188', ip: '172.16.116.151', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_91' Driver info: org.openqa.selenium.chrome.ChromeDriver Capabilities [{applicationCacheEnabled=false, rotatable=false, chrome={userDataDir=C:\Users\lgrecu\AppData\Local\Temp\scoped_dir5352_12600}, takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true, version=53.0.2785.116, platform=WIN8_1, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}] Session ID: fe314c5e2184e76f1b3d934159ae2887 Build info: version: '2.53.1', revision: 'a36b8b1cd5757287168e54b817830adce9b0158d', time: '2016-06-30 19:26:09' System info: host: 'EN610188', ip: '172.16.116.151', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_91' Driver info: driver.version: unknown

1 Answer 1

1

Try removing the single quotes from String source = "li[draggable='true']"; and try now. guess it will work.

String source = "li[draggable=true]";
Sign up to request clarification or add additional context in comments.

6 Comments

welcome:) if my solution worked then make a vote for it.
Do you have any idea how could I convert this xpath: .//*[contains(@class,'removable secure')]//span[@title='value']/ancestor::li, into a css locator ? I have to use dynamic xpaths for simulating drag and drop action, so value will be replaced with multiple strings
Instead of passing the element locators into executeScript method you can use something like this, WebElement ele1 = driver.findElement(By.xpath("someXpath")); WebElement ele2 = driver.findElement(By.xpath("someXpath")); javaScript = javaScript + "arguments[0].simulateDragDrop({ dropTarget: arguments[1] });"; ((JavascriptExecutor)getDriver()).executeScript(javaScript,ele1,ele2);
I tried that approach, but I get this error: org.openqa.selenium.WebDriverException: unknown error: arguments[0].simulateDragDrop is not a function (Session info: chrome=53.0.2785.116) (Driver info: chromedriver=2.9.248315,platform=Windows NT 6.3 x86_64) (WARNING: The server did not provide any stacktrace information)
Eventually, it worked with: javaScript = javaScript + "$(arguments[0]).simulateDragDrop({ dropTarget: arguments[1]});";
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.