如何在硒网格中执行拖拽

时间:2015-03-16 14:33:15

标签: java selenium

我成功安装了selenium网格,但无法在selenium网格中执行拖放操作。如果我在testng中运行代码它正在按预期工作,对于网格来说它失败了我学习selenium grid因此无法解决这个问题,

jar:selenium-server-standalone-2.45.0.jar

Selenium JAR:来自“selenium-2.45.0”(最新)的所有罐子

CODE:

public class DragdropElements {WebDriver driver;
String nodeURL;
@BeforeTest
public void draganddrop() throws MalformedURLException
{
    // driver = new FirefoxDriver();
    System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Jdk14Logger");
    nodeURL = "http://localhost:4444/wd/hub";
    DesiredCapabilities cap = new DesiredCapabilities();
    cap.setBrowserName("firefox");
/// cap.setVersion("31.0");
    //cap.setPlatform(Platform.VISTA);
 driver = new RemoteWebDriver(new URL(nodeURL),cap);
    driver.get("http://only-testing-blog.blogspot.in/2014/09/drag-and-drop.html");
}

@Test
public void DragdropElements1() throws InterruptedException   
{
WebElement DragFrom = driver.findElement(By.xpath("//*[@id='dragdiv']"));
WebElement DragTo = driver.findElement(By.xpath("//*[@id='dropdiv']"));

    Actions builder = new Actions(driver);
    Action dragAndDrop3 =  builder.dragAndDrop(DragFrom, DragTo).build();
    dragAndDrop3.perform();

    String Texttocompare = driver.findElement(By.xpath("//*[@id='Blog1']/div[1]/div/div/div/div[1]/h3")).getText();
    System.out.println(""+Texttocompare);
Assert.assertEquals(Texttocompare, "Drag and Drop");
}
}

例外:

  

java.lang.ClassCastException:org.openqa.selenium.remote.RemoteWebDriver无法强制转换为org.openqa.selenium.interactions.HasInputDevices     在org.openqa.selenium.interactions.Actions。(Actions.java:41)     在qa.DragdropElements.DragdropElements1(DragdropElements.java:45)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

1 个答案:

答案 0 :(得分:0)

试试这个,它可能会起作用

Actions actionsBuilder = new Actions(driver);
builder.keyDown(Keys.CONTROL)
   .click(elementName)
   .keyUp(Keys.CONTROL);
Action action = actionsBuilder.build();
action.perform();   

不确定为什么拖放不适合你。如果您在节点中运行多线程,请尝试仅使用音调线程运行。此外,它值得一提您的节点操作系统信息。