selenium-webdriver
Version:
The official WebDriver JavaScript bindings from the Selenium project
123 lines (120 loc) • 8.72 kB
HTML
<meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no"><meta http-equiv="Content-Language" content="en"><meta http-equiv="X-UA-Compatible" content="IE=edge"><title>Index</title><link href="dossier.css" rel="stylesheet" type="text/css"><header><div><form><div><input type="search" placeholder="Search" tabindex="1"></div></form></div></header><main><article class="indexfile"><h1>selenium-webdriver</h1>
<p>Selenium is a browser automation library. Most often used for testing
web-applications, Selenium may be used for any task that requires automating
interaction with the browser.</p>
<h2>Installation</h2>
<p>Install via npm with</p>
<pre><code>npm install selenium-webdriver
</code></pre>
<p>Out of the box, Selenium includes everything you need to work with Firefox. You
will need to download additional components to work with the other major
browsers. The drivers for Chrome, IE, PhantomJS, and Opera are all standalone
executables that should be placed on your
<a href="http://en.wikipedia.org/wiki/PATH_%28variable%29">PATH</a>. The SafariDriver
browser extension should be installed in your browser before using Selenium; we
recommend disabling the extension when using the browser without Selenium or
installing the extension in a profile only used for testing.</p>
<h2>Usage</h2>
<p>The sample below and others are included in the <code>example</code> directory. You may
also find the tests for selenium-webdriver informative.</p>
<pre><code>var webdriver = require('selenium-webdriver'),
By = require('selenium-webdriver').By,
until = require('selenium-webdriver').until;
var driver = new webdriver.Builder()
.forBrowser('firefox')
.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();
</code></pre>
<h3>Using the Builder API</h3>
<p>The <code>Builder</code> class is your one-stop shop for configuring new WebDriver
instances. Rather than clutter your code with branches for the various browsers,
the builder lets you set all options in one flow. When you call
<code>Builder#build()</code>, all options irrelevant to the selected browser are dropped:</p>
<pre><code>var webdriver = require('selenium-webdriver'),
chrome = require('selenium-webdriver/chrome'),
firefox = require('selenium-webdriver/firefox');
var driver = new webdriver.Builder()
.forBrowser('firefox')
.setChromeOptions(/* ... */)
.setFirefoxOptions(/* ... */)
.build();
</code></pre>
<p>Why would you want to configure options irrelevant to the target browser? The
<code>Builder</code>'s API defines your <em>default</em> configuration. You can change the target
browser at runtime through the <code>SELENIUM_BROWSER</code> environment variable. For
example, the <code>example/google_search.js</code> script is configured to run against
Firefox. You can run the example against other browsers just by changing the
runtime environment</p>
<pre><code># cd node_modules/selenium-webdriver
node example/google_search
SELENIUM_BROWSER=chrome node example/google_search
SELENIUM_BROWSER=safari node example/google_search
</code></pre>
<h3>The Standalone Selenium Server</h3>
<p>The standalone Selenium Server acts as a proxy between your script and the
browser-specific drivers. The server may be used when running locally, but it's
not recommend as it introduces an extra hop for each request and will slow
things down. The server is required, however, to use a browser on a remote host
(most browser drivers, like the IEDriverServer, do not accept remote
connections).</p>
<p>To use the Selenium Server, you will need to install the
<a href="http://www.oracle.com/technetwork/java/javase/downloads/index.html">JDK</a> and
download the latest server from <a href="http://selenium-release.storage.googleapis.com/index.html">Selenium</a>. Once downloaded, run the
server with</p>
<pre><code>java -jar selenium-server-standalone-2.45.0.jar
</code></pre>
<p>You may configure your tests to run against a remote server through the Builder
API:</p>
<pre><code>var driver = new webdriver.Builder()
.forBrowser('firefox')
.usingServer('http://localhost:4444/wd/hub')
.build();
</code></pre>
<p>Or change the Builder's configuration at runtime with the <code>SELENIUM_REMOTE_URL</code>
environment variable:</p>
<pre><code>SELENIUM_REMOTE_URL="http://localhost:4444/wd/hub" node script.js
</code></pre>
<p>You can experiment with these options using the <code>example/google_search.js</code>
script provided with <code>selenium-webdriver</code>.</p>
<h2>Documentation</h2>
<p>API documentation is included in the <code>docs</code> directory and is also available
online from the <a href="http://selenium.googlecode.com/git/docs/api/javascript/index.html">Selenium project</a>. Addition resources include</p>
<ul><li>the #selenium channel on freenode IRC</li><li>the <a href="https://groups.google.com/forum/#!forum/selenium-users">selenium-users@googlegroups.com</a> list</li><li><a href="http://www.seleniumhq.org/docs/">SeleniumHQ</a> documentation</li></ul>
<h2>Contributing</h2>
<p>Contributions are accepted either through <a href="https://github.com/SeleniumHQ/selenium/">GitHub</a> pull requests or patches
via the <a href="https://github.com/SeleniumHQ/selenium/issues">Selenium issue tracker</a>. You must sign our
<a href="http://goo.gl/qC50R">Contributor License Agreement</a> before your changes will be accepted.</p>
<h2>Issues</h2>
<p>Please report any issues using the <a href="https://github.com/SeleniumHQ/selenium/issues">Selenium issue tracker</a>. When using
the issue tracker</p>
<ul><li><strong>Do</strong> include a detailed description of the problem.</li><li><strong>Do</strong> include a link to a <a href="http://gist.github.com/">gist</a> with any
interesting stack traces/logs (you may also attach these directly to the bug
report).</li><li><strong>Do</strong> include a <a href="http://www.webkit.org/quality/reduction.html">reduced test case</a>. Reporting "unable to find
element on the page" is <em>not</em> a valid report - there's nothing for us to
look into. Expect your bug report to be closed if you do not provide enough
information for us to investigate.</li><li><strong>Do not</strong> use the issue tracker to submit basic help requests. All help
inquiries should be directed to the <a href="https://groups.google.com/forum/#!forum/selenium-users">user forum</a> or #selenium IRC
channel.</li><li><strong>Do not</strong> post empty "I see this too" or "Any updates?" comments. These
provide no additional information and clutter the log.</li><li><strong>Do not</strong> report regressions on closed bugs as they are not actively
monitored for upates (especially bugs that are >6 months old). Please open a
new issue and reference the original bug in your report.</li></ul>
<h2>License</h2>
<p>Licensed to the Software Freedom Conservancy (SFC) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The SFC licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at</p>
<p>http://www.apache.org/licenses/LICENSE-2.0</p>
<p>Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.</p>
</article><nav><h3><a href="index.html" tabindex="2">Overview</a></h3><div><input type="checkbox" id="nav-modules" checked/><label for="nav-modules"><h3><span class="selectable" tabindex="2">Modules</span></h3></label><div id="nav-modules-view"></div></div><div><input type="checkbox" id="nav-types" checked/><label for="nav-types"><h3><span class="selectable" tabindex="2">Types</span></h3></label><div id="nav-types-view"></div></div><h3><a href="Changes.html" tabindex="2">Changes</a></h3></nav></main><footer><div><a href="https://github.com/jleyba/js-dossier">Generated by dossier</a></div></footer><script src="types.js"></script><script src="dossier.js"></script>