The Tale:
A unique jasmine matcher has been crafted by us, with the ability to perform 2 key tasks:
- hover over a specified element
- verify the presence of a tooltip displaying the expected text
Execution:
toHaveTooltip: function() {
return {
compare: function(elm, expectedTooltip) {
var tooltipPage = requirePO("tooltip");
browser.actions().mouseMove(elm).perform();
browser.wait(EC.visibilityOf(tooltipPage.tooltip), 5000, "The tooltip is not yet visible.");
return {
pass: tooltipPage.tooltip.getText().then(function(actualTooltip) {
return jasmine.matchersUtil.equals(actualTooltip, expectedTooltip);
}),
message: "Missing expected tooltip '" + expectedTooltip + "'."
};
}
};
},
Moreover, the tooltipPage
serves as a separately defined Page Object:
var Tooltip = function () {
this.tooltip = element(by.css(".tooltip"));
};
module.exports = new Tooltip();
This utility has proven to be handy for us, upholding the DRY principle in maintaining our test codebase clear and comprehensible:
expect(page.fromDateInput).toHaveTooltip("After");
The Challenge and Inquiry:
Presently, I am aiming to enhance the matcher's functionality to handle two distinct scenarios separately:
- absence of any tooltip displayed upon hovering (essentially, when the
browser.wait()
promise is rejected) - presence of a tooltip that doesn't match the expected one
How could the matcher be refined to address these issues individually and report distinct errors?
Evaluating Solutions Attempted:
toHaveTooltip: function() {
return {
compare: function(elm, expectedTooltip) {
var tooltipPage = requirePO("tooltip");
browser.actions().mouseMove(elm).perform();
return browser.wait(EC.visibilityOf(tooltipPage.tooltip), 5000, "The tooltip is still invisible.").then(function () {
return {
pass: tooltipPage.tooltip.getText().then(function(actualTooltip) {
return jasmine.matchersUtil.equals(actualTooltip, expectedTooltip);
}),
message: "Element lacks the expected tooltip '" + expectedTooltip + "'."
};
}, function () {
return {
pass: false,
message: "No tooltip appeared upon hovering over the element"
}
});
}
};
},
An attempt was made to explicitly handle the browser.wait()
operation and distinguish between the "success" and "error" outcomes. However, this led to a Jasmine Spec timeout and a lengthy red error message on the console:
Expected ({ ptor_: ({ setFileDetector: Function, ...
5 minutes scrolling here
... InnerHtml: Function, getId: Function, getRawId: Function }) to have tooltip 'After'.
It appears challenging to return a promise from the "compare" function.