async_await_test.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Licensed to the Software Freedom Conservancy (SFC) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The SFC licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. /**
  18. * @fileoverview Example Mocha tests using async/await.
  19. *
  20. * Usage:
  21. *
  22. * mocha -t 20000 --harmony_async_await \
  23. * selenium-webdriver/example/async_await_test.js
  24. *
  25. * You can change which browser is started with the SELENIUM_BROWSER environment
  26. * variable:
  27. *
  28. * SELENIUM_BROWSER=chrome \
  29. * mocha -t 20000 --harmony_async_await \
  30. * selenium-webdriver/example/async_await_test.js
  31. */
  32. 'use strict';
  33. const assert = require('assert');
  34. const {Builder, By, Key, promise, until} = require('..');
  35. // async/await do not work well when the promise manager is enabled.
  36. // See https://github.com/SeleniumHQ/selenium/issues/3037
  37. //
  38. // 3037 does not impact these specific examples, but it is still recommended
  39. // that you disable the promise manager when using async/await.
  40. promise.USE_PROMISE_MANAGER = false;
  41. describe('Google Search', function() {
  42. let driver;
  43. beforeEach(async function() {
  44. driver = await new Builder().forBrowser('firefox').build();
  45. });
  46. afterEach(async function() {
  47. await driver.quit();
  48. });
  49. it('example', async function() {
  50. await driver.get('https://www.google.com/ncr');
  51. await driver.findElement(By.name('q')).sendKeys('webdriver', Key.RETURN);
  52. await driver.wait(until.titleIs('webdriver - Google Search'), 1000);
  53. let url = await driver.getCurrentUrl();
  54. assert.ok(
  55. url.startsWith('https://www.google.com/search'),
  56. 'unexpected url: ' + url);
  57. });
  58. });