Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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(function()) structure

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

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

   

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

browser.wait(function() {
   return element(by.id('create')).isPresent();
}, 5000);
   
element(by.id('create')).click();

Check visibility

element(by.id('create')).isPresent() // Be careful with this: element is often present while it's not displayed...

element(by.id('create')).isEnabled() //Enabled/disabled, as in ng-disabled...

element(by.id('create')).isDisplayed() //Is element currently visible/displayed?

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

element(by.id('user_name'))

element(by.css('#myItem'))

element(by.model('person.name')) // refers to ng-model directive

element(by.binding('person.concatName')); // refers to ng-bind directive

element(by.textarea('person.extraDetails'));

element (by.input( 'username' ));

element (by.input( 'username' )).clear();

element(by.buttonText('Save'));

element(by.partialButtonText('Save'));

element(by.linkText('Save'));

element(by.partialLinkText('Save'));

element(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('//div

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:

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

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

Jasmine Matchers

to(N­ot)­Be( null | true | false )
to(N­ot)­Equ­al( value )
to(N­ot)­Mat­ch( regex | string )
toBe­Def­ine­d()
toBe­Und­efi­ned()
toBe­Nul­l()
toBe­Tru­thy()
toBe­Fal­sy()
to(N­ot)­Con­tain( string )
toBe­Les­sTh­an( number )
toBe­Gre­ate­rTh­an( number )
toBe­NaN()
toBe­Clo­seTo( number, precision )
toTh­row()
@RHeijnen

This comment has been minimized.

Copy link

commented Nov 18, 2016

Much appreciated

@fedesc

This comment has been minimized.

Copy link

commented Dec 4, 2016

Thanks so much very helpful

@rafalf

This comment has been minimized.

Copy link

commented Dec 12, 2016

thanks

@ivan-profit-place

This comment has been minimized.

Copy link

commented Dec 20, 2016

very good

@alancapc

This comment has been minimized.

Copy link

commented Jan 5, 2017

Thank you!

@nikhiletc

This comment has been minimized.

Copy link

commented Jan 17, 2017

that's what i looking for long time, thanks

@Kappyh

This comment has been minimized.

Copy link

commented Feb 14, 2017

\o/ Thank you

@renatoch

This comment has been minimized.

Copy link

commented Feb 20, 2017

Awesome! Thanks!

@justin858

This comment has been minimized.

Copy link

commented Mar 10, 2017

Thanks

@rchovatiya88

This comment has been minimized.

Copy link

commented Mar 15, 2017

Holy Crap! Thanks so much!!

@EwanMcP

This comment has been minimized.

Copy link

commented Mar 23, 2017

Respect, very handy. Thanks.

@shane-reaume

This comment has been minimized.

Copy link

commented Apr 24, 2017

Looks like this is old? getLocationAbsUrl() is returning as deprecated.

@arionalmond

This comment has been minimized.

Copy link

commented May 4, 2017

element(by.id('user_name').sendKeys("user1"); Needs to be changed to element(by.id('user_name')).sendKeys("user1"); Missed a parenthesis.

@daominhdam

This comment has been minimized.

Copy link

commented May 30, 2017

Thanks you so much!

@John-Okleberry

This comment has been minimized.

Copy link

commented Jul 17, 2017

This is great! I'm just starting out on Protractor but, if I come across something useful, I'll try to contribute.

@arepalli-praveenkumar

This comment has been minimized.

Copy link

commented Sep 11, 2017

Thank you

@daominhdam

This comment has been minimized.

Copy link

commented Sep 25, 2017

Thanks you

@masashiyokoi

This comment has been minimized.

Copy link

commented Dec 8, 2017

Thank you very match.

@husseinkorly

This comment has been minimized.

Copy link

commented Dec 15, 2017

Thank you

@maldzi

This comment has been minimized.

Copy link

commented Dec 30, 2017

It's so useful, thank.

@SiddharthDivakaran

This comment has been minimized.

Copy link

commented Apr 19, 2018

It's really useful thanks.

@chitranshamahor

This comment has been minimized.

Copy link

commented May 2, 2018

Great !!

@akshayraikar1

This comment has been minimized.

Copy link

commented May 24, 2018

Thanks a lot, much appreciated.

@flashjpr

This comment has been minimized.

Copy link

commented Aug 7, 2018

Kudos!

@amandarfernandes

This comment has been minimized.

Copy link

commented Sep 15, 2018

Nice! Thank you

@vermin-scripts

This comment has been minimized.

Copy link

commented Nov 5, 2018

Thanks This helped a-lot

@Umesh2Patel

This comment has been minimized.

Copy link

commented Nov 6, 2018

Thank you. This is very helpful.

@nitin4tstin

This comment has been minimized.

Copy link

commented Nov 22, 2018

Excellent! time-saving... Thanks

@prajaktashah

This comment has been minimized.

Copy link

commented Jan 15, 2019

Awesome.....Time saving..Thanks a ton.

@manish-kumar0525

This comment has been minimized.

Copy link

commented Jan 28, 2019

Great resource, concise and to the point.Thank you

@vmmartinezlona

This comment has been minimized.

Copy link

commented May 24, 2019

Thanks!!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.