Selenium HTML5 Drag-and-drop workaround in java using javascript shows an error

十年热恋 提交于 2019-12-11 17:11:03

问题


I am trying to use java script workaround using https://github.com/vikramvi/Selenium-Java/commit/a1354ca5854315fded8fc80ba24a4717927d08c7 to get Drag and Drop in HTML5 working with selenium Webdriver with java on Chrome. The application I'm trying to test just has drag and drop function and I need to make this working but I keep getting error. Please help!

Here is my code:

@Then("^I drag element having css from \"([^\"]*)\" to element having css \"([^\"]*)\"$")
public void i_drag_element_having_css_from_to_element_having_css(String arg1, String arg2) throws InterruptedException, IOException {

        final String JQUERY_LOAD_SCRIPT = ("/Users/Downloads/selenium-cucumber-java-dev/src/test/resources/jquery_load_helper.js");
        String jQueryLoader = readFile(JQUERY_LOAD_SCRIPT);

        //System.out.println("Javascript:" + jQueryLoader);
        driver.manage().timeouts().setScriptTimeout(10, TimeUnit.SECONDS);
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeAsyncScript(
                jQueryLoader /* , http://localhost:8080/jquery-1.7.2.js */);


        js.executeScript("jQuery(function($) { " + " $('input[name=\"q\"]').val('bada-bing').closest('form').submit(); "
                + " }); ");

        final String DROP = ("/Users/Downloads/selenium-cucumber-java-dev/src/test/resources/drag_and_drop_helper.js");
        String scriptLoader = readFile(DROP);
        //System.out.println("Javascript:" + scriptLoader);
        driver.manage().timeouts().setScriptTimeout(10, TimeUnit.SECONDS);
        JavascriptExecutor js1 = (JavascriptExecutor) driver;
        js1.executeScript(
                scriptLoader /* , http://localhost:8080/jquery-1.7.2.js */);
        //((JavascriptExecutor) driver).executeScript(js1 + "$('.btn.btn-primary.btn-xs.btn-block.formcomponent.ng-binding').simulateDragDrop({ dropTarget: '#firstName'});");
        ((JavascriptExecutor) driver).executeScript(js1 + "$('" + arg1 + "').simulateDragDrop({ dropTarget: '" + arg2 + "'});");


}

private static String readFile(String file) throws IOException {
    Charset cs = Charset.forName("UTF-8");
    FileInputStream stream = new FileInputStream(file);
    try {
        Reader reader = new BufferedReader(new InputStreamReader(stream, cs));
        StringBuilder builder = new StringBuilder();
        char[] buffer = new char[8192];
        int read;
        while ((read = reader.read(buffer, 0, buffer.length)) > 0) {
            builder.append(buffer, 0, read);
        }
        return builder.toString();
    } finally {
        stream.close();
    }
}

So this is my cucumber step Then I drag element having css from ".btn.btn-primary.btn-xs.btn-block.formcomponent.ng-binding" to element having css "#firstName" I have passed the css selector and used it in the last line((JavascriptExecutor) driver).executeScript(js1 + "$('" + arg1 + "').simulateDragDrop({ dropTarget: '" + arg2 + "'});"); I have also tried to hardcode the last line by giving the css selector directly and it also not working.

I am getting the following error

(Scenario: Sampel feature): unknown error: Runtime.evaluate threw exception: SyntaxError: Unexpected identifier(..)

来源:https://stackoverflow.com/questions/46812774/selenium-html5-drag-and-drop-workaround-in-java-using-javascript-shows-an-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!