Path: blob/trunk/javascript/selenium-webdriver/example/headless.js
2884 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617/**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 that21* chromedriver 2.30+ is present on your system PATH:22* <https://chromedriver.chromium.org/downloads>23*24* SELENIUM_BROWSER=chrome node selenium-webdriver/example/headless.js25*26* To run with Firefox, ensure you have Firefox 57+ installed and that27* 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.js31*/3233const chrome = require('../chrome')34const firefox = require('../firefox')35const { Builder, By, Key, until } = require('..')3637const width = 64038const height = 4803940let driver = new Builder()41.forBrowser('chrome')42.setChromeOptions(new chrome.Options().addArguments('-headless').windowSize({ width, height }))43.setFirefoxOptions(new firefox.Options().addArguments('-headless').windowSize({ width, height }))44.build()4546driver47.get('http://www.google.com/ncr')48.then((_) => driver.findElement(By.name('q')).sendKeys('webdriver', Key.RETURN))49.then((_) => driver.wait(until.titleIs('webdriver - Google Search'), 1000))50.then(51(_) => driver.quit(),52(e) =>53driver.quit().then(() => {54throw e55}),56)575859