dop2144 2017-01-31 12:58
浏览 102
已采纳

Codeception测试用例的前提条件

I am writing automation tests using codeception framework. I have a test cases which are verifying some functionality after user is logged in. There are around 20 test cases with different functionality. All test cases need an user to be logged in to the system, so I have written a login functionality under _before callback. 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?

  • 写回答

1条回答 默认 最新

  • dongyao1915 2017-01-31 23:22
    关注

    You can use what are known as Helpers in codeception. 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');
        }
    }
    

    See http://codeception.com/docs/06-ReusingTestCode#session-snapshot for more info on snapshots.

    Your acceptance.suite.yml should look something like this:

    # 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c