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

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
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.

client.waitForExist("parentSelector");
const doesNotExist = client.waitForExist("childElement", null, true);

expect(doesNotExist).toBe(true);
Assert on number of elements in DOM
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)
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
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
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
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)
client.waitForExist("selector");
client.moveToObject("selector");
client.click("selector");
Click a single element in a list of matches for a selector
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
client.waitForExist("selector");
client.moveToObject("selector");
Drag an element
client.waitForExist("selector");
client.moveToObject("selector", 0, 0);
client.buttonDown();
client.moveToObject("selector", 50, 5);
client.buttonUp();
Assert on an elements x position
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)
client.waitForExist("selector");
client.selectorExecute("selector", function(element) {
    $(element).trigger("mouseenter");
});
Assert on url navigation
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.