From 8b758e89ca2d6e1a4e6001af502c97c531d22d02 Mon Sep 17 00:00:00 2001 From: davert Date: Tue, 22 Oct 2024 02:23:00 +0300 Subject: [PATCH] implemented artifacts --- server.js | 17 +++++++---------- src/Playwright.php | 15 ++++++++++++--- tests/E2e.suite.yml | 14 ++++++++++++++ tests/E2e/DemoCest.php | 20 ++++++++++++++++++++ tests/support/E2eTester.php | 28 ++++++++++++++++++++++++++++ tests/web/WebDriverTest.php | 11 ++++++++++- 6 files changed, 91 insertions(+), 14 deletions(-) create mode 100644 tests/E2e.suite.yml create mode 100644 tests/E2e/DemoCest.php create mode 100644 tests/support/E2eTester.php diff --git a/server.js b/server.js index 245d63e..248c261 100755 --- a/server.js +++ b/server.js @@ -1,6 +1,7 @@ #!/usr/bin/env node const express = require('express'); const Playwright = require('codeceptjs/lib/helper/Playwright'); +const path = require('path'); const app = express(); const port = 8191; app.use(express.json()); @@ -47,13 +48,6 @@ app.get('/test/:id', async (req, res) => { res.status(200).json(tests[id]); }); -app.post('/test', async (req, res) => { - const { id, title } = req.body; - - if (!tests[id]) tests[id] = { title } - -}); - app.post('/hook', async (req, res) => { const { command, arguments } = req.body; @@ -73,16 +67,19 @@ app.post('/hook', async (req, res) => { const fileName = `${id}_failed.png`; try { await playwright.saveScreenshot(fileName); - tests[id].artifacts ||= {} - tests[id].artifacts.screenshot = fileName; + if (!tests[id]) tests[id] = { title } + if (!tests[id].artifacts) tests[id].artifacts = {} + tests[id].artifacts.screenshot = path.join(output_dir, fileName); } catch (err) { + console.error('Error saving screenshot: ', err); // not matter } default: result = await playwright[`_${hook}`](tests[id]); } - res.status(200).json({ result, test: tests[id] }); + const test = tests[id]; + res.status(200).json({ result, test }); } catch (error) { const message = error.inspect ? error.inspect() : error.message; res.status(500).json({ message }); diff --git a/src/Playwright.php b/src/Playwright.php index a4fa459..bcfbe66 100644 --- a/src/Playwright.php +++ b/src/Playwright.php @@ -92,13 +92,18 @@ public function _after(TestInterface $test): void 'id' => $this->testId, ], 'hook'); } - $this->sendCommand('_after', [ + $this->sendCommand('after', [ 'id' => $this->testId, ], 'hook'); - if ($this->currentTest && $this->currentTest['artifacts']) { + if ($this->currentTest) { + + $this->debugSection('Test', $this->currentTest); + + if (isset($this->currentTest['artifacts'])) { foreach ($this->currentTest['artifacts'] as $artifact => $file) { - $test->getMetadata()->addReport($artifact, $file); + $test->getMetadata()->addReport($artifact, $file); + } } unset($this->currentTest); } @@ -894,6 +899,10 @@ private function sendCommand(string $command, array $arguments = [], string $end $url = $this->config['pw_server']; + if (!str_starts_with($endpoint, '/')) { + $endpoint = '/' . $endpoint; + } + $ch = curl_init($url . $endpoint); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); diff --git a/tests/E2e.suite.yml b/tests/E2e.suite.yml new file mode 100644 index 0000000..5175f37 --- /dev/null +++ b/tests/E2e.suite.yml @@ -0,0 +1,14 @@ +actor: E2eTester +suite_namespace: \E2e +modules: + enabled: + - Playwright: + url: http://127.0.0.1:8000 + browser: chromium + window_size: 1200x768 + timeout: 1000 + show: false + restart: true + pw_debug: true + video: true + trace: true diff --git a/tests/E2e/DemoCest.php b/tests/E2e/DemoCest.php new file mode 100644 index 0000000..278c413 --- /dev/null +++ b/tests/E2e/DemoCest.php @@ -0,0 +1,20 @@ +amOnPage('/'); + $I->see('Welcome'); + // $I->see('Welcome back'); + } +} diff --git a/tests/support/E2eTester.php b/tests/support/E2eTester.php new file mode 100644 index 0000000..6aa8065 --- /dev/null +++ b/tests/support/E2eTester.php @@ -0,0 +1,28 @@ +module->amOnPage('/'); $this->module->waitForElementClickable('#link'); $this->module->waitForElementVisible('#link'); - + $this->module->amOnPage('/info'); $this->module->waitForElementNotVisible('Invisible text'); + + } + + #[Skip('Bug in CodeceptJS')] + public function testWaitForText() + { + $this->module->amOnPage('/'); + $this->module->waitForText('debug!'); + $this->module->waitForText('"debug!"'); } public function testGrabPageSourceWhenOnPage()