Skip to content

Commit

Permalink
add function to allow a closure to be executed outside of the current…
Browse files Browse the repository at this point in the history
… scope
  • Loading branch information
Infamoustrey committed Jun 11, 2020
1 parent 6c1e4f6 commit 8fa7910
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/Browser.php
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,32 @@ public function with($selector, Closure $callback)
return $this;
}

/**
* Execute a Closure outside of the current browser scope.
*
* @param string $selector
* @param \Closure $callback
* @return $this
*/
public function elsewhere($selector, Closure $callback)
{
$browser = new static(
$this->driver, new ElementResolver($this->driver, 'body ' . $selector)
);

if ($this->page) {
$browser->onWithoutAssert($this->page);
}

if ($selector instanceof Component) {
$browser->onComponent($selector, $this->resolver);
}

call_user_func($callback, $browser);

return $this;
}

/**
* Set the current component state.
*
Expand Down
13 changes: 13 additions & 0 deletions tests/BrowserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,19 @@ public function test_within_method_with_page()
});
}

public function test_elsewhere_method()
{
$driver = m::mock(stdClass::class);
$browser = new Browser($driver);

$browser->with('prefix', function ($browser) {
$browser->elsewhere('.my-class', function($browser) {
$this->assertInstanceof(Browser::class, $browser);
$this->assertEquals('body .my-class', $browser->resolver->prefix);
});
});
}

public function test_page_macros()
{
$driver = m::mock(stdClass::class);
Expand Down

0 comments on commit 8fa7910

Please sign in to comment.