Last active
November 5, 2019 10:45
-
-
Save nojimage/48e6848866dd6017dd82f2d364adcf60 to your computer and use it in GitHub Desktop.
Codeception / WebDriver / ChromeDriver でモバイルエミュレーションする
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# WebDriver より前に読み込ませること | |
actor: AcceptanceTester | |
modules: | |
enabled: | |
- \Helper\Acceptance | |
- \Helper\MobileEmulation\Chrome | |
- WebDriver |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* | |
* Copyright 2018 ELASTIC Consultants Inc. | |
* | |
*/ | |
namespace Helper\MobileEmulation; | |
use Codeception\Module; | |
use Codeception\Module\WebDriver; | |
use Codeception\Test\Cest; | |
use Codeception\TestInterface; | |
use Facebook\WebDriver\Chrome\ChromeOptions; | |
/** | |
* WebDriver拡張 | |
*/ | |
class Chrome extends Module | |
{ | |
/** | |
* auto load when Cest class has $mobileEmulation property. | |
* | |
* @param TestInterface $test | |
*/ | |
public function _before(TestInterface $test) | |
{ | |
if (!is_a($test, Cest::class)) { | |
return; | |
} | |
$testClass = $test->getTestClass(); | |
$mobileEmulation = property_exists($testClass, 'mobileEmulation') ? $testClass->mobileEmulation : false; | |
if ($mobileEmulation === true) { | |
$this->_mobileEmulation(); | |
} elseif ($mobileEmulation) { | |
$this->_mobileEmulation($mobileEmulation); | |
} | |
} | |
/** | |
* set mobileEmulation and restart driver | |
* | |
* @param string $deviceName | |
* @link https://sites.google.com/a/chromium.org/chromedriver/mobile-emulation | |
*/ | |
public function emulationMobile($deviceName = 'iPhone 6') | |
{ | |
$this->_mobileEmulation($deviceName); | |
$this->getModule('WebDriver')->_restart(); | |
} | |
/** | |
* @param string $deviceName | |
* @link https://sites.google.com/a/chromium.org/chromedriver/mobile-emulation | |
*/ | |
protected function _mobileEmulation($deviceName = 'iPhone 6') | |
{ | |
$userAgent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/56.0.2924.75 Mobile/14E5239e Safari/602.1'; | |
$deviceMetrics = [ | |
'width' => 360, | |
'height' => 640, | |
'pixelRaito' => 3.0, | |
]; | |
$mobileEmulation = [ | |
'deviceName' => $deviceName, | |
// 'userAgent' => $userAgent, | |
// 'deviceMetrics' => $deviceMetrics, | |
]; | |
$driver = $this->getModule('WebDriver'); | |
/* @var $driver WebDriver */ | |
$driver->_capabilities(function($currentCapabilities) use ($mobileEmulation) { | |
$chromeOptions = new ChromeOptions(); | |
$chromeOptions->setExperimentalOption('mobileEmulation', $mobileEmulation); | |
$currentCapabilities[ChromeOptions::CAPABILITY] = $chromeOptions->toArray(); | |
return $currentCapabilities; | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class SampleCest | |
{ | |
public $mobileEmulation = false; // これをtrueまたはデバイス名指定で、このシナリオではモバイルアクセス | |
// tests | |
public function tryAccessToHome(AcceptanceTester $I) | |
{ | |
$I->expect('PC版のページが表示されている'); | |
$I->amOnPage('/'); | |
$I->see('PC版'); | |
} | |
// tests | |
public function tryAccessToHomeByMobile(AcceptanceTester $I) | |
{ | |
$I->emulationMobile(); | |
$I->expect('モバイル版のページが表示されている'); | |
$I->amOnPage('/'); | |
$$I->see('モバイル版'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
モジュール化 done https://github.com/nojimage/codeception-mobileemulation