Skip to content

Instantly share code, notes, and snippets.

@philmander
Last active August 29, 2015 13:58
Show Gist options
  • Save philmander/10012495 to your computer and use it in GitHub Desktop.
Save philmander/10012495 to your computer and use it in GitHub Desktop.
Assert Microdata test util for Casper JS. Requires https://gist.github.com/philmander/10012466
/**
* Asserts a microdata item value matches the given expected value in the remote DOM environment
* Depends on the casper.fetchMicrodata method
*
* @param itemType {String} The item type of the item scopes to look for properties in
* @param property {String} The name of the property the compare the expected value against
* @param expectedItemValue {String} The expected value of the item property
* @param message {String} The assertion message
* @param itemTypeIndex {Number} A specific index of an item scope to use. If omitted or null, will look for matches in
* all item scopes
* @param itemValueIndex {Number} A specific property index to match againist. If omitted or null, will look for all
* values in the property array
*/
casper.test.assertMicrodata = function(itemType, property, expectedItemValue, message, itemTypeIndex, itemValueIndex) {
"use strict";
var foundMatch = false;
var microdata = casper.fetchMicrodata(itemType);
if(typeof itemTypeIndex === "number" && microdata.items[itemTypeIndex]) {
microdata.items = [ microdata.items[itemTypeIndex] ];
}
microdata.items.forEach(function(item) {
var props = item.properties[property];
if(props) {
if(typeof itemTypeIndex === "number" && props[itemValueIndex]) {
props = [ props[itemValueIndex] ];
}
for(var i = 0; i < props.length && !foundMatch; i++) {
foundMatch = utils.equals(props[i], expectedItemValue);
}
}
});
return this.assert(foundMatch, message, {
type: "assertMicrodata",
standard: "Matching microdata itemValue found for " + itemType + ":" + property,
values: {
subject: property,
expected: expectedItemValue
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment