UNPKG

selenium-webdriver

Version:

The official WebDriver JavaScript bindings from the Selenium project

46 lines (41 loc) 11.8 kB
<!DOCTYPE html><meta charset="UTF-8"><meta http-equiv="Content-Language" content="en" /><title>goog.testing.PropertyReplacer</title><link href="dossier.css" rel="stylesheet" type="text/css"><div id="main-wrapper"><input type="checkbox" id="sidenav-toggle" /><main><header><h1>Class goog.testing.PropertyReplacer</h1><a class="source" href="source/lib/goog/testing/propertyreplacer.js.src.html#l65">code &raquo;</a></header><section><p>Helper class for stubbing out variables and object properties for unit tests. This class can change the value of some variables before running the test cases, and to reset them in the tearDown phase. See googletest.StubOutForTesting as an analogy in Python: http://protobuf.googlecode.com/svn/trunk/python/stubout.py Example usage: <pre>var stubs = new goog.testing.PropertyReplacer(); function setUp() { // Mock functions used in all test cases. stubs.set(Math, 'random', function() { return 4; // Chosen by fair dice roll. Guaranteed to be random. }); } function tearDown() { stubs.reset(); } function testThreeDice() { // Mock a constant used only in this test case. stubs.set(goog.global, 'DICE_COUNT', 3); assertEquals(12, rollAllDice()); }</pre> Constraints on altered objects: <ul> <li>DOM subclasses aren't supported. <li>The value of the objects' constructor property must either be equal to the real constructor or kept untouched. </ul><h2>Constructor</h2><div class="ctor wrap-details public"><div><div class="ctor"><span class="member">goog.testing.PropertyReplacer <span class="args">( )</span></span></div></div></div></section><div id="visibility-controls"><b>Show:</b><label for="show-public"><span><input type="checkbox" id="show-public" checked/></span>Public</label><label for="show-protected"><span><input type="checkbox" id="show-protected"/></span>Protected</label><label for="show-private"><span><input type="checkbox" id="show-private"/></span>Private</label></div><section id="instance-methods"><h2>Instance Methods</h2><div class="wrap-details public"><div><details class="function"><summary><div><a class="source" href="source/lib/goog/testing/propertyreplacer.js.src.html#l224">code &raquo;</a><span class="member"><a name="remove">remove</a> <span class="args">( obj, key )</span></span></div><p>Deletes the key from the object while saving its original value.</summary><div class="info"><table><tbody><tr><th>Parameters<tr><td><dl><dt>obj: <code class="type">(<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>|<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function">Function</a>)</code><dd>The JavaScript or native object or function to alter. See the constraints in the class description.<dt>key: <code class="type"><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">string</a></code><dd>The key to delete.</dl></table></div></details></div></div><div class="wrap-details public"><div><details class="function"><summary><div><a class="source" href="source/lib/goog/testing/propertyreplacer.js.src.html#l183">code &raquo;</a><span class="member"><a name="replace">replace</a> <span class="args">( obj, key, value )</span></span></div><p>Changes an existing value in an object to another one of the same type while saving its original state. The advantage of <code >replace</code> over <code class="type"><a class="unresolved-link">#set</a></code> is that <code >replace</code> protects against typos and erroneously passing tests after some members have been renamed during a refactoring.</summary><div class="info"><table><tbody><tr><th>Parameters<tr><td><dl><dt>obj: <code class="type">(<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>|<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function">Function</a>)</code><dd>The JavaScript or native object or function to alter. See the constraints in the class description.<dt>key: <code class="type"><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">string</a></code><dd>The key to change the value for. It has to be present either in <code >obj</code> or in its prototype chain.<dt>value: <code class="type">*</code><dd>The new value to set. It has to have the same type as the original value. The types are compared with <code class="type"><a href="namespace_goog.html#goog.typeOf">goog.typeOf</a></code>.</dl><tr><th>Throws<tr><td><dl><dt><code class="type"><a href="http://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Error">Error</a></code><dd>In case of missing key or type mismatch.</dl></table></div></details></div></div><div class="wrap-details public"><div><details class="function"><summary><div><a class="source" href="source/lib/goog/testing/propertyreplacer.js.src.html#l235">code &raquo;</a><span class="member"><a name="reset">reset</a> <span class="args">( )</span></span></div><p>Resets all changes made by goog.testing.PropertyReplacer.prototype.set.</summary></details></div></div><div class="wrap-details public"><div><details class="function"><summary><div><a class="source" href="source/lib/goog/testing/propertyreplacer.js.src.html#l162">code &raquo;</a><span class="member"><a name="set">set</a> <span class="args">( obj, key, value )</span></span></div><p>Adds or changes a value in an object while saving its original state.</summary><div class="info"><table><tbody><tr><th>Parameters<tr><td><dl><dt>obj: <code class="type">(<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>|<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function">Function</a>)</code><dd>The JavaScript or native object or function to alter. See the constraints in the class description.<dt>key: <code class="type"><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">string</a></code><dd>The key to change the value for.<dt>value: <code class="type">*</code><dd>The new value to set.</dl></table></div></details></div></div><div class="wrap-details public"><div><details class="function"><summary><div><a class="source" href="source/lib/goog/testing/propertyreplacer.js.src.html#l201">code &raquo;</a><span class="member"><a name="setPath">setPath</a> <span class="args">( path, value )</span></span></div><p>Builds an object structure for the provided namespace path. Doesn't overwrite those prefixes of the path that are already objects or functions.</summary><div class="info"><table><tbody><tr><th>Parameters<tr><td><dl><dt>path: <code class="type"><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">string</a></code><dd>The path to create or alter, e.g. 'goog.ui.Menu'.<dt>value: <code class="type">*</code><dd>The value to set.</dl></table></div></details></div></div></section><section id="instance-properties"><h2>Instance Properties</h2><div class="wrap-details private"><div><details><summary><div><a class="source" href="source/lib/goog/testing/propertyreplacer.js.src.html#l74">code &raquo;</a><span class="member"><a name="original_">original_</a> : <code class="type"><a href="http://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array">Array</a>.&lt;<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>&gt;</code></span></div><p>Stores the values changed by the set() method in chronological order. Its items are objects with 3 fields: 'object', 'key', 'value'. The original value for the given key in the given object is stored under the 'value' key.</summary></details></div></div></section><section id="static-functions"><h2>Static Functions</h2><div class="wrap-details private"><div><details class="function"><summary><div><a class="source" href="source/lib/goog/testing/propertyreplacer.js.src.html#l134">code &raquo;</a><span class="member"><a name="goog.testing.PropertyReplacer.deleteKey_">goog.testing.PropertyReplacer.deleteKey_</a> <span class="args">( obj, key )</span></span></div><p>Deletes a key from an object. Sets it to undefined or empty string if the delete failed.</summary><div class="info"><table><tbody><tr><th>Parameters<tr><td><dl><dt>obj: <code class="type">(<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>|<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function">Function</a>)</code><dd>The object or function to delete a key from.<dt>key: <code class="type"><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">string</a></code><dd>The key to delete.</dl></table></div></details></div></div><div class="wrap-details private"><div><details class="function"><summary><div><a class="source" href="source/lib/goog/testing/propertyreplacer.js.src.html#l94">code &raquo;</a><span class="member"><a name="goog.testing.PropertyReplacer.hasKey_">goog.testing.PropertyReplacer.hasKey_</a> <span class="args">( obj, key )</span> &rArr; <code class="type"><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean">boolean</a></code></span></div><p>Tells if the given key exists in the object. Ignores inherited fields.</summary><div class="info"><table><tbody><tr><th>Parameters<tr><td><dl><dt>obj: <code class="type">(<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>|<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function">Function</a>)</code><dd>The JavaScript or native object or function whose key is to be checked.<dt>key: <code class="type"><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">string</a></code><dd>The key to check.</dl><tr><th>Returns<tr><td><dl>Whether the object has the key as own key.</dl></table></div></details></div></div></section><section id="static-properties"><h2>Static Properties</h2><div class="wrap-details private"><div><details><summary><div><a class="source" href="source/lib/goog/testing/propertyreplacer.js.src.html#l83">code &raquo;</a><span class="member"><a name="goog.testing.PropertyReplacer.NO_SUCH_KEY_">goog.testing.PropertyReplacer.NO_SUCH_KEY_</a> : <code class="type"><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a></code></span></div><p>Indicates that a key didn't exist before having been set by the set() method.</summary></details></div></div></section></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>