Skip to content

Commit

Permalink
implemented artifacts
Browse files Browse the repository at this point in the history
  • Loading branch information
DavertMik committed Oct 21, 2024
1 parent b29698e commit 8b758e8
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 14 deletions.
17 changes: 7 additions & 10 deletions server.js
Original file line number Diff line number Diff line change
@@ -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());
Expand Down Expand Up @@ -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;

Expand All @@ -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 });
Expand Down
15 changes: 12 additions & 3 deletions src/Playwright.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions tests/E2e.suite.yml
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions tests/E2e/DemoCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace E2e;

use \E2eTester;

class DemoCest
{
public function _before(E2eTester $I)
{
}

// tests
public function tryToTest(E2eTester $I)
{
$I->amOnPage('/');
$I->see('Welcome');
// $I->see('Welcome back');
}
}
28 changes: 28 additions & 0 deletions tests/support/E2eTester.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);


/**
* Inherited Methods
* @method void wantTo($text)
* @method void wantToTest($text)
* @method void execute($callable)
* @method void expectTo($prediction)
* @method void expect($prediction)
* @method void amGoingTo($argumentation)
* @method void am($role)
* @method void lookForwardTo($achieveValue)
* @method void comment($description)
* @method void pause($vars = [])
*
* @SuppressWarnings(PHPMD)
*/
class E2eTester extends \Codeception\Actor
{
use _generated\E2eTesterActions;

/**
* Define custom actions here
*/
}
11 changes: 10 additions & 1 deletion tests/web/WebDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -455,9 +455,18 @@ public function testWaiters()
$this->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()
Expand Down

0 comments on commit 8b758e8

Please sign in to comment.