Selenium and chrome mobile emulation in php

Below example shows how to start the chrome with Mobile Emulation mode using Selenium in PHP.
 
<?php
class MyTest extends PHPUnit_Framework_TestCase {

protected $driver;

public function setUp()
{
$options = new ChromeOptions();

// Use different chrome binary
//$options->setBinary(‘/path_to_binary’);

//Setting Arguments for the chrome browser
$options->addArguments(array(
‘–disable-extensions’,
‘start-maximized’,
‘disable-popup-blocking’,
‘test-type’
));

$mobile_emulation = [ “deviceName” => “Apple iPhone 6” ];

//$mobile_emulation = [ “deviceName” => “Apple iPhone 5” ];

//$mobile_emulation = [ “deviceName” => “Google Nexus 5” ];

$options->setExperimentalOption(“mobileEmulation”, $mobile_emulation);
$caps = DesiredCapabilities::chrome();
$caps->setCapability(ChromeOptions::CAPABILITY, $options);
$this->driver = RemoteWebDriver::create(‘https://localhost:4444/wd/hub’, $caps);
}

public function testEmulation()
{
$this->driver->get(“https://www.softpost.org/selenium-test-page”);

$mainHandle = $this->driver->getWindowHandle();
echo (“
 Main window handle -> ” . $mainHandle );

}

public function tearDown()
{
//Quit the driver
$this->driver->quit();
}
}
?>

Complex problems, Simple Solutions