簡體   English   中英

Selenium Java 拖放與 HTML5

[英]Selenium Java Drag and Drop with HTML5

在這里的幾個線程中,發布了針對 selenium 拖放的解決方法,其中使用 HTML5 進行拖放的頁面。 This work-around involves using javascript to simulate the drag and drop, for example Unable to perform HTML5 drag and drop using javascript for Selenium WebDriver test , and https://gist.github.com/rcorreia/2362544 . 此解決方案在此頁面上運行良好, http://the-internet.herokuapp.com/drag_and_drop

The general approach is to read the javascript file here ( https://gist.github.com/rcorreia/2362544#file-drag_and_drop_helper-js ) into a string, referred to as 'jsfile' below.

然后在 selenium(使用 java)中,傳入源和目標的 css 選擇器,其中 #column-a 是源的 id,#column-b 是目標。

 ((JavascriptExecutor) driver).executeScript(jsfile +"$('#column-a').simulateDragDrop({ dropTarget: '#column-b'});");

它在那個頁面上就像一個冠軍。

但是,類似的方法似乎在此頁面上不起作用, https://crossbrowsertesting.github.io/drag-and-drop.html 當我跑步時什么都沒有發生

 ((JavascriptExecutor) driver).executeScript(jsfile +"$('#draggable').simulateDragDrop({ dropTarget: '#droppable'});");

我有一些看起來像第二頁的頁面(例如,沒有拖放)。 作為理解這一點的第一步,我想知道為什么這種方法在后一種情況下似乎不起作用。

On re-testing https://crossbrowsertesting.github.io/drag-and-drop.html , it looks like the straight-forward use of the Actions class does the trick for drag and drop. 在我正在測試的特定應用程序中,它設置了一些額外的代碼來幫助實現可訪問性,我能夠通過將焦點設置在第一個元素上並點擊返回鍵,然后將焦點設置在目標元素並再次點擊返回。 我相當確定這是自定義事件處理,因此可能不適用於其他應用程序。 以防萬一,我在這里發布了代碼,它在 selenium 中執行此操作。

 public void dndHtml5(String xPathSource, String xPathDestination) {
    clickEnterKeyOnElement(xPathSource);
    clickEnterKeyOnElement(xPathDestination);
}

public void clickEnterKeyOnElement(String xPath) {
    setFocusOnElement(xPath);
    WebElement target=element(xPath);
    target.sendKeys(Keys.ENTER);
}

public void setFocusOnElement(String xPath) {
    WebElement element = element(xPath);
    Actions actions = new Actions(driver);
    actions.moveToElement(element).build().perform();
}

public WebElement element(String xPath){
    return driver.findElementByXPath(xPath);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM