Completed
Pull Request — master (#51)
by
unknown
01:52 queued 33s
created

Browser::with()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace BeyondCode\DuskDashboard\Dusk;
4
5
use Closure;
6
use BeyondCode\DuskDashboard\BrowserActionCollector;
7
8
class Browser extends \Laravel\Dusk\Browser
9
{
10
    use Concerns\InteractsWithAuthentication,
11
        Concerns\InteractsWithCookies,
12
        Concerns\InteractsWithElements,
13
        Concerns\InteractsWithJavascript,
14
        Concerns\InteractsWithMouse,
15
        Concerns\MakesAssertions,
16
        Concerns\MakesUrlAssertions,
17
        Concerns\WaitsForElements;
18
19
    /** @var BrowserActionCollector */
20
    protected $actionCollector;
21
22
    public function setActionCollector(BrowserActionCollector $collector)
23
    {
24
        $this->actionCollector = $collector;
25
    }
26
27
    /**
28
     * @return BrowserActionCollector|null
29
     */
30
    public function getActionCollector()
31
    {
32
        return $this->actionCollector;
33
    }
34
35
    /** {@inheritdoc} */
36
    public function visit($url)
37
    {
38
        $browser = parent::visit($url);
39
40
        $this->actionCollector->collect(__FUNCTION__, func_get_args(), $this);
41
42
        return $browser;
43
    }
44
45
    /** {@inheritdoc} */
46
    public function visitRoute($route, $parameters = [])
47
    {
48
        $browser = parent::visitRoute($route, $parameters);
49
50
        $this->actionCollector->collect(__FUNCTION__, func_get_args(), $this);
51
52
        return $browser;
53
    }
54
55
    /** {@inheritdoc} */
56
    public function refresh()
57
    {
58
        $browser = parent::refresh();
59
60
        $this->actionCollector->collect(__FUNCTION__, func_get_args(), $this);
61
62
        return $browser;
63
    }
64
65
    /** {@inheritdoc} */
66
    public function with($selector, Closure $callback)
67
    {
68
        $action_collector_callback = function ($browser) use ($callback) {
69
            $browser->setActionCollector($this->getActionCollector());
70
71
            return $callback($browser);
72
        };
73
74
        return parent::with($selector, $action_collector_callback);
75
    }
76
77
    /** {@inheritDoc} */
78
    public function onComponent($component, $parentResolver)
79
    {
80
        if ($this->getActionCollector() === null) {
81
            $this->setActionCollector(new BrowserActionCollector('dog'));
82
        }
83
84
        parent::onComponent($component, $parentResolver);
85
    }
86
87
    public function getCurrentPageSource()
88
    {
89
        $this->ensurejQueryIsAvailable();
90
91
        $this->restoreHtml();
92
93
        return $this->driver->executeScript('return document.documentElement.innerHTML;');
94
    }
95
96
    protected function restoreHtml()
97
    {
98
        $this->driver->executeScript("jQuery('input').attr('value', function() { return jQuery(this).val(); });");
99
100
        $this->driver->executeScript("jQuery('input[type=checkbox]').each(function() { jQuery(this).attr('checked', jQuery(this).prop(\"checked\")); });");
101
102
        $this->driver->executeScript("jQuery('textarea').each(function() { jQuery(this).html(jQuery(this).val()); });");
103
104
        $this->driver->executeScript("jQuery('input[type=radio]').each(function() { jQuery(this).attr('checked', this.checked); });");
105
106
        $this->driver->executeScript("jQuery('select option').each(function() { jQuery(this).attr('selected', this.selected); });");
107
    }
108
}
109