Skip to content

Commit

Permalink
Merge branch '3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Aug 12, 2018
2 parents 0eb9ed1 + 9f94502 commit a85e1dd
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 16 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -11,7 +11,7 @@
],
"require": {
"php": ">=7.1.0",
"facebook/webdriver": "~1.0",
"facebook/webdriver": "~1.3",
"nesbot/carbon": "~1.20",
"illuminate/console": "~5.6",
"illuminate/support": "~5.6",
Expand Down
27 changes: 26 additions & 1 deletion src/Browser.php
Expand Up @@ -112,7 +112,7 @@ public function __construct($driver, $resolver = null)
/**
* Browse to the given URL.
*
* @param string $url
* @param string|Page $url
* @return $this
*/
public function visit($url)
Expand Down Expand Up @@ -268,6 +268,24 @@ public function storeConsoleLog($name)
return $this;
}

/**
* Switch to a specified frame in the browser and execute the given callback.
*
* @param string $selector
* @param \Closure $callback
* @return $this
*/
public function withinFrame($selector, Closure $callback)
{
$this->driver->switchTo()->frame($this->resolver->findOrFail($selector));

$callback($this);

$this->driver->switchTo()->defaultContent();

return $this;
}

/**
* Execute a Closure with a scoped browser instance.
*
Expand Down Expand Up @@ -306,6 +324,13 @@ public function with($selector, Closure $callback)
return $this;
}

/**
* Set the current component state.
*
* @param \Laravel\Dusk\Component $component
* @param \Laravel\Dusk\ElementResolver $parentResolver
* @return void
*/
public function onComponent($component, $parentResolver)
{
$this->component = $component;
Expand Down
2 changes: 1 addition & 1 deletion src/Concerns/InteractsWithMouse.php
Expand Up @@ -98,7 +98,7 @@ public function rightClick($selector = null)
}

/**
* Release the currenctly clicked mouse button.
* Release the currently clicked mouse button.
*
* @return $this
*/
Expand Down
40 changes: 34 additions & 6 deletions src/Concerns/MakesAssertions.php
Expand Up @@ -271,35 +271,63 @@ protected function assertHasQueryStringParameter($name)
/**
* Assert that the given cookie is present.
*
* @param string $name
* @param string $name
* @param bool $decrypt
* @return $this
*/
public function assertHasCookie($name)
public function assertHasCookie($name, $decrypt = true)
{
$cookie = $decrypt ? $this->cookie($name) : $this->plainCookie($name);

PHPUnit::assertTrue(
! is_null($this->cookie($name)),
! is_null($cookie),
"Did not find expected cookie [{$name}]."
);

return $this;
}

/**
* Assert that the given cookie is not present.
* Assert that the given plain cookie is present.
*
* @param string $name
* @return $this
*/
public function assertCookieMissing($name)
public function assertHasPlainCookie($name)
{
return $this->assertHasCookie($name, false);
}

/**
* Assert that the given cookie is not present.
*
* @param string $name
* @param bool $decrypt
* @return $this
*/
public function assertCookieMissing($name, $decrypt = true)
{
$cookie = $decrypt ? $this->cookie($name) : $this->plainCookie($name);

PHPUnit::assertTrue(
is_null($this->cookie($name)),
is_null($cookie),
"Found unexpected cookie [{$name}]."
);

return $this;
}

/**
* Assert that the given plain cookie is not present.
*
* @param string $name
* @return $this
*/
public function assertPlainCookieMissing($name)
{
return $this->assertCookieMissing($name, false);
}

/**
* Assert that an encrypted cookie has a given value.
*
Expand Down
3 changes: 3 additions & 0 deletions src/Concerns/ProvidesBrowser.php
Expand Up @@ -85,6 +85,7 @@ public function browse(Closure $callback)
*
* @param \Closure $callback
* @return array
* @throws \ReflectionException
*/
protected function createBrowsersFor(Closure $callback)
{
Expand Down Expand Up @@ -117,6 +118,7 @@ protected function newBrowser($driver)
*
* @param \Closure $callback
* @return int
* @throws \ReflectionException
*/
protected function browsersNeededFor(Closure $callback)
{
Expand Down Expand Up @@ -182,6 +184,7 @@ public static function closeAll()
* Create the remote web driver instance.
*
* @return \Facebook\WebDriver\Remote\RemoteWebDriver
* @throws \Exception
*/
protected function createWebDriver()
{
Expand Down
22 changes: 16 additions & 6 deletions src/Concerns/WaitsForElements.php
Expand Up @@ -19,6 +19,7 @@ trait WaitsForElements
* @param Closure $callback
* @param int $seconds
* @return $this
* @throws \Facebook\WebDriver\Exception\TimeOutException
*/
public function whenAvailable($selector, Closure $callback, $seconds = null)
{
Expand All @@ -31,6 +32,7 @@ public function whenAvailable($selector, Closure $callback, $seconds = null)
* @param string $selector
* @param int $seconds
* @return $this
* @throws \Facebook\WebDriver\Exception\TimeOutException
*/
public function waitFor($selector, $seconds = null)
{
Expand All @@ -45,6 +47,7 @@ public function waitFor($selector, $seconds = null)
* @param string $selector
* @param int $seconds
* @return $this
* @throws \Facebook\WebDriver\Exception\TimeOutException
*/
public function waitUntilMissing($selector, $seconds = null)
{
Expand All @@ -65,6 +68,7 @@ public function waitUntilMissing($selector, $seconds = null)
* @param string $text
* @param int $seconds
* @return $this
* @throws \Facebook\WebDriver\Exception\TimeOutException
*/
public function waitForText($text, $seconds = null)
{
Expand All @@ -79,12 +83,13 @@ public function waitForText($text, $seconds = null)
* @param string $link
* @param int $seconds
* @return $this
* @throws \Facebook\WebDriver\Exception\TimeOutException
*/
public function waitForLink($link, $seconds = null)
{
return $this->waitUsing($seconds, 100, function () use ($link) {
return $this->seeLink($link);
});
}, "Waited %s seconds for link [{$link}].");
}

/**
Expand All @@ -93,10 +98,11 @@ public function waitForLink($link, $seconds = null)
* @param string $path
* @param int $seconds
* @return $this
* @throws \Facebook\WebDriver\Exception\TimeOutException
*/
public function waitForLocation($path, $seconds = null)
{
return $this->waitUntil("window.location.pathname == '{$path}'", $seconds);
return $this->waitUntil("window.location.pathname == '{$path}'", $seconds, "Waited %s seconds for location [{$path}].");
}

/**
Expand All @@ -106,20 +112,23 @@ public function waitForLocation($path, $seconds = null)
* @param array $parameters
* @param int $seconds
* @return $this
* @throws \Facebook\WebDriver\Exception\TimeOutException
*/
public function waitForRoute($route, $parameters = [], $seconds = null)
{
return $this->waitForLocation(route($route, $parameters), $seconds);
return $this->waitForLocation(route($route, $parameters, false), $seconds);
}

/**
* Wait until the given script returns true.
*
* @param string $script
* @param int $seconds
* @param string $message
* @return $this
* @throws \Facebook\WebDriver\Exception\TimeOutException
*/
public function waitUntil($script, $seconds = null)
public function waitUntil($script, $seconds = null, $message = null)
{
if (! Str::startsWith($script, 'return ')) {
$script = 'return '.$script;
Expand All @@ -131,7 +140,7 @@ public function waitUntil($script, $seconds = null)

return $this->waitUsing($seconds, 100, function () use ($script) {
return $this->driver->executeScript($script);
});
}, $message);
}

/**
Expand All @@ -157,6 +166,7 @@ public function waitForDialog($seconds = null)
* @param Closure $callback
* @param int $seconds
* @return $this
* @throws \Facebook\WebDriver\Exception\TimeOutException
*/
public function waitForReload($callback = null, $seconds = null)
{
Expand All @@ -181,7 +191,7 @@ public function waitForReload($callback = null, $seconds = null)
* @param Closure $callback
* @param string|null $message
* @return $this
* @throws TimeOutException
* @throws \Facebook\WebDriver\Exception\TimeOutException
*/
public function waitUsing($seconds, $interval, Closure $callback, $message = null)
{
Expand Down
5 changes: 5 additions & 0 deletions src/Console/DuskCommand.php
Expand Up @@ -3,6 +3,7 @@
namespace Laravel\Dusk\Console;

use Dotenv\Dotenv;
use Illuminate\Support\Str;
use Illuminate\Console\Command;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Process\Process;
Expand Down Expand Up @@ -91,6 +92,10 @@ protected function binary()
*/
protected function phpunitArguments($options)
{
$options = array_values(array_filter($options, function ($option) {
return ! Str::startsWith($option, '--env=');
}));

return array_merge(['-c', base_path('phpunit.dusk.xml')], $options);
}

Expand Down
8 changes: 8 additions & 0 deletions src/ElementResolver.php
Expand Up @@ -77,6 +77,7 @@ public function pageElements(array $elements)
*
* @param string $field
* @return \Facebook\WebDriver\Remote\RemoteWebElement
* @throws \Exception
*/
public function resolveForTyping($field)
{
Expand All @@ -94,6 +95,7 @@ public function resolveForTyping($field)
*
* @param string $field
* @return \Facebook\WebDriver\Remote\RemoteWebElement
* @throws \Exception
*/
public function resolveForSelection($field)
{
Expand All @@ -112,6 +114,7 @@ public function resolveForSelection($field)
* @param string $field
* @param array $values
* @return \Facebook\WebDriver\Remote\RemoteWebElement[]
* @throws \Exception
*/
public function resolveSelectOptions($field, array $values)
{
Expand All @@ -133,6 +136,7 @@ public function resolveSelectOptions($field, array $values)
* @param string $field
* @param string $value
* @return \Facebook\WebDriver\Remote\RemoteWebElement
* @throws \Exception
*/
public function resolveForRadioSelection($field, $value = null)
{
Expand All @@ -157,6 +161,7 @@ public function resolveForRadioSelection($field, $value = null)
* @param string $field
* @param string $value
* @return \Facebook\WebDriver\Remote\RemoteWebElement
* @throws \Exception
*/
public function resolveForChecking($field, $value = null)
{
Expand All @@ -180,6 +185,7 @@ public function resolveForChecking($field, $value = null)
*
* @param string $field
* @return \Facebook\WebDriver\Remote\RemoteWebElement
* @throws \Exception
*/
public function resolveForAttachment($field)
{
Expand All @@ -197,6 +203,7 @@ public function resolveForAttachment($field)
*
* @param string $field
* @return \Facebook\WebDriver\Remote\RemoteWebElement
* @throws \Exception
*/
public function resolveForField($field)
{
Expand Down Expand Up @@ -320,6 +327,7 @@ public function find($selector)
*
* @param array $selectors
* @return \Facebook\WebDriver\Remote\RemoteWebElement
* @throws \Exception
*/
public function firstOrFail($selectors)
{
Expand Down
20 changes: 19 additions & 1 deletion tests/WaitsForElementsTest.php
Expand Up @@ -24,7 +24,7 @@ public function test_default_wait_time()
$this->assertEquals(2, floor(microtime(true) - $then));
}

public function test_default_wait_time_can_be_overriden()
public function test_default_wait_time_can_be_overridden()
{
Browser::$waitSeconds = 2;

Expand Down Expand Up @@ -62,4 +62,22 @@ public function test_wait_using_failure()
return false;
});
}

public function test_can_wait_for_location()
{
$driver = Mockery::mock(StdClass::class);
$driver->shouldReceive('executeScript')->with("return window.location.pathname == '/home';")->andReturnTrue();
$browser = new Browser($driver);

$browser->waitForLocation('/home');
}

public function test_can_wait_for_route()
{
$driver = Mockery::mock(StdClass::class);
$driver->shouldReceive('executeScript')->with("return window.location.pathname == '/home/';")->andReturnTrue();
$browser = new Browser($driver);

$browser->waitForRoute('home');
}
}

0 comments on commit a85e1dd

Please sign in to comment.