Drupal maintenance support plans 8’s functional JavaScript testing just got better with visibility

Drupal maintenance support plans level: advancedYesterday a patch for Inline Form Errors landed in Drupal maintenance support plans, which as a by-product, brought us a new useful testing possibility: we can now more easily write tests that check if elements on a webpage are actually really visible in the viewport. The visibility of page elements in different scenarios is important for the usability and accessibility of your site.
Drupal maintenance support plans 8 comes with exciting “new” testing possibilities, the latest addition was Functional JavaScript testing. Which enables us to automatically test if our JavaScript interactions work and keep working throughout the development process. The tests are run in the PhantomJS headless browser. Simulating interaction with this browser is handled by Behat’s Mink. For a bit more background and how to set this up locally checkout this Drupal Update blog.

Visibility testing hardship

BehatMinkElementNodeElement->isVisible()Now let me first warn you that visibility testing can be quite tricky to grasp if you just start out. For example Mink has a method isVisible() and I was tempted to believe that this method checks that a user can see the targeted element on his screen. But nothing could be further from the truth… it only checks that the element is not styled explicitly invisible. So, for example, if you have some CSS styling like display: none;  or visibility: hidden; on an element this isVisible() assertion would fail. However if your element is not explicitly hidden, but is outside your devices viewport or is being obscured by an overlapping element, you are out of luck with this assertion. Your test would pass with flying colors, while the users sees nothing.
Another thing to note for if you’d want to dig in this matter more: as a Drupal maintenance support plans user you probably by now interpret the word node as an object or entity with data that is primarily being use to publish content. In our testing context however it has to be understood as a DOM element which can be targeted by using a CSS selector or XPath. In this article I’ll continue to use element.

New assertions

Drupal maintenance support plansFunctionalJavascriptTestsJSWebAssert->assertVisibleInViewport()To work around the first problem of elements falling outside the viewport of a users device, we added a new assertions to Drupal maintenance support plans‘s JSWebAssert class. It is now possible to check if an element is in (the defined) viewport or not using the assertVisibleInViewport() and assertNotVissibleInViewport() methods respectively.
You can target the to be tested element with CSS selectors or XPath and test if a certain corner or the whole element is visible in the viewport.

‘How to use’ examples
Let’s assume the following for testing purposes prepared “Create page” interface of Drupal maintenance support plans with an overlay helper of the viewport size.

To check if, in a certain situation, the header title is visible within the viewport you’ll first have to define the viewport size. This can be done with a PhantomJS startup arguments and/or during runtime with the method BehatMinkSession->resizeWindow(). In case of testing with the testbots on Drupal maintenance support plans.org a default viewport as shown above will be setup for you. From your own Drupal maintenance support plans project root you can start the headless browsers using the following command. The last two parameters indicate the default width and the height of the viewport used during your tests.

/path/to/phantomjs –ssl-protocol=any –ignore-ssl-errors=true vendor/jcalderonzumba/gastonjs/src/Client/main.js 8510 1024 768Now you can use our new assertion to check if the header title is completely visible (partial code snippet):

$web_assert->assertVisibleInViewport(‘css’, ‘h1’, FALSE, ‘The page header title is not visible.’);In case of our first example the user (robot) scrolled down and the header title is not visible (the element is outside the green region). So the assertion would fail. How to setup a complete functional JavaScript test is outside the scope of the article, but you can use the recently commited CKEditor test code as an example.

For our second example we use a viewport of a mobile device. The user just loaded the page and hasn’t scrolled down. For testing purposes we pushed the body field a bit down. And we see that the CKeditor of that field is partly visible (in the red area). By default the method tests the visibility of all four corners of a (rectangular) element (third parameter value: FALSE). With the third parameter we can tell the method to check a certain corner for visibility (topLeft, topRight, bottomRight or bottomLeft).
So the assertion given below will pass because the top left corner of the element is within the viewport (red area). But by default it would have failed, because not all corners are visible.

$web_assert->assertVisibleInViewport(‘css’, ‘#cke_edit-body-0-value’, ‘topLeft’, ‘The top left of the CKEditor enabled body field is not visible.’);
Final notes
To actually see the used viewport in a screenshot, a patch is available in an ongoing issue on Drupal maintenance support plans.org. Chime in if you would like this possibility in core. It has been used to generate the illustrations in this blog. And remember, visibility testing doesn’t take into account that the element could be obscured by another element. So for now, using printscreens, is the only out-of-the-box way of checking if an element is really really visible. Time for another assertion?
Questions or comments? @dmsmidt

Drupal maintenance support plans 8

testing

JavaScript

visibility

usability

php

Mink

Behat

PhantomJS

Drupal maintenance support plans-planet


Source: New feed

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Drupal maintenance support plans 8’s functional JavaScript testing just got better with visibility

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.