From 6fa3c01f48ed26e357127d998d05f8795aa3fbf6 Mon Sep 17 00:00:00 2001 From: Jason Leyba Date: Wed, 2 Nov 2016 13:24:43 -0700 Subject: [PATCH] [js] Make sure examples are all updated for #2969 --- .../example/chrome_android.js | 31 +++++++----- .../example/chrome_mobile_emulation.js | 34 +++++++------ .../example/google_search.js | 6 ++- .../example/google_search_generator.js | 41 +++++++++------- .../example/google_search_test.js | 48 ++++++++++++------- .../selenium-webdriver/example/logging.js | 26 ++++------ .../example/parallel_flows.js | 3 ++ 7 files changed, 107 insertions(+), 82 deletions(-) diff --git a/javascript/node/selenium-webdriver/example/chrome_android.js b/javascript/node/selenium-webdriver/example/chrome_android.js index 990a4c44563ec..bc0701cf90397 100644 --- a/javascript/node/selenium-webdriver/example/chrome_android.js +++ b/javascript/node/selenium-webdriver/example/chrome_android.js @@ -21,18 +21,23 @@ * AVD). */ -var webdriver = require('..'), - By = webdriver.By, - until = webdriver.until, - chrome = require('../chrome'); +'use strict'; -var driver = new webdriver.Builder() - .forBrowser('chrome') - .setChromeOptions(new chrome.Options().androidChrome()) - .build(); +const {Builder, By, promise, until} = require('..'); +const {Options} = require('../chrome'); -driver.get('http://www.google.com/ncr'); -driver.findElement(By.name('q')).sendKeys('webdriver'); -driver.findElement(By.name('btnG')).click(); -driver.wait(until.titleIs('webdriver - Google Search'), 1000); -driver.quit(); +promise.consume(function* () { + let driver; + try { + driver = yield new Builder() + .forBrowser('chrome') + .setChromeOptions(new Options().androidChrome()) + .build(); + yield driver.get('http://www.google.com/ncr'); + yield driver.findElement(By.name('q')).sendKeys('webdriver'); + yield driver.findElement(By.name('btnG')).click(); + yield driver.wait(until.titleIs('webdriver - Google Search'), 1000); + } finally { + yield driver && driver.quit(); + } +}).then(_ => console.log('SUCCESS'), err => console.error('ERROR: ' + err)); diff --git a/javascript/node/selenium-webdriver/example/chrome_mobile_emulation.js b/javascript/node/selenium-webdriver/example/chrome_mobile_emulation.js index d3081127d8805..790be2bcf3de5 100644 --- a/javascript/node/selenium-webdriver/example/chrome_mobile_emulation.js +++ b/javascript/node/selenium-webdriver/example/chrome_mobile_emulation.js @@ -20,20 +20,24 @@ * ChromeDriver. */ -var webdriver = require('..'), - By = webdriver.By, - until = webdriver.until, - chrome = require('../chrome'); +'use strict'; +const {Builder, By, promise, until} = require('..'); +const {Options} = require('../chrome'); -var driver = new webdriver.Builder() - .forBrowser('chrome') - .setChromeOptions(new chrome.Options() - .setMobileEmulation({deviceName: 'Google Nexus 5'})) - .build(); - -driver.get('http://www.google.com/ncr'); -driver.findElement(By.name('q')).sendKeys('webdriver'); -driver.findElement(By.name('btnG')).click(); -driver.wait(until.titleIs('webdriver - Google Search'), 1000); -driver.quit(); +promise.consume(function* () { + let driver; + try { + driver = yield new Builder() + .forBrowser('chrome') + .setChromeOptions( + new Options().setMobileEmulation({deviceName: 'Google Nexus 5'})) + .build(); + yield driver.get('http://www.google.com/ncr'); + yield driver.findElement(By.name('q')).sendKeys('webdriver'); + yield driver.findElement(By.name('btnG')).click(); + yield driver.wait(until.titleIs('webdriver - Google Search'), 1000); + } finally { + yield driver && driver.quit(); + } +}).then(_ => console.log('SUCCESS'), err => console.error('ERROR: ' + err)); diff --git a/javascript/node/selenium-webdriver/example/google_search.js b/javascript/node/selenium-webdriver/example/google_search.js index fc95acea7322e..b9b8213282d58 100644 --- a/javascript/node/selenium-webdriver/example/google_search.js +++ b/javascript/node/selenium-webdriver/example/google_search.js @@ -16,8 +16,10 @@ // under the License. /** - * @fileoverview An example WebDriver script. This requires the chromedriver - * to be present on the system PATH. + * @fileoverview An example WebDriver script. + * + * Before running this script, ensure that Mozilla's geckodriver is present on + * your system PATH: * * Usage: * // Default behavior diff --git a/javascript/node/selenium-webdriver/example/google_search_generator.js b/javascript/node/selenium-webdriver/example/google_search_generator.js index 983c8d84f7f8c..25df93ab9f60e 100644 --- a/javascript/node/selenium-webdriver/example/google_search_generator.js +++ b/javascript/node/selenium-webdriver/example/google_search_generator.js @@ -18,28 +18,33 @@ /** * @fileoverview An example WebDriver script using generator functions. * - * Usage: node selenium-webdriver/example/google_search_generator.js + * Before running this script, ensure that Mozilla's geckodriver is present on + * your system PATH: + * + * Usage: + * + * node selenium-webdriver/example/google_search_generator.js */ -var webdriver = require('..'), - By = webdriver.By; +'use strict'; + +const {Builder, By, promise, until} = require('..'); -var driver = new webdriver.Builder() - .forBrowser('firefox') - .build(); +promise.consume(function* () { + let driver; + try { + driver = yield new Builder().forBrowser('firefox').build(); -driver.get('http://www.google.com/ncr'); -driver.call(function* () { - var query = yield driver.findElement(By.name('q')); - query.sendKeys('webdriver'); + yield driver.get('http://www.google.com/ncr'); - var submit = yield driver.findElement(By.name('btnG')); - submit.click(); -}); + let q = yield driver.findElement(By.name('q')); + yield q.sendKeys('webdriver'); -driver.wait(function* () { - var title = yield driver.getTitle(); - return 'webdriver - Google Search' === title; -}, 1000); + let btnG = yield driver.findElement(By.name('btnG')); + yield btnG.click(); -driver.quit(); + yield driver.wait(until.titleIs('webdriver - Google Search'), 1000); + } finally { + yield driver && driver.quit(); + } +}).then(_ => console.log('SUCCESS'), err => console.error('ERROR: ' + err)); diff --git a/javascript/node/selenium-webdriver/example/google_search_test.js b/javascript/node/selenium-webdriver/example/google_search_test.js index 823e2c578debb..a292782584736 100644 --- a/javascript/node/selenium-webdriver/example/google_search_test.js +++ b/javascript/node/selenium-webdriver/example/google_search_test.js @@ -17,31 +17,45 @@ /** * @fileoverview An example test that may be run using Mocha. - * Usage: mocha -t 10000 selenium-webdriver/example/google_search_test.js + * + * Usage: + * + * mocha -t 10000 selenium-webdriver/example/google_search_test.js + * + * You can change which browser is started with the SELENIUM_BROWSER environment + * variable: + * + * SELENIUM_BROWSER=chrome \ + * mocha -t 10000 selenium-webdriver/example/google_search_test.js */ -var webdriver = require('..'), - By = webdriver.By, - until = webdriver.until, - test = require('../testing'); +const {Builder, By, until} = require('..'); +const test = require('../testing'); test.describe('Google Search', function() { - var driver; + let driver; - test.before(function() { - driver = new webdriver.Builder() - .forBrowser('firefox') - .build(); + test.before(function *() { + driver = yield new Builder().forBrowser('firefox').build(); }); - test.it('should append query to title', function() { - driver.get('http://www.google.com'); - driver.findElement(By.name('q')).sendKeys('webdriver'); - driver.findElement(By.name('btnG')).click(); - driver.wait(until.titleIs('webdriver - Google Search'), 1000); + // You can write tests either using traditional promises. + it('works with promises', function() { + return driver.get('http://www.google.com') + .then(_ => driver.findElement(By.name('q')).sendKeys('webdriver')) + .then(_ => driver.findElement(By.name('btnG')).click()) + .then(_ => driver.wait(until.titleIs('webdriver - Google Search'), 1000)); }); - test.after(function() { - driver.quit(); + // Or you can define the test as a generator function. The test will wait for + // any yielded promises to resolve before invoking the next step in the + // generator. + test.it('works with generators', function*() { + yield driver.get('http://www.google.com/ncr'); + yield driver.findElement(By.name('q')).sendKeys('webdriver'); + yield driver.findElement(By.name('btnG')).click(); + yield driver.wait(until.titleIs('webdriver - Google Search'), 1000); }); + + test.after(() => driver.quit()); }); diff --git a/javascript/node/selenium-webdriver/example/logging.js b/javascript/node/selenium-webdriver/example/logging.js index ae1d4cc2aa755..633ac90c212d5 100644 --- a/javascript/node/selenium-webdriver/example/logging.js +++ b/javascript/node/selenium-webdriver/example/logging.js @@ -21,23 +21,15 @@ 'use strict'; -var webdriver = require('..'), - By = webdriver.By, - until = webdriver.until; +const {Builder, By, logging, until} = require('..'); -webdriver.logging.installConsoleHandler(); -webdriver.logging.getLogger('webdriver.http') - .setLevel(webdriver.logging.Level.ALL); +logging.installConsoleHandler(); +logging.getLogger('webdriver.http').setLevel(logging.Level.ALL); -var driver = new webdriver.Builder() - .forBrowser('firefox') - .build(); +var driver = new Builder().forBrowser('firefox').build(); -driver.get('http://www.google.com/ncr'); - -var searchBox = driver.wait(until.elementLocated(By.name('q')), 3000); -searchBox.sendKeys('webdriver'); - -driver.findElement(By.name('btnG')).click(); -driver.wait(until.titleIs('webdriver - Google Search'), 1000); -driver.quit(); +driver.get('http://www.google.com/ncr') + .then(_ => driver.findElement(By.name('q')).sendKeys('webdriver')) + .then(_ => driver.findElement(By.name('btnG')).click()) + .then(_ => driver.wait(until.titleIs('webdriver - Google Search'), 1000)) + .then(_ => driver.quit()); diff --git a/javascript/node/selenium-webdriver/example/parallel_flows.js b/javascript/node/selenium-webdriver/example/parallel_flows.js index f416922341d55..59ff103fb46e1 100644 --- a/javascript/node/selenium-webdriver/example/parallel_flows.js +++ b/javascript/node/selenium-webdriver/example/parallel_flows.js @@ -18,6 +18,9 @@ /** * @fileoverview An example of starting multiple WebDriver clients that run * in parallel in separate control flows. + * + * This example will only work when the promise manager is enabled + * (see ). */ var webdriver = require('..'),