Created
June 9, 2016 15:10
-
-
Save brian-mann/fbce91e15d83d8210c9fdb9c6e9169aa to your computer and use it in GitHub Desktop.
contains
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Cypress.addDualCommand({ | |
| contains: function(subject, filter, text, options) { | |
| var checkToAutomaticallyRetry, getErr, getFirstDeepestElement, getOpts, getPhrase, onConsole, resolveElements, selector, setEl; | |
| if (options == null) { | |
| options = {}; | |
| } | |
| if (subject && !Cypress.Utils.hasElement(subject)) { | |
| subject = null; | |
| } | |
| switch (false) { | |
| case !_.isObject(text): | |
| options = text; | |
| text = filter; | |
| filter = ""; | |
| break; | |
| case !_.isUndefined(text): | |
| text = filter; | |
| filter = ""; | |
| } | |
| _.defaults(options, { | |
| log: true | |
| }); | |
| this.ensureNoCommandOptions(options); | |
| if (!(_.isString(text) || _.isFinite(text))) { | |
| $Cypress.Utils.throwErrByPath("contains.invalid_argument"); | |
| } | |
| if (_.isBlank(text)) { | |
| $Cypress.Utils.throwErrByPath("contains.empty_string"); | |
| } | |
| getPhrase = function(type, negated) { | |
| var node; | |
| switch (false) { | |
| case !filter: | |
| return "within the selector: '" + filter + "' "; | |
| case !subject: | |
| node = Cypress.Utils.stringifyElement(subject, "short"); | |
| return "within the element: " + node + " "; | |
| default: | |
| return ""; | |
| } | |
| }; | |
| getErr = function(err) { | |
| var negated, node, type; | |
| type = err.type, negated = err.negated, node = err.node; | |
| switch (type) { | |
| case "existence": | |
| if (negated) { | |
| return "Expected not to find content: '" + text + "' " + (getPhrase(type, negated)) + "but continuously found it."; | |
| } else { | |
| return "Expected to find content: '" + text + "' " + (getPhrase(type, negated)) + "but never did."; | |
| } | |
| } | |
| }; | |
| if (options.log !== false) { | |
| onConsole = { | |
| Content: text, | |
| "Applied To": Cypress.Utils.getDomElements(subject || this.prop("withinSubject")) | |
| }; | |
| options._log = Cypress.Log.command({ | |
| message: _.compact([filter, text]), | |
| type: subject ? "child" : "parent", | |
| onConsole: function() { | |
| return onConsole; | |
| } | |
| }); | |
| } | |
| getOpts = _.extend(_.clone(options), { | |
| withinSubject: subject || this.prop("withinSubject") || this.$$("body"), | |
| filter: true, | |
| log: false, | |
| verify: false | |
| }); | |
| setEl = function($el) { | |
| if (options.log === false) { | |
| return; | |
| } | |
| onConsole.Returned = Cypress.Utils.getDomElements($el); | |
| onConsole.Elements = $el != null ? $el.length : void 0; | |
| return options._log.set({ | |
| $el: $el | |
| }); | |
| }; | |
| getFirstDeepestElement = function(elements, index) { | |
| var $current, $next, $priorities; | |
| if (index == null) { | |
| index = 0; | |
| } | |
| $current = elements.slice(index, index + 1); | |
| $next = elements.slice(index + 1, index + 2); | |
| if (!$next) { | |
| return $current; | |
| } | |
| if ($.contains($current.get(0), $next.get(0))) { | |
| return getFirstDeepestElement(elements, index + 1); | |
| } else { | |
| if ($current.is(priorityElement)) { | |
| return $current; | |
| } | |
| $priorities = elements.filter($current.parents(priorityElement)); | |
| if ($priorities.length) { | |
| return $priorities.last(); | |
| } else { | |
| return $current; | |
| } | |
| } | |
| }; | |
| text = Cypress.Utils.escapeQuotes(text); | |
| selector = filter + ":not(script):contains('" + text + "'), " + filter + "[type='submit'][value~='" + text + "']"; | |
| checkToAutomaticallyRetry = function(count, $el) { | |
| if (count !== 0 || ($el && $el.length)) { | |
| return; | |
| } | |
| throw new Error(); | |
| }; | |
| return (resolveElements = (function(_this) { | |
| return function() { | |
| return _this.execute("get", selector, getOpts).then(function($elements) { | |
| var $el; | |
| $el = (function() { | |
| switch (false) { | |
| case !($elements && $elements.length && filter): | |
| return $elements.last(); | |
| case !($elements && $elements.length): | |
| return getFirstDeepestElement($elements); | |
| default: | |
| return $elements; | |
| } | |
| })(); | |
| setEl($el); | |
| return _this.verifyUpcomingAssertions($el, options, { | |
| onRetry: resolveElements, | |
| onFail: function(err) { | |
| switch (err.type) { | |
| case "length": | |
| if (err.expected > 1) { | |
| return $Cypress.Utils.throwErrByPath("contains.length_option", { | |
| onFail: options._log | |
| }); | |
| } | |
| break; | |
| case "existence": | |
| return err.longMessage = getErr(err); | |
| } | |
| } | |
| }); | |
| }); | |
| }; | |
| })(this))(); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment