如何在geb spock中进行鼠标悬停

How to do mouse hover in geb spock

我正在使用 geb spock 并尝试将鼠标悬停在某个元素上,但是出现错误。以下是详细信息。 页面对象 Class

class HomePage extends Page {

    static at ={
        title.contains("Activity Dashboard")
    }

    static content = {
        tabConnections (wait : true) {$("a", "class" : contains("dropdown-toggle"), "text" : "Connections")}
        subMenuManageConnections (wait: true) {tabConnections.find("ul").find("a" , "href": "/managecash/EDGE_Network" , "text" : "Manage Connections")}
    }


    public void mouseHoverMethod(){
        waitFor {tabConnections.displayed}

        Actions actions = new Actions(driver)
        actions.moveToElement(tabConnections).build().perform()
    }
}

当我从我的 spock 规范文件调用 mouseHoverMethod 方法时,收到以下错误消息: 在线 (actions.moveToElement(tabConnections).build().perform()) 如下:

错误信息:

groovy.lang.MissingMethodException: No signature of method: org.openqa.selenium.interactions.Actions.moveToElement() is applicable for argument types: (geb.content.TemplateDerivedPageContent) values: [pageobjects.general.HomePage -> tabConnections: geb.navigator.NonEmptyNavigator] Possible solutions: moveToElement(org.openqa.selenium.WebElement), moveToElement(org.openqa.selenium.WebElement, int, int)

你能帮我在 Geb Spock 中如何实现鼠标悬停吗?

错误消息告诉您您正在为 moveToElement() 方法提供一个 TemplateDerivedPageContent 实例 (tabConnections)。但是,如果您检查该方法的签名,您会发现它需要一个 WebElement 参数。当然,Selenium WebDriver 对 Geb-specific 类 一无所知。因此,您必须像这样从导航器中获取网络元素:

actions.moveToElement(tabConnections.firstElement()).build().perform()

您也可以使用 Geb 的交互块,参见 http://www.gebish.org/manual/current/#complex-interactions

你的方法看起来像 ->

public void mouseHoverMethod(){
    waitFor {tabConnections.displayed}
    interact {
        moveToElement(tabConnections)
    }
}

@kriegaex,@erdi。 感谢您的解决方案。我还能够找到一个有效的解决方案并在页面对象中创建以下方法。三种方法都很好。

public void mouseHoverMethodOne (TemplateDerivedPageContent element){
        waitFor {element}
        element.jquery.mouseover()
        element.click()
    }

public void mouseHoverMethodTwo (TemplateDerivedPageContent element){
        waitFor {element.displayed}
        Actions actions = new Actions(driver)
        actions.moveToElement(element.firstElement()).build().perform()
        element.click()
    }

public void mouseHoverMethodThree (TemplateDerivedPageContent element){
        waitFor {element.displayed}
        interact {
            moveToElement(element)
        }
        element.click()
    }

感谢您对此的帮助。我也对您的回答进行了评分,因为这些给了我很多见解。