1

I started learning codeception and I wrote a small piece of code for acceptance testing which is as follows:

<?php
$I = new WebGuy($scenario);
$I->amOnPage('account/sign_in');
$I->fillField('login_identity','[email protected]');
$I->fillField('login_password','don');
$I->click('submit');
$I->see('The Captcha Answer field is required.');
?>

As you can see the user name and password are hard-coded. Is it possible that I can scan the input (namely login_identity and login_password) from the user. If so can you please explain me the procedure to do so.

1 Answer 1

2

First off, a login routine is likely to be used very often throughout testing, so I would recommend abstracting it into a helper class.

<?php
// loginHelper.php
    class loginHelper {
        function login($username, $password, $I) {
            $I->fillField('login_identity', $username);
            $I->fillField('login_password', $password);
        }
    }
?>

You would need to include loginHelper as a module to enable in your acceptance.suite.yml and save it in the _helpers/ directory. Now we can freely use the login function in the rest of our acceptance tests.

<?php
    $I = new WebGuy($scenario);
    $I->amOnPage('account/sign_in');
    login('[email protected]', 'don', $I);
    $I->click('submit');
    $I->see('The Captcha Answer field is required.');
?>

Now to answer your question, you can prevent the username and password being hardcoded into the tests by saving them in a .yml file in the _data/ directory. Use Yaml::parse(__DIR__ . '/../_data/login.yml'), feeding the contents into an array. I would suggest putting the yaml parser into a helper as well so you may include it globally across all of your tests, meaning you only need to change the username and password in login.yml to change the values used in all of your tests. You can also have multiple usernames and passwords this way, and you will soon realise login details aren't the only values you can store.

Sign up to request clarification or add additional context in comments.

1 Comment

it is an brilliant suggestion but some how i dont know how to implement it @Tro please help me as what i got is a acceptance.suite.yml file and two urls to drive in one 'env' module it works when i have only one user for two different urls but problem occurs when i have to run two different url for two different users then how can i tell the tests the user has changed and now run url2 with user2 and url1 with user1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.