##### Table of Contents * [Resolving promises](#resolving-promises) * [Working with the Browser](#browser) * [Working with Locators](#working-with-locators) * [Working with Elements](#working-with-elements) * [Jasmine expect and matchers](#jasmine-expect-and-matchers) # Protractor Cheatsheet ### Resolving promises most of the commands/functions return promises, which can be resolved by using `expect()` or `then()`. ```Javascript //using return to resolve promises

Hello world

return element(by.tagName('h1')).getText().then(function(text){ expect(text).toContain('Hello'); }); ``` ### Browser ```Javascript browser.get('https://google.com') // Navigate to the given destination browser.restart() // restart the browser browser.close() // closes the browser browser.refresh() // Makes a full reload of the current page browser.getTitle() // get the title of the current page browser.sleep(10000) // Schedules a command to make the driver sleep for the given amount of time. browser.waitForAngular() // Instruct webdriver to wait until Angular has finished rendering and has no outstanding `$http` or `$timeout` calls before continuing. browser.switchTo() // switch WebDriver's focus to a frame or window (e.g. an alert, an iframe, another window). browser.getCurrentUrl() // returns the current url browser.takeScreenshot() // takes a screenshot ``` ### Working with Locators ```Javascript by.css() // Locates elements using a CSS selector

hello world

var h1 = element(by.css('.hello')); ``` ```Javascript by.cssContainingText() // Locate elements using CSS selector with certain string

hello world

hello universe

var h1 = element(by.css('.hello world')); // retruns h1 with hello world ``` ```Javascript by.tagName() // Locate elements with a given tag name.

hello world

var h1 = element(by.tagName('h1')); ``` ```Javascript by.linkText() // locate elements whose text matches the given text google var link = element(by.linkText('google')); ``` ```Javascript by.partialLinkText() // locate elements whose text matches the given substring google var link = element(by.linkText('goo')); ``` ```Javascript by.id() // locate element by its id

hello world

var byId = element(by.id('#hello')); ``` ```Javascript by.className() // locate element by its class name

hello world

var h1 = browser.findElement(by.className('hello')); ``` ```Javascript by.name() // Locates elements whose name attribute has the given value var dog = browser.findElement(by.name('dog_name')); ``` ```Javascript by.buttonText() // locate button by text var btn = element(by.buttonText('Protractor')); ``` ```Javascript by.partialButtonText() // locate button by partial text var btn = element(by.partialButtonText('Pro')); ``` ```Javascript by.binding() // Find an element by text binding {{person.name}} var bind = element(by.binding('person.name')); ``` ```Javascript by.model() // Find an element by ng-model var model = element(by.model('person.name')); ``` ```Javascript by.repeator() // Find an element by ng-repeat
{{cat.name}} {{cat.age}}
var repeat = element.all(by.repeator('pets')); // returns a list of pets var cat = element(by.repeator('cat in pets').get(1)); //returns the second cat ``` ```Javascript by.options() // Find an element by ng-options var allOptions = element.all(by.options('c for c in colors')); ``` ```Javascript by.xpath() // Locates elements matching a XPath selector var li = browser.findElement(by.xpath('//ul/li/a')); ``` ### Working with Elements ```Javascript element.all() // retruns an array of elements var ul = element.all(by.css('.list')) ``` ```Javascript element.get() // Get an element within the array by index var ul = element.all(by.css('.list')) // retruns an array var first = ul.get(0) ``` ```Javascript element.first() // Get the first element in the array var ul = element.all(by.css('.list')) // retruns an array var first = ul.first(); ``` ```Javascript element.last() // Get the last element in the array var ul = element.all(by.css('.list')) // retruns an array var first = ul.last(); ``` ```Javascript element.count() // counts the number of elements var ul = element.all(by.css('.list')) // retruns an array var total = ul.count(); ``` ```Javascript element.isPresent() // Determine whether the element is present on the page. element(by.model('person.name')).isPresent() // use expect to test for conditions ``` ```Javascript element.each() // loops over array of elements var ul = element.all(by.css('.list')); ul.each(function(elem, index) { //loops over each element }); ``` ```Javascript element.click() // loops over array of elements element(by.css('.register')).click(); // clicks the button ``` ```Javascript element.sendKeys() // send input to form inputs element(by.css('.name')).sendKeys('John Doe'); // fills the input with text "John Doe" ``` ```Javascript by.getTagName() // get tag name of an element

hello world

var tag = element(by.css('.hello')).getTagName(); ``` ```Javascript by.getCssValue() // get css value of an element

hello world

var cssValue = element(by.tagName('h1')).getCssValue(); ``` ```Javascript by.getAttribute() // get value of an attribute

hello world

var id = element(by.tagName('h1')).getAttribute('id'); ``` ```Javascript by.getText() // get text of an element

hello world

var text = element(by.tagName('h1')).getText(); ``` ```Javascript by.getSize() // returns the size of an element in pixels

hello world

var size = element(by.tagName('h1')).getSize(); // returns object with width and height ``` ```Javascript by.getLocation() // returns the location of the element

hello world

var location = element(by.tagName('h1')).getLocation(); // returns the location of an element {x,y} ``` ```Javascript element.isEnabled() // Determine whether the element is enabled. element(by.model('person.name')).isEnabled() // use with expect to test for conditions ``` ```Javascript element.isSelected() // Determine whether the element is selected. element(by.id('foo')).isSelected() // use with expect to test for conditions ``` ```Javascript element.submit() // submits a form
element(by.id('login')).submit(); ``` ```Javascript element.sendKeys() // clears the value of an input element(by.css('.name')).clear(); ``` ```Javascript element.isDisplayed() // Determine whether the element is currently displayed. element(by.model('person.name')).isPresent() // use expect to test for conditions ``` ### Jasmine expect and matchers ```Javascript expect(condition).toBeFalsy(); expect(condition).toBeNull(); expect(condition).toBeTruthy(); expect(condition).toBeUndefined(); expect(condition).toEqual(mixed); expect(condition).toContain(member); expect(condition).toBeCloseTo(number, decimalPlaces); expect(condition).toBeGreaterThan(number); expect(condition).toBeLessThan(number); expect(condition).toBeNaN(); expect(condition).toHaveBeenCalled(); expect(condition).toHaveBeenCalledTimes(number); expect(condition).toHaveBeenCalledWith(args); expect(condition).toThrow(string); expect(condition).toThrowError(string); expect(condition).toBe(instance); expect(condition).toBeDefined(); expect(condition).toMatch(pattern); ```