簡體   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