Completed
Pull Request — master (#51)
by
unknown
01:28
created

Browser::onComponent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
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
            print 'Entering the wrapped callback...';
70
            $browser->setActionCollector($this->getActionCollector());
71
72
            print 'Wrapped callback invoked & updated browser!';
73
            return $callback($browser);
74
        };
75
76
        return parent::with($selector, $action_collector_callback);
77
    }
78
79
    /** {@inheritDoc} */
80
    public function onComponent($component, $parentResolver)
81
    {
82
        if ($this->getActionCollector() === null) {
83
            $this->setActionCollector(new BrowserActionCollector('dog'));
84
        }
85
86
        parent::onComponent($component, $parentResolver);
87
    }
88
89
    public function getCurrentPageSource()
90
    {
91
        $this->ensurejQueryIsAvailable();
92
93
        $this->restoreHtml();
94
95
        return $this->driver->executeScript('return document.documentElement.innerHTML;');
96
    }
97
98
    protected function restoreHtml()
99
    {
100
        $this->driver->executeScript("jQuery('input').attr('value', function() { return jQuery(this).val(); });");
101
102
        $this->driver->executeScript("jQuery('input[type=checkbox]').each(function() { jQuery(this).attr('checked', jQuery(this).prop(\"checked\")); });");
103
104
        $this->driver->executeScript("jQuery('textarea').each(function() { jQuery(this).html(jQuery(this).val()); });");
105
106
        $this->driver->executeScript("jQuery('input[type=radio]').each(function() { jQuery(this).attr('checked', this.checked); });");
107
108
        $this->driver->executeScript("jQuery('select option').each(function() { jQuery(this).attr('selected', this.selected); });");
109
    }
110
}
111