Locator functions

Define Locator::functions in elements

Codeception has a set of very useful Locator functions that may be used by elements inside a section.

Declare an element with a locatorFunction:

1
<element name="simpleLocator" type="button" locatorFunction="Locator::contains('label', 'Name')"/>

When using the locatorFunction, omit Locator:: for code simplicity:

1
<element name="simpleLocatorShorthand" type="button" locatorFunction="contains('label', 'Name')"/>

An element’s locatorFunction can also be parameterized the same way as parameterized selectors:

1
<element name="simpleLocatorTwoParam" type="button" locatorFunction="contains({{arg1}}, {{arg2}})" parameterized="true"/>

An element cannot, however, have both a selector and a locatorFunction.

Call Elements that use locatorFunction

Given the above element definitions, you call the elements in a test just like any other element. No special reference is required, as you are still just referring to an element inside a section:

1
2
3
4
<test name="LocatorFuctionTest">
   <click selector="{{LocatorFunctionSection.simpleLocator}}" stepKey="SimpleLocator"/>
   <click selector="{{LocatorFunctionSection.simpleLocatorTwoParam('string1', 'string2')}}" stepKey="TwoParamLiteral"/>
</test>