Groovy Geb页面对象方法在cucumber步骤实现中不可访问

Groovy Geb页面对象方法在cucumber步骤实现中不可访问,groovy,cucumber,geb,Groovy,Cucumber,Geb,我能够通过cucumber steps文件读取网页上元素的文本,但当我想将该方法移动到页面对象文件时,系统会抛出一个错误 使用的框架:Groovy、Geb、Cucumber、Gradle 步骤文件 com.checkout.automation.buy.stepdefs import com.buyautomation.pages.CartPage import com.buyautomation.pages.common.CookieTrait import com.checkout.aut

我能够通过cucumber steps文件读取网页上元素的文本,但当我想将该方法移动到页面对象文件时,系统会抛出一个错误

使用的框架: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()
    }
错误:当使用方法获取文本使用页面对象函数,然后我得到下面的错误。然而,如果我在step文件中直接引用元素,那么代码就可以工作了

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)

尝试使用页面对象类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

谢谢,拉什比。我尝试了它,但这并没有解决我的问题更新以确保您已导入CartPage以使itemQuantity可用:import com.buyautomation.pages.CartPage