在黄瓜步骤实现中无法访问的Geb Page对象方法

时间:2018-11-02 19:07:50

标签: groovy cucumber geb

我能够通过我的黄瓜步骤文件读取网页上某个元素的文本,但是当我想将该方法移至“页面对象”文件时,系统会引发错误。

使用的框架:Groovy,Geb,Cucumber,Gradle

步骤文件

com.checkout.automation.buy.stepdefs

import com.buyautomation.pages.CartPage
import com.buyautomation.pages.common.CookieTrait
import com.checkout.automation.services.Util
import com.expectedData.CartPageExpectedData
import cucumber.api.java.en.Given
import cucumber.api.java.en.Then
import cucumber.api.java.en.When

class CartSteps extends GebCuke implements CookieTrait {

private Util util

 CartSteps(Util util) {
        this.util = util
    }

@Then("I am notified that my cart is empty")
def emptyCartNotificationCheck() {
if (CIASteps.user == "Guest User") {
   at CartPage
// This does not work
   assert getEmptyCartText()== CartPageExpectedData.expectedText.emptyCartGuestUserHeading + CartPageExpectedData.expectedText.emptyCartGuestUserSubText

//This works
    assert $("div.empty-cart__info").text() == CartPageExpectedData.expectedText.emptyCartGuestUserHeading + CartPageExpectedData.expectedText.emptyCartGuestUserSubText
}
else {
    assert $("div.empty-cart__info").text() == CartPageExpectedData.expectedText.emptyCartRegisteredUserHeading + CartPageExpectedData.expectedText.emptyCartRegisteredUserText
     }
  }
}

页面对象文件。我想在此文件中存储元素定位器和处理元素的函数

    package com.buyautomation.pages
    import geb.Page

    class CartPage extends Page {

        static at = { assert title == "Cart" }

    static content = {
        emptyCart(wait:true) {$("div", class:"empty-cart__info")}
        itemQuantity(wait:true) {$("div", class:"cart-item__quantity-block")}
    }

    def getEmptyCartText() {
        emptyCart.text()
    }

错误:使用方法使用页面对象函数获取文本时,出现以下错误。但是,如果我直接在步骤文件中引用元素,则代码有效

groovy.lang.MissingPropertyException: Unable to resolve itemQuantity as content for geb.Page, or as a property on its Navigator context. Is itemQuantity a class you forgot to import?
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:83)
    at org.codehaus.groovy.reflection.CachedConstructor.doConstructorInvoke(CachedConstructor.java:77)
    at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrap.callConstructor(ConstructorSite.java:84)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:60)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:235)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:263)
    at geb.content.PageContentSupport.propertyMissing(PageContentSupport.groovy:45)
    at geb.content.PageContentSupport$propertyMissing.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
    at geb.Page.propertyMissing(Page.groovy:112)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
    at groovy.lang.MetaClassImpl.invokeMissingProperty(MetaClassImpl.java:878)
    at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java:1859)
    at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java:3758)
    at geb.Page.getProperty(Page.groovy)
    at org.codehaus.groovy.runtime.InvokerHelper.getProperty(InvokerHelper.java:174)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:456)
    at geb.Browser.propertyMissing(Browser.groovy:216)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
    at groovy.lang.MetaClassImpl.invokeMissingProperty(MetaClassImpl.java:878)
    at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java:1859)
    at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java:3758)
    at geb.Browser.getProperty(Browser.groovy)
    at org.codehaus.groovy.runtime.InvokerHelper.getProperty(InvokerHelper.java:174)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:456)
    at com.checkout.automation.buy.stepdefs.GebCuke.propertyMissing(GebCuke.groovy:48)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
    at groovy.lang.MetaClassImpl.invokeMissingProperty(MetaClassImpl.java:878)
    at groovy.lang.MetaClassImpl$12.getProperty(MetaClassImpl.java:2024)
    at org.codehaus.groovy.runtime.callsite.GetEffectivePogoPropertySite.getProperty(GetEffectivePogoPropertySite.java:85)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:307)
    at com.checkout.automation.buy.stepdefs.CartSteps.exceedMaxLimit(CartSteps.groovy:57)
    at ✽.When I update the quantity to exceed the maximum limit(cartMessages.feature:33)

1 个答案:

答案 0 :(得分:0)

尝试使用页面对象类CartPage为方法调用添加前缀:

   assert CartPage.getEmptyCartText()== CartPageExpectedData.expectedText.emptyCartGuestUserHeading + CartPageExpectedData.expectedText.emptyCartGuestUserSubText

尽管您的错误是指出它找不到在“何时”步骤中正在调用的itemQuantity:

When I update the quantity to exceed the maximum limit(cartMessages.feature:33)

请确保在引用它时使用CartPage.itemQuantity。

并在测试中导入CartPage:

import com.buyautomation.pages.CartPage