文档章节

installing phantomjs and casperjs on ubuntu

Airship
 Airship
发布于 2017/07/24 20:15
字数 1024
阅读 16
收藏 0

installing phantomjs and casperjs on ubuntu

PhantomJS is a “headless WebKit scriptable with a JavaScript API”, with CasperJS building on it to give high-level functions.

PhantomJS is used quite a lot in headless browser testing and similar tasks. I’ve looked over various projects which deal with screenshots, and it’s been on my TODO-list to add responsive screenshotting into my build process. I was kicked into gear reading the following tutorial which gives a barebones script. It also used CasperJS, which is why the script that I made (based on their script) uses it.

Another reason is because I looked closer at CasperJS and noticed that it (at least in theory) would also work with another headless browser using Gecko, SlimerJS, which means that a future iteration will theoretically be able to support both WebKit and Gecko screenshots. I have not yet tested this theory, which is why SlimerJS is not touched on in this article.

prerequisites

  • Python 2.6 or higher, which is pre-installed on Ubuntu
  • Git

install phantomjs

Instructions can be found on their page or if you feel confident around the terminal, follow the commands below (note, it’s using the version that’s the most current of the writing, 1.9.1, so if you want to ensure you have the newest, check the webpage and just replace 1.9.1 with whatever is the current newest). The one thing you should not do issudo apt-get install phantomjs, as the version in the repositories is 1.4, and as we’ve established, the latest version now is 1.9.

cd ~/Downloads #default folder for downloads, duh

Decide if you need the 64-bit or 32-bit download, where the difference in file name is that 64-bit has the suffix-x86_64.tar.bz2 and 32-bit has the suffix -i686.tar.bz2.

#64-bit, v. 1.9.1
wget http://phantomjs.googlecode.com/files/phantomjs-1.9.1-linux-x86_64.tar.bz2

#32-bit, v. 1.9.1
wget http://phantomjs.googlecode.com/files/phantomjs-1.9.1-linux-i686.tar.bz2

#Unpack the tarball!
#64-bit, v. 1.9.1
tar -xvf phantomjs-1.9.1-linux-x86_64.tar.bz2

#32-bit, v. 1.9.1
tar -xvf phantomjs-1.9.1-linux-i686.tar.bz2

This is where there are two different paths to take. The binary bin/phantomjs is ready to use, so you can

  1. Just move it and delete the rest of the files once you clear your Download folder the next time or
  2. Move the entire folder somewhere (say the Repositories folder) and symlink the relevant binary

In case you haven’t caught this nice little tip before: Write the first few letters of the path- or filename and presstab for autocomplete, if you’re not just copy/pasting.

sidenote, ~/bin vs /usr/local/bin

I like to use the home-folder to store things like binaries, because it makes it a lot easier for me to backup everything I need the next time I need to reinstall, which is why I’m writing this with the assumption that you will put the binaries in ~/bin. I will then store most source packages under /home/user/repositories. Another reason is that I prefer to not need to use sudo on normal commands.

If you prefer another way, or need to share packages between several users on one computer, you may want to replace ~/bin with /usr/local/bin, and ~/repositories with /usr/local/src, but then you need to ensure that you are prepending the commands with sudo, as the /usr/local/ folder is owned by root.

path 1

#If you don't have bin under your home folder, create it
mkdir ~/bin

#64-bit
mv phantomjs-1.9.1-linux-x86_64/bin/phantomjs ~/bin/phantomjs

#32-bit
mv phantomjs-1.9.1-linux-i686/bin/phantomjs ~/bin/phantomjs

path 2

This will just be shown using the 64-bit, but as always prior: replace x86_64 with i686 to instead use the 32-bit one.

#If it does not exist, create ~/repositories
mkdir ~/repositories

#Move it and rename
mv phantomjs-1.9.1-linux-x86_64 ~/repositories/phantomjs

Create a symbolic link to your local bin folder (creating it under your home first, see path 1) ln -sf ~/repositories/phantomjs/bin/phantomjs ~/bin/phantomjs

test it!

phantomjs --version

If you get an error that it’s not installed, close the terminal and open a new one. You might be able to just run the command . ~/.bashrc which reloads the file that appends your /home/user/bin folder to your PATH. If you’re still out of luck, google whatever error message you’re getting. It’s always a good practice when you’re running into issues.

installing casperjs

Unlike PhantomJS, you have to take the “second path” of cloning the repository and then symlinking it. The instructions are more or less taken from the installation information, with some changes in where to store things.

#If it does not exist, create ~/repositories
mkdir ~/repositories
cd ~/repositories

#clone the repository from the master branch
git clone git://github.com/n1k0/casperjs.git

#Symlink the relevant file into your bin
ln -sf ~/repositories/casperjs/bin/casperjs ~/bin/casperjs

Now test it. Assuming everything worked, you’ll get a nice helpmessage, as well as which PhantomJS version you’re using. casperjs

In case it tells you that it can’t find the file, try running the command python ~/bin/casperjs. That’s what happened when I ran it on my working machine. On the test machine (a virtual 12.04LTS Ubuntu) these commands worked without a hitch.

What I found out was the issue is that somewhere in there, the line endings had ended up screwed up, using DOS line endings where it should be using Unix. What worked for me (and is thus a good start for you) is that beautiful command dos2unix.

sudo apt-get install dos2unix #installs it if it isn't already installed
dos2unix ~/repositories/casperjs/bin/casperjs #Can't be ran on a linked resource
casperjs #Check to make sure you get said helpmessage

finishing comments

By now you should have both PhantomJS and CasperJS, which will be relevant in future articles, in particular on my workflow.

Posted 00:37

By Marie

For the moment comments are not enabled, but feel free to reach out on Twitter.

本文转载自:http://mariehogebrandt.se/articles/installing-phantomjs-and-casperjs-on-ubuntu/

共有 人打赏支持
Airship
粉丝 39
博文 908
码字总数 19854
作品 0
南京
高级程序员
私信 提问
CasperJS 的安装和快速入门

CasperJS 是一个开源的导航脚本和测试工具,使用 JavaScript 基于 PhantomJS 编写,用于测试 Web 应用功能,Phantom JS是一个服务器端的 JavaScript API 的 WebKit。其支持各种Web标准: DO...

红薯
2012/05/30
10.9K
4
PhantomJs+CasperJs的安装与使用

PhantomJs是所谓的“headless”浏览器,可以理解为一个没有GUI的全功能浏览器。CasperJs是它的一个扩展。 安装流程: 1,安装Nodejs: 下载地址:https://nodejs.org/en/ 2,安装Phantomjs:...

leochensh
2015/11/25
1
0
CasperJS 1.0 发布,Web 功能测试工具

CasperJS 1.0 发布了,包含重要的改进: 不再支持 PhantomJS 1.6.x ,要求 1.7 和 1.8 版本 移除废弃的 injector 模块 创建了 1.0 维护分支 主分支开始进行 CasperJS 1.1 开发 其他方面的改进...

oschina
2012/12/25
1K
1
CasperJS 抓取特殊汉字时,无法正确是吧

利用CasperJS 去抓取网页中含有特殊文字【湧】 发现,无法正确识别这个汉字。 不知道各位大神是否有过类似的经历,求教! 系统环境 Ubuntu 12.04 + PhantomJS 1.7 +CasperJS 1.0...

倚小丁
2014/06/19
148
0
casperjs phantomjs 爬虫结果保存到数据库?

@bobshi 你好,想跟你请教个问题:在您博客看到关于casperjs phantomjs的文章,获益匪浅,但是有个问题,casperjs抓取到的内容我怎么才能保存到数据库里面去呢? 最近公司老板让我写个爬虫,爬网页...

工科暴徒
2014/11/06
4.7K
7

没有更多内容

加载失败,请刷新页面

加载更多

网站老被劫持快照跳转怎么办

最近很多公司的网站被劫持跳转到了bo彩、cai票网站上去,客户从百度点击进去会跳转,直接输入网站的域名不会跳转,网站快照也被劫持成bo彩的内容,site查看网站在百度的收录也出现问题,收录...

网站安全
8分钟前
0
0
给自己灌鸡汤

前言 看极客时间的《数据结构与算法之美》,中间有一篇文章是在说怎么坚持学习的,摘抄一些片段过来,激励自己。 https://time.geekbang.org/column/article/69607 致使你掉队的原因 一个人在...

grace_233
10分钟前
0
0
如何在网络视听行业建一扇内容安全大门?

在人工智能飞速发展的三年里,整个网络视听产业数字化发展速度非常迅猛。大数据作为如今这个时代的产物,正不断推动整个产业的发展。网络视听行业中存在海量的数据,比如视频内容、视频浏览量...

阿里云官方博客
19分钟前
0
0
云安全简史

课程介绍: DT时代我们每个人的生活在不知不觉中与云计算发生了千丝万缕的联系,起床、吃饭、出行、社交、工作、娱乐、购物等大多数应用都在云上,然而安全事件频发时刻在为我们敲响警钟,面对...

mcy0425
21分钟前
1
0
数字IT基础-数据采集总线

摘要: 日志服务是阿里自产自用的产品,在双十一、双十二和新春红包期间承载阿里云/蚂蚁全站、阿里电商板块、云上几千商家数据链路,每日处理来自百万节点几十PB数据,峰值流量达到每秒百GB,...

阿里云云栖社区
24分钟前
0
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部