Skip to content

Commit d74fc85

Browse files
Infamoustreytaylorotwell
authored andcommitted
add documentation for Browser function elsewhere
1 parent 820ee90 commit d74fc85

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

dusk.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,16 @@ Sometimes you may wish to perform several operations while scoping all of the op
522522
->clickLink('Delete');
523523
});
524524

525+
You may also wish to execute assertions outside of the current scope. For example, you may wish to interact with a portion of the web page outside of the current scope such as in a component. You may use the `elsewhere` method to accomplish this. The operations performed in the callback will break the current scope to the given one.
526+
527+
$browser->with('.table', function ($table) {
528+
// selector is `body .table`
529+
$browser->elsewhere('.page-title', function ($title) {
530+
// selector is `body .page-title`
531+
$title->assertSee('Hello World');
532+
});
533+
});
534+
525535
<a name="waiting-for-elements"></a>
526536
### Waiting For Elements
527537

@@ -1442,6 +1452,90 @@ Once the component has been defined, we can easily select a date within the date
14421452
});
14431453
}
14441454
}
1455+
1456+
#### Non-Standard Components
1457+
1458+
Sometimes a given component does not have a single ancestor in the dom, this can occur in scenarios where popups are rendered to a seperate node such as this custom dropdown component.
1459+
1460+
<div class="dropdown">
1461+
<label class="dropdown-toggle">My Custom Dropdown</label>
1462+
<input class="dropdown-input">
1463+
</div>
1464+
1465+
...
1466+
1467+
<div class="dropdown-menu">
1468+
<ul class="dropdown-list">
1469+
<li data-value="1">Option 1</li>
1470+
<li data-value="2">Option 2</li>
1471+
<li data-value="3">Option 3</li>
1472+
</ul>
1473+
</div>
1474+
1475+
It makes sense to define the logic for interacting with this component in the same component class, but because they have seperate ancestors selecting the second portion with a scoped selector becomes untenable. In this scenarios you may bypass the current selector scope with the `elsewhere` method.
1476+
1477+
<?php
1478+
1479+
namespace Tests\Browser\Components;
1480+
1481+
use Laravel\Dusk\Browser;
1482+
use Laravel\Dusk\Component as BaseComponent;
1483+
1484+
class DropdownPicker extends BaseComponent
1485+
{
1486+
/**
1487+
* Get the root selector for the component.
1488+
*
1489+
* @return string
1490+
*/
1491+
public function selector()
1492+
{
1493+
return '.dropdown';
1494+
}
1495+
1496+
/**
1497+
* Assert that the browser page contains the component.
1498+
*
1499+
* @param Browser $browser
1500+
* @return void
1501+
*/
1502+
public function assert(Browser $browser)
1503+
{
1504+
$browser->assertVisible($this->selector());
1505+
}
1506+
1507+
/**
1508+
* Get the element shortcuts for the component.
1509+
*
1510+
* @return array
1511+
*/
1512+
public function elements()
1513+
{
1514+
return [
1515+
'@toggle' => '.dropdown-toggle',
1516+
'@list' => '.dropdown-menu .dropdown-list',
1517+
];
1518+
}
1519+
1520+
/**
1521+
* Select the given option.
1522+
*
1523+
* @param \Laravel\Dusk\Browser $browser
1524+
* @param string $value
1525+
* @return void
1526+
*/
1527+
public function selectFromDropdown($browser, $value)
1528+
{
1529+
// selector is `body .dropdown`
1530+
$browser->click('@toggle')
1531+
->elsewhere('@list', function ($browser) use ($value) {
1532+
// selector is `body .dropdown-menu .dropdown-list`
1533+
$browser->click("li[data-value=$value]");
1534+
});
1535+
}
1536+
}
1537+
1538+
14451539

14461540
<a name="continuous-integration"></a>
14471541
## Continuous Integration

0 commit comments

Comments
 (0)