Selecting Random Form Fields in Laravel Dusk
While doing automation testing of your web-application. At times it’s best to select random data in your form fields, this is because there are certain errors or issues which occur for particular set of data, which might get missed with your manual testing.
Selecting a random form field option will make sure to catch any data related issue in your application. Let’s see how you can select random fields in your Laravel dusk tests.
Select Random Option from Drop Down
Laravel dusk has a out of the box solution for selecting a random option from the select box.
If you use the select method on the browser instance, and only pass the name of the field. It will choose a random option every-time the test runs.
Click Random Radio Option
Unfortunately Laravel dusk does not have a default option to select random radio button from a group of radio buttons. But we can go around and directly use the webdriver instance to do this.
$radio_options = $browser->driver->findElements(WebDriverBy::name('radio-option-name')); $radio_options[array_rand($radio_options)]->click();
In the code above we are using the
findElements method on webdriver to get all the radio buttons with the same name,
findElements returns an array of elements and then we make use of
array_rand method provided by php to select any random radio element and then click on it.
If you find that you are frequently using the random radio option, you can create a custom broser macro for choosing a random radio and then click on it with that shortcut
Don’t forget to include the webdriver class into the namespace
Check Random Checkbox
Let’s say you have a group of checkboxes in your form something like this
<input id="inlineCheckbox1" name="my_checkbox" type="checkbox" value="option1" /> 1 <input id="inlineCheckbox2" name="my_checkbox" type="checkbox" value="option2" /> 2 <input id="inlineCheckbox3" name="my_checkbox" type="checkbox" value="option3" /> 3
To select a random checkbox, you can follow a similar approach as of radio button
$checkbox_options = $browser->driver->findElements(WebDriverBy::name('my_checkbox')); $checkbox_options[array_rand($checkbox_options)]->click();
Laravel Dusk Beginner to Advanced
Video Course on Automation Testing, Web Scraping and Creating Bots using Dusk
> 60% OFF
Limited Time Offer