twistersfury/codeception-gherkin

用于Codeception的页面对象助手

v0.2.1 2019-07-13 02:06 UTC

This package is auto-updated.

Last update: 2024-09-13 13:17:50 UTC


README

codecov pipelines

Codeception Gherkin页面对象助手

该项目为Codeception的各种模块提供默认步骤。它利用页面对象模型的概念,允许您轻松配置特定元素,而无需直接将这些元素放入功能文件中。

步骤

将任何包含的特质添加到您的测试类中。添加后,您可以使用Codeception codecept gherkin:steps查看所有选项。

可用特质

  • TwistersFury\Codeception\Gherkin\Module\Db
  • TwistersFury\Codeception\Gherkin\Module\WebDriver
示例步骤
haveInDatabase
Scenario: Do Something
  Given I have in database "table"
    | field1 | field2 | field3 |
    | value 1| value 2| value #|
seeInDatabase
Scenario: Do Something
  Then I see in database "table"
    | field1 | field2 | field3 |
    | value 1| value 2| value #|

页面对象

在Codeception引导文件中注册您的页面。页面名称将是您在Gherkin功能文件中使用的标识符。类名是要初始化的类。作为您场景的一部分,使用我在页面上步骤来初始化页面。

您的页面对象必须扩展TwistersFury\Codeception\Gherkin\Page\AbstractPage并定义以下方法

  • getUrl 页面的相对URL。
  • getElementMap 元素映射(见下文)
  • getDefaultElement 默认HTML元素。

元素映射

元素映射用于定义页面上的元素。选择系统使用Codeception Location类在命令中构建选择器。如果请求的元素不存在,则返回请求的值。映射的可能值包括

  • method 要使用的位置方法。选项包括
    • find - 默认 -
    • href
    • name
  • attributes - 在find中定义搜索
  • element - 在findname中定义HTML元素。默认为getDefaultElement
  • url - 在href方法中使用。
示例

bootstrap.php

<?php

use My\Pages\AdminPage;
use My\Pages\Signup;
use My\Pages\ProfilePage
use TwistersFury\Codeception\Gherkin\Page\Factory;

Factory::reset();
Factory::getInstance()->addPages(
   [
        'admin'  => AdminPage::class,
        'signup' => SignUp::class
   ]
);

Factory::getInstance()->addPage(
    'profile', ProfilePage::class
);

tests/_support/Pages/Signup.php

<?php

namespace My\Pages;

class Signup {
    protected function getDefaultElement(): string 
    {
        return 'input';
    }
    
    public function getUrl(): string 
    {
        return '/signup';
    }

    protected function getElementMap(): array
    {
        return [
            'signup' => [
                'element' => 'button',
                'attributes' => [
                    'class' => 'signup'
                ]
            ],
            'forgot-my-pass' => [
                'method' => 'href',
                'url'    => '/forgot-pass/'
            ],
            'username' => [
                'method' => 'name'
            ]
        ]; 
    }
}