Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xpath Geb:在运行时定义页面对象_Xpath_Groovy_Geb - Fatal编程技术网

Xpath Geb:在运行时定义页面对象

Xpath Geb:在运行时定义页面对象,xpath,groovy,geb,Xpath,Groovy,Geb,我正在使用geb自动化web应用程序。我想定义一个页面对象,我们称它为Page1。根据环境的不同,Page1可以有不同数量的按钮元素。在一个环境中,按钮“A”可能存在,而在另一个环境中,按钮“A”可能不存在 在运行时,我能够根据我使用的环境下载预期按钮的列表。让我们称之为列表按钮列表。我想使用此列表在运行时定义我的页面对象 这就是我试图解决这个问题的方法,但似乎不起作用。我总是会遇到这样的错误:“groovy.lang.MissingMethodException:没有方法的签名:groovy.

我正在使用geb自动化web应用程序。我想定义一个页面对象,我们称它为Page1。根据环境的不同,Page1可以有不同数量的按钮元素。在一个环境中,按钮“A”可能存在,而在另一个环境中,按钮“A”可能不存在

在运行时,我能够根据我使用的环境下载预期按钮的列表。让我们称之为列表按钮列表。我想使用此列表在运行时定义我的页面对象

这就是我试图解决这个问题的方法,但似乎不起作用。我总是会遇到这样的错误:“groovy.lang.MissingMethodException:没有方法的签名:groovy.util.slurpersupport.NodeChild.call()

通常,如果您知道需要哪些按钮,并且每次都是相同的,那么您可以这样定义页面对象。但是由于我上面解释的原因,“这”不是我的选择

class Page1 extends Page{

    static conntent = {
        button1 {$(By.xpath("//*[text() = 'button1']")) }
        button3 {$(By.xpath("//*[text() = 'button3']")) }
        button4 {$(By.xpath("//*[text() = 'button4']")) }
        ...
    }
}
在我的顶级测试脚本中,我初始化了这个类,这样我就可以为这个页面对象提供它所需要的按钮列表。这是顶级脚本代码的抽象:

def "Test Case"(){
    when:
        butList = ["button1", "button3", "button4",...] //this list is generated somewhere else. it's not actually hard coded like you see here. the buttons in the list depend on the environment
        def pageInstance = new Page1()
        pageInstance.buttonList = butList
    then:
        Thread.sleep(30)
    when:
        //click something to bring me to the Page1 page
    then:
        at pageInstance
    when:
        button3.click()//in this case button 3 exists but this is where the code fails 
    then:
        thread.sleep(5000)
}

我希望这能解释我所面临的难题。我可能会用完全错误的方法来解决问题。我正在寻找关于如何解决这个问题的建议。如果问题仍然不清楚,请随意评论,我可以尝试把事情说得更清楚。

如果我遇到这种情况,我会这样处理-

class Page1 extends Page{

    static content = {
        allButtons { $("input[type=button]")} 
    }
}
或者对按钮使用一些其他通用选择器,这样可以很好地收集它们。然后当我测试它们时,我会选择-

def "Are my buttons here"() {
    given: "I am on the conundrum causing page"
        at Page1

    and: "I have some expected button text list"
        def expectedButtonText = ["button1","button4","button5","button6"]

    expect: "All your buttons are belong to us"
        allButtons*.text == expectedButtonText
}
其中一些是有点通用的,但我认为您可以看到模式,只需收集allButtons文本并与预期列表进行比较,如果此测试通过,那么您应该很高兴能够参考下面的单个按钮

def "Does my button work"() {
    given: "I am on the conundrum causing page"
        at Page1

    when: "I click on one of the buttons"
        allButtons.find(text: "button1").click()

    then: "magic happens"
        // insert goodness here
}
干杯,
Deon.

只需添加上述答案,您始终可以使用wait或required标志来播放一点:

static content = 
{ 
    labelBotonEntrar(wait: true,required: false)
    {
        $("div.gwt-label.newButton-label",1)    
    }
}

我发现我在这里做错了什么

这:

可以这样定义:

class Page1 extends Page{
    static def buttonList

    static conntent = {
        buttonList.each{ button ->
           "${button}" {$(By.xpath("//*[text() = '${button}']")) }
        }
    }
}
静态内容是一个哈希映射,因此我可以用这种方式动态定义哈希映射中的所有键/变量

我相信oher的一些答案是有用的,但这正是我发帖时想要的

class Page1 extends Page{
    static def buttonList

    static conntent = {
        buttonList.each{ button ->
           button {$(By.xpath("//*[text() = '${button}']")) }
        }
    }
}
class Page1 extends Page{
    static def buttonList

    static conntent = {
        buttonList.each{ button ->
           "${button}" {$(By.xpath("//*[text() = '${button}']")) }
        }
    }
}