Skip to content

Instantly share code, notes, and snippets.

@TWiStErRob
Forked from ajambrovic/protractorAPICheatsheet.md
Last active October 27, 2017 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TWiStErRob/8c070e864749756d6dfb1349322cb8dd to your computer and use it in GitHub Desktop.
Save TWiStErRob/8c070e864749756d6dfb1349322cb8dd to your computer and use it in GitHub Desktop.
Protractor API Cheatsheet

Protractor API

http://angular.github.io/protractor/#/api

Note: Most commands return promises, so you only resolve their values through using jasmine expect API or using .then(()=>{}) structure

Based on this post: https://spagettikoodi.wordpress.com/2015/01/14/angular-testing-cheat-sheet/ by @crystoll

and updated for ES6/jasmine2

Control browser

browser.get('yoururl'); // Load address, can also use '#yourpage'
browser.navigate().back();
browser.navigate().forward();
browser.sleep(10000); // if your test is outrunning the browser
browser.waitForAngular(); // if your test is outrunning the browser
browser.getLocationAbsUrl() // get the current address
browser.ignoreSynchronization = true; // If true, Protractor will not attempt to synchronize with the page 
before performing actions

// Toaster/Growl fix 

// Protractor failing to find the element, due to toaster/growl working with setTimeout,
// and proctractor waiting for the timeout to end before scanning for the element

const growlMessage = by.css('.ui-growl .ui-growl-item p');
browser.ignoreSynchronization = true;
const until = protractor.ExpectedConditions;
browser.wait(until.visibilityOf(element(growlMessage)), 5000);

element.all(growlMessage).then(values => {
	expect(values.length).toBe(1);
	expect(values[0].getText()).toContain('Transakcija uspješna');
	browser.ignoreSynchronization = false;
});
   

Here's a trick how to wait for something to become present/visible:

browser.wait(() => element(by.id('create')).isPresent(), 5000);
   
element(by.id('create')).click();

Check visibility

const el = element(locator);
el.isPresent() // Be careful with this: element is often present while it's not displayed...
el.isEnabled() //Enabled/disabled, as in ng-disabled...
el.isDisplayed() //Is element currently visible/displayed?

Find an element by id, model, binding, ...

// to be used as element(...);
by.id('user_name')
by.css('#myItem')
by.model('person.name') // refers to ng-model directive
by.binding('person.concatName') // refers to ng-bind directive
by.textarea('person.extraDetails')
by.input('username')
element(by.input('username')).clear()
by.buttonText('Save')
by.partialButtonText('Save')
by.linkText('Save')
by.partialLinkText('Save')
by.css('[ng-click="cancel()"]') 

var dog = element(by.cssContainingText('.pet', 'Dog'));

var allOptions = element.all(by.options('c c in colors')); //When ng-options is used with selectbox

Find collection of elements by css, repeater, xpath..

var list = element.all(by.css('.items'));
var list2 = element.all(by.repeater('personhome.results'));
var list3 = element.all(by.xpath("//html[1]/body[1]/div[1]"));

expect(list.count()).toBe(3);
expect(list.get(0).getText()).toBe('First')
expect(list.get(1).getText()).toBe('Second')
expect(list.first().getText()).toBe('First')
expect(list.last().getText()).toBe('Last')

Send keystrokes, clear

element(by.id('user_name').sendKeys("user1");
sendKeys(protractor.Key.ENTER);
sendKeys(protractor.Key.TAB);
element(by.id('user_name')).clear()

Position and size, also how to deal with promises (also, how to log):

element(by.id('item1')).getLocation().then(location => {
  var x = location.x;
  var y = location.y;
});

element(by.id('item1')).getSize().then(size => {
  var width = size.width;
  var height = size.height;
});

element(by.binding('listView.totalRecords')).getText().then(value => console.log(value));

Jasmine Matchers

to(Not)Be( null | true | false )
to(Not)Equal( value )
to(Not)Match( regex | string )
toBeDefined()
toBeUndefined()
toBeNull()
toBeTruthy()
toBeFalsy()
to(Not)Contain( string )
toBeLessThan( number )
toBeGreaterThan( number )
toBeNaN()
toBeCloseTo( number, precision )
toThrow()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment