无头Chrome就像是在没有UI(浏览器)的情况下运行chrome浏览器一样,也就是说,它在无头环境中运行,这对于运行自动化测试非常有用。
那么,无头Chrome可以为我们做些什么呢?
最令人兴奋的事情之一就是能够测试最新和最强大的Web平台功能,例如ES6模块,服务工作者和流。使用无头chrome,您可以编写应用程序并使用最新的渲染功能测试这些应用程序。它解锁的另一件事是这些很棒的功能,例如网络限制,设备仿真和代码覆盖。
使用无头chrome,您甚至无需编写任何代码即可执行测试,那不是很爽!
您可以仅通过命令行执行它们。
1
2
3
4
|
$ google-chrome-stable \
--headless \
--disable-gpu \
--print-to-pdf=www.meipian.cn.pdf https://www.meipian.cn/
|
1
2
3
4
|
$ google-chrome-stable \
--headless \
--disable-gpu \
--screenshot=www.meipian.cn.png https://www.meipian.cn/
|
1
2
3
4
5
|
$ google-chrome-stable \
--headless \
--disable-gpu \
--dump-dom \
https://www.meipian.cn/ >> www.meipian.cn.html
|
以上是一些常用的功能!更多功能可以学习Chrome浏览器功能具体列表。
Step1:更新CentOS
使用以下命令更新CentOS系统。
Step2:创建Chrome存储库YUM源
将google chrome存储库添加到系统。在/etc/yum.repos.d/
中创建一个文件google-chrome.repo
,并添加如下配置数据信息。
1
2
3
4
5
6
|
[google-chrome]
name=google-chrome
baseurl=http://dl.google.com/linux/chrome/rpm/stable/$basearch
enabled=1
gpgcheck=1
gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub
|
Step3:安装Chrome
运行下面的yum命令以在CentOS系统上安装google chrome。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
$ sudo yum -y install google-chrome-stable
Loaded plugins: fastestmirror
Determining fastest mirrors
base | 3.6 kB 00:00:00
docker-ce-stable | 3.5 kB 00:00:00
epel | 4.7 kB 00:00:00
extras | 2.9 kB 00:00:00
google-chrome | 1.3 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/7): epel/x86_64/group_gz | 96 kB 00:00:00
(2/7): epel/x86_64/updateinfo | 1.0 MB 00:00:00
(3/7): extras/7/x86_64/primary_db | 232 kB 00:00:00
(4/7): google-chrome/x86_64/primary | 1.7 kB 00:00:00
(5/7): epel/x86_64/primary_db | 6.9 MB 00:00:00
(6/7): updates/7/x86_64/primary_db | 7.1 MB 00:00:00
(7/7): docker-ce-stable/x86_64/primary_db | 60 kB 00:00:01
google-chrome 3/3
Resolving Dependencies
--> Running transaction check
---> Package google-chrome-stable.x86_64 0:90.0.4430.85-1 will be installed
--> Processing Dependency: xdg-utils for package: google-chrome-stable-90.0.4430.85-1.x86_64
--> Processing Dependency: liberation-fonts for package: google-chrome-stable-90.0.4430.85-1.x86_64
--> Processing Dependency: libvulkan.so.1()(64bit) for package: google-chrome-stable-90.0.4430.85-1.x86_64
--> Running transaction check
---> Package liberation-fonts.noarch 1:1.07.2-16.el7 will be installed
--> Processing Dependency: liberation-serif-fonts = 1:1.07.2-16.el7 for package: 1:liberation-fonts-1.07.2-16.el7.noarch
--> Processing Dependency: liberation-sans-fonts = 1:1.07.2-16.el7 for package: 1:liberation-fonts-1.07.2-16.el7.noarch
--> Processing Dependency: liberation-narrow-fonts = 1:1.07.2-16.el7 for package: 1:liberation-fonts-1.07.2-16.el7.noarch
--> Processing Dependency: liberation-mono-fonts = 1:1.07.2-16.el7 for package: 1:liberation-fonts-1.07.2-16.el7.noarch
---> Package vulkan.x86_64 0:1.1.97.0-1.el7 will be installed
--> Processing Dependency: vulkan-filesystem = 1.1.97.0-1.el7 for package: vulkan-1.1.97.0-1.el7.x86_64
---> Package xdg-utils.noarch 0:1.1.0-0.17.20120809git.el7 will be installed
--> Running transaction check
---> Package liberation-mono-fonts.noarch 1:1.07.2-16.el7 will be installed
--> Processing Dependency: liberation-fonts-common = 1:1.07.2-16.el7 for package: 1:liberation-mono-fonts-1.07.2-16.el7.noarch
---> Package liberation-narrow-fonts.noarch 1:1.07.2-16.el7 will be installed
---> Package liberation-sans-fonts.noarch 1:1.07.2-16.el7 will be installed
---> Package liberation-serif-fonts.noarch 1:1.07.2-16.el7 will be installed
---> Package vulkan-filesystem.noarch 0:1.1.97.0-1.el7 will be installed
--> Running transaction check
---> Package liberation-fonts-common.noarch 1:1.07.2-16.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
========================================================================================================
Package Arch Version Repository Size
========================================================================================================
Installing:
google-chrome-stable x86_64 90.0.4430.85-1 google-chrome 79 M
Installing for dependencies:
liberation-fonts noarch 1:1.07.2-16.el7 base 13 k
liberation-fonts-common noarch 1:1.07.2-16.el7 base 27 k
liberation-mono-fonts noarch 1:1.07.2-16.el7 base 227 k
liberation-narrow-fonts noarch 1:1.07.2-16.el7 base 202 k
liberation-sans-fonts noarch 1:1.07.2-16.el7 base 279 k
liberation-serif-fonts noarch 1:1.07.2-16.el7 base 298 k
vulkan x86_64 1.1.97.0-1.el7 base 3.6 M
vulkan-filesystem noarch 1.1.97.0-1.el7 base 6.3 k
xdg-utils noarch 1.1.0-0.17.20120809git.el7 base 70 k
Transaction Summary
========================================================================================================
Install 1 Package (+9 Dependent packages)
Total download size: 84 M
Installed size: 273 M
Downloading packages:
(1/10): liberation-fonts-1.07.2-16.el7.noarch.rpm | 13 kB 00:00:00
(2/10): liberation-fonts-common-1.07.2-16.el7.noarch.rpm | 27 kB 00:00:00
(3/10): liberation-mono-fonts-1.07.2-16.el7.noarch.rpm | 227 kB 00:00:00
(4/10): liberation-narrow-fonts-1.07.2-16.el7.noarch.rpm | 202 kB 00:00:00
(5/10): liberation-sans-fonts-1.07.2-16.el7.noarch.rpm | 279 kB 00:00:00
(6/10): liberation-serif-fonts-1.07.2-16.el7.noarch.rpm | 298 kB 00:00:00
(7/10): vulkan-filesystem-1.1.97.0-1.el7.noarch.rpm | 6.3 kB 00:00:00
(8/10): xdg-utils-1.1.0-0.17.20120809git.el7.noarch.rpm | 70 kB 00:00:00
(9/10): vulkan-1.1.97.0-1.el7.x86_64.rpm | 3.6 MB 00:00:00
warning: /var/cache/yum/x86_64/7/google-chrome/packages/google-chrome-stable-90.0.4430.85-1.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 7fac5991: NOKEY
Public key for google-chrome-stable-90.0.4430.85-1.x86_64.rpm is not installed
(10/10): google-chrome-stable-90.0.4430.85-1.x86_64.rpm | 79 MB 00:00:06
--------------------------------------------------------------------------------------------------------
Total 13 MB/s | 84 MB 00:00:06
Retrieving key from https://dl-ssl.google.com/linux/linux_signing_key.pub
GPG key retrieval failed: [Errno 14] curl#7 - "Failed connect to dl-ssl.google.com:443; Operation now in progress"
|
报错了!没有安装成功哦,可以看到它从https://dl-ssl.google.com/linux/linux_signing_key.pub
检索一个签署文件,也就是公私文件。由于国内的网络环境,服务器不能直接访问到Google的服务器,所示它GPG密钥检索失败了。可以到可以访问到Google的网络中下载linux_signing_key.pub
这个文件。然后上传到服务。
Step4: 手动导入签名密钥
如果没有报错请直接到Step5, 一般不出意外都会报错哦,哈哈~
上传文件后执行导入签名密钥操作。
1
|
$ sudo rpm --import linux_signing_key.pub
|
执行以上命令后即可将签名密钥导入到rpm包管理器,再次执行第三步即可安装成功。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
$ sudo yum -y install google-chrome-stable
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package google-chrome-stable.x86_64 0:90.0.4430.85-1 will be installed
--> Processing Dependency: xdg-utils for package: google-chrome-stable-90.0.4430.85-1.x86_64
--> Processing Dependency: liberation-fonts for package: google-chrome-stable-90.0.4430.85-1.x86_64
--> Processing Dependency: libvulkan.so.1()(64bit) for package: google-chrome-stable-90.0.4430.85-1.x86_64
--> Running transaction check
---> Package liberation-fonts.noarch 1:1.07.2-16.el7 will be installed
--> Processing Dependency: liberation-serif-fonts = 1:1.07.2-16.el7 for package: 1:liberation-fonts-1.07.2-16.el7.noarch
--> Processing Dependency: liberation-sans-fonts = 1:1.07.2-16.el7 for package: 1:liberation-fonts-1.07.2-16.el7.noarch
--> Processing Dependency: liberation-narrow-fonts = 1:1.07.2-16.el7 for package: 1:liberation-fonts-1.07.2-16.el7.noarch
--> Processing Dependency: liberation-mono-fonts = 1:1.07.2-16.el7 for package: 1:liberation-fonts-1.07.2-16.el7.noarch
---> Package vulkan.x86_64 0:1.1.97.0-1.el7 will be installed
--> Processing Dependency: vulkan-filesystem = 1.1.97.0-1.el7 for package: vulkan-1.1.97.0-1.el7.x86_64
---> Package xdg-utils.noarch 0:1.1.0-0.17.20120809git.el7 will be installed
--> Running transaction check
---> Package liberation-mono-fonts.noarch 1:1.07.2-16.el7 will be installed
--> Processing Dependency: liberation-fonts-common = 1:1.07.2-16.el7 for package: 1:liberation-mono-fonts-1.07.2-16.el7.noarch
---> Package liberation-narrow-fonts.noarch 1:1.07.2-16.el7 will be installed
---> Package liberation-sans-fonts.noarch 1:1.07.2-16.el7 will be installed
---> Package liberation-serif-fonts.noarch 1:1.07.2-16.el7 will be installed
---> Package vulkan-filesystem.noarch 0:1.1.97.0-1.el7 will be installed
--> Running transaction check
---> Package liberation-fonts-common.noarch 1:1.07.2-16.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
========================================================================================================================
Package Arch Version Repository Size
========================================================================================================================
Installing:
google-chrome-stable x86_64 90.0.4430.85-1 google-chrome 79 M
Installing for dependencies:
liberation-fonts noarch 1:1.07.2-16.el7 base 13 k
liberation-fonts-common noarch 1:1.07.2-16.el7 base 27 k
liberation-mono-fonts noarch 1:1.07.2-16.el7 base 227 k
liberation-narrow-fonts noarch 1:1.07.2-16.el7 base 202 k
liberation-sans-fonts noarch 1:1.07.2-16.el7 base 279 k
liberation-serif-fonts noarch 1:1.07.2-16.el7 base 298 k
vulkan x86_64 1.1.97.0-1.el7 base 3.6 M
vulkan-filesystem noarch 1.1.97.0-1.el7 base 6.3 k
xdg-utils noarch 1.1.0-0.17.20120809git.el7 base 70 k
Transaction Summary
========================================================================================================================
Install 1 Package (+9 Dependent packages)
Total size: 84 M
Installed size: 273 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : 1:liberation-fonts-common-1.07.2-16.el7.noarch 1/10
Installing : 1:liberation-narrow-fonts-1.07.2-16.el7.noarch 2/10
Installing : 1:liberation-mono-fonts-1.07.2-16.el7.noarch 3/10
Installing : 1:liberation-sans-fonts-1.07.2-16.el7.noarch 4/10
Installing : 1:liberation-serif-fonts-1.07.2-16.el7.noarch 5/10
Installing : 1:liberation-fonts-1.07.2-16.el7.noarch 6/10
Installing : vulkan-filesystem-1.1.97.0-1.el7.noarch 7/10
Installing : vulkan-1.1.97.0-1.el7.x86_64 8/10
Installing : xdg-utils-1.1.0-0.17.20120809git.el7.noarch 9/10
Installing : google-chrome-stable-90.0.4430.85-1.x86_64 10/10
Verifying : vulkan-1.1.97.0-1.el7.x86_64 1/10
Verifying : 1:liberation-fonts-1.07.2-16.el7.noarch 2/10
Verifying : 1:liberation-fonts-common-1.07.2-16.el7.noarch 3/10
Verifying : 1:liberation-narrow-fonts-1.07.2-16.el7.noarch 4/10
Verifying : xdg-utils-1.1.0-0.17.20120809git.el7.noarch 5/10
Verifying : 1:liberation-mono-fonts-1.07.2-16.el7.noarch 6/10
Verifying : 1:liberation-sans-fonts-1.07.2-16.el7.noarch 7/10
Verifying : 1:liberation-serif-fonts-1.07.2-16.el7.noarch 8/10
Verifying : google-chrome-stable-90.0.4430.85-1.x86_64 9/10
Verifying : vulkan-filesystem-1.1.97.0-1.el7.noarch 10/10
Installed:
google-chrome-stable.x86_64 0:90.0.4430.85-1
Dependency Installed:
liberation-fonts.noarch 1:1.07.2-16.el7 liberation-fonts-common.noarch 1:1.07.2-16.el7
liberation-mono-fonts.noarch 1:1.07.2-16.el7 liberation-narrow-fonts.noarch 1:1.07.2-16.el7
liberation-sans-fonts.noarch 1:1.07.2-16.el7 liberation-serif-fonts.noarch 1:1.07.2-16.el7
vulkan.x86_64 0:1.1.97.0-1.el7 vulkan-filesystem.noarch 0:1.1.97.0-1.el7
xdg-utils.noarch 0:1.1.0-0.17.20120809git.el7
Complete!
|
Step5: 验证是否安装成功
1
2
|
$ google-chrome-stable -version
Google Chrome 90.0.4430.85
|
Step6: 你可以探索了
使用无头的Chrome可以做很多事情,AJAX爬虫、截屏、保存网页为PDF等等吧~
参考文档