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

Geb Page objects methods not accessible in cucumber steps implementation

我可以通过我的黄瓜步骤文件读取网页上元素的文本,但是当我想将该方法移动到页面对象文件时,系统会抛出错误。

使用的框架: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.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)

尝试在方法调用前加上页面对象 class CartPage:

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

尽管您的错误表明它找不到在您的 When 步骤中调用的 itemQuantity:

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

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

并在您的测试中导入 CartPage:

import com.buyautomation.pages.CartPage