headless.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 An example of running Chrome or Firefox in headless mode.
  19. *
  20. * To run with Chrome, ensure you have Chrome 59+ installed and that
  21. * chromedriver 2.30+ is present on your system PATH:
  22. * <https://sites.google.com/a/chromium.org/chromedriver/downloads>
  23. *
  24. * SELENIUM_BROWSER=chrome node selenium-webdriver/example/headless.js
  25. *
  26. * To run with Firefox, ensure you have Firefox 57+ installed and that
  27. * geckodriver 0.19.0+ is present on your system PATH:
  28. * <https://github.com/mozilla/geckodriver/releases>
  29. *
  30. * SELENIUM_BROWSER=firefox node selenium-webdriver/example/headless.js
  31. */
  32. const chrome = require('../chrome');
  33. const firefox = require('../firefox');
  34. const {Builder, By, Key, until} = require('..');
  35. const width = 640;
  36. const height = 480;
  37. let driver = new Builder()
  38. .forBrowser('chrome')
  39. .setChromeOptions(
  40. new chrome.Options().headless().windowSize({width, height}))
  41. .setFirefoxOptions(
  42. new firefox.Options().headless().windowSize({width, height}))
  43. .build();
  44. driver.get('http://www.google.com/ncr')
  45. .then(_ =>
  46. driver.findElement(By.name('q')).sendKeys('webdriver', Key.RETURN))
  47. .then(_ => driver.wait(until.titleIs('webdriver - Google Search'), 1000))
  48. .then(
  49. _ => driver.quit(),
  50. e => driver.quit().then(() => { throw e; }));