|
| 1 | +// Based off of spec/basic/elements_spec.js |
| 2 | +import {promise as ppromise, browser, element, by, By, $, $$, ExpectedConditions, ElementFinder} from '../../..'; |
| 3 | + |
| 4 | +describe('ElementFinder', function() { |
| 5 | + it('should return the same result as browser.findElement', async function() { |
| 6 | + await browser.get('index.html#/form'); |
| 7 | + const nameByElement = element(by.binding('username')); |
| 8 | + |
| 9 | + await expect(nameByElement.getText()).toEqual( |
| 10 | + browser.findElement(by.binding('username')).getText()); |
| 11 | + }); |
| 12 | + |
| 13 | + it('should wait to grab the WebElement until a method is called', async function() { |
| 14 | + // These should throw no error before a page is loaded. |
| 15 | + const usernameInput = element(by.model('username')); |
| 16 | + const name = element(by.binding('username')); |
| 17 | + |
| 18 | + await browser.get('index.html#/form'); |
| 19 | + |
| 20 | + await expect(name.getText()).toEqual('Anon'); |
| 21 | + |
| 22 | + await usernameInput.clear(); |
| 23 | + await usernameInput.sendKeys('Jane'); |
| 24 | + await expect(name.getText()).toEqual('Jane'); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should chain element actions', async function() { |
| 28 | + await browser.get('index.html#/form'); |
| 29 | + |
| 30 | + const usernameInput = element(by.model('username')); |
| 31 | + const name = element(by.binding('username')); |
| 32 | + |
| 33 | + await expect(name.getText()).toEqual('Anon'); |
| 34 | + |
| 35 | + await ((usernameInput.clear() as any) as ElementFinder).sendKeys('Jane'); |
| 36 | + await expect(name.getText()).toEqual('Jane'); |
| 37 | + }); |
| 38 | + |
| 39 | + it('chained call should wait to grab the WebElement until a method is called', |
| 40 | + async function() { |
| 41 | + // These should throw no error before a page is loaded. |
| 42 | + const reused = element(by.id('baz')). |
| 43 | + element(by.binding('item.reusedBinding')); |
| 44 | + |
| 45 | + await browser.get('index.html#/conflict'); |
| 46 | + |
| 47 | + await expect(reused.getText()).toEqual('Inner: inner'); |
| 48 | + await expect(reused.isPresent()).toBe(true); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should differentiate elements with the same binding by chaining', |
| 52 | + async function() { |
| 53 | + await browser.get('index.html#/conflict'); |
| 54 | + |
| 55 | + const outerReused = element(by.binding('item.reusedBinding')); |
| 56 | + const innerReused = |
| 57 | + element(by.id('baz')).element(by.binding('item.reusedBinding')); |
| 58 | + |
| 59 | + await expect(outerReused.getText()).toEqual('Outer: outer'); |
| 60 | + await expect(innerReused.getText()).toEqual('Inner: inner'); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should chain deeper than 2', async function() { |
| 64 | + // These should throw no error before a page is loaded. |
| 65 | + const reused = element(by.css('body')).element(by.id('baz')). |
| 66 | + element(by.binding('item.reusedBinding')); |
| 67 | + |
| 68 | + await browser.get('index.html#/conflict'); |
| 69 | + |
| 70 | + await expect(reused.getText()).toEqual('Inner: inner'); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should allow handling errors', async function() { |
| 74 | + await browser.get('index.html#/form'); |
| 75 | + try { |
| 76 | + await $('.nopenopenope').getText(); |
| 77 | + |
| 78 | + // The above line should have throw an error. Fail. |
| 79 | + await expect(true).toEqual(false); |
| 80 | + } catch (e) { |
| 81 | + await expect(true).toEqual(true); |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + it('should allow handling chained errors', async function() { |
| 86 | + await browser.get('index.html#/form'); |
| 87 | + try { |
| 88 | + await $('.nopenopenope').$('furthernope').getText(); |
| 89 | + |
| 90 | + // The above line should have throw an error. Fail. |
| 91 | + await expect(true).toEqual(false); |
| 92 | + } catch (e) { |
| 93 | + await expect(true).toEqual(true); |
| 94 | + } |
| 95 | + }); |
| 96 | + |
| 97 | + it('should keep a reference to the original locator', async function() { |
| 98 | + await browser.get('index.html#/form'); |
| 99 | + |
| 100 | + const byCss = by.css('body'); |
| 101 | + const byBinding = by.binding('greet'); |
| 102 | + |
| 103 | + await expect(element(byCss).locator()).toEqual(byCss); |
| 104 | + await expect(element(byBinding).locator()).toEqual(byBinding); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should propagate exceptions', async function() { |
| 108 | + await browser.get('index.html#/form'); |
| 109 | + |
| 110 | + const invalidElement = element(by.binding('INVALID')); |
| 111 | + const successful = invalidElement.getText().then(function() { |
| 112 | + return true; |
| 113 | + } as any as (() => ppromise.Promise<void>), function() { |
| 114 | + return false; |
| 115 | + } as any as (() => ppromise.Promise<void>)); |
| 116 | + await expect(successful).toEqual(false); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should be returned from a helper without infinite loops', async function() { |
| 120 | + await browser.get('index.html#/form'); |
| 121 | + const helperPromise = ppromise.when(true).then(function() { |
| 122 | + return element(by.binding('greeting')); |
| 123 | + }); |
| 124 | + |
| 125 | + await helperPromise.then(async function(finalResult: ElementFinder) { |
| 126 | + await expect(finalResult.getText()).toEqual('Hiya'); |
| 127 | + } as any as (() => ppromise.Promise<void>)); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should be usable in WebDriver functions', async function() { |
| 131 | + await browser.get('index.html#/form'); |
| 132 | + const greeting = element(by.binding('greeting')); |
| 133 | + await browser.executeScript('arguments[0].scrollIntoView', greeting); |
| 134 | + }); |
| 135 | + |
| 136 | + it('should allow null as success handler', async function() { |
| 137 | + await browser.get('index.html#/form'); |
| 138 | + |
| 139 | + const name = element(by.binding('username')); |
| 140 | + |
| 141 | + await expect(name.getText()).toEqual('Anon'); |
| 142 | + await expect( |
| 143 | + name.getText().then(null, function() {}) |
| 144 | + ).toEqual('Anon'); |
| 145 | + |
| 146 | + }); |
| 147 | + |
| 148 | + it('should check equality correctly', async function() { |
| 149 | + await browser.get('index.html#/form'); |
| 150 | + |
| 151 | + const usernameInput = element(by.model('username')); |
| 152 | + const name = element(by.binding('username')); |
| 153 | + |
| 154 | + await expect(usernameInput.equals(usernameInput)).toEqual(true); |
| 155 | + await expect(usernameInput.equals(name)).toEqual(false); |
| 156 | + }); |
| 157 | +}); |
0 commit comments