refs #279 - support for frames through waitForFrame() and withFrame()
Showing
2 changed files
with
67 additions
and
17 deletions
... | @@ -1728,10 +1728,28 @@ Casper.prototype.waitFor = function waitFor(testFx, then, onTimeout, timeout) { | ... | @@ -1728,10 +1728,28 @@ Casper.prototype.waitFor = function waitFor(testFx, then, onTimeout, timeout) { |
1728 | }; | 1728 | }; |
1729 | 1729 | ||
1730 | /** | 1730 | /** |
1731 | * Waits for a child page having its url matching the provided pattern to be opened | 1731 | * Waits for a child frame page to be loaded. |
1732 | * | ||
1733 | * @param String frameName The frame name | ||
1734 | * @param Function then The next step function (optional) | ||
1735 | * @param Function onTimeout Function to call on operation timeout (optional) | ||
1736 | * @param Number timeout Timeout in milliseconds (optional) | ||
1737 | * @return Casper | ||
1738 | */ | ||
1739 | Casper.prototype.waitForFrame = function waitForFrame(frameName, then, onTimeout, timeout) { | ||
1740 | "use strict"; | ||
1741 | return this.waitFor(function() { | ||
1742 | return this.page.childFramesName().some(function(name) { | ||
1743 | return name === frameName; | ||
1744 | }); | ||
1745 | }, then, onTimeout, timeout); | ||
1746 | }; | ||
1747 | |||
1748 | /** | ||
1749 | * Waits for a popup page having its url matching the provided pattern to be opened | ||
1732 | * and loaded. | 1750 | * and loaded. |
1733 | * | 1751 | * |
1734 | * @param String|RegExp urlPattern The child page url pattern | 1752 | * @param String|RegExp urlPattern The popup url pattern |
1735 | * @param Function then The next step function (optional) | 1753 | * @param Function then The next step function (optional) |
1736 | * @param Function onTimeout Function to call on operation timeout (optional) | 1754 | * @param Function onTimeout Function to call on operation timeout (optional) |
1737 | * @param Number timeout Timeout in milliseconds (optional) | 1755 | * @param Number timeout Timeout in milliseconds (optional) |
... | @@ -1863,10 +1881,38 @@ Casper.prototype.waitWhileVisible = function waitWhileVisible(selector, then, on | ... | @@ -1863,10 +1881,38 @@ Casper.prototype.waitWhileVisible = function waitWhileVisible(selector, then, on |
1863 | }; | 1881 | }; |
1864 | 1882 | ||
1865 | /** | 1883 | /** |
1866 | * Makes the provided child page as the currently active one. Note that the | 1884 | * Makes the provided frame page as the currently active one. Note that the |
1885 | * active page will be reverted when finished. | ||
1886 | * | ||
1887 | * @param String frameName Target frame name | ||
1888 | * @param Function then Next step function | ||
1889 | * @return Casper | ||
1890 | */ | ||
1891 | Casper.prototype.withFrame = function withFrame(frameName, then) { | ||
1892 | "use strict"; | ||
1893 | this.then(function _step() { | ||
1894 | // make the frame page the currently active one | ||
1895 | this.page.switchToChildFrame(frameName); | ||
1896 | }); | ||
1897 | try { | ||
1898 | this.then(then); | ||
1899 | } catch (e) { | ||
1900 | // revert to main page on error | ||
1901 | this.log("error while processing frame step: " + e, "error"); | ||
1902 | this.page.switchToMainFrame(); | ||
1903 | throw e; | ||
1904 | } | ||
1905 | return this.then(function _step() { | ||
1906 | // revert to main page | ||
1907 | this.page.switchToMainFrame(); | ||
1908 | }); | ||
1909 | }; | ||
1910 | |||
1911 | /** | ||
1912 | * Makes the provided frame page as the currently active one. Note that the | ||
1867 | * active page will be reverted when finished. | 1913 | * active page will be reverted when finished. |
1868 | * | 1914 | * |
1869 | * @param String|RegExp|WebPage popup Target child page information | 1915 | * @param String|RegExp|WebPage popup Target frame page information |
1870 | * @param Function then Next step function | 1916 | * @param Function then Next step function |
1871 | * @return Casper | 1917 | * @return Casper |
1872 | */ | 1918 | */ | ... | ... |
1 | /*global casper __utils__*/ | 1 | /*global casper __utils__*/ |
2 | /*jshint strict:false*/ | 2 | /*jshint strict:false*/ |
3 | casper.start('tests/site/frames.html', function() { | 3 | casper.start('tests/site/frames.html'); |
4 | |||
5 | casper.waitForFrame('frame1', function() { | ||
6 | this.test.pass('Casper.waithForFrame() can wait for a frame to be loaded.'); | ||
4 | this.test.assertTitle('CasperJS test frames'); | 7 | this.test.assertTitle('CasperJS test frames'); |
5 | this.page.switchToChildFrame("frame1"); | 8 | }); |
9 | |||
10 | casper.waitForFrame('frame2', function() { | ||
11 | this.test.pass('Casper.waithForFrame() can wait for another frame to be loaded.'); | ||
12 | this.test.assertTitle('CasperJS test frames'); | ||
13 | }); | ||
14 | |||
15 | casper.withFrame('frame1', function() { | ||
6 | this.test.assertTitle('CasperJS frame 1'); | 16 | this.test.assertTitle('CasperJS frame 1'); |
7 | this.test.assertExists("#f1"); | 17 | }); |
8 | this.test.assertDoesntExist("#f2"); | 18 | |
9 | this.page.switchToParentFrame(); | 19 | casper.withFrame('frame2', function() { |
10 | this.page.switchToChildFrame("frame2"); | ||
11 | this.test.assertTitle('CasperJS frame 2'); | 20 | this.test.assertTitle('CasperJS frame 2'); |
12 | this.test.assertExists("#f2"); | ||
13 | this.test.assertDoesntExist("#f1"); | ||
14 | this.test.assertEval(function() { | ||
15 | return '__utils__' in window && 'getBinary' in __utils__; | ||
16 | }, '__utils__ object is available in child frame'); | ||
17 | }); | 21 | }); |
18 | 22 | ||
19 | casper.run(function() { | 23 | casper.run(function() { |
20 | this.page.switchToMainFrame(); | 24 | this.test.assertTitle('CasperJS test frames'); |
21 | this.test.done(8); | 25 | this.test.done(7); |
22 | }); | 26 | }); | ... | ... |
-
Please register or sign in to post a comment