Search code examples
seleniumbehatmink

Validate the calendar view of a page displays current month and date - behat test automation script


I am new to test automation and currently we are using a combination of behat + mink + selenium to automate tests. I want to check whether the calendar view displays the correct month and date. Here is my gherkin script:

Feature: Users see the current date when the calendar view is clicked! 
@javascript
Scenario: As a registered user, when I click on calendar, the page should display current date (mm-dd)
    Given I go to "URL"
    When I fill in "username" with "username"
    When I fill in "password" with "password"
    Then I press "edit-submit"
    Then I follow "calendar"
    Then I should see "June 28"

This is what I added in the FeatureContext.php file:

public function getCurrentDate() {
    return $this->currentDate()->format('Y-m-d');
}

protected function currentDate() {
    return new Date();
}

Since it this test should be automated I would like to change the last step in my script to something like this:

Then I should see "current date (mm-dd)"

I am not sure if it is even possible but like I said earlier, I am new to behat and I'm not sure how would I implement something like this. I am using Behat with mink and selenium. Thank you for your help!


Solution

  • I figured it out and wanted to post a solution in case someone needed it in the future:

    Add this to your FeatureContext.php file. Comments on top of each function explain what it does!

    First function:

    // call currentDate() to fetch and format the date (month year)
    public function getCurrentDate() {
        return $this->currentdate = $this->currentDate()->format('F Y');
    }
    

    Second function:

    // Get current date time
    protected function currentDate() {
        return new DateTime();
    }
    

    Third function:

    //month year date matcher does not take any arguments
    /**
     * Checks, that page contains specified text.
     *
     * @Then /^match current month year with page$/
     */
    public function MonthYearDateMatcher()
    {
        $this->assertSession()->pageTextContains($this->fixStepArgument($this->getCurrentDate()));
    }