Every once in a while, you would like to highlight a current, active page in the navigation. There are number of solutions available to this problem, but none of them was flexible enough for me to use in my projects.
The CurrentPageMiddleware is very useful and saves a lot of time, but its main problem is that it only adds CSS classes to anchor elements, but what if you wanted to add CSS class to its parent element?
On the other hand the solutions that use template tags are very verbose and often require passing the request object along with the URL we want to test.
However, the biggest drawback I came across is that they are also too generic and don’t work as expected with URLs using custom parameters. That’s why I came up with the following solution for my Django 1.4 applications.
You can find a solution to this problem in WordPress Codex. The only problem is that it only allows you to pass a single post ID or an array of post IDs, not allowing you to use your category slug, not to mention multiple slugs.
The documentation of this function tells you to see
get_term_by() if you want to use category name or slug, but why not extend this function to do this for you?
During the second phase of GSoC an idea came into my mind. Why not allow other developers test their SilverStripe modules behaviourally?
There was already an extension created for basic interactions with the temporary database, but it was not in the state where you would be able to use it outside of the Sapphire framework.
But that’s the thing of the past. Now, let me introduce you to the new, shiny SilverStripeExtension.
SilverStripe relies heavily on AJAX interactions with feedback times varying from 200ms to multiple seconds. In order to make testing both stable and fast we needed a more sophisticated system than fixed timeouts.
This has already been implemented for the old spec-by-example module.
As you can see in Developing Web Applications with Behat and Mink cookbook there is a separate step added that tells the scenario to wait for the specific event to occur:
And I wait for the suggestion box to appear
Then the following step is defined:
* @Then /^I wait for the suggestion box to appear$/
public function iWaitForTheSuggestionBoxToAppear()
"$('.suggestions-results').children().length > 0"
Good thing about the
wait() function is that it continues after the condition provided as a second argument is true, so it doesn’t have to wait the entire time it has. But the problem with this solution is that this separate step is not really an active step that a non-technical user would spell out, and more importantly, it doesn’t add any value to the test descriptiveness as such.
Next you can find the complete code implemented by me that suppress the need of an additional step. It assumes that you use jQuery, but this can be changed easily. The code is then followed by a short explanation.