Skip to content

Instantly share code, notes, and snippets.

@drbyte
Forked from brandonferens/MobileApplication.php
Created March 6, 2017 22:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drbyte/2e17bf5d5c51f4847a7508d0a8c2fc03 to your computer and use it in GitHub Desktop.
Save drbyte/2e17bf5d5c51f4847a7508d0a8c2fc03 to your computer and use it in GitHub Desktop.
Trait for testing mobile devices in Dusk

Put MobileApplication.php in /tests.

Then in the Dusk test you want to test for mobile add the following line:

use MobileApplciation;

If you would like to change the default mobile device, the set a private property on your test:

private $device = 'Apple iPad Mini';

The test will run in a mobile emulator!

<?php
namespace Tests;
use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Remote\RemoteWebDriver;
trait MobileApplication
{
/**
* Create the RemoteWebDriver instance.
*
* @return \Facebook\WebDriver\Remote\RemoteWebDriver
*/
protected function driver()
{
$options = new ChromeOptions();
$options->setExperimentalOption('mobileEmulation', [
'deviceName' => $this->device(),
]);
return RemoteWebDriver::create(
'http://localhost:9515', $options->toCapabilities()
);
}
/**
* Type of device that should be emulated during the test The default is
* an Apple iPhone 6, but any of the devices listed below are acceptable:
*
* Amazon Kindle Fire HDX, Apple iPad, Apple iPad Mini, Apple iPhone 4, Apple iPhone 5, Apple iPhone 6,
* Apple iPhone 6 Plus, BlackBerry PlayBook, BlackBerry Z30, Google Nexus 4, Google Nexus 5, Google Nexus 6,
* Google Nexus 7, Google Nexus 10, Laptop with touch, Laptop with HiDPI screen, Laptop with MDPI screen,
* LG Optimus L70, Nokia Lumia 520, Nokia N9, Samsung Galaxy Note II, Samsung Galaxy S III, Samsung Galaxy S4
*
* @return string
*/
protected function device()
{
return property_exists($this, 'device') ? $this->device : 'Apple iPhone 6';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment