0% found this document useful (0 votes)
259 views

Cheat Sheet For Protractor API Testing

The document outlines various commands available in the Protractor API for controlling the browser, interacting with page elements, and making assertions including navigating to pages, waiting for elements to be present, finding elements by locator strategies like ID or model, checking element properties like visibility, sending keys to elements, and matching expectations using Jasmine matchers. It provides details on how most commands in Protractor return promises and how to resolve values from those promises using the Jasmine expect API or promise handlers.

Uploaded by

vio gby
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
259 views

Cheat Sheet For Protractor API Testing

The document outlines various commands available in the Protractor API for controlling the browser, interacting with page elements, and making assertions including navigating to pages, waiting for elements to be present, finding elements by locator strategies like ID or model, checking element properties like visibility, sending keys to elements, and matching expectations using Jasmine matchers. It provides details on how most commands in Protractor return promises and how to resolve values from those promises using the Jasmine expect API or promise handlers.

Uploaded by

vio gby
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Protractor API

Note: Most commands return promises, so you only resolve their values through using
jasmine expect API or using .then(function()) structure.
Control browser
browser.get('yoururl'); // Load address, can also use '#yourpage'

browser.navigate().back();

browser.navigate().forward();

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.

Wait for a specific state to be loaded:


beforeAll(() => { // or beforeEach if needed.
let targetLocation = 'some/target/location';

browser.get('/'); // Login page.


element(by.buttonText('Sign In')).click(); // Login.
browser.setLocation(targetLocation);

// Login takes some time, so wait until it's done.


browser.wait(() => {
return browser.getCurrentUrl().then((url) => {
let isMatch = url.match(targetLocation);

if (isMatch) {
page = new Page; // Assuming you're using the page object
design pattern.
} else {
browser.setLocation(targetLocation);
}

return isMatch;
});
}, 10000, 'Error message.');
});

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?

Wait for a specific element to become present/visible:


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

element(by.id('create')).click();

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((location) => {
let x = location.x;
let y = location.y;
});

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

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()

You might also like