2015-03-19 93 views
1

我現在正在自動化一個web應用程序。我已經使用了一個列表數組來定位一個容器內的幾個對象。我需要做的是,我必須將鼠標懸停在第一個元素上並單擊相同的按鈕。我在另一個類中寫入的鼠標懸停方法作爲常用函數。因此,我可以使用列表數組的對象以任何方式傳遞給鼠標懸停方法。我們可以將列表<WebElement>的對象作爲參數傳遞給函數嗎?

SCREEN SHOT

找到在容器中的元素。

By by = By.xpath("//ul[@id='sortable']"); 
List<WebElement> featureList= element.findElements(by.tagName("a")); 


//Mouse-hover method 

public static void moveMouseOver(WebDriver driver, By locator) { 
     WebElement element = waitForElementPresent(driver, locator); 
     (new Actions(driver)).moveToElement(element).build().perform(); 
    } 


Here can I change the 'By Locator' argument to replace with List array object ? 

回答

1

您可以將您的方法更改爲:

public static void moveMouseOver(WebDriver driver, By locator, String...action) { 
    List<WebElement> lstElements = driver.findElements(locator); 
    for (WebElement webelement : lstElements){ 
     if (action.length > 0 && action.equalsIgnoreCase("click")) 
      (new Actions(driver)).moveToElement(element).click().build().perform(); 
     else 
      (new Actions(driver)).moveToElement(element).build().perform(); 
} 

在這種情況下它會爲單個元素也和多工作,你將不會被要求改變使用在先前的方案,雖然u需要在今後的處理不同的情況,因爲這一個處理單擊只。

+0

It限制在'MoveMouseOver方法中插入WebElement webElements,它返回錯誤爲「ExecutionHelper類型中的方法moveMouseOver(WebDriver,By,String)不適用於參數(WebDriver,By,Object [])」 – 2015-03-20 06:48:24

+0

調用方法? – 2015-03-20 06:49:53

+0

ExecutionHelper.moveMouseOver(driver,by,featureList.toArray()); – 2015-03-20 06:54:44

2

你可以試着改變moveMouseOver喜歡的東西:

public static void moveMouseOver(WebDriver driver, WebElement... webElements){ 
    if(null != webElements){ 
     for(WebElement webEl : webElements){ 
      // do something here 
     } 
     } 
    } 

,然後把這個作爲

moveMouseOver(driver, ((WebElement[])featureList.toArray())) 

的語法錯誤,請檢查是否有因爲我只寫了這個在這裏而不是在IDE中檢查

+0

它限制在'MoveMouseOver方法中插入WebElement webElements,它將返回錯誤爲'_ExecutionHelper類型中的方法moveMouseOver(WebDriver,WebElement)不適用於參數(WebDriver,Object [])Custom Components_「 – 2015-03-19 09:42:35

相關問題