当前位置: 首页 > 工具软件 > CasperJS > 使用案例 >

casperjs 安装

戴博
2023-12-01

安装

brew install casperjs 
安装完成后使用 
1. phantomjs –version 
2. casperjs –version 
查看安装的版本 
因为 phantomjs 2.00对支持 casperjs 有问题 
需要修改 
/usr/local/homebrew/Cellar/casperjs/1.0.4/libexec/bin/bootstrap.js 增加一段代码

<code class="hljs perl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">var <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">system</span> = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">require</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'system'</span>);
var argsdeprecated = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">system</span>.args;
argsdeprecated.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">shift</span>();
phantom.args = argsdeprecated;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>

实例代码,登陆人人

<code class="hljs javascript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//登陆人人</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> casper = <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">require</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'casper'</span>).create({   
    verbose: <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">true</span>, 
    logLevel: <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'debug'</span>,
    pageSettings: {
         loadImages:  <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">false</span>,        
         loadPlugins: <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">true</span>,         
         userAgent: <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Mozilla/5.0 (Windows NT 6.1; rv:17.0) Gecko/20100101 Firefox/17.0'</span>
    }
});
casper.options.viewportSize = {width: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1680</span>, height: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">924</span>};
casper.start(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'http://www.renren.com'</span>);
casper.waitForSelector(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"form#loginForm"</span>,
    <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span> <span class="hljs-title" style="box-sizing: border-box;">success</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">()</span> {</span>
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>.test.assertExists(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"form input[name='email']"</span>);
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>.fill(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'form[id="loginForm"]'</span>,{
            <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'email'</span>:<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'your name'</span>
            <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'password'</span>:<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'your pass'</span>
        },<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">false</span>);
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>.click(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"input#login"</span>);
    },
    <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span> <span class="hljs-title" style="box-sizing: border-box;">fail</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">()</span> {</span>
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>.test.assertExists(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"form input[name='email']"</span>);
});

casper.waitFor(<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span> <span class="hljs-title" style="box-sizing: border-box;">check</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">()</span> {</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>.getCurrentUrl().indexOf(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"www.renren.com/***"</span>)>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>;
}, <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span> <span class="hljs-title" style="box-sizing: border-box;">then</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">()</span> {</span>
    console.log(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"登录成功!!!!!!!!!!!!"</span>);
}).then(<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">()</span>{</span>
    console.log(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"执行登录后的其它操作!!!!!!!!!!!!"</span>);
    console.log(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"打开链接并截图"</span>);    
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>.capture(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"./renren.png"</span>);  
});
casper.run(<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">()</span> {</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>.test.renderResults(<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">true</span>);});</code>
 类似资料: