Skip to main content

Laravel Dusk Tip: Click Bootstrap Toggle

We use BootStrap Toggle extensiverly at work. The problem with writing a browser test to toggle is that, the checkbox item that we use in the html template is never visible as it bootstrap toggle renders it as a button. I find writing a clean simple macro to solve the issue:

    Browser::macro('clickToggle', function($inputSelector) {
        $this->ensurejQueryIsAvailable();
        $this->driver->executeScript("jQuery(\"{$inputSelector}\").parent().click();");
        return $this;
    });
    
Once you define this macro using it is simple:

    $this->browse(function (Browser $browser) {
        $browser->login()
                ->visit(new MyPage())
                ->clickToggle('#mycheckbox');
    });
    

Comments