简体   繁体   English

webdriver中的多个动作系列

[英]Series of Multiple Actions in webdriver

I am just trying to enter a text in capital letters using set of keyboard actions.我只是想使用一组键盘操作以大写字母输入文本。 Here code using interface 'Action':这里使用接口“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();

Above is working fine.以上工作正常。

But if we do not use interface, it is not working why ???但是如果我们不使用接口,它不起作用为什么??? Though this is executing fine but not performing the task.虽然这执行得很好但没有执行任务。 I think both should work.我认为两者都应该有效。

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

In your second example, it's because you're calling build() then perform()在你的第二个例子中,这是因为你调用build()然后perform()

If you look at the Actions class implementations of those two methods:如果您查看这两个方法的 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();
  }

When you call build() it actually resets the internal state of the builder!当您调用build() 时,它实际上会重置构建器的内部状态!

After that when you call perform() it rebuilds an empty object reference which has no behavior defined.之后,当您调用perform() 时,它会重建一个没有定义行为的空对象引用。

To correct the problem, I suggest replacing your call to build() with a call to perform() as shown below.为了解决这个问题,我建议您将调用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();

I would expect that to do what you want, based on the implementation.我希望根据实现来做你想做的事。

My investigation was done against selenium v 2.53.0我的调查是针对 selenium v​​ 2.53.0 进行的

Because Actions#build() method resets a state of itself before returning the action,因为Actions#build()方法在返回动作之前重置了自身的状态,
see the implementation here: 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在这里查看实现: 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  }

Pay attention to resetCompositeAction();注意resetCompositeAction(); call - it resets the builder.调用 - 它重置构建器。

If you perform this:如果您执行此操作:

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

Then build() returns the action and resets a state of builder object.然后build()返回操作并重置builder对象的状态。
Then if you call builder.perform() , it's do nothing.然后如果你调用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