我正在使用代码接收框架编写自动化测试。我有一个测试用例,用于在用户登录后验证某些功能。大约有 20 个具有不同功能的测试用例。所有测试用例都需要用户登录系统,因此我在 _before 回调下编写了登录功能。当我执行所有测试用例时,在每个测试用例之前都会检查登录功能,这需要很多时间。我们可以编写登录功能作为前提条件,一旦用户登录,它就应该执行所有测试用例吗?
最佳答案
您可以在代码接收中使用所谓的助手。您可以使用以下命令生成帮助程序:
供应商/bin/codecept生成:帮助程序登录
然后您可以在类中放入一个方法来登录用户,如下所示:
<?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');
}
}
参见http://codeception.com/docs/06-ReusingTestCode#session-snapshot有关快照的更多信息。
您的 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
现在我们有了一个可以在所有测试中重用的辅助类。让我们看一个例子:
<?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
}
}
现在运行 UserCest 测试时,它应该只登录用户一次。
关于php - 代码接收测试用例先决条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41958227/