Skip to content

Commit

Permalink
Add option url for Page::addScriptTag
Browse files Browse the repository at this point in the history
  • Loading branch information
gsouf committed Nov 13, 2018
1 parent aa786f5 commit e1f26eb
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 13 deletions.
28 changes: 19 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,16 @@ Once the page has completed the navigation you can evaluate arbitrary script on
$value = $evaluation->getReturnValue();
```


Sometime the script you evaluate will click a link or submit a form, in this case the page will reload and you
will want to wait for the new page to reload.

You can achieve this by using ``$page->evaluate('some js that will reload the page')->waitForPageReload()``.
An example is available in [form-submit.php](./examples/form-submit.php)

#### Call a function

This is an alternative to evaluate that allows to call a given function with the given arguments in the page context:
This is an alternative to ``evaluate`` that allows to call a given function with the given arguments in the page context:

```php
$evaluation = $page->callFunction(
Expand All @@ -258,19 +265,22 @@ This is an alternative to evaluate that allows to call a given function with the
That's useful if you want to add jQuery (or anything else) to the page:

```php
$evaluation = $page->addScriptTag([
$page->addScriptTag([
'content' => file_get_contents('path/to/jquery.js')
])->getReturnValue();
])->waitForResponse();

$evaluation->evaluate('$(".my.element").html()');
$page->evaluate('$(".my.element").html()');
```

You can also use an url to feed the src attribute:

Sometime the script you evaluate will click a link or submit a form, in this case the page will reload and you
will want to wait for the new page to reload.

You can achieve this by using ``$page->evaluate('some js that will reload the page')->waitForPageReload()``.
An example is available in [form-submit.php](./examples/form-submit.php)
```php
$page->addScriptTag([
'url' => 'https://code.jquery.com/jquery-3.3.1.min.js'
])->waitForResponse();

$page->evaluate('$(".my.element").html()');
```

### Add a script to evaluate upon page navigation

Expand Down
17 changes: 14 additions & 3 deletions src/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,20 @@ public function addScriptTag(array $options): PageEvaluation
if (isset($options['url']) && isset($options['content'])) {
throw new \InvalidArgumentException('addScript accepts "url" or "content" option, not both');
} elseif (isset($options['url'])) {
throw new \Exception('TODO'); // TODO
$scriptFunction = 'async function(src) {
const script = document.createElement("script");
script.type = "text/javascript";
script.src = src;
const promise = new Promise((res, rej) => {
script.onload = res;
script.onerror = rej;
});
document.head.appendChild(script);
await promise;
}';
$arguments = [$options['url']];
} elseif (isset($options['content'])) {
$scriptFunction = 'async function(scriptContent) {
var script = document.createElement("script");
Expand All @@ -247,8 +260,6 @@ public function addScriptTag(array $options): PageEvaluation
if (error) {
throw error;
}
return script;
}';
$arguments = [$options['content']];
} else {
Expand Down
1 change: 1 addition & 0 deletions test/resources/static-web/jsInclude.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
window.testJsIsIncluded = "isIncluded";
24 changes: 23 additions & 1 deletion test/suites/PageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function testCallFunction()
$this->assertEquals(3, $page->evaluate('window.foo')->getReturnValue());
}

public function testAddScriptTag()
public function testAddScriptTagContent()
{
$factory = new BrowserFactory();

Expand All @@ -129,6 +129,28 @@ public function testAddScriptTag()
$this->assertEquals('bar', $page->evaluate('window.foo')->getReturnValue());
}

public function testAddScriptTagUrl()
{
$factory = new BrowserFactory();

$browser = $factory->createBrowser();
$page = $browser->createPage();
$page->navigate(
$this->sitePath('a.html')
)->waitForNavigation();

$page->addScriptTag([
'url' => $this->sitePath('jsInclude.js')
])->waitForResponse();

$isIncluded = $page->evaluate('window.testJsIsIncluded')->getReturnValue();
$scriptSrc = $page->evaluate('document.querySelector("script").getAttribute("src")')->getReturnValue();

$this->assertEquals('isIncluded', $isIncluded);
$this->assertStringStartsWith('file://', $scriptSrc);
$this->assertStringEndsWith('/jsInclude.js', $scriptSrc);
}

public function testGetLayoutMetrics()
{
$factory = new BrowserFactory();
Expand Down

0 comments on commit e1f26eb

Please sign in to comment.