繁体   English   中英

webdriver中的多个动作系列

[英]Series of Multiple Actions in webdriver

我只是想使用一组键盘操作以大写字母输入文本。 这里使用接口“Action”的代码:

WebElement element = driver.findElement(By.id("email")); 
Actions builder = new Actions(driver); 
Action act = builder.moveToElement(element)
                    .keyDown(element,Keys.SHIFT)
                    .sendKeys("vishal")
                    .keyUp(Keys.SHIFT)
                    .build();
act.perform();

以上工作正常。

但是如果我们不使用接口,它不起作用为什么??? 虽然这执行得很好但没有执行任务。 我认为两者都应该有效。

WebElement element = driver.findElement(By.id("email")); 
Actions builder = new Actions(driver);
builder.moveToElement(element)
       .keyDown(element,Keys.SHIFT)
       .sendKeys("vishal")
       .keyUp(Keys.SHIFT)
       .build();
builder.perform();

在你的第二个例子中,这是因为你调用build()然后perform()

如果您查看这两个方法的 Actions 类实现:

  /**
   * Generates a composite action containing all actions so far, ready to be performed (and
   * resets the internal builder state, so subsequent calls to build() will contain fresh
   * sequences).
   *
   * @return the composite action
   */
  public Action build() {
    CompositeAction toReturn = action;
    resetCompositeAction();
    return toReturn;
  }

  /**
   * A convenience method for performing the actions without calling build() first.
   */
  public void perform() {
    build().perform();
  }

当您调用build() 时,它实际上会重置构建器的内部状态!

之后,当您调用perform() 时,它会重建一个没有定义行为的空对象引用。

为了解决这个问题,我建议您将调用build()替换为调用perform() ,如下所示。

 WebElement element = driver.findElement(By.id("email")); 
 Actions builder = new Actions(driver);
 builder.moveToElement(element).keyDown(element, Keys.SHIFT).sendKeys("vishal").keyUp(Keys.SHIFT).perform();

我希望根据实现来做你想做的事。

我的调查是针对 selenium v​​ 2.53.0 进行的

因为Actions#build()方法在返回动作之前重置了自身的状态,
在这里查看实现: http : //grepcode.com/file/repo1.maven.org/maven2/org.seleniumhq.selenium/selenium-api/2.18.0/org/openqa/selenium/interactions/Actions.java#Actions .build%28%29

338
339  public Action More ...build() {
340    CompositeAction toReturn = action;
341    resetCompositeAction();
342    return toReturn;
343  }

注意resetCompositeAction(); 调用 - 它重置构建器。

如果您执行此操作:

 builder............
        ........ ().build();
builder.perform();

然后build()返回操作并重置builder对象的状态。
然后如果你调用builder.perform() ,它什么都不做。

builder.moveToElement(element)
       .keyDown(element,Keys.SHIFT)
       .sendKeys("vishal")
       .keyUp(Keys.SHIFT)
       .build().perform();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM