【问题标题】:Unable to perform HTML5 drag and drop using javascript for Selenium WebDriver test无法使用用于 Selenium WebDriver 测试的 javascript 执行 HTML5 拖放
【发布时间】: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


    【解决方案1】:

    您的问题是 JQuery 使用类似 CSS 的语法。 Xpath 在这种情况下不起作用。

    如果您必须使用 Xpath,则必须先将 Xpath 字符串转换为 CSS,然后再将其附加到此 JQuery 字符串中:

    javaScript = javaScript + "$('" + source + "').simulateDragDrop({ dropTarget: '" + target + "'});";
    

    如果您只是使用 Xpath 来使用 ID 识别 divs,那么您可以在 Java 中尝试:

    Pattern pattern = Pattern.compile("'(.*?)'");
    Matcher matcherSource = pattern.matcher(source);
    Matcher matcherTarget = pattern.matcher(target);
    String cssSource = "#" + matcherSource.group(1);
    String cssTarget = "#" + matcherTarget.group(1);
    javaScript = javaScript + "$('" + cssSource + "').simulateDragDrop({ dropTarget: '" + cssTarget + "'});";
    

    【讨论】:

      猜你喜欢
      • 2017-02-04
      • 1970-01-01
      • 2012-04-05
      • 1970-01-01
      • 1970-01-01
      • 2014-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多