Skip to content

Commit d58fbd2

Browse files
committed
Merge branch 'tests/debug-context'
2 parents cc8c825 + 3098ec1 commit d58fbd2

File tree

8 files changed

+98
-147
lines changed

8 files changed

+98
-147
lines changed

src/Behat/Context/DebugContext.php

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,9 @@
99

1010
namespace Metadrop\Behat\Context;
1111

12-
use Behat\Behat\Hook\Scope\AfterScenarioScope;
13-
use Behat\Behat\Hook\Scope\AfterStepScope;
1412
use Behat\Testwork\Tester\Result\TestResult;
15-
use Behat\Gherkin\Node\FeatureNode;
16-
use Behat\Gherkin\Node\StepNode;
1713
use Behat\Step\Then;
18-
use Behat\Step\When;
14+
use Behat\Step\Given;
1915
use NuvoleWeb\Drupal\DrupalExtension\Context\ScreenShotContext as NuvoleScreenshotContext;
2016

2117
/**
@@ -264,13 +260,26 @@ public function savePngFile($filename = '') {
264260
}
265261
}
266262

263+
264+
/**
265+
* @deprecated Because it is not easy to get a full page capture and it may
266+
* only work in Chrome. Resize viewport and do a normal capture.
267+
*/
267268
#[Then('capture full page with a width of :width')]
268269
public function captureFullPageWithAWidthOf($width) {
270+
echo "\033[33m[DEPRECATED]\033[0m This step will be removed in the next major version. Use any other capture step.\n";
271+
269272
$this->captureFullPageWithWidthOfToWithName($width, $this->getScreenshotsPath(), $this->generateFilenameDateBased());
270273
}
271274

275+
/**
276+
* @deprecated Because it is not easy to get a full page capture and it may
277+
* only work in Chrome. Resize viewport and do a normal capture.
278+
*/
272279
#[Then('capture full page with a width of :width with name :filename')]
273280
public function captureFullPageWithAWidthOfWithFilename($width, $filename) {
281+
echo "\033[33m[DEPRECATED]\033[0m This step will be removed in the next major version. Use any other capture step.\n";
282+
274283
$this->captureFullPageWithWidthOfToWithName($width, $this->getScreenshotsPath(), $this->generateFilenameDateBased());
275284
}
276285

@@ -279,13 +288,32 @@ public function generateFilenameDateBased() {
279288
return 'Screenshot-' . date("Ymd--H-i-s") . '.' . $milliseconds['usec'] . '.png';
280289
}
281290

291+
292+
/**
293+
* @deprecated Because it is not easy to get a full page capture and it may
294+
* only work in Chrome. Resize viewport and do a normal capture.
295+
*/
282296
#[Then('capture full page with a width of :width to :path')]
283297
public function captureFullPageWithWidthOfTo($width, $path) {
298+
299+
echo "\033[33m[DEPRECATED]\033[0m This step will be removed in the next major version. Use any other capture step.\n";
300+
301+
print_r($this->getScreenshotAbsolutePath($path) . "\n");
302+
print_r("Width: " . $width);
303+
304+
284305
$this->captureFullPageWithWidthOfToWithName($width, $this->getScreenshotAbsolutePath($path), $this->generateFilenameDateBased());
285306
}
286307

308+
/**
309+
* @deprecated Because it is not easy to get a full page capture and it may
310+
* only work in Chrome. Resize viewport and do a normal capture.
311+
*/
287312
#[Then('capture full page with a width of :width to :path with name :filename')]
288313
public function captureFullPageWithWidthOfToWithName($width, $filepath, $filename) {
314+
315+
echo "\033[33m[DEPRECATED]\033[0m This step will be removed in the next major version. Use any other capture step.\n";
316+
289317
// Use default height as screenshot is going to capture the complete page.
290318
$this->getSession()->resizeWindow((int) $width, $this::DEFAULT_HEIGHT, 'current');
291319
$message = "Screenshot created in @file_name";
@@ -298,7 +326,7 @@ public function captureFullPageWithWidthOfToWithName($width, $filepath, $filenam
298326
*/
299327
#[Given('save last response')]
300328
public function saveLastResponse() {
301-
$this->createScreenshot($this->getScreenshotsPath() . DIRECTORY_SEPARATOR . 'last_response', 'File saved in @file_name');
329+
$this->createScreenshot($this->getScreenshotsPath() . DIRECTORY_SEPARATOR . '_last_response', 'File saved in @file_name');
302330
}
303331

304332
/**

tests/contexts/debug-context.bats

Lines changed: 19 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -24,102 +24,32 @@ teardown() {
2424
}
2525

2626
#
27-
# Test 1: Screenshot URL output format validation
28-
#
29-
# Validates that DebugContext outputs screenshot URL when generating screenshots
27+
# Tests the complete context.
3028
#
3129
@test "DebugContext: screenshot generation outputs URL" {
32-
# Skip if DDEV is not running
33-
if ! is_ddev_running; then
34-
skip "DDEV is not running"
35-
fi
36-
37-
# Get the feature file path
38-
local feature_file="tests/features/debug-screenshot.feature"
39-
40-
# Run Behat with the screenshot feature
41-
run ddev exec behat "${feature_file}"
42-
43-
# Debug output for troubleshooting
44-
echo "Status: ${status}" >&3
45-
echo "Output: ${output}" >&3
46-
47-
# Check that Behat ran (may pass or fail, we care about screenshot output)
48-
# Status 0 = success, non-zero may occur if site not fully configured
49-
50-
# Validate screenshot URL is in output
51-
[[ "${output}" =~ "Screenshot url:" ]] || [[ "${output}" =~ "Screenshot created in" ]]
52-
}
53-
54-
#
55-
# Test 2: Error report output format validation
56-
#
57-
# Validates that DebugContext outputs error report URLs (txt, html, png)
58-
# when a scenario fails
59-
#
60-
@test "DebugContext: error report outputs three file types" {
61-
# Skip if DDEV is not running
62-
if ! is_ddev_running; then
63-
skip "DDEV is not running"
64-
fi
6530

66-
# Get the feature file path (this feature is designed to fail)
67-
local feature_file="tests/features/debug-error.feature"
31+
mkdir -p web/sites/default/files/behat/screenshots
32+
mkdir -p web/sites/default/files/behat/errors
6833

69-
# Run Behat with the error feature (expect failure)
70-
run ddev exec behat "${feature_file}"
34+
prepare_test "debug-test.feature"
7135

72-
# Debug output
73-
echo "Status: ${status}" >&3
74-
echo "Output: ${output}" >&3
75-
76-
# Behat should fail (non-zero status) since the test is designed to fail
77-
[ "${status}" -ne 0 ] || skip "Test feature did not fail as expected"
78-
79-
# Check for error report output patterns
80-
# The context outputs three types: info (txt), html, and png
81-
[[ "${output}" =~ "info (exception):" ]] || \
82-
[[ "${output}" =~ "html (output):" ]] || \
83-
[[ "${output}" =~ "png (screenshot):" ]] || \
84-
[[ "${output}" =~ ".txt" ]] || \
85-
[[ "${output}" =~ ".html" ]]
86-
}
87-
88-
#
89-
# Test 3: Error report file creation verification
90-
#
91-
# Validates that error report files are actually created in the filesystem
92-
#
93-
@test "DebugContext: error report files are created" {
94-
# Skip if DDEV is not running
95-
if ! is_ddev_running; then
96-
skip "DDEV is not running"
97-
fi
98-
99-
# Get the feature file path
100-
local feature_file="tests/features/debug-error.feature"
101-
102-
# Clean up any previous error reports
103-
ddev exec rm -rf /var/www/html/web/sites/default/files/behat/errors/* 2>/dev/null || true
104-
105-
# Run Behat with the error feature (expect failure)
106-
run ddev exec behat "${feature_file}"
36+
# Run Behat with the screenshot feature
37+
run ddev exec behat --config="$BEHAT_YML_CURRENT_TEST_PATH" "$FEATURE_UNDER_TEST_PATH"
38+
# The feature triggers an error to test the functionality of creating error
39+
# report files, so lest's expect a failure here.
40+
assert_failure
10741

108-
# Debug output
109-
echo "Status: ${status}" >&3
42+
local filepath
43+
filepath="$TEST_ROOT_DIR/web/sites/default/files/behat"
11044

111-
# Check if error directory exists and has files
112-
run ddev exec "ls -la /var/www/html/web/sites/default/files/behat/errors/ 2>/dev/null | wc -l"
45+
# Assert the "save last response" screenshot file was created
46+
assert_file_exists "$filepath/screenshots/varwwwhtmlwebsitesdefaultfilesbehatscreenshots_last_response.png"
11347

114-
echo "Error directory file count: ${output}" >&3
48+
# Assert the error report files were created
49+
local report_filename_template
50+
report_filename_template="$filepath/errors/behat-failed__-var-www-html-tests-behat-local-features-feature_under_test.feature-And_I-should-be-on-non-existent-page"
11551

116-
# If directory exists and has files (more than 2 lines from ls -la = . and ..)
117-
# then files were created
118-
if [ "${output}" -gt 2 ] 2>/dev/null; then
119-
# Files were created
120-
return 0
121-
else
122-
# No files created, but this is acceptable if error reporting is not enabled
123-
skip "Error reporting may not be enabled or path differs"
124-
fi
52+
assert_file_exists "$report_filename_template.html"
53+
assert_file_exists "$report_filename_template.png"
54+
assert_file_exists "$report_filename_template.txt"
12555
}
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,10 @@ teardown() {
2929
# when write_report is enabled
3030
#
3131
@test "LogsContext: outputs 'Created dblog report' message" {
32-
# Skip if DDEV is not running
33-
if ! is_ddev_running; then
34-
skip "DDEV is not running"
35-
fi
3632

37-
# Get the feature file path
38-
local feature_file="tests/features/logs-test.feature"
33+
34+
prepare_test "tests/features/logs-test.feature"
35+
3936

4037
# Run Behat with the logs feature
4138
run ddev exec behat "${feature_file}"

tests/contexts/setup_suite.bash

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@ configure_composer_github_token() {
2121
create_temp_dir() {
2222
# Create unique temp directory for this test
2323
local tmp_dir="${1:-/tmp}"
24-
TEST_TEMP_DIR=$(mktemp -d ${tmp_dir:-~/tmp}/bats-behat-XXXXXX)
25-
echo "# Created temporary test directory: ${TEST_TEMP_DIR}" >&3
26-
cd "$TEST_TEMP_DIR" || exit 1
24+
export TEST_ROOT_DIR
25+
TEST_ROOT_DIR=$(mktemp -d ${tmp_dir:-~/tmp}/bats-behat-XXXXXX)
26+
echo "# Created temporary test directory: ${TEST_ROOT_DIR}" >&3
27+
cd "$TEST_ROOT_DIR" || exit 1
28+
29+
2730
}
2831

2932
# Configure DDEV and install Aljibe
@@ -58,15 +61,11 @@ determine_source_path() {
5861
# Overrides behat-contexts installation to use local source that is being tested
5962
install_behat_contexts_from_source() {
6063

61-
62-
# Install behat-contexts library from local source
64+
# Install behat-contexts library from local source
6365
echo "# Installing behat-contexts from local source..." >&3
6466

6567
rm vendor/metadrop/behat-contexts -rf || true
6668
cp -r "${BEHAT_CONTEXTS_SOURCE_PATH}" vendor/metadrop/behat-contexts
67-
68-
ls -la vendor/metadrop/behat-contexts >&3
69-
7069
}
7170

7271
#
@@ -96,11 +95,11 @@ teardown_suite() {
9695
echo "# Tearing down test environment..." >&3
9796

9897
# Destroy DDEV containers before cleanup
99-
cd "${TEST_TEMP_DIR}" || exit 1
100-
ddev delete -Oy ${TEST_TEMP_DIR} >/dev/null 2>&1
98+
cd "${TEST_ROOT_DIR}" || exit 1
99+
ddev delete -Oy ${TEST_ROOT_DIR} >/dev/null 2>&1
101100
echo "# DDEV project deleted." >&3
102101

103102
cd ..
104-
rm -rf "${TEST_TEMP_DIR}"
105-
echo "# Removed temporary test directory: ${TEST_TEMP_DIR}" >&3
103+
rm -rf "${TEST_ROOT_DIR}"
104+
echo "# Removed temporary test directory: ${TEST_ROOT_DIR}" >&3
106105
}

tests/features/debug-screenshot.feature

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ Feature: Debug Context - Error Report Generation
33
I want to verify that DebugContext can generate error reports
44
So that I can validate error reporting functionality
55

6-
@api
6+
@api @javascript
77
Scenario: Trigger error report generation
88
Given I am on the homepage
9+
Then save last response
910
# This step is intentionally designed to fail to trigger error report
10-
When I am on "/non-existent-path-that-should-404"
11-
Then I should see "This page should exist"
11+
And I should be on "non-existent-page"

tests/run_tests_locally.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ if [ $? -ne 0 ]; then
2121
exit 1
2222
fi
2323

24-
bats tests/contexts/cookie-compliance-context.bats
24+
bats tests/contexts

tests/test_helper/common-setup.bash

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
# with DDEV and Aljibe.
88
#
99

10-
# Test temporary directory for artifacts
11-
export TEST_TEMP_DIR=""
12-
1310
# DDEV project name (can be overridden)
1411
export DDEV_PROJECT_NAME="${DDEV_PROJECT_NAME:-behat-contexts-test}"
1512

@@ -45,6 +42,9 @@ teardown_test_environment() {
4542
# $2 - Begin marker in behat.yml to locate insertion point
4643
# $3 - End marker in behat.yml to locate insertion point
4744
# $4 - Replacement text to insert between markers
45+
#
46+
# Parameters $2, $3, and $4 are optional; if not provided, the behat.yml
47+
# will be copied without modifications.
4848
prepare_test() {
4949

5050
local BEHAT_YML_TEMPLATE_PATH="tests/behat/local/behat.yml"
@@ -56,20 +56,35 @@ prepare_test() {
5656
skip "DDEV is not running"
5757
fi
5858

59-
# Get the feature file path
60-
local FEATURE_FILE="tests/features/$1"
61-
local BEGIN_MARK="$2"
62-
local END_MARK="$3"
63-
local REPLACEMENT="$4"
59+
if [ -f "$BEHAT_YML_CURRENT_TEST_PATH" ]; then
60+
rm "$BEHAT_YML_CURRENT_TEST_PATH"
61+
fi
6462

63+
if [ -f "$FEATURE_UNDER_TEST_PATH" ]; then
64+
rm "$FEATURE_UNDER_TEST_PATH"
65+
fi
6566

66-
echo "Copying feature file: cp $BEHAT_CONTEXTS_SOURCE_PATH/$FEATURE_FILE" "$FEATURE_UNDER_TEST_PATH"
67+
# At this point all checks and clean up actions are done.
68+
69+
# Now copy the feature file and the behat.yml template
70+
local FEATURE_FILE="tests/features/$1"
6771
cp "$BEHAT_CONTEXTS_SOURCE_PATH/$FEATURE_FILE" "$FEATURE_UNDER_TEST_PATH"
6872

69-
echo "Copying YML template file: cp $BEHAT_YML_TEMPLATE_PATH" "$BEHAT_YML_CURRENT_TEST_PATH"
73+
# Now decide whether to modify behat.yml or just copy it depending on
74+
# given parameters.
75+
if [ $# -eq 1 ]; then
76+
cp "$BEHAT_YML_TEMPLATE_PATH" "$BEHAT_YML_CURRENT_TEST_PATH"
77+
return
78+
fi
7079

71-
cp "$BEHAT_YML_TEMPLATE_PATH" "$BEHAT_YML_CURRENT_TEST_PATH"
80+
if [ -z "$2" ] || [ -z "$3" ] || [ -z "$4" ]; then
81+
echo "Error: Missing parameters for prepare_test function: begin marker, end marker and replacement are all required if any of them is provided (please provide all or none)." >&2
82+
exit 1
83+
fi
7284

85+
local BEGIN_MARK="$2"
86+
local END_MARK="$3"
87+
local REPLACEMENT="$4"
7388

7489
# Escape backslashes for sed safely
7590
sed_start=$(printf '%s\n' "$BEGIN_MARK" | sed 's/\\/\\\\/g')
@@ -145,6 +160,7 @@ get_test_features_dir() {
145160
# Removes error reports, screenshots, logs, etc.
146161
#
147162
cleanup_behat_artifacts() {
163+
return
148164
ddev exec rm -rf \
149165
"${DDEV_DOCROOT}/sites/default/files/behat" \
150166
/var/www/html/reports/behat \
@@ -180,16 +196,6 @@ get_timestamp() {
180196
date +"%Y%m%d_%H%M%S"
181197
}
182198

183-
#
184-
# Debug: Print test environment info
185-
#
186-
print_test_info() {
187-
echo "# Test Environment Info:" >&3
188-
echo "# TEST_TEMP_DIR: ${TEST_TEMP_DIR}" >&3
189-
echo "# DDEV Project: ${DDEV_PROJECT_NAME}" >&3
190-
echo "# DDEV Running: $(is_ddev_running && echo 'Yes' || echo 'No')" >&3
191-
}
192-
193199
#
194200
# Load bats-support and bats-assert if available
195201
#

0 commit comments

Comments
 (0)