Selenium HTML5使用javascript在java中拖放解决方法显示错误

时间:2017-10-18 14:37:29

标签: javascript java html5 selenium drag-and-drop

我正在尝试使用https://github.com/vikramvi/Selenium-Java/commit/a1354ca5854315fded8fc80ba24a4717927d08c7使用java脚本解决方法来使用selenium Webdriver和Chrome上的java一起使用HTML5中的Drag and Drop。我正在尝试测试的应用程序只有拖放功能,我需要让它工作但我不断收到错误。请帮忙!

这是我的代码:

@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();
    }
}

所以这是我的黄瓜步骤 然后我将带有css的元素从“.btn.btn-primary.btn-xs.btn-block.formcomponent.ng-binding”拖到具有css的元素“# firstName“ 我已经通过了css选择器并在最后一行使用了它((JavascriptExecutor) driver).executeScript(js1 + "$('" + arg1 + "').simulateDragDrop({ dropTarget: '" + arg2 + "'});");我还尝试通过直接提供css选择器来硬编码最后一行,它也无效。< / p>

我收到以下错误

  

(场景:Sampel功能):未知错误:Runtime.evaluate引发异常:SyntaxError:意外的标识符(..)

0 个答案:

没有答案