简体   繁体   English

Codeception 测试用例前提条件

[英]Codeception test cases precondition

I am writing automation tests using codeception framework.我正在使用 codeception 框架编写自动化测试。 I have a test cases which are verifying some functionality after user is logged in. There are around 20 test cases with different functionality.我有一个测试用例,它在用户登录后验证某些功能。大约有 20 个具有不同功能的测试用例。 All test cases need an user to be logged in to the system, so I have written a login functionality under _before callback.所有的测试用例都需要一个用户登录系统,所以我在_before回调下写了一个登录功能。 When I execute all the test cases, before every test cases login functionality is checked which takes a lot of time.当我执行所有测试用例时,在检查每个测试用例登录功能之前,这需要很多时间。 Can we write login functionality as precondition and once user is logged in it should execute all test cases?我们可以编写登录功能作为前提条件,一旦用户登录它就应该执行所有测试用例吗?

You can use what are known as Helpers in codeception.您可以在 codeception 中使用所谓的 Helpers。 You can generate a helper using the following command:您可以使用以下命令生成帮助程序:

vendor/bin/codecept generate:helper Login

Then you could put a method into the class for logging in a user like so:然后你可以在类中放入一个方法来登录用户,如下所示:

<?php
namespace Helper;

// here you can define custom actions
// all public methods declared in helper class will be available in $I

class Login extends \Codeception\Module
{
    public function login($username, $password)
    {
        /** @var \Codeception\Module\WebDriver $webDriver */
        $webDriver = $this->getModule('WebDriver');
        // if snapshot exists - skipping login
        if ($webDriver->loadSessionSnapshot('login')) {
            return;
        }
        // logging in
        $webDriver->amOnPage('/login');
        $webDriver->submitForm('#loginForm', [
            'login' => $username,
            'password' => $password
        ]);
        $webDriver->see($username, '.navbar');
        // saving snapshot
        $webDriver->saveSessionSnapshot('login');
    }
}

Seehttp://codeception.com/docs/06-ReusingTestCode#session-snapshot for more info on snapshots.有关快照的更多信息,请参阅http://codeception.com/docs/06-ReusingTestCode#session-snapshot

Your acceptance.suite.yml should look something like this:你的acceptance.suite.yml 应该是这样的:

# Codeception Test Suite Configuration
#
# Suite for acceptance tests.
# Perform tests in browser using the WebDriver or PhpBrowser.
# If you need both WebDriver and PHPBrowser tests - create a separate suite.

class_name: AcceptanceTester
modules:
    enabled:
        # Note we must use WebDriver for us to use session snapshots
        - WebDriver:
            url: http://localhost/myapp
            browser: chrome

        - \Helper\Acceptance

        # Note that we must add the Login Helper class we generated here
        - \Helper\Login

Now we have a helper class that can be reused in all our tests.现在我们有了一个可以在所有测试中重用的辅助类。 Let's look at an example:让我们看一个例子:

<?php


class UserCest
{
    // tests
    public function testUserCanLogin(AcceptanceTester $I)
    {
        $I->login('username', 'password');
    }

    public function testUserCanCarryOutTask(AcceptanceTester $I)
    {
        $I->login('username', 'password');
        $I->amOnPage('/task-page');
        $I->see('Task Page');
        // other assertions below
    }

    public function testUserCanCarryOutAnotherTask(AcceptanceTester $I)
    {
        $I->login('username', 'password');
        $I->amOnPage('/another-task-page');
        $I->see('Another Task Page');
        // other assertions below
    }
}

Now when running the UserCest test, it should only login the user once.现在在运行 UserCest 测试时,它应该只登录用户一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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