Finding DOM Elements For Codeception Acceptance & Functional Testing

When writing functional and acceptance tests for your PHP application I highly recommend Codeception. The platform is incredible to get up and running for your test automation needs. That being said, I am far from an expert, but like everything, the more I use it, the more I am learning about it.

I wanted to create this article in a fashion that will allow me to periodically update it with new methods and findings I learn about or create in order to keep track of them, and share with you!

Finding &amp Filling In Dynamically Named & Hidden Form Fields

If you have an application where the form fields’ id and name attributes can change on each page load, writing a method using the xpath like $I->fillField('//input[@name="some_form_field"]') is not going to work. One page load the name attribute might be “some_form_field_12345” and the next page load it might be “some_form_field_54321”. That’s where $I->executeJS() comes in very handy. Now you can find and fill out the field using this method in an example shown below.

$I->executeJS('$(\'input[name*="some_form_field"]\').val("Your Value Here")');

To make this even better, I want to make it reusable, so I will add a custom method to my helper class.

    /**
     * @param $selector
     * @param $value
     * @return mixed
     */
    public function fillInDynamicField($selector, $value)
    {
        $webDriver = $this->getModule('WebDriver');

        return $webDriver->executeJS('$(' . $selector . ').val(' . $value . ')');
    }

Now you can use it in all your tests like below.

$I->fillInDynamicField('\'input[name*="clientSinceDate"]\'', "06/01/2017");
Published: June 2, 2017 10:51 am Categorized in:

No Comments

Share Your Comments

I value your privacy, your email address will not be published or shared.

This site uses Akismet to reduce spam. Learn how your comment data is processed.