-
Notifications
You must be signed in to change notification settings - Fork 0
/
BasePuppeteerTestCase.php
103 lines (86 loc) · 2.24 KB
/
BasePuppeteerTestCase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
namespace Ninton\Test;
use Nesk\Puphpeteer\Puppeteer;
use Nesk\Puphpeteer\Resources\Browser;
use Nesk\Puphpeteer\Resources\Page;
use Nesk\Rialto\Data\JsFunction;
use PHPUnit\Framework\TestCase;
abstract class BasePuppeteerTestCase extends TestCase
{
/**
* @var Browser
*/
protected $browser;
/**
* @var Page
*/
protected $page;
public function setUp(): void
{
parent::setUp();
$puppeteer = new Puppeteer();
$this->browser = $puppeteer->launch($this->launchOptions());
$this->page = ($this->browser->pages())[0];
}
public function tearDown(): void
{
$this->browser->close();
parent::tearDown();
}
protected function launchOptions(): array
{
return [
'defaultViewport' => [
'width' => 0,
'height' => 0,
],
'headless' => false,
'ignoreHTTPSErrors' => true,
'slowMo' => 50,
'args' => [
'--no-sandbox',
'--disable-setuid-sandbox',
"--window-size=1280,1024",
],
];
}
protected function waitForPageLoad(): void
{
$this->page->waitForNavigation("{waitUntil: ['load', 'networkidle2']}");
}
protected function screenShot(): void
{
$path = __DIR__ . '/../tmp/' . strftime('%Y-%m-%d-%H-%M-%S.jpg');
$this->page->screenshot([
'path' => $path,
'fullPage' => true,
]);
}
/**
* @param string $selector
*/
protected function pageTypeClear(string $selector): void
{
$jsFunc = JsFunction::createWithBody("document.querySelector('${selector}').value = '';");
$this->page->evaluate($jsFunc);
usleep(100000);
}
/**
* @param string $selector
* @param string $text
*/
protected function pageType(string $selector, string $text): void
{
$this->pageTypeClear($selector);
$this->page->type($selector, $text);
}
/**
* @param int $n
*/
protected function selectPage(int $n): void
{
$pages = $this->browser->pages();
var_dump(count($pages));
$this->page = $pages[$n];
}
}