expectedConditions.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const selenium_webdriver_1 = require("selenium-webdriver");
  4. const util_1 = require("./util");
  5. /**
  6. * Represents a library of canned expected conditions that are useful for
  7. * protractor, especially when dealing with non-angular apps.
  8. *
  9. * Each condition returns a function that evaluates to a promise. You may mix
  10. * multiple conditions using `and`, `or`, and/or `not`. You may also
  11. * mix these conditions with any other conditions that you write.
  12. *
  13. * See ExpectedCondition Class in Selenium WebDriver codebase.
  14. * http://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html
  15. *
  16. *
  17. * @example
  18. * var EC = protractor.ExpectedConditions;
  19. * var button = $('#xyz');
  20. * var isClickable = EC.elementToBeClickable(button);
  21. *
  22. * browser.get(URL);
  23. * browser.wait(isClickable, 5000); //wait for an element to become clickable
  24. * button.click();
  25. *
  26. * // You can define your own expected condition, which is a function that
  27. * // takes no parameter and evaluates to a promise of a boolean.
  28. * var urlChanged = function() {
  29. * return browser.getCurrentUrl().then(function(url) {
  30. * return url === 'http://www.angularjs.org';
  31. * });
  32. * };
  33. *
  34. * // You can customize the conditions with EC.and, EC.or, and EC.not.
  35. * // Here's a condition to wait for url to change, $('abc') element to contain
  36. * // text 'bar', and button becomes clickable.
  37. * var condition = EC.and(urlChanged, EC.textToBePresentInElement($('abc'),
  38. * 'bar'), isClickable);
  39. * browser.get(URL);
  40. * browser.wait(condition, 5000); //wait for condition to be true.
  41. * button.click();
  42. *
  43. * @alias ExpectedConditions
  44. * @constructor
  45. */
  46. class ProtractorExpectedConditions {
  47. constructor(browser) {
  48. this.browser = browser;
  49. }
  50. ;
  51. /**
  52. * Negates the result of a promise.
  53. *
  54. * @example
  55. * var EC = protractor.ExpectedConditions;
  56. * var titleIsNotFoo = EC.not(EC.titleIs('Foo'));
  57. * // Waits for title to become something besides 'foo'.
  58. * browser.wait(titleIsNotFoo, 5000);
  59. *
  60. * @alias ExpectedConditions.not
  61. * @param {!function} expectedCondition
  62. *
  63. * @returns {!function} An expected condition that returns the negated value.
  64. */
  65. not(expectedCondition) {
  66. return () => {
  67. return expectedCondition().then((bool) => {
  68. return !bool;
  69. });
  70. };
  71. }
  72. /**
  73. * Helper function that is equivalent to the logical_and if defaultRet==true,
  74. * or logical_or if defaultRet==false
  75. *
  76. * @private
  77. * @param {boolean} defaultRet
  78. * @param {Array.<Function>} fns An array of expected conditions to chain.
  79. *
  80. * @returns {!function} An expected condition that returns a promise which
  81. * evaluates to the result of the logical chain.
  82. */
  83. logicalChain_(defaultRet, fns) {
  84. let self = this;
  85. return () => {
  86. if (fns.length === 0) {
  87. return defaultRet;
  88. }
  89. let fn = fns[0];
  90. return fn().then((bool) => {
  91. if (bool === defaultRet) {
  92. return self.logicalChain_(defaultRet, fns.slice(1))();
  93. }
  94. else {
  95. return !defaultRet;
  96. }
  97. });
  98. };
  99. }
  100. /**
  101. * Chain a number of expected conditions using logical_and, short circuiting
  102. * at the first expected condition that evaluates to false.
  103. *
  104. * @example
  105. * var EC = protractor.ExpectedConditions;
  106. * var titleContainsFoo = EC.titleContains('Foo');
  107. * var titleIsNotFooBar = EC.not(EC.titleIs('FooBar'));
  108. * // Waits for title to contain 'Foo', but is not 'FooBar'
  109. * browser.wait(EC.and(titleContainsFoo, titleIsNotFooBar), 5000);
  110. *
  111. * @alias ExpectedConditions.and
  112. * @param {Array.<Function>} fns An array of expected conditions to 'and'
  113. * together.
  114. *
  115. * @returns {!function} An expected condition that returns a promise which
  116. * evaluates to the result of the logical and.
  117. */
  118. and(...args) {
  119. return this.logicalChain_(true, args);
  120. }
  121. /**
  122. * Chain a number of expected conditions using logical_or, short circuiting
  123. * at the first expected condition that evaluates to true.
  124. *
  125. * @alias ExpectedConditions.or
  126. * @example
  127. * var EC = protractor.ExpectedConditions;
  128. * var titleContainsFoo = EC.titleContains('Foo');
  129. * var titleContainsBar = EC.titleContains('Bar');
  130. * // Waits for title to contain either 'Foo' or 'Bar'
  131. * browser.wait(EC.or(titleContainsFoo, titleContainsBar), 5000);
  132. *
  133. * @param {Array.<Function>} fns An array of expected conditions to 'or'
  134. * together.
  135. *
  136. * @returns {!function} An expected condition that returns a promise which
  137. * evaluates to the result of the logical or.
  138. */
  139. or(...args) {
  140. return this.logicalChain_(false, args);
  141. }
  142. /**
  143. * Expect an alert to be present.
  144. *
  145. * @example
  146. * var EC = protractor.ExpectedConditions;
  147. * // Waits for an alert pops up.
  148. * browser.wait(EC.alertIsPresent(), 5000);
  149. *
  150. * @alias ExpectedConditions.alertIsPresent
  151. * @returns {!function} An expected condition that returns a promise
  152. * representing whether an alert is present.
  153. */
  154. alertIsPresent() {
  155. return () => {
  156. return this.browser.driver.switchTo().alert().then(() => {
  157. return true;
  158. }, (err) => {
  159. if (err instanceof selenium_webdriver_1.error.NoSuchAlertError) {
  160. return false;
  161. }
  162. else {
  163. throw err;
  164. }
  165. });
  166. };
  167. }
  168. /**
  169. * An Expectation for checking an element is visible and enabled such that you
  170. * can click it.
  171. *
  172. * @example
  173. * var EC = protractor.ExpectedConditions;
  174. * // Waits for the element with id 'abc' to be clickable.
  175. * browser.wait(EC.elementToBeClickable($('#abc')), 5000);
  176. *
  177. * @alias ExpectedConditions.elementToBeClickable
  178. * @param {!ElementFinder} elementFinder The element to check
  179. *
  180. * @returns {!function} An expected condition that returns a promise
  181. * representing whether the element is clickable.
  182. */
  183. elementToBeClickable(elementFinder) {
  184. return this.and(this.visibilityOf(elementFinder), () => {
  185. return elementFinder.isEnabled().then(util_1.passBoolean, util_1.falseIfMissing);
  186. });
  187. }
  188. /**
  189. * An expectation for checking if the given text is present in the
  190. * element. Returns false if the elementFinder does not find an element.
  191. *
  192. * @example
  193. * var EC = protractor.ExpectedConditions;
  194. * // Waits for the element with id 'abc' to contain the text 'foo'.
  195. * browser.wait(EC.textToBePresentInElement($('#abc'), 'foo'), 5000);
  196. *
  197. * @alias ExpectedConditions.textToBePresentInElement
  198. * @param {!ElementFinder} elementFinder The element to check
  199. * @param {!string} text The text to verify against
  200. *
  201. * @returns {!function} An expected condition that returns a promise
  202. * representing whether the text is present in the element.
  203. */
  204. textToBePresentInElement(elementFinder, text) {
  205. let hasText = () => {
  206. return elementFinder.getText().then((actualText) => {
  207. // MSEdge does not properly remove newlines, which causes false
  208. // negatives
  209. return actualText.replace(/\r?\n|\r/g, '').indexOf(text) > -1;
  210. }, util_1.falseIfMissing);
  211. };
  212. return this.and(this.presenceOf(elementFinder), hasText);
  213. }
  214. /**
  215. * An expectation for checking if the given text is present in the element’s
  216. * value. Returns false if the elementFinder does not find an element.
  217. *
  218. * @example
  219. * var EC = protractor.ExpectedConditions;
  220. * // Waits for the element with id 'myInput' to contain the input 'foo'.
  221. * browser.wait(EC.textToBePresentInElementValue($('#myInput'), 'foo'), 5000);
  222. *
  223. * @alias ExpectedConditions.textToBePresentInElementValue
  224. * @param {!ElementFinder} elementFinder The element to check
  225. * @param {!string} text The text to verify against
  226. *
  227. * @returns {!function} An expected condition that returns a promise
  228. * representing whether the text is present in the element's value.
  229. */
  230. textToBePresentInElementValue(elementFinder, text) {
  231. let hasText = () => {
  232. return elementFinder.getAttribute('value').then((actualText) => {
  233. return actualText.indexOf(text) > -1;
  234. }, util_1.falseIfMissing);
  235. };
  236. return this.and(this.presenceOf(elementFinder), hasText);
  237. }
  238. /**
  239. * An expectation for checking that the title contains a case-sensitive
  240. * substring.
  241. *
  242. * @example
  243. * var EC = protractor.ExpectedConditions;
  244. * // Waits for the title to contain 'foo'.
  245. * browser.wait(EC.titleContains('foo'), 5000);
  246. *
  247. * @alias ExpectedConditions.titleContains
  248. * @param {!string} title The fragment of title expected
  249. *
  250. * @returns {!function} An expected condition that returns a promise
  251. * representing whether the title contains the string.
  252. */
  253. titleContains(title) {
  254. return () => {
  255. return this.browser.driver.getTitle().then((actualTitle) => {
  256. return actualTitle.indexOf(title) > -1;
  257. });
  258. };
  259. }
  260. /**
  261. * An expectation for checking the title of a page.
  262. *
  263. * @example
  264. * var EC = protractor.ExpectedConditions;
  265. * // Waits for the title to be 'foo'.
  266. * browser.wait(EC.titleIs('foo'), 5000);
  267. *
  268. * @alias ExpectedConditions.titleIs
  269. * @param {!string} title The expected title, which must be an exact match.
  270. *
  271. * @returns {!function} An expected condition that returns a promise
  272. * representing whether the title equals the string.
  273. */
  274. titleIs(title) {
  275. return () => {
  276. return this.browser.driver.getTitle().then((actualTitle) => {
  277. return actualTitle === title;
  278. });
  279. };
  280. }
  281. /**
  282. * An expectation for checking that the URL contains a case-sensitive
  283. * substring.
  284. *
  285. * @example
  286. * var EC = protractor.ExpectedConditions;
  287. * // Waits for the URL to contain 'foo'.
  288. * browser.wait(EC.urlContains('foo'), 5000);
  289. *
  290. * @alias ExpectedConditions.urlContains
  291. * @param {!string} url The fragment of URL expected
  292. *
  293. * @returns {!function} An expected condition that returns a promise
  294. * representing whether the URL contains the string.
  295. */
  296. urlContains(url) {
  297. return () => {
  298. return this.browser.driver.getCurrentUrl().then((actualUrl) => {
  299. return actualUrl.indexOf(url) > -1;
  300. });
  301. };
  302. }
  303. /**
  304. * An expectation for checking the URL of a page.
  305. *
  306. * @example
  307. * var EC = protractor.ExpectedConditions;
  308. * // Waits for the URL to be 'foo'.
  309. * browser.wait(EC.urlIs('foo'), 5000);
  310. *
  311. * @alias ExpectedConditions.urlIs
  312. * @param {!string} url The expected URL, which must be an exact match.
  313. *
  314. * @returns {!function} An expected condition that returns a promise
  315. * representing whether the url equals the string.
  316. */
  317. urlIs(url) {
  318. return () => {
  319. return this.browser.driver.getCurrentUrl().then((actualUrl) => {
  320. return actualUrl === url;
  321. });
  322. };
  323. }
  324. /**
  325. * An expectation for checking that an element is present on the DOM
  326. * of a page. This does not necessarily mean that the element is visible.
  327. * This is the opposite of 'stalenessOf'.
  328. *
  329. * @example
  330. * var EC = protractor.ExpectedConditions;
  331. * // Waits for the element with id 'abc' to be present on the dom.
  332. * browser.wait(EC.presenceOf($('#abc')), 5000);
  333. *
  334. * @alias ExpectedConditions.presenceOf
  335. * @param {!ElementFinder} elementFinder The element to check
  336. *
  337. * @returns {!function} An expected condition that returns a promise
  338. * representing whether the element is present.
  339. */
  340. presenceOf(elementFinder) {
  341. return elementFinder.isPresent.bind(elementFinder);
  342. }
  343. ;
  344. /**
  345. * An expectation for checking that an element is not attached to the DOM
  346. * of a page. This is the opposite of 'presenceOf'.
  347. *
  348. * @example
  349. * var EC = protractor.ExpectedConditions;
  350. * // Waits for the element with id 'abc' to be no longer present on the dom.
  351. * browser.wait(EC.stalenessOf($('#abc')), 5000);
  352. *
  353. * @alias ExpectedConditions.stalenessOf
  354. * @param {!ElementFinder} elementFinder The element to check
  355. *
  356. * @returns {!function} An expected condition that returns a promise
  357. * representing whether the element is stale.
  358. */
  359. stalenessOf(elementFinder) {
  360. return this.not(this.presenceOf(elementFinder));
  361. }
  362. /**
  363. * An expectation for checking that an element is present on the DOM of a
  364. * page and visible. Visibility means that the element is not only displayed
  365. * but also has a height and width that is greater than 0. This is the
  366. * opposite
  367. * of 'invisibilityOf'.
  368. *
  369. * @example
  370. * var EC = protractor.ExpectedConditions;
  371. * // Waits for the element with id 'abc' to be visible on the dom.
  372. * browser.wait(EC.visibilityOf($('#abc')), 5000);
  373. *
  374. * @alias ExpectedConditions.visibilityOf
  375. * @param {!ElementFinder} elementFinder The element to check
  376. *
  377. * @returns {!function} An expected condition that returns a promise
  378. * representing whether the element is visible.
  379. */
  380. visibilityOf(elementFinder) {
  381. return this.and(this.presenceOf(elementFinder), () => {
  382. return elementFinder.isDisplayed().then(util_1.passBoolean, util_1.falseIfMissing);
  383. });
  384. }
  385. /**
  386. * An expectation for checking that an element is either invisible or not
  387. * present on the DOM. This is the opposite of 'visibilityOf'.
  388. *
  389. * @example
  390. * var EC = protractor.ExpectedConditions;
  391. * // Waits for the element with id 'abc' to be no longer visible on the dom.
  392. * browser.wait(EC.invisibilityOf($('#abc')), 5000);
  393. *
  394. * @alias ExpectedConditions.invisibilityOf
  395. * @param {!ElementFinder} elementFinder The element to check
  396. *
  397. * @returns {!function} An expected condition that returns a promise
  398. * representing whether the element is invisible.
  399. */
  400. invisibilityOf(elementFinder) {
  401. return this.not(this.visibilityOf(elementFinder));
  402. }
  403. /**
  404. * An expectation for checking the selection is selected.
  405. *
  406. * @example
  407. * var EC = protractor.ExpectedConditions;
  408. * // Waits for the element with id 'myCheckbox' to be selected.
  409. * browser.wait(EC.elementToBeSelected($('#myCheckbox')), 5000);
  410. *
  411. * @alias ExpectedConditions.elementToBeSelected
  412. * @param {!ElementFinder} elementFinder The element to check
  413. *
  414. * @returns {!function} An expected condition that returns a promise
  415. * representing whether the element is selected.
  416. */
  417. elementToBeSelected(elementFinder) {
  418. return this.and(this.presenceOf(elementFinder), () => {
  419. return elementFinder.isSelected().then(util_1.passBoolean, util_1.falseIfMissing);
  420. });
  421. }
  422. }
  423. exports.ProtractorExpectedConditions = ProtractorExpectedConditions;
  424. //# sourceMappingURL=expectedConditions.js.map