UNPKG

selenium-webdriver

Version:

The official WebDriver JavaScript bindings from the Selenium project

1 lines 12.5 kB
<!DOCTYPE html><meta charset="UTF-8"><meta http-equiv="Content-Language" content="en" /><title>corsclient.js</title><link href="../../../../dossier.css" rel="stylesheet" type="text/css"><div id="main-wrapper"><input type="checkbox" id="sidenav-toggle" /><main><header><h1>lib/webdriver/http/corsclient.js</h1></header><pre><table class="srcfile"><tbody><tr><td><a name="l1" href="#l1">1</a><td>// Copyright 2011 Software Freedom Conservancy. All Rights Reserved.<tr><td><a name="l2" href="#l2">2</a><td>//<tr><td><a name="l3" href="#l3">3</a><td>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);<tr><td><a name="l4" href="#l4">4</a><td>// you may not use this file except in compliance with the License.<tr><td><a name="l5" href="#l5">5</a><td>// You may obtain a copy of the License at<tr><td><a name="l6" href="#l6">6</a><td>//<tr><td><a name="l7" href="#l7">7</a><td>// http://www.apache.org/licenses/LICENSE-2.0<tr><td><a name="l8" href="#l8">8</a><td>//<tr><td><a name="l9" href="#l9">9</a><td>// Unless required by applicable law or agreed to in writing, software<tr><td><a name="l10" href="#l10">10</a><td>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,<tr><td><a name="l11" href="#l11">11</a><td>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.<tr><td><a name="l12" href="#l12">12</a><td>// See the License for the specific language governing permissions and<tr><td><a name="l13" href="#l13">13</a><td>// limitations under the License.<tr><td><a name="l14" href="#l14">14</a><td><tr><td><a name="l15" href="#l15">15</a><td>goog.provide(&#39;webdriver.http.CorsClient&#39;);<tr><td><a name="l16" href="#l16">16</a><td><tr><td><a name="l17" href="#l17">17</a><td>goog.require(&#39;goog.json&#39;);<tr><td><a name="l18" href="#l18">18</a><td>goog.require(&#39;webdriver.http.Response&#39;);<tr><td><a name="l19" href="#l19">19</a><td><tr><td><a name="l20" href="#l20">20</a><td><tr><td><a name="l21" href="#l21">21</a><td><tr><td><a name="l22" href="#l22">22</a><td>/**<tr><td><a name="l23" href="#l23">23</a><td> * Communicates with a WebDriver server, which may be on a different domain,<tr><td><a name="l24" href="#l24">24</a><td> * using the &lt;a href=&quot;http://www.w3.org/TR/cors/&quot;&gt;cross-origin resource sharing<tr><td><a name="l25" href="#l25">25</a><td> * &lt;/a&gt; (CORS) extension to WebDriver&#39;s JSON wire protocol.<tr><td><a name="l26" href="#l26">26</a><td> *<tr><td><a name="l27" href="#l27">27</a><td> * &lt;p&gt;Each command from the standard JSON protocol will be encoded in a<tr><td><a name="l28" href="#l28">28</a><td> * JSON object with the following form:<tr><td><a name="l29" href="#l29">29</a><td> * {method:string, path:string, data:!Object}<tr><td><a name="l30" href="#l30">30</a><td> *<tr><td><a name="l31" href="#l31">31</a><td> * &lt;p&gt;The encoded command is then sent as a POST request to the server&#39;s /xdrpc<tr><td><a name="l32" href="#l32">32</a><td> * endpoint. The server will decode the command, re-route it to the appropriate<tr><td><a name="l33" href="#l33">33</a><td> * handler, and then return the command&#39;s response as a standard JSON response<tr><td><a name="l34" href="#l34">34</a><td> * object. The JSON responses will &lt;em&gt;always&lt;/em&gt; be returned with a 200<tr><td><a name="l35" href="#l35">35</a><td> * response from the server; clients must rely on the response&#39;s &quot;status&quot; field<tr><td><a name="l36" href="#l36">36</a><td> * to determine whether the command succeeded.<tr><td><a name="l37" href="#l37">37</a><td> *<tr><td><a name="l38" href="#l38">38</a><td> * &lt;p&gt;This client cannot be used with the standard wire protocol due to<tr><td><a name="l39" href="#l39">39</a><td> * limitations in the various browser implementations of the CORS specification:<tr><td><a name="l40" href="#l40">40</a><td> * &lt;ul&gt;<tr><td><a name="l41" href="#l41">41</a><td> * &lt;li&gt;IE&#39;s &lt;a href=&quot;http://goo.gl/6l3kA&quot;&gt;XDomainRequest&lt;/a&gt; object is only<tr><td><a name="l42" href="#l42">42</a><td> * capable of generating the types of requests that may be generated through<tr><td><a name="l43" href="#l43">43</a><td> * a standard &lt;a href=&quot;http://goo.gl/vgzAU&quot;&gt;HTML form&lt;/a&gt; - it can not send<tr><td><a name="l44" href="#l44">44</a><td> * DELETE requests, as is required in the wire protocol.<tr><td><a name="l45" href="#l45">45</a><td> * &lt;li&gt;WebKit&#39;s implementation of CORS does not follow the spec and forbids<tr><td><a name="l46" href="#l46">46</a><td> * redirects: https://bugs.webkit.org/show_bug.cgi?id=57600<tr><td><a name="l47" href="#l47">47</a><td> * This limitation appears to be intentional and is documented in WebKit&#39;s<tr><td><a name="l48" href="#l48">48</a><td> * Layout tests:<tr><td><a name="l49" href="#l49">49</a><td> * //LayoutTests/http/tests/xmlhttprequest/access-control-and-redirects.html<tr><td><a name="l50" href="#l50">50</a><td> * &lt;li&gt;If the server does not return a 2xx response, IE and Opera&#39;s<tr><td><a name="l51" href="#l51">51</a><td> * implementations will fire the XDomainRequest/XMLHttpRequest object&#39;s<tr><td><a name="l52" href="#l52">52</a><td> * onerror handler, but without the corresponding response text returned by<tr><td><a name="l53" href="#l53">53</a><td> * the server. This renders IE and Opera incapable of handling command<tr><td><a name="l54" href="#l54">54</a><td> * failures in the standard JSON protocol.<tr><td><a name="l55" href="#l55">55</a><td> * &lt;/ul&gt;<tr><td><a name="l56" href="#l56">56</a><td> *<tr><td><a name="l57" href="#l57">57</a><td> * @param {string} url URL for the WebDriver server to send commands to.<tr><td><a name="l58" href="#l58">58</a><td> * @constructor<tr><td><a name="l59" href="#l59">59</a><td> * @implements {webdriver.http.Client}<tr><td><a name="l60" href="#l60">60</a><td> * @see &lt;a href=&quot;http://www.w3.org/TR/cors/&quot;&gt;CORS Spec&lt;/a&gt;<tr><td><a name="l61" href="#l61">61</a><td> * @see &lt;a href=&quot;http://code.google.com/p/selenium/wiki/JsonWireProtocol&quot;&gt;<tr><td><a name="l62" href="#l62">62</a><td> * JSON wire protocol&lt;/a&gt;<tr><td><a name="l63" href="#l63">63</a><td> */<tr><td><a name="l64" href="#l64">64</a><td>webdriver.http.CorsClient = function(url) {<tr><td><a name="l65" href="#l65">65</a><td> if (!webdriver.http.CorsClient.isAvailable()) {<tr><td><a name="l66" href="#l66">66</a><td> throw Error(&#39;The current environment does not support cross-origin &#39; +<tr><td><a name="l67" href="#l67">67</a><td> &#39;resource sharing&#39;);<tr><td><a name="l68" href="#l68">68</a><td> }<tr><td><a name="l69" href="#l69">69</a><td><tr><td><a name="l70" href="#l70">70</a><td> /** @private {string} */<tr><td><a name="l71" href="#l71">71</a><td> this.url_ = url + webdriver.http.CorsClient.XDRPC_ENDPOINT;<tr><td><a name="l72" href="#l72">72</a><td>};<tr><td><a name="l73" href="#l73">73</a><td><tr><td><a name="l74" href="#l74">74</a><td><tr><td><a name="l75" href="#l75">75</a><td>/**<tr><td><a name="l76" href="#l76">76</a><td> * Resource URL to send commands to on the server.<tr><td><a name="l77" href="#l77">77</a><td> * @type {string}<tr><td><a name="l78" href="#l78">78</a><td> * @const<tr><td><a name="l79" href="#l79">79</a><td> */<tr><td><a name="l80" href="#l80">80</a><td>webdriver.http.CorsClient.XDRPC_ENDPOINT = &#39;/xdrpc&#39;;<tr><td><a name="l81" href="#l81">81</a><td><tr><td><a name="l82" href="#l82">82</a><td><tr><td><a name="l83" href="#l83">83</a><td>/**<tr><td><a name="l84" href="#l84">84</a><td> * Tests whether the current environment supports cross-origin resource sharing.<tr><td><a name="l85" href="#l85">85</a><td> * @return {boolean} Whether cross-origin resource sharing is supported.<tr><td><a name="l86" href="#l86">86</a><td> * @see http://www.w3.org/TR/cors/<tr><td><a name="l87" href="#l87">87</a><td> */<tr><td><a name="l88" href="#l88">88</a><td>webdriver.http.CorsClient.isAvailable = function() {<tr><td><a name="l89" href="#l89">89</a><td> return typeof XDomainRequest !== &#39;undefined&#39; ||<tr><td><a name="l90" href="#l90">90</a><td> (typeof XMLHttpRequest !== &#39;undefined&#39; &amp;&amp;<tr><td><a name="l91" href="#l91">91</a><td> goog.isBoolean(new XMLHttpRequest().withCredentials));<tr><td><a name="l92" href="#l92">92</a><td>};<tr><td><a name="l93" href="#l93">93</a><td><tr><td><a name="l94" href="#l94">94</a><td><tr><td><a name="l95" href="#l95">95</a><td>/** @override */<tr><td><a name="l96" href="#l96">96</a><td>webdriver.http.CorsClient.prototype.send = function(request, callback) {<tr><td><a name="l97" href="#l97">97</a><td> try {<tr><td><a name="l98" href="#l98">98</a><td> var xhr = new (typeof XDomainRequest !== &#39;undefined&#39; ?<tr><td><a name="l99" href="#l99">99</a><td> XDomainRequest : XMLHttpRequest);<tr><td><a name="l100" href="#l100">100</a><td> xhr.open(&#39;POST&#39;, this.url_, true);<tr><td><a name="l101" href="#l101">101</a><td><tr><td><a name="l102" href="#l102">102</a><td> xhr.onload = function() {<tr><td><a name="l103" href="#l103">103</a><td> callback(null, webdriver.http.Response.fromXmlHttpRequest(<tr><td><a name="l104" href="#l104">104</a><td> /** @type {!XMLHttpRequest} */ (xhr)));<tr><td><a name="l105" href="#l105">105</a><td> };<tr><td><a name="l106" href="#l106">106</a><td><tr><td><a name="l107" href="#l107">107</a><td> var url = this.url_;<tr><td><a name="l108" href="#l108">108</a><td> xhr.onerror = function() {<tr><td><a name="l109" href="#l109">109</a><td> callback(Error([<tr><td><a name="l110" href="#l110">110</a><td> &#39;Unable to send request: POST &#39;, url,<tr><td><a name="l111" href="#l111">111</a><td> &#39;\nPerhaps the server did not respond to the preflight request &#39;,<tr><td><a name="l112" href="#l112">112</a><td> &#39;with valid access control headers?&#39;<tr><td><a name="l113" href="#l113">113</a><td> ].join(&#39;&#39;)));<tr><td><a name="l114" href="#l114">114</a><td> };<tr><td><a name="l115" href="#l115">115</a><td><tr><td><a name="l116" href="#l116">116</a><td> // Define event handlers for all events on the XDomainRequest. Apparently,<tr><td><a name="l117" href="#l117">117</a><td> // if we don&#39;t do this, IE9+10 will silently abort our request. Yay IE.<tr><td><a name="l118" href="#l118">118</a><td> // Note, we&#39;re not using goog.nullFunction, because it tends to get<tr><td><a name="l119" href="#l119">119</a><td> // optimized away by the compiler, which leaves us where we were before.<tr><td><a name="l120" href="#l120">120</a><td> xhr.onprogress = xhr.ontimeout = function() {};<tr><td><a name="l121" href="#l121">121</a><td><tr><td><a name="l122" href="#l122">122</a><td> xhr.send(goog.json.serialize({<tr><td><a name="l123" href="#l123">123</a><td> &#39;method&#39;: request.method,<tr><td><a name="l124" href="#l124">124</a><td> &#39;path&#39;: request.path,<tr><td><a name="l125" href="#l125">125</a><td> &#39;data&#39;: request.data<tr><td><a name="l126" href="#l126">126</a><td> }));<tr><td><a name="l127" href="#l127">127</a><td> } catch (ex) {<tr><td><a name="l128" href="#l128">128</a><td> callback(ex);<tr><td><a name="l129" href="#l129">129</a><td> }<tr><td><a name="l130" href="#l130">130</a><td>};</table></pre></main><nav id="topnav"><div><div id="menubutton"><label for="sidenav-toggle">Menu</label></div><form id="searchbox"><div><input type="search" placeholder="Search" tabindex="1"></div></form></div></nav><nav id="sidenav"><input type="checkbox" id="sidenav-types-ctrl" /><input type="checkbox" id="sidenav-files-ctrl" /><input type="checkbox" id="sidenav-modules-ctrl" /><a id="sidenav-overview"><div><h4>Overview</h4></div></a><div id="sidenav-types"><label for="sidenav-types-ctrl"><h4>Types</h4></label><i>Loading</i></div><div id="sidenav-modules"><label for="sidenav-modules-ctrl"><h4>Modules</h4></label><i>Loading</i></div><div id="sidenav-files"><label for="sidenav-files-ctrl"><h4>Files</h4></label><i>Loading</i></div><a href="license.html"><div><h4>License</h4></div></a></nav><div id="push-footer"></div></div><footer><a href="https://github.com/jleyba/js-dossier">Generated by dossier</a></footer><script src="../../../../types.js"></script><script src="../../../../dossier.js"></script>