简体   繁体   中英

Codeception Gherkin, defining the step implementation path

I'm new to php and codeception and I wanted to use Gherkin with Codeception, and I've already setup the bare minimum to make feature files run in Codeception. I now find myself trying to make a scalable structure and make use of the PageObject framework. I created a Steps Folder and I wanted my step implementations kept in that folder. By default running codecept run some.feature loads the class defined in the acceptance.suite.yml file.

Motivation: I want to be able keep my step implementations into it's own separate folder

Given I have an acceptance.suite.yml file configuration of:

gherkin:
    contexts:
        default: 
            - AcceptanceTester
modules:
    enabled:
        - WebDriver:
            url: https://www.google.com/
            browser: chrome
        - \Helper\Acceptance

And I have a codeception.yml file configuration of:

paths:
    tests: tests
    output: tests/_output
    data: tests/_data
    support: tests/_support
    envs: tests/_envs
actor_suffix: Tester
extensions:
    enabled:
        - Codeception\Extension\RunFailed

And I have my Steps folder under _support :

在此处输入图片说明

How do I change the configuration to allow my step implementation to be called from the Steps folder?

In the gherkin: section of the suite configuration, you need to list your steps classes organised under default: , role: and/or tag: sections. There are example configurations in the official documentation: Gherkin options .

Below is an example from a recent project (using Codeception 2.5.6):

file structure

/app/common
├── codeception.yml
├── tests
│   ├── acceptance.suite.yml
│   ├── _bootstrap.php
│   ├── _data
│   │   └── user.php
│   ├── _support
│   │   ├── AcceptanceTester.php
│   │   ├── Step
│   │   │   └── Acceptance
│   │   │       └── CuratorSteps.php

The layout above for the step class is the default one when generating step object using codecept generate:stepobject command like so:

 $ /app/vendor/bin/codecept -c /app/common generate:stepobject acceptance CuratorSteps

acceptance.suite.yml:

# acceptance.suite.yml
namespace: common\tests
suite_namespace: common\tests\acceptance
bootstrap: false
actor: AcceptanceTester
modules:
    enabled:
        - PhpBrowser:
            url: http://example.com/
gherkin:
    contexts:
        default:
            - common\tests\AcceptanceTester
        role:
            curator:
                - common\tests\Step\Acceptance\CuratorSteps

The documentation doesn't mention it, but I notice I have to list the full namespace of the step classes, otherwise I'll get "Step definition for ... not found in contexts" errors when running the tests and the gherkin:steps codecept command won't return the step definitions.

output

$ /app/vendor/bin/codecept -vvv -c /app/common gherkin:steps acceptance
Steps from role:curator context:
+--------------------------------------------------------------------+------------------------------------------------------------------------------------------+
| Step                                                               | Implementation                                                                           |
+--------------------------------------------------------------------+------------------------------------------------------------------------------------------+
| I sign in as an admin                                              | common\tests\Step\Acceptance\CuratorSteps::iSignInAsAnAdmin                              |
| I should see a :arg1 button                                        | common\tests\Step\Acceptance\CuratorSteps::iShouldSeeAButton                             |
+--------------------------------------------------------------------+------------------------------------------------------------------------------------------+
Steps from default context:
+-------------------------------------+---------------------------------------------------------+
| Step                                | Implementation                                          |
+-------------------------------------+---------------------------------------------------------+
| I take a screenshot with name :arg1 | common\tests\AcceptanceTester::itakeAScreenshotWithName |
+-------------------------------------+---------------------------------------------------------+

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM