lighthouse-ruby 0.1.0 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +73 -6
- data/lib/lighthouse/ruby/builder.rb +14 -9
- data/lib/lighthouse/ruby/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d1ca3844c3413f05c11059a98490eddfd08a9c7e3adf59379cade79dc326f989
|
4
|
+
data.tar.gz: 30d942de593720dd36694f3e82b2e179f318597893b5fe30f4dee895957b2ada
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6afebb5ecd2f6f4eb9fc681319bb1e597491e4e4b0e2fec5712415c19ade2bec348fe99281c9c6b2208d877f28f8776ab848e5a0467fd0e7c208653a14a4e0fa
|
7
|
+
data.tar.gz: 70b9758b20661eb5a17880aa98f90259d854913392b47fa0a1ddb167bf11af127589978a88ae5b0fb7f2db9bfb715148c1486c2b1ac832ef7e2edbc55c0dd510
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -9,7 +9,9 @@ TODO: Delete this and the text above, and describe your gem
|
|
9
9
|
Add this line to your application's Gemfile:
|
10
10
|
|
11
11
|
```ruby
|
12
|
-
|
12
|
+
group :test do
|
13
|
+
gem 'lighthouse-ruby'
|
14
|
+
end
|
13
15
|
```
|
14
16
|
|
15
17
|
And then execute:
|
@@ -20,15 +22,80 @@ Or install it yourself as:
|
|
20
22
|
|
21
23
|
$ gem install lighthouse-ruby
|
22
24
|
|
23
|
-
|
25
|
+
Another dependency you also need to have the `lighthouse` CLI tool available. The gem will automatically pick up the tool
|
26
|
+
if `lighthouse` already installed, or if there are no executable lighthouse file then it will automatically install for you.
|
27
|
+
Additionally if you want install it manually and added to dev dependency on the`package.sjon`, feel free to use any of this commands:
|
28
|
+
|
29
|
+
* `npm install --save-dev lighthouse`
|
30
|
+
* `yarn add --dev lighthouse`
|
24
31
|
|
25
|
-
|
32
|
+
If you have the `lighthouse` CLI tool installed, but available somewhere on your system, you can set the location manually.
|
33
|
+
See [Configuration](#configuration) for further instructions.
|
26
34
|
|
27
|
-
##
|
35
|
+
## Usage
|
36
|
+
After all the installation, now the `lighthouse\ruby` can be imported by adding this code to your test helper.
|
28
37
|
|
29
|
-
|
38
|
+
```
|
39
|
+
require "lighthouse/ruby"
|
40
|
+
```
|
41
|
+
The idea to have this inside your test framework without adding too much overhead and un-necessary code in order to use
|
42
|
+
the same Chrome session as your system test framework (e.g. the page requires a logged-in user), then you should change
|
43
|
+
the definition of your system test Chrome browser arguments to define a "remote debugging port". Without defining this
|
44
|
+
port, The `lighthouse/ruby` cannot connect to your existing Chrome session and will begin a new
|
45
|
+
one, clearing any session information.
|
46
|
+
|
47
|
+
## Setup Code
|
48
|
+
```
|
49
|
+
Capybara.register_driver :chrome do |app|
|
50
|
+
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
|
51
|
+
chromeOptions: { args: %w(headless remote-debugging-port=9222) }
|
52
|
+
)
|
53
|
+
|
54
|
+
Capybara::Selenium::Driver.new app,
|
55
|
+
browser: :chrome,
|
56
|
+
desired_capabilities: capabilities
|
57
|
+
end
|
58
|
+
|
59
|
+
Capybara.javascript_driver = :chrome
|
60
|
+
Lighthouse::Matchers.remote_debugging_port = 9222
|
61
|
+
```
|
30
62
|
|
31
|
-
|
63
|
+
#### For Rspec
|
64
|
+
Place the configuration on `spec/spec_helper.rb` or `spec/rails_helper.rb`
|
65
|
+
|
66
|
+
#### For Cucumber
|
67
|
+
Place the configuration on `features/env.rb` or where the Capybara and Chromedriver defined.
|
68
|
+
|
69
|
+
## Configuration
|
70
|
+
There are several additional configuration that accessible and align with official `lighthouse-cli`.
|
71
|
+
All the additional options:
|
72
|
+
* **`remote_debugging_port`:** If defined, Lighthouse will connect to this Chrome debugging port.
|
73
|
+
This allows the test to run in the same session as the Chrome session that created the port
|
74
|
+
(in th case of Capybara, this will be the same _current_state_ of `page` or `Capybara::Session` under test)
|
75
|
+
by match up the remote debugging port that has been configured for the Chrome browser instance.
|
76
|
+
* **`lighthouse_path`:** The path to the Lighthouse CLI tool. By default, it will check `/usr/bin/`, `/usr/local/bin`
|
77
|
+
and `node_modules/.bin/` for the CLI. Therefor when the gem cannot find any executable file do not panic because
|
78
|
+
it will install the latest `lighthouse` from npm globally.
|
79
|
+
* **`chrome_flags`:** Any additional flags that should be passed to Chrome when Lighthouse launches a browser instance.
|
80
|
+
As an example, running Lighthouse in Docker container or CI environment might requires the headless Chrome flags
|
81
|
+
(`--headless`, `--no-sandbox`) in order Chrome to successfully start. Chrome flags can be either specified as an array
|
82
|
+
or string.(eq: `["headless", "no-sandbox"]` or `"headless", "no-sandbox"`).
|
83
|
+
* **`lighthouse_options`:** Any additional options that can be passed to Lighthouse CLI. All the options can be found by
|
84
|
+
running `lighthouse --help` in the terminal or go this [link](https://github.com/GoogleChrome/lighthouse#cli-options) for more details. For an example usage, running Lighthouse in different emulated form factor
|
85
|
+
such as: a mobile, or desktop use `emulated-form-factor=<type>`. Another usage by adding additional headers during the
|
86
|
+
erequest simply add `--extra-headers`. Also, Lighthouse options can either be specified as an array or string.
|
87
|
+
(eq: `["emulated-form-factor=desktop", "--extra-headers '{\"food\":\"burger\", \"drink\":\"coke\"}'"]` or `"emulated-form-factor=desktop", "--extra-headers '{\"food\":\"burger\", \"drink\":\"coke\"}'"`).
|
88
|
+
|
89
|
+
## Run Test
|
90
|
+
How to get the test report simply create object for `lighthouse-ruby` with url that need to be tested.
|
91
|
+
For `Capybara::Session` then `current_url` is the URL that you want to access.
|
92
|
+
```
|
93
|
+
report = Lighthouse::Ruby::Builder.new(current_url)
|
94
|
+
report.execute # => will get all JSON object
|
95
|
+
report.test_scores # => Will get only meaningful hash of scores such as: performance, accessibility, best-practices, and SEO
|
96
|
+
=> {:url=>"http://test.example.com:3001/", :run_time=>2020-05-14 13:10:12 +0000, :performance=>100, :accessibility=>80.0, :best_practices=>77.0, :seo=>90.0}
|
97
|
+
```
|
98
|
+
To be more flexible `report.execute` will return the JSON report and it should be easily parse into Ruby Hash as needed.
|
32
99
|
|
33
100
|
## Contributing
|
34
101
|
|
@@ -5,6 +5,8 @@ module Lighthouse
|
|
5
5
|
module Ruby
|
6
6
|
class Builder
|
7
7
|
|
8
|
+
attr_reader :response
|
9
|
+
|
8
10
|
def initialize(url)
|
9
11
|
@url = url
|
10
12
|
@runner = Lighthouse::Preferences.runner
|
@@ -14,8 +16,13 @@ module Lighthouse
|
|
14
16
|
@lighthouse_options = Lighthouse::Preferences.lighthouse_options
|
15
17
|
end
|
16
18
|
|
17
|
-
def
|
18
|
-
|
19
|
+
def execute
|
20
|
+
@response = @runner.call("#{@cli} #{options}")
|
21
|
+
get_test_scores(parsed_response)
|
22
|
+
end
|
23
|
+
|
24
|
+
def parsed_response
|
25
|
+
@parsed_response = JSON.parse(@response)
|
19
26
|
end
|
20
27
|
|
21
28
|
private
|
@@ -30,15 +37,13 @@ module Lighthouse
|
|
30
37
|
end.strip
|
31
38
|
end
|
32
39
|
|
33
|
-
def get_test_scores
|
34
|
-
json_result = JSON.parse(@runner.call("#{@cli} #{options}"))
|
35
|
-
|
40
|
+
def get_test_scores(response)
|
36
41
|
@test_scores = { url: @url}
|
37
42
|
@test_scores[:run_time] = Time.now
|
38
|
-
@test_scores[:performance] =
|
39
|
-
@test_scores[:accessibility] =
|
40
|
-
@test_scores[:best_practices] =
|
41
|
-
@test_scores[:seo] =
|
43
|
+
@test_scores[:performance] = response.dig("categories", "performance" , "score").to_f * 100
|
44
|
+
@test_scores[:accessibility] = response.dig("categories", "accessibility" , "score").to_f * 100
|
45
|
+
@test_scores[:best_practices] = response.dig("categories", "best-practices" , "score").to_f * 100
|
46
|
+
@test_scores[:seo] = response.dig("categories", "seo" , "score").to_f * 100
|
42
47
|
@test_scores
|
43
48
|
end
|
44
49
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lighthouse-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Budi Sugianto
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-06-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|