{"_id":"56269a8b44c87f0d00fe6378","project":"559b65bd8b04e90d00702d54","user":"559b658d8b04e90d00702d52","category":{"_id":"559b65be8b04e90d00702d58","version":"559b65bd8b04e90d00702d57","pages":["559b65bf8b04e90d00702d5a","55a46241ffd44e0d00fca056","55a46f45e75ca60d00a39323","55a470b768c7e20d0036d4d6","55a4a758dad9d00d0071fcba","55b6b6276127b12500364560","55baf505d0736b3100bf1033","55c25ef1f2cff20d006d0b8a","56269a8b44c87f0d00fe6378","56498f62f176080d000c78ac","566f59c4fc3abb0d0020fcd5","567cd2228eb5b20d0021059d"],"project":"559b65bd8b04e90d00702d54","__v":12,"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-07-07T05:38:06.409Z","from_sync":false,"order":0,"slug":"chimpjs","title":"Chimp.js"},"version":{"_id":"559b65bd8b04e90d00702d57","project":"559b65bd8b04e90d00702d54","__v":6,"createdAt":"2015-07-07T05:38:05.890Z","releaseDate":"2015-07-07T05:38:05.890Z","categories":["559b65be8b04e90d00702d58","55a46e7468c7e20d0036d4d1","55a4971be6c2470d002a6483","55a4a690e10ba50d00b40bba","55a4bcf31a5f991700a94066","56cf5c8d336aa60b0086a40d"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"__v":10,"parentDoc":null,"updates":["56597d8960571a0d002cd1d3","56a6bde9f857190d00c91302","57fb2964f9c7091700d0d0c9","588dfa260b08260f00d22716","59693b85528ad6001566db92","59883955467353000f685d0d"],"next":{"pages":[],"description":""},"createdAt":"2015-10-20T19:48:27.979Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":10,"body":"The following is a list of examples of how to do common tasks and assertions using:\n* Webdriver.io\n* Synchronous execution\n* Jasmine expectations\n\n*[Click here the full WebdriverIO API](http://webdriver.io/api.html)*\n\nThe variable `browser` is a handle to webdriver. It is shorthand for, or the equivalent of:\n\n`var webdriverio = require('webdriverio');\nvar options = {};\nvar browser=webdriverio.remote(options)`\n\n###### Assert that element exists in DOM\n```javascript\nconst doesExist = client.waitForExist(\"selector\");\n\nexpect(doesExist).toBe(true);\n```\n\n###### Assert that element is not in DOM (removed)\nMake sure to wait for the parent element to exist. The parent and checked element should be rendered at the same time for the check to not give a false positive.\n\nUse `null` for second parameter to keep default wait timeout. Third parameter negates the check.\n\n```javascript\nclient.waitForExist(\"parentSelector\");\nconst doesNotExist = client.waitForExist(\"childElement\", null, true);\n\nexpect(doesNotExist).toBe(true);\n```\n\n###### Assert on number of elements in DOM\n```javascript\nclient.waitForExist(\"selector\");\nconst elements = client.elements(\"selector\");\n\nexpect(elements.value.length).toEqual(2);\n// expect(elements.value.length).toBeGreaterThan(2);\n// expect(elements.value.length >= 2).toBe(true);\n```\n\n###### Assert that a single element has text value (assuming only 1 match exists for the selector)\n```javascript\nclient.waitForExist(\"selector\");\nclient.moveToObject(\"selector\");\nconst actualText = client.getText(\"selector\");\n\nexpect(actualText).toEqual(\"text\");\n```\n\n###### Assert that a single element in a list of matches for a selector has text value\n```javascript\nclient.waitForExist(\"selector\");\nconst elements = client.elements(\"selector\");\nconst element = elements.value[<index_of_the_desired_element>];\nclient.moveTo(element.ELEMENT);\nconst actualText = client.elementIdText(element.ELEMENT).value;\nexpect(actualText).toEqual(\"text\");\n```\n\n###### Find a single element in a list of matches based on its text value\n```javascript\nconst elements = client.elements(\"selector\");\nlet matchingElementId = null;\nfor (const element of elements.value) {\n    const text = client.elementIdText(element.ELEMENT).value;\n    if (text === textToMatch) {\n        matchingElementId = element.ELEMENT;\n        break;\n    }\n}\nexpect(matchingElementId).not.toBe(null);\nclient.moveTo(matchingElementId);\n```\n\n###### Assert that element has class\n```javascript\nclient.waitForExist(\"selector\");\nconst cssClass = client.getAttribute(\"selector\", \"class\");\n\nexpect(cssClass).toContain(\"my-custom-class\");\n```\n\n###### Click a single element (assuming only 1 match exists for the selector)\n```javascript\nclient.waitForExist(\"selector\");\nclient.moveToObject(\"selector\");\nclient.click(\"selector\");\n```\n\n###### Click a single element in a list of matches for a selector\n```javascript\nclient.waitForExist(\"selector\");\nconst elements = client.elements(\"selector\");\nconst element = elements.value[<index_of_the_desired_element>];\nclient.moveTo(element.ELEMENT);\nclient.elementIdClick(element.ELEMENT);\n```\n\n###### Move to an element\n```javascript\nclient.waitForExist(\"selector\");\nclient.moveToObject(\"selector\");\n```\n\n###### Drag an element\n```javascript\nclient.waitForExist(\"selector\");\nclient.moveToObject(\"selector\", 0, 0);\nclient.buttonDown();\nclient.moveToObject(\"selector\", 50, 5);\nclient.buttonUp();\n```\n\n###### Assert on an elements x position\n```javascript\nclient.waitForExist(\"selector\");\nconst x = client.getLocation(\"selector\", \"x\") // Get x or y, if not specified, an object {x, y} is returned.\n\nexpect(x).toEqual(50);\n```\n\n###### Execute custom action (in this case trigger mouseenter on an element)\n```javascript\nclient.waitForExist(\"selector\");\nclient.selectorExecute(\"selector\", function(element) {\n    $(element).trigger(\"mouseenter\");\n});\n```\n\n###### Assert on url navigation\n```javascript\nbrowser .url(url + path)\nexpect(browser.getUrl()).toEqual(url + path)\n---\n\n<a href=\"http://quality.xolv.io/?utm_source=XolvOSS&utm_medium=OSSDocs&utm_content=ChimpRM-Footer&utm_campaign=QFLaunch\"><img src=\"https://files.readme.io/764e495-qualityfasterbook-commas.small.png\" width=\"200\" align=\"right\" /></a>\n##Learn the Fundamentals of Testing, Specifications and Become a Chimp Ninja!\n[Checkout our new book](http://quality.xolv.io/?utm_source=XolvOSS&utm_medium=OSSDocs&utm_content=ChimpRM-Footer&utm_campaign=QFLaunch) where you can learn how to can use Chimp across the Full Stack from React to Node.JS, Mocha, Meteor and more.\n\n\n**[Quality, Faster.](http://quality.xolv.io/?utm_source=XolvOSS&utm_medium=OSSDocs&utm_content=ChimpRM-Footer&utm_campaign=QFLaunch)** By Sam Hatoum, creator of Chimp.","excerpt":"Common cases with Chimp","slug":"cheat-sheet","type":"basic","title":"Cheat Sheet"}

Cheat Sheet

Common cases with Chimp

The following is a list of examples of how to do common tasks and assertions using: * Webdriver.io * Synchronous execution * Jasmine expectations *[Click here the full WebdriverIO API](http://webdriver.io/api.html)* The variable `browser` is a handle to webdriver. It is shorthand for, or the equivalent of: `var webdriverio = require('webdriverio'); var options = {}; var browser=webdriverio.remote(options)` ###### Assert that element exists in DOM ```javascript const doesExist = client.waitForExist("selector"); expect(doesExist).toBe(true); ``` ###### Assert that element is not in DOM (removed) Make sure to wait for the parent element to exist. The parent and checked element should be rendered at the same time for the check to not give a false positive. Use `null` for second parameter to keep default wait timeout. Third parameter negates the check. ```javascript client.waitForExist("parentSelector"); const doesNotExist = client.waitForExist("childElement", null, true); expect(doesNotExist).toBe(true); ``` ###### Assert on number of elements in DOM ```javascript client.waitForExist("selector"); const elements = client.elements("selector"); expect(elements.value.length).toEqual(2); // expect(elements.value.length).toBeGreaterThan(2); // expect(elements.value.length >= 2).toBe(true); ``` ###### Assert that a single element has text value (assuming only 1 match exists for the selector) ```javascript client.waitForExist("selector"); client.moveToObject("selector"); const actualText = client.getText("selector"); expect(actualText).toEqual("text"); ``` ###### Assert that a single element in a list of matches for a selector has text value ```javascript client.waitForExist("selector"); const elements = client.elements("selector"); const element = elements.value[<index_of_the_desired_element>]; client.moveTo(element.ELEMENT); const actualText = client.elementIdText(element.ELEMENT).value; expect(actualText).toEqual("text"); ``` ###### Find a single element in a list of matches based on its text value ```javascript const elements = client.elements("selector"); let matchingElementId = null; for (const element of elements.value) { const text = client.elementIdText(element.ELEMENT).value; if (text === textToMatch) { matchingElementId = element.ELEMENT; break; } } expect(matchingElementId).not.toBe(null); client.moveTo(matchingElementId); ``` ###### Assert that element has class ```javascript client.waitForExist("selector"); const cssClass = client.getAttribute("selector", "class"); expect(cssClass).toContain("my-custom-class"); ``` ###### Click a single element (assuming only 1 match exists for the selector) ```javascript client.waitForExist("selector"); client.moveToObject("selector"); client.click("selector"); ``` ###### Click a single element in a list of matches for a selector ```javascript client.waitForExist("selector"); const elements = client.elements("selector"); const element = elements.value[<index_of_the_desired_element>]; client.moveTo(element.ELEMENT); client.elementIdClick(element.ELEMENT); ``` ###### Move to an element ```javascript client.waitForExist("selector"); client.moveToObject("selector"); ``` ###### Drag an element ```javascript client.waitForExist("selector"); client.moveToObject("selector", 0, 0); client.buttonDown(); client.moveToObject("selector", 50, 5); client.buttonUp(); ``` ###### Assert on an elements x position ```javascript client.waitForExist("selector"); const x = client.getLocation("selector", "x") // Get x or y, if not specified, an object {x, y} is returned. expect(x).toEqual(50); ``` ###### Execute custom action (in this case trigger mouseenter on an element) ```javascript client.waitForExist("selector"); client.selectorExecute("selector", function(element) { $(element).trigger("mouseenter"); }); ``` ###### Assert on url navigation ```javascript browser .url(url + path) expect(browser.getUrl()).toEqual(url + path) --- <a href="http://quality.xolv.io/?utm_source=XolvOSS&utm_medium=OSSDocs&utm_content=ChimpRM-Footer&utm_campaign=QFLaunch"><img src="https://files.readme.io/764e495-qualityfasterbook-commas.small.png" width="200" align="right" /></a> ##Learn the Fundamentals of Testing, Specifications and Become a Chimp Ninja! [Checkout our new book](http://quality.xolv.io/?utm_source=XolvOSS&utm_medium=OSSDocs&utm_content=ChimpRM-Footer&utm_campaign=QFLaunch) where you can learn how to can use Chimp across the Full Stack from React to Node.JS, Mocha, Meteor and more. **[Quality, Faster.](http://quality.xolv.io/?utm_source=XolvOSS&utm_medium=OSSDocs&utm_content=ChimpRM-Footer&utm_campaign=QFLaunch)** By Sam Hatoum, creator of Chimp.