Skip to content

Commit

Permalink
Merge a7d67c2 into 636b55d
Browse files Browse the repository at this point in the history
  • Loading branch information
adamraine committed Oct 30, 2020
2 parents 636b55d + a7d67c2 commit d3ecaef
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 70 deletions.
9 changes: 9 additions & 0 deletions lighthouse-core/test/chromium-web-tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ To run the devtools webtests, `run_web_tests.py` requires the inspector resource

`run_web_tests.py` normally serves these files by mounting the Chromium build folder for the DevTools output to the path `/inspector-sources` [1](https://source.chromium.org/chromium/chromium/src/+/master:third_party/blink/tools/blinkpy/web_tests/port/base.py;l=1280;drc=e8e4dcd1d1684251c33cda9b9fc93d7ea808e4bd) [2](https://source.chromium.org/chromium/chromium/src/+/master:third_party/blink/tools/blinkpy/web_tests/servers/apache_http.py;l=118;drc=32408e19204a7ffceebfe774d7e99f2041cf4338). Instead, we fetch the DevTools frontend, roll Lighthouse, build it, then copy the build output to the `inspector-sources` at the root of our `npx http-server` web server.

## Testing Lighthouse from DevTools

`run_web_tests.py` is used to automatically test Lighthouse from DevTools.

```sh
# Runs Lighthouse from DevTools. Outputs results to ./latest-run/devtools-lhr.json.
yarn run-devtools http://example.com
```

## TODO

* Keep LighthouseTestRunner here too.
Expand Down
24 changes: 24 additions & 0 deletions lighthouse-core/test/chromium-web-tests/roll-devtools.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash

##
# @license Copyright 2020 The Lighthouse Authors. All Rights Reserved.
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
##

set -euo pipefail

roll_devtools() {
# Roll devtools. Besides giving DevTools the latest lighthouse source files,
# this also copies over the webtests.
cd "$LH_ROOT"
yarn devtools "$DEVTOOLS_PATH"
cd -
}

# Setup inspector-sources.
cd "$DEVTOOLS_PATH"
git --no-pager log -1
roll_devtools
autoninja -C out/Default # Build devtools resources.
cd -
74 changes: 4 additions & 70 deletions lighthouse-core/test/chromium-web-tests/run-web-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,81 +6,15 @@
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
##

set -euo pipefail
set -u

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
LH_ROOT="$SCRIPT_DIR/../../.."
export LH_ROOT="$SCRIPT_DIR/../../.."

# Get newest folder
latest_content_shell_dir=$(ls -t "$LH_ROOT/.tmp/chromium-web-tests/content-shells/" | head -n1)
latest_content_shell="$LH_ROOT/.tmp/chromium-web-tests/content-shells/$latest_content_shell_dir"

roll_devtools() {
# Roll devtools. Besides giving DevTools the latest lighthouse source files,
# this also copies over the webtests.
cd "$LH_ROOT"
yarn devtools "$DEVTOOLS_PATH"
cd -
}

# Run a very basic server on port 8000. Only thing we need is:
# - /devtools -> the layout tests for devtools frontend
# - /inspector-sources -> the inspector resources from the content shell
# - CORS (Access-Control-Allow-Origin header)

# Setup inspector-sources.
cd "$DEVTOOLS_PATH"
git --no-pager log -1
roll_devtools
autoninja -C out/Default # Build devtools resources.
cd -
ln -s "$DEVTOOLS_PATH/out/Default/resources/inspector" "$DEVTOOLS_PATH/test/webtests/http/tests/inspector-sources"

# Kill background jobs when script ends.
cleanup() {
rm "$DEVTOOLS_PATH/test/webtests/http/tests/inspector-sources"
kill ${SERVER_PID}
}
trap 'cleanup' EXIT

# Serve from devtools frontend webtests folder.
(npx http-server@0.12.3 "$DEVTOOLS_PATH/test/webtests/http/tests" -p 8000 --cors > /dev/null 2>&1) &
SERVER_PID=$!

echo "Waiting for server"
health_check_url='http://localhost:8000/inspector-sources/integration_test_runner.html?experiments=true&test=http://127.0.0.1:8000/devtools/lighthouse/lighthouse-view-trace-run.js'
until $(curl --output /dev/null --silent --head --fail $health_check_url); do
printf '.'
sleep 1
done
echo "Server is up"

# webtests sometimes error if results are already present.
rm -rf "$latest_content_shell/out/Release/layout-test-results"

# Add typ to python path. The regular method assumes there is a Chromium checkout.
export PYTHONPATH="${PYTHONPATH:-}:$BLINK_TOOLS_PATH/latest/third_party/typ"

# Don't quit if the python command fails.
set +e
# Print the python command.
set -x

python \
"$BLINK_TOOLS_PATH/latest/third_party/blink/tools/run_web_tests.py" \
--layout-tests-directory="$DEVTOOLS_PATH/test/webtests" \
--build-directory="$latest_content_shell/out" \
$* \
http/tests/devtools/lighthouse
bash "$SCRIPT_DIR/roll-devtools.sh" || exit 1
bash "$SCRIPT_DIR/web-test-server.sh" http/tests/devtools/lighthouse $*
status=$?

set +x
set -e

rm -rf "$LH_ROOT/.tmp/layout-test-results"
cp -r "$latest_content_shell/out/Release/layout-test-results" "$LH_ROOT/.tmp/layout-test-results"
cp "$DEVTOOLS_PATH/test/webtests/http/tests/devtools/lighthouse/"*-expected.txt "$LH_ROOT/third-party/chromium-webtests/webtests/http/tests/devtools/lighthouse"

if [ ! $status -eq 0 ]; then
# Print failure diffs to stdout.
find "$LH_ROOT/.tmp/layout-test-results/retry_3" -name '*-diff.txt' -exec cat {} \;
Expand Down
59 changes: 59 additions & 0 deletions lighthouse-core/test/chromium-web-tests/test-page-devtools.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env bash

##
# @license Copyright 2020 The Lighthouse Authors. All Rights Reserved.
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
##

set -euo pipefail

if [ -z "$1" ]; then
echo "ERROR: No URL provided."
exit 1
fi

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
export LH_ROOT="$( cd "$SCRIPT_DIR/../../.." && pwd )"

# Setup dependencies.
TEST_DIR="$LH_ROOT/.tmp/chromium-web-tests"
export DEPOT_TOOLS_PATH="$TEST_DIR/depot-tools"
export DEVTOOLS_PATH=${DEVTOOLS_PATH:-"$TEST_DIR/devtools/devtools-frontend"}
export BLINK_TOOLS_PATH="$TEST_DIR/blink_tools"
export PATH=$DEPOT_TOOLS_PATH:$PATH

bash "$SCRIPT_DIR/download-depot-tools.sh"
bash "$SCRIPT_DIR/download-devtools.sh"
bash "$SCRIPT_DIR/download-blink-tools.sh"
bash "$SCRIPT_DIR/download-content-shell.sh"

bash "$SCRIPT_DIR/roll-devtools.sh"

# Add test to run lighthouse in DevTools and print LHR.
echo "
(async function() {
await TestRunner.navigatePromise('$1');
await TestRunner.loadModule('lighthouse_test_runner');
await TestRunner.showPanel('lighthouse');
LighthouseTestRunner.getRunButton().click();
const {lhr} = await LighthouseTestRunner.waitForResults();
TestRunner.addResult(JSON.stringify(lhr));
TestRunner.completeTest();
})();
" > "$DEVTOOLS_PATH/test/webtests/http/tests/devtools/lighthouse/lighthouse-run-dt.js"

set +e
bash "$SCRIPT_DIR/web-test-server.sh" --no-show-results --time-out-ms=60000 http/tests/devtools/lighthouse/lighthouse-run-dt.js
set -e

# Copy results to latest-run folder.
# Sometimes there will be extra output before the line with LHR. To get around this, only copy the last line with content.
grep "lighthouseVersion" -m 1 \
"$LH_ROOT/.tmp/layout-test-results/http/tests/devtools/lighthouse/lighthouse-run-dt-actual.txt" \
> "$LH_ROOT/latest-run/devtools-lhr.json"

echo "Open the LHR at $LH_ROOT/latest-run/devtools-lhr.json"
66 changes: 66 additions & 0 deletions lighthouse-core/test/chromium-web-tests/web-test-server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env bash

##
# @license Copyright 2020 The Lighthouse Authors. All Rights Reserved.
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
##

set -euo pipefail

# Get newest folder
latest_content_shell_dir=$(ls -t "$LH_ROOT/.tmp/chromium-web-tests/content-shells/" | head -n1)
export latest_content_shell="$LH_ROOT/.tmp/chromium-web-tests/content-shells/$latest_content_shell_dir"

# Run a very basic server on port 8000. Only thing we need is:
# - /devtools -> the layout tests for devtools frontend
# - /inspector-sources -> the inspector resources from the content shell
# - CORS (Access-Control-Allow-Origin header)

ln -s "$DEVTOOLS_PATH/out/Default/resources/inspector" "$DEVTOOLS_PATH/test/webtests/http/tests/inspector-sources"

# Kill background jobs when script ends.
cleanup() {
rm "$DEVTOOLS_PATH/test/webtests/http/tests/inspector-sources"
kill ${SERVER_PID}
}
trap 'cleanup' EXIT

# Serve from devtools frontend webtests folder.
(npx http-server@0.12.3 "$DEVTOOLS_PATH/test/webtests/http/tests" -p 8000 --cors > /dev/null 2>&1) &
SERVER_PID=$!

echo "Waiting for server"
health_check_url='http://localhost:8000/inspector-sources/integration_test_runner.html?experiments=true&test=http://127.0.0.1:8000/devtools/lighthouse/lighthouse-view-trace-run.js'
until $(curl --output /dev/null --silent --head --fail $health_check_url); do
printf '.'
sleep 1
done
echo "Server is up"

# webtests sometimes error if results are already present.
rm -rf "$latest_content_shell/out/Release/layout-test-results"

# Add typ to python path. The regular method assumes there is a Chromium checkout.
export PYTHONPATH="${PYTHONPATH:-}:$BLINK_TOOLS_PATH/latest/third_party/typ"

# Don't quit if the python command fails.
set +e
# Print the python command.
set -x

python \
"$BLINK_TOOLS_PATH/latest/third_party/blink/tools/run_web_tests.py" \
--layout-tests-directory="$DEVTOOLS_PATH/test/webtests" \
--build-directory="$latest_content_shell/out" \
$*
status=$?

set +x
set -e

rm -rf "$LH_ROOT/.tmp/layout-test-results"
cp -r "$latest_content_shell/out/Release/layout-test-results" "$LH_ROOT/.tmp/layout-test-results"
cp "$DEVTOOLS_PATH/test/webtests/http/tests/devtools/lighthouse/"*-expected.txt "$LH_ROOT/third-party/chromium-webtests/webtests/http/tests/devtools/lighthouse"

exit $status
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"update:test-devtools": "bash lighthouse-core/test/chromium-web-tests/test-locally.sh --reset-results",
"test-devtools": "bash lighthouse-core/test/chromium-web-tests/test-locally.sh",
"open-devtools": "bash lighthouse-core/scripts/open-devtools.sh",
"run-devtools": "bash lighthouse-core/test/chromium-web-tests/test-page-devtools.sh",
"diff:sample-json": "yarn i18n:checks && bash lighthouse-core/scripts/assert-golden-lhr-unchanged.sh",
"computeBenchmarkIndex": "./lighthouse-core/scripts/benchmark.js",
"minify-latest-run": "./lighthouse-core/scripts/lantern/minify-trace.js ./latest-run/defaultPass.trace.json ./latest-run/defaultPass.trace.min.json && ./lighthouse-core/scripts/lantern/minify-devtoolslog.js ./latest-run/defaultPass.devtoolslog.json ./latest-run/defaultPass.devtoolslog.min.json",
Expand Down

0 comments on commit d3ecaef

Please sign in to comment.