{"id":411,"date":"2014-04-10T17:36:16","date_gmt":"2014-04-10T09:36:16","guid":{"rendered":"http:\/\/www.cnmiss.cn\/?p=411"},"modified":"2014-04-10T17:36:16","modified_gmt":"2014-04-10T09:36:16","slug":"casperjs-%e4%bb%8b%e7%bb%8d","status":"publish","type":"post","link":"https:\/\/www.cnmiss.cn\/?p=411","title":{"rendered":"CasperJS \u4ecb\u7ecd"},"content":{"rendered":"<p>\u4ec0\u4e48\u662fCasperJS<\/p>\n<p>\u5b98\u65b9\u89e3\u91caCasperJS \u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u5bfc\u822a\u811a\u672c\u548c\u6d4b\u8bd5\u5de5\u5177\uff0c\u4f7f\u7528 JavaScript \u57fa\u4e8e PhantomJS \u7f16\u5199\uff0c\u7528\u4e8e\u6d4b\u8bd5 Web \u5e94\u7528\u529f\u80fd\uff0cPhantom JS\u662f\u4e00\u4e2a\u670d\u52a1\u5668\u7aef\u7684 JavaScript API \u7684 WebKit\u3002\u5176\u652f\u6301\u5404\u79cdWeb\u6807\u51c6\uff1a DOM \u5904\u7406, CSS \u9009\u62e9\u5668, JSON, Canvas, \u548c SVG\u3002<\/p>\n<p>\u5b83\u662f\u4e00\u4e2a\u975e\u5e38\u68d2\u7684\u5de5\u5177\u3002\u975e\u5e38\u7b80\u5355\u7684,\u7528JavaScript\u7f16\u5199,\u6ee1\u8db3\u5404\u79cd\u5404\u6837\u7684\u6d4b\u8bd5\u9700\u6c42,\u5b83\u53ef\u4ee5\u65b9\u4fbf\u548c\u6211\u4eec\u7684\u5f00\u53d1\u73af\u5883\u96c6\u6210. \u5b8c\u7f8e\u7684\u7ec4\u5408\u524d\u7aef\u548c\u540e\u7aef.<\/p>\n<p>CasperJS\u7b80\u5355\u6765\u8bf4\u5c31\u662f\u4e00\u4e2a\u7f51\u7ad9\u6d4b\u8bd5\u7684\u5de5\u5177\uff0c\u5b89\u88c5CasperJS\u540e\uff0c\u4f60\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u8fd0\u884c\u4e00\u4e2aJS\u6587\u4ef6\uff0c\u4f8b\u5982\u5728\u63a7\u5236\u53f0\u4e0b\u53ef\u4ee5\u8fd0\u884c\uff0c\u8fd0\u884c\u547d\u4ee4\uff1aC:\\Users\\Administrator&gt;casperjs test d:\/casperjs\/tests\/snap.js \u2013url=https:\/\/www.cnmiss.cn\uff0ccasperjs test\u662f\u6267\u884c\u7684\u547d\u4ee4\uff0csnap.js\u662f\u81ea\u5df1\u7f16\u5199\u7684\u4e00\u4e2aJS\u811a\u672c\uff0c\u2013url=https:\/\/www.cnmiss.cn \u8fd9\u4e2a\u662f\u5411\u811a\u672c\u4e2d\u4f20\u9012\u7684\u53c2\u6570\u3002\u5728snap.js\u811a\u672c\u4e2d\uff0c\u4f60\u53ef\u4ee5\u6a21\u62df\u4f60\u5728\u7f51\u7ad9\u4e0a\u7684\u64cd\u4f5c\uff0c\u901a\u8fc7\u811a\u672c\u7684\u5f62\u5f0f\u5199\u51fa\u6765\u3002\u4f8b\u5982\u767b\u9646\u7f51\u7ad9\uff0c\u6ce8\u518c\u7528\u6237\u7b49\uff0c\u8fd9\u4e9b\u90fd\u662f\u53ef\u4ee5\u5728JS\u811a\u672c\u4e2d\u6a21\u62df\u51fa\u6765\u7684\u3002\u4ee5\u4e0b\u662fsnap.js\u7684\u4e00\u4e2a\u811a\u672c\u7247\u6bb5\u3002<\/p>\n<blockquote><p>var BASE_URL = casper.cli.get(&#8216;url&#8217;);<br \/>\n\/\/ Go to home<br \/>\ncasper.test.comment(&#8216;Go to home&#8217;);<br \/>\ncasper.start(BASE_URL, function() {<br \/>\nthis.test.pass(&#8216;Home was loaded&#8217;);<br \/>\n});<br \/>\n\/\/ Go to login page<br \/>\ncasper.then(function() {<br \/>\nthis.click(&#8216;div.quick-access li.last a&#8217;);<br \/>\nthis.test.pass(&#8216;login page was loaded&#8217;);<br \/>\n});<br \/>\ncasper.then(function() {<br \/>\nthis.click(&#8216;div.account-login div.buttons-set button&#8217;);<br \/>\nthis.test.pass(&#8216;register page was loaded&#8217;);<br \/>\n});<br \/>\n\/\/ Fill login form and submit<br \/>\ncasper.then(function() {<br \/>\nthis.test.info(&#8216;Current location is &#8216; + this.getCurrentUrl());<br \/>\nthis.fill(&#8216;#form-validate&#8217;, {<br \/>\n&#8216;firstname&#8217;: &#8216;stamba&#8217;,<br \/>\n&#8216;lastname&#8217;: &#8216;stambic&#8217;,<br \/>\n&#8217;email&#8217;: &#8216;stamba@stamba.com&#8217;,<br \/>\n&#8216;password&#8217;: &#8216;johndoe123&#8217;,<br \/>\n&#8216;confirmation&#8217;: &#8216;johndoe123&#8217;<br \/>\n}, true);<br \/>\nthis.test.pass(&#8216;form populated&#8217;);<br \/>\n});<br \/>\n\/\/ Registration goes well<br \/>\ncasper.then(function() {<br \/>\nthis.test.info(&#8216;Current location is &#8216; + this.getCurrentUrl());<br \/>\nthis.test.pass(&#8216;Registered&#8217;);<br \/>\n});<br \/>\n\/\/ Account dashboard welcome<br \/>\ncasper.then(function() {<br \/>\n\/\/this.test.assertTextExists(&#8216;Hello, stamba stambic!&#8217;);<br \/>\nthis.test.assertTextExists(&#8216;Hello, stamba stambic!&#8217;, &#8216;page body contains &#8220;Hello, stamba stambic!&#8221;&#8216;);<br \/>\nthis.test.info(&#8216;Current location is &#8216; + this.getCurrentUrl());<br \/>\nthis.test.pass(&#8216;Dashboard in&#8217;);<br \/>\n});<br \/>\ncasper.run(function() {<br \/>\nthis.test.done();<br \/>\n});<\/p><\/blockquote>\n<p>\u8fd9\u6bb5\u811a\u672c\u7684\u505a\u4f5c\u7528\u662f\u767b\u9646\u7f51\u7ad9\uff0c\u7136\u540e\u6ce8\u518c\u4e00\u4e2a\u7528\u6237\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ec0\u4e48\u662fCasperJS \u5b98\u65b9\u89e3\u91caCasperJS \u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u5bfc\u822a\u811a\u672c\u548c\u6d4b\u8bd5\u5de5\u5177 &hellip; <a href=\"https:\/\/www.cnmiss.cn\/?p=411\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[62],"class_list":["post-411","post","type-post","status-publish","format-standard","hentry","category-xxbj","tag-casperjs"],"_links":{"self":[{"href":"https:\/\/www.cnmiss.cn\/index.php?rest_route=\/wp\/v2\/posts\/411","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cnmiss.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cnmiss.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cnmiss.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cnmiss.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=411"}],"version-history":[{"count":0,"href":"https:\/\/www.cnmiss.cn\/index.php?rest_route=\/wp\/v2\/posts\/411\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.cnmiss.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=411"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cnmiss.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=411"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cnmiss.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=411"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}