12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154 |
- <?php
- use Codeception\Step;
- use Codeception\Util\Stub;
- use Facebook\WebDriver\Cookie;
- use Facebook\WebDriver\Remote\DesiredCapabilities;
- use Facebook\WebDriver\Remote\RemoteWebDriver;
- use Facebook\WebDriver\WebDriverBy;
- use Facebook\WebDriver\WebDriverKeys;
- require_once codecept_data_dir() . 'app/data.php';
- require_once __DIR__ . '/../unit/Codeception/Module/TestsForBrowsers.php';
- class WebDriverTest extends TestsForBrowsers
- {
- /**
- * @var \Codeception\Module\WebDriver
- */
- protected $module;
- /**
- * @var RemoteWebDriver
- */
- protected $webDriver;
- const MODULE_CLASS = 'Codeception\Module\WebDriver';
- const WEBDRIVER_CLASS = 'Facebook\WebDriver\Remote\RemoteWebDriver';
- public function _before()
- {
- $this->module = $this->getModule('WebDriver');
- $this->webDriver = &$this->getModule('WebDriver')->webDriver;
- }
- public function _after()
- {
- data::clean();
- }
- public function testClickEventOnCheckbox()
- {
- $this->module->amOnPage('/form/checkbox');
- $this->module->uncheckOption('#checkin');
- $this->module->dontSee('ticked', '#notice');
- $this->module->checkOption('#checkin');
- $this->module->see('ticked', '#notice');
- }
- public function testAcceptPopup()
- {
- $this->notForPhantomJS();
- $this->module->amOnPage('/form/popup');
- $this->module->click('Confirm');
- $this->module->acceptPopup();
- $this->module->see('Yes', '#result');
- }
- public function testCancelPopup()
- {
- $this->notForPhantomJS();
- $this->module->amOnPage('/form/popup');
- $this->module->click('Confirm');
- $this->module->cancelPopup();
- $this->module->see('No', '#result');
- }
- public function testSelectByCss()
- {
- $this->module->amOnPage('/form/select');
- $this->module->selectOption('form select[name=age]', '21-60');
- $this->module->click('Submit');
- $form = data::get('form');
- $this->assertEquals('adult', $form['age']);
- }
- public function testSelectInvalidOptionForSecondSelectFails()
- {
- $this->shouldFail();
- $this->module->amOnPage('/form/select_second');
- $this->module->selectOption('#select2', 'Value2');
- }
- public function testSeeInPopup()
- {
- $this->notForPhantomJS();
- $this->module->amOnPage('/form/popup');
- $this->module->click('Alert');
- $this->module->seeInPopup('Really?');
- $this->module->cancelPopup();
- }
- public function testFailedSeeInPopup()
- {
- $this->notForPhantomJS();
- $this->setExpectedException(
- '\PHPUnit\Framework\AssertionFailedError',
- 'Failed asserting that \'Really?\' contains "Different text"'
- );
- $this->module->amOnPage('/form/popup');
- $this->module->click('Alert');
- $this->module->seeInPopup('Different text');
- $this->module->cancelPopup();
- }
- public function testDontSeeInPopup()
- {
- $this->notForPhantomJS();
- $this->module->amOnPage('/form/popup');
- $this->module->click('Alert');
- $this->module->dontSeeInPopup('Different text');
- $this->module->cancelPopup();
- }
- public function testFailedDontSeeInPopup()
- {
- $this->notForPhantomJS();
- $this->setExpectedException(
- '\PHPUnit\Framework\AssertionFailedError',
- 'Failed asserting that \'Really?\' does not contain "Really?"'
- );
- $this->module->amOnPage('/form/popup');
- $this->module->click('Alert');
- $this->module->dontSeeInPopup('Really?');
- $this->module->cancelPopup();
- }
- public function testScreenshot()
- {
- $this->module->amOnPage('/');
- @unlink(\Codeception\Configuration::outputDir().'testshot.png');
- $testName="debugTest";
- $this->module->makeScreenshot($testName);
- $this->assertFileExists(\Codeception\Configuration::outputDir().'debug/'.$testName.'.png');
- @unlink(\Codeception\Configuration::outputDir().'debug/'.$testName.'.png');
- $this->module->_saveScreenshot(\Codeception\Configuration::outputDir().'testshot.png');
- $this->assertFileExists(\Codeception\Configuration::outputDir().'testshot.png');
- @unlink(\Codeception\Configuration::outputDir().'testshot.png');
- }
- public function testSubmitForm()
- {
- $this->module->amOnPage('/form/complex');
- $this->module->submitForm('form', [
- 'name' => 'Davert',
- 'age' => 'child',
- 'terms' => 'agree',
- 'description' => 'My Bio'
- ]);
- $form = data::get('form');
- $this->assertEquals('Davert', $form['name']);
- $this->assertEquals('kill_all', $form['action']);
- $this->assertEquals('My Bio', $form['description']);
- $this->assertEquals('agree', $form['terms']);
- $this->assertEquals('child', $form['age']);
- }
- public function testSubmitFormWithNumbers()
- {
- $this->module->amOnPage('/form/complex');
- $this->module->submitForm('form', [
- 'name' => 'Davert',
- 'age' => 'child',
- 'terms' => 'agree',
- 'description' => 10
- ]);
- $form = data::get('form');
- $this->assertEquals('Davert', $form['name']);
- $this->assertEquals('kill_all', $form['action']);
- $this->assertEquals('10', $form['description']);
- $this->assertEquals('agree', $form['terms']);
- $this->assertEquals('child', $form['age']);
- }
- /**
- * @dataProvider strictSelectorProvider
- */
- public function testSubmitFormWithButtonAsStrictSelector(array $selector)
- {
- $this->module->amOnPage('/form/strict_selectors');
- $this->module->submitForm('form', [
- 'name' => 'Davert',
- 'age' => 'child',
- 'terms' => 'agree',
- 'description' => 'My Bio'
- ], $selector);
- $form = data::get('form');
- $this->assertEquals('Davert', $form['name']);
- $this->assertEquals('kill_all', $form['action']);
- $this->assertEquals('My Bio', $form['description']);
- $this->assertEquals('agree', $form['terms']);
- $this->assertEquals('child', $form['age']);
- }
- public function strictSelectorProvider()
- {
- return [
- 'by id' => [['id' => 'submit_button']],
- 'by name' => [['name' => 'submit_button_name']],
- 'by css' => [['css' => 'form #submit_button']],
- 'by xpath' => [['xpath' => '//*[@id="submit_button"]']],
- 'by link' => [['link' => 'Submit']],
- 'by class' => [['class' => 'button']],
- ];
- }
- /**
- * @dataProvider webDriverByProvider
- */
- public function testSubmitFormWithButtonAsWebDriverBy(WebDriverBy $selector)
- {
- $this->module->amOnPage('/form/strict_selectors');
- $this->module->submitForm('form', [
- 'name' => 'Davert',
- 'age' => 'child',
- 'terms' => 'agree',
- 'description' => 'My Bio'
- ], $selector);
- $form = data::get('form');
- $this->assertEquals('Davert', $form['name']);
- $this->assertEquals('kill_all', $form['action']);
- $this->assertEquals('My Bio', $form['description']);
- $this->assertEquals('agree', $form['terms']);
- $this->assertEquals('child', $form['age']);
- }
- public function webDriverByProvider()
- {
- return [
- 'by id' => [WebDriverBy::id('submit_button')],
- 'by name' => [WebDriverBy::name('submit_button_name')],
- 'by css selector' => [WebDriverBy::cssSelector('form #submit_button')],
- 'by xpath' => [WebDriverBy::xpath('//*[@id="submit_button"]')],
- 'by link text' => [WebDriverBy::linkText('Submit')],
- 'by class name' => [WebDriverBy::className('button')],
- ];
- }
- public function testRadioButtonByValue()
- {
- $this->module->amOnPage('/form/radio');
- $this->module->selectOption('form', 'disagree');
- $this->module->click('Submit');
- $form = data::get('form');
- $this->assertEquals('disagree', $form['terms']);
- }
- public function testRadioButtonByLabelOnContext()
- {
- $this->module->amOnPage('/form/radio');
- $this->module->selectOption('form input', 'Get Off');
- $this->module->seeOptionIsSelected('form input', 'disagree');
- $this->module->dontSeeOptionIsSelected('form input', 'agree');
- $this->module->click('Submit');
- $form = data::get('form');
- $this->assertEquals('disagree', $form['terms']);
- }
- public function testRadioButtonByLabel()
- {
- $this->module->amOnPage('/form/radio');
- $this->module->checkOption('Get Off');
- $this->module->click('Submit');
- $form = data::get('form');
- $this->assertEquals('disagree', $form['terms']);
- }
- public function testRawSelenium()
- {
- $this->module->amOnPage('/');
- $this->module->executeInSelenium(function ($webdriver) {
- $webdriver->findElement(WebDriverBy::id('link'))->click();
- });
- $this->module->seeCurrentUrlEquals('/info');
- }
- public function testKeys()
- {
- $this->module->amOnPage('/form/field');
- $this->module->pressKey('#name', ['ctrl', 'a'], WebDriverKeys::DELETE);
- $this->module->pressKey('#name', 'test', ['shift', '111']);
- $this->module->pressKey('#name', '1');
- $this->module->seeInField('#name', 'test!!!1');
- }
- public function testWait()
- {
- $this->module->amOnPage('/');
- $time = time();
- $this->module->wait(3);
- $this->assertGreaterThanOrEqual($time+3, time());
- }
- public function testSelectInvalidOptionFails()
- {
- $this->shouldFail();
- $this->module->amOnPage('/form/select');
- $this->module->selectOption('#age', '13-22');
- }
- public function testAppendFieldSelect()
- {
- $this->module->amOnPage('/form/select_multiple');
- $this->module->selectOption('form #like', 'eat');
- $this->module->appendField('form #like', 'code');
- $this->module->click('Submit');
- $form = data::get('form');
- $this->assertEmpty(array_diff($form['like'], ["eat", "code"]));
- }
- public function testAppendFieldSelectFails()
- {
- $this->shouldFail();
- $this->module->amOnPage('/form/select_multiple');
- $this->module->appendField('form #like', 'code123');
- }
- public function testAppendFieldTextarea()
- {
- $this->module->amOnPage('/form/textarea');
- $this->module->fillField('form #description', 'eat');
- $this->module->appendField('form #description', ' code');
- $this->module->click('Submit');
- $form = data::get('form');
- $this->assertEquals('eat code', $form['description']);
- }
- public function testAppendFieldTextareaFails()
- {
- $this->shouldFail();
- $this->module->amOnPage('/form/textarea');
- $this->module->appendField('form #description123', ' code');
- }
- public function testAppendFieldText()
- {
- $this->module->amOnPage('/form/field');
- $this->module->appendField('form #name', ' code');
- $this->module->click('Submit');
- $form = data::get('form');
- $this->assertEquals('OLD_VALUE code', $form['name']);
- }
- public function testAppendFieldTextFails()
- {
- $this->shouldFail();
- $this->module->amOnPage('/form/field');
- $this->module->appendField('form #name123', ' code');
- }
- public function testAppendFieldCheckboxByValue()
- {
- $this->module->amOnPage('/form/checkbox');
- $this->module->appendField('form input[name=terms]', 'agree');
- $this->module->click('Submit');
- $form = data::get('form');
- $this->assertEquals('agree', $form['terms']);
- }
- public function testAppendFieldCheckboxByValueFails()
- {
- $this->shouldFail();
- $this->module->amOnPage('/form/checkbox');
- $this->module->appendField('form input[name=terms]', 'agree123');
- }
- public function testAppendFieldCheckboxByLabel()
- {
- $this->module->amOnPage('/form/checkbox');
- $this->module->appendField('form input[name=terms]', 'I Agree');
- $this->module->click('Submit');
- $form = data::get('form');
- $this->assertEquals('agree', $form['terms']);
- }
- public function testAppendFieldCheckboxByLabelFails()
- {
- $this->shouldFail();
- $this->module->amOnPage('/form/checkbox');
- $this->module->appendField('form input[name=terms]', 'I Agree123');
- }
- public function testAppendFieldRadioButtonByValue()
- {
- $this->module->amOnPage('/form/radio');
- $this->module->appendField('form input[name=terms]', 'disagree');
- $this->module->click('Submit');
- $form = data::get('form');
- $this->assertEquals('disagree', $form['terms']);
- }
- public function testAppendFieldRadioButtonByValueFails()
- {
- $this->shouldFail();
- $this->module->amOnPage('/form/radio');
- $this->module->appendField('form input[name=terms]', 'disagree123');
- }
- public function testAppendFieldRadioButtonByLabel()
- {
- $this->module->amOnPage('/form/radio');
- $this->module->appendField('form input[name=terms]', 'Get Off');
- $this->module->click('Submit');
- $form = data::get('form');
- $this->assertEquals('disagree', $form['terms']);
- }
- public function testAppendFieldRadioButtonByLabelFails()
- {
- $this->shouldFail();
- $this->module->amOnPage('/form/radio');
- $this->module->appendField('form input[name=terms]', 'Get Off123');
- }
- public function testPauseExecution()
- {
- $this->module->amOnPage('/');
- $this->module->pauseExecution();
- }
- // Issue https://github.com/Codeception/Codeception/pull/875
- public function testFillPasswordOnFormSubmit()
- {
- $this->module->amOnPage('/form/complex');
- $this->module->submitForm('form', [
- 'password' => '123456'
- ]);
- $form = data::get('form');
- $this->assertEquals('123456', $form['password']);
- }
- public function testEmptyFormSubmit()
- {
- $this->shouldFail();
- $this->module->amOnPage('/form/complex');
- $this->module->submitForm('form111', []);
- }
- public function testWebDriverByLocators()
- {
- $this->module->amOnPage('/login');
- $this->module->seeElement(WebDriverBy::id('submit-label'));
- $this->module->seeElement(WebDriverBy::name('password'));
- $this->module->seeElement(WebDriverBy::className('optional'));
- $this->module->seeElement(WebDriverBy::cssSelector('form.global_form_box'));
- $this->module->seeElement(WebDriverBy::xpath(\Codeception\Util\Locator::tabIndex(4)));
- $this->module->fillField(WebDriverBy::name('password'), '123456');
- $this->module->amOnPage('/form/select');
- $this->module->selectOption(WebDriverBy::name('age'), 'child');
- $this->module->amOnPage('/form/checkbox');
- $this->module->checkOption(WebDriverBy::name('terms'));
- $this->module->amOnPage('/');
- $this->module->seeElement(WebDriverBy::linkText('Test'));
- $this->module->click(WebDriverBy::linkText('Test'));
- $this->module->seeCurrentUrlEquals('/form/hidden');
- }
- public function testSeeVisible()
- {
- $this->module->amOnPage('/info');
- $this->module->dontSee('Invisible text');
- $this->module->dontSee('Invisible', '.hidden');
- $this->module->seeInPageSource('Invisible text');
- }
- public function testSeeInvisible()
- {
- $this->shouldFail();
- $this->module->amOnPage('/info');
- $this->module->see('Invisible text');
- }
- public function testFailWebDriverByLocator()
- {
- $this->shouldFail();
- $this->module->amOnPage('/form/checkbox');
- $this->module->checkOption(WebDriverBy::name('age'));
- }
- // fails in PhpBrowser :(
- public function testSubmitUnchecked()
- {
- $this->module->amOnPage('/form/unchecked');
- $this->module->seeCheckboxIsChecked('#checkbox');
- $this->module->uncheckOption('#checkbox');
- $this->module->click('#submit');
- ;
- $this->module->see('0', '#notice');
- }
- public function testCreateCeptScreenshotFail()
- {
- $fakeWd = Stub::make('\Facebook\WebDriver\Remote\RemoteWebDriver', [
- 'takeScreenshot' => Stub::once(function () {
- }),
- 'getPageSource' => Stub::once(function () {
- }),
- 'manage' => Stub::make('\Facebook\WebDriver\WebDriverOptions', [
- 'getAvailableLogTypes' => Stub::atLeastOnce(function () {
- return [];
- }),
- ]),
- ]);
- $module = Stub::make(self::MODULE_CLASS, ['webDriver' => $fakeWd]);
- $cept = (new \Codeception\Test\Cept('loginCept', 'loginCept.php'));
- $module->_failed($cept, new \PHPUnit\Framework\AssertionFailedError());
- }
- public function testCreateCestScreenshotOnFail()
- {
- $fakeWd = Stub::make(self::WEBDRIVER_CLASS, [
- 'takeScreenshot' => Stub::once(function ($filename) {
- PHPUnit_Framework_Assert::assertEquals(codecept_log_dir('stdClass.login.fail.png'), $filename);
- }),
- 'getPageSource' => Stub::once(function () {
- }),
- 'manage' => Stub::make('\Facebook\WebDriver\WebDriverOptions', [
- 'getAvailableLogTypes' => Stub::atLeastOnce(function () {
- return [];
- }),
- ]),
- ]);
- $module = Stub::make(self::MODULE_CLASS, ['webDriver' => $fakeWd]);
- $cest = new \Codeception\Test\Cest(new stdClass(), 'login', 'someCest.php');
- $module->_failed($cest, new \PHPUnit\Framework\AssertionFailedError());
- }
- public function testCreateTestScreenshotOnFail()
- {
- $test = Stub::make('\Codeception\TestCase\Test', ['getName' => 'testLogin']);
- $fakeWd = Stub::make(self::WEBDRIVER_CLASS, [
- 'takeScreenshot' => Stub::once(function ($filename) use ($test) {
- PHPUnit_Framework_Assert::assertEquals(
- codecept_log_dir(get_class($test).'.testLogin.fail.png'),
- $filename
- );
- }),
- 'getPageSource' => Stub::once(function () {
- }),
- 'manage' => Stub::make('\Facebook\WebDriver\WebDriverOptions', [
- 'getAvailableLogTypes' => Stub::atLeastOnce(function () {
- return [];
- }),
- ]),
- ]);
- $module = Stub::make(self::MODULE_CLASS, ['webDriver' => $fakeWd]);
- $module->_failed($test, new \PHPUnit\Framework\AssertionFailedError());
- }
- public function testWebDriverWaits()
- {
- $fakeWd = Stub::make(self::WEBDRIVER_CLASS, ['wait' => Stub::exactly(16, function () {
- return new \Codeception\Util\Maybe();
- })]);
- $module = Stub::make(self::MODULE_CLASS, ['webDriver' => $fakeWd]);
- $module->waitForElement(WebDriverBy::partialLinkText('yeah'));
- $module->waitForElement(['id' => 'user']);
- $module->waitForElement(['css' => '.user']);
- $module->waitForElement('//xpath');
- $module->waitForElementVisible(WebDriverBy::partialLinkText('yeah'));
- $module->waitForElementVisible(['id' => 'user']);
- $module->waitForElementVisible(['css' => '.user']);
- $module->waitForElementVisible('//xpath');
- $module->waitForElementNotVisible(WebDriverBy::partialLinkText('yeah'));
- $module->waitForElementNotVisible(['id' => 'user']);
- $module->waitForElementNotVisible(['css' => '.user']);
- $module->waitForElementNotVisible('//xpath');
- $module->waitForElementClickable(WebDriverBy::partialLinkText('yeah'));
- $module->waitForElementClickable(['id' => 'user']);
- $module->waitForElementClickable(['css' => '.user']);
- $module->waitForElementClickable('//xpath');
- }
- public function testWaitForElement()
- {
- $this->module->amOnPage('/form/timeout');
- $this->module->waitForElement('#btn');
- $this->module->click('Click');
- $this->module->see('Hello');
- }
- public function testImplicitWait()
- {
- $this->module->_reconfigure(['wait' => 5]);
- $this->module->amOnPage('/form/timeout');
- $this->module->click('#btn');
- $this->module->see('Hello');
- }
- public function testBug1467()
- {
- $this->module->amOnPage('/form/bug1467');
- $this->module->selectOption('form[name=form2] input[name=first_test_radio]', 'Yes');
- $this->module->selectOption('form[name=form2] input[name=second_test_radio]', 'No');
- $this->module->seeOptionIsSelected('form[name=form2] input[name=first_test_radio]', 'Yes');
- $this->module->seeOptionIsSelected('form[name=form2] input[name=second_test_radio]', 'No');
- // shouldn't have touched form1 at all
- $this->module->dontSeeOptionIsSelected('form[name=form1] input[name=first_test_radio]', 'No');
- $this->module->dontSeeOptionIsSelected('form[name=form1] input[name=first_test_radio]', 'Yes');
- $this->module->dontSeeOptionIsSelected('form[name=form1] input[name=second_test_radio]', 'No');
- $this->module->dontSeeOptionIsSelected('form[name=form1] input[name=second_test_radio]', 'Yes');
- }
- /**
- * @Issue 1598
- */
- public function testWaitForTextBug1598()
- {
- $this->module->amOnPage('/form/bug1598');
- $this->module->waitForText('12,345', 10, '#field');
- }
- public function testSeeElementMalformedWdLocator()
- {
- $this->setExpectedException('Codeception\Exception\MalformedLocatorException');
- $this->module->amOnPage('/');
- $this->module->seeElement(WebDriverBy::xpath('H---EY!'));
- }
- public function testBug1637()
- {
- $this->module->amOnPage('/form/bug1637');
- // confirm that options outside a form are still selectable
- $this->module->selectOption('input[name=first_test_radio]', 'Yes');
- // confirm that it did what we expected and did not do anything else
- $this->module->seeOptionIsSelected('input[name=first_test_radio]', 'Yes');
- $this->module->dontSeeOptionIsSelected('input[name=first_test_radio]', 'No');
- }
- public function testBug2046()
- {
- $this->module->webDriver = null;
- $this->module->_saveScreenshot(\Codeception\Configuration::outputDir().'testshot.png');
- }
- public function testSessionSnapshots()
- {
- $this->notForPhantomJS();
- $this->module->amOnPage('/');
- $this->module->setCookie('PHPSESSID', '123456', ['path' => '/']);
- $this->module->saveSessionSnapshot('login');
- $this->module->seeCookie('PHPSESSID');
- $this->webDriver->manage()->deleteAllCookies();
- $this->module->dontSeeCookie('PHPSESSID');
- $this->module->loadSessionSnapshot('login');
- $this->module->seeCookie('PHPSESSID');
- }
- public function testSessionSnapshotsAreDeleted()
- {
- $this->notForPhantomJS();
- $this->module->amOnPage('/');
- $this->module->setCookie('PHPSESSID', '123456', ['path' => '/']);
- $this->module->saveSessionSnapshot('login');
- $this->webDriver->manage()->deleteAllCookies();
- $this->module->deleteSessionSnapshot('login');
- $this->assertFalse($this->module->loadSessionSnapshot('login'));
- $this->module->dontSeeCookie('PHPSESSID');
- }
- public function testSaveSessionSnapshotsExcludeInvalidCookieDomains()
- {
- $this->notForPhantomJS();
- $fakeWdOptions = Stub::make('\Facebook\WebDriver\WebDriverOptions', [
- 'getCookies' => Stub::atLeastOnce(function () {
- return [
- [
- 'name' => 'PHPSESSID',
- 'value' => '123456',
- 'path' => '/',
- ],
- [
- 'name' => '3rdParty',
- 'value' => '_value_',
- 'path' => '/',
- 'domain' => '.3rd-party.net',
- ],
- ];
- }),
- ]);
- $fakeWd = Stub::make(self::WEBDRIVER_CLASS, [
- 'manage' => Stub::atLeastOnce(function () use ($fakeWdOptions) {
- return $fakeWdOptions;
- }),
- ]);
- // Mock the WebDriverOptions::getCookies() method on the first call to introduce a 3rd-party cookie
- // which has to be ignored when saving a snapshot.
- $originalWebDriver = $this->module->webDriver;
- $this->module->webDriver = $fakeWd;
- $this->module->seeCookie('PHPSESSID');
- $this->module->seeCookie('3rdParty');
- $this->module->saveSessionSnapshot('login');
- // Restore the original WebDriver
- $this->module->webDriver = $originalWebDriver;
- $this->webDriver->manage()->deleteAllCookies();
- $this->module->dontSeeCookie('PHPSESSID');
- $this->module->dontSeeCookie('3rdParty');
- $this->module->amOnPage('/');
- $this->module->loadSessionSnapshot('login');
- $this->module->seeCookie('PHPSESSID');
- $this->module->dontSeeCookie('3rdParty');
- }
- public function testSeeInFieldTextarea()
- {
- $this->module->amOnPage('/form/textarea');
- //make sure we see 'sunrise' which is the default text in the textarea
- $this->module->seeInField('#description', 'sunrise');
- if ($this->notForSelenium()) {
- $this->module->seeInField('#whitespaces', ' no_whitespaces ');
- }
- $this->module->seeInField('#whitespaces', 'no_whitespaces');
- //fill in some new text and see if we can see it
- $textarea_value = 'test string';
- $this->module->fillField('#description', $textarea_value);
- $this->module->seeInField('#description', $textarea_value);
- }
- public function testSeeInFieldSelect()
- {
- $this->module->amOnPage('/form/select_second');
- if ($this->notForSelenium()) {
- $this->module->seeInField('#select2', ' no_whitespaces ');
- }
- $this->module->seeInField('#select2', 'no_whitespaces');
- // select new option and check it
- $option_value = 'select2_value1';
- $this->module->selectOption('#select2', $option_value);
- $this->module->seeInField('#select2', $option_value);
- }
- public function testAppendFieldDiv()
- {
- $this->notForPhantomJS();
- $this->module->amOnPage('/form/div_content_editable');
- //make sure we see 'sunrise' which is the default text in the textarea
- $this->module->see('sunrise', '#description');
- //fill in some new text and see if we can see it
- $textarea_value = 'moonrise';
- $this->module->appendField('#description', $textarea_value);
- $this->module->see('sunrise' . $textarea_value, '#description');
- }
- public function testOpenPageException()
- {
- if (!$this->module->_getConfig('restart')) {
- $this->markTestSkipped('works only on restarts');
- }
- parent::testOpenPageException();
- }
- public function testCookies()
- {
- $this->notForPhantomJS();
- parent::testCookies();
- }
- public function testSendingCookies()
- {
- $this->notForPhantomJS();
- parent::testSendingCookies();
- }
- public function testCookiesWithPath()
- {
- $this->notForPhantomJS();
- parent::testCookiesWithPath();
- }
- protected function notForPhantomJS()
- {
- if ($this->module->_getConfig('browser') == 'phantomjs') {
- $this->markTestSkipped('does not work for phantomjs');
- }
- }
- protected function notForSelenium()
- {
- if ($this->module->_getConfig('browser') != 'phantom') {
- $this->markTestSkipped('does not work for selenium');
- }
- }
- public function testScrollTo()
- {
- $this->module->amOnPage('/form/example18');
- $this->module->scrollTo('#clickme');
- $this->module->click('Submit');
- $this->module->see('Welcome to test app!');
- }
- /**
- * @Issue 2921
- */
- public function testSeeInFieldForTextarea()
- {
- $this->module->amOnPage('/form/bug2921');
- $this->module->seeInField('foo', 'bar baz');
- }
- /**
- * @Issue 4726
- */
- public function testClearField()
- {
- $this->module->amOnPage('/form/textarea');
- $this->module->fillField('#description', 'description');
- $this->module->clearField('#description');
- $this->module->dontSeeInField('#description', 'description');
- }
- public function testClickHashLink()
- {
- $this->module->amOnPage('/form/anchor');
- $this->module->click('Hash Link');
- $this->module->seeCurrentUrlEquals('/form/anchor#b');
- }
- /**
- * @Issue 3865
- */
- public function testClickNumericLink()
- {
- $this->module->amOnPage('/form/bug3865');
- $this->module->click('222');
- $this->module->see('Welcome to test app');
- }
- public function testClickHashButton()
- {
- $this->module->amOnPage('/form/anchor');
- $this->module->click('Hash Button');
- $this->module->seeCurrentUrlEquals('/form/anchor#c');
- }
- public function testSubmitHashForm()
- {
- $this->module->amOnPage('/form/anchor');
- $this->module->click('Hash Form');
- $this->module->seeCurrentUrlEquals('/form/anchor#a');
- }
- public function testSubmitHashFormTitle()
- {
- $this->module->amOnPage('/form/anchor');
- $this->module->click('Hash Form Title');
- $this->module->seeCurrentUrlEquals('/form/anchor#a');
- }
- public function testSubmitHashButtonForm()
- {
- $this->module->amOnPage('/form/anchor');
- $this->module->click('Hash Button Form');
- $this->module->seeCurrentUrlEquals('/form/anchor#a');
- }
- public function testJSErrorLoggingPositive()
- {
- // arrange
- $this->module->_setConfig(['log_js_errors' => true]);
- $cept = new \Codeception\Test\Cept('foo', 'bar');
- // act
- $this->module->amOnPage('/jserroronload');
- $this->module->_failed($cept, 'anyFailMessage');
- // assert
- /* @var $steps Step[] */
- $steps = $cept->getScenario()->getSteps();
- $this->assertGreaterThan(0, count($steps));
- $lastStep = end($steps);
- $this->assertContains(
- "TypeError",
- $lastStep->getHtml()
- );
- }
- public function testJSErrorLoggingNegative()
- {
- // arrange
- $this->module->_setConfig(['log_js_errors' => false]);
- $cept = new \Codeception\Test\Cept('foo', 'bar');
- // act
- $this->module->amOnPage('/jserroronload');
- $this->module->_failed($cept, 'anyFailMessage');
- // assert
- /* @var $steps Step[] */
- $steps = $cept->getScenario()->getSteps();
- $this->assertCount(0, $steps);
- }
- public function testMoveMouseOver()
- {
- $this->module->amOnPage('/form/click');
- $this->module->moveMouseOver(null, 123, 88);
- $this->module->clickWithLeftButton(null, 0, 0);
- $this->module->see('click, offsetX: 123 - offsetY: 88');
- $this->module->moveMouseOver(null, 10, 10);
- $this->module->clickWithLeftButton(null, 0, 0);
- $this->module->see('click, offsetX: 133 - offsetY: 98');
- $this->module->moveMouseOver('#element2');
- $this->module->clickWithLeftButton(null, 0, 0);
- $this->module->see('click, offsetX: 58 - offsetY: 158');
- $this->module->moveMouseOver('#element2', 0, 0);
- $this->module->clickWithLeftButton(null, 0, 0);
- $this->module->see('click, offsetX: 8 - offsetY: 108');
- }
- public function testLeftClick()
- {
- $this->module->amOnPage('/form/click');
- $this->module->clickWithLeftButton(null, 123, 88);
- $this->module->see('click, offsetX: 123 - offsetY: 88');
- $this->module->clickWithLeftButton('body');
- $this->module->see('click, offsetX: 600 - offsetY: 384');
- $this->module->clickWithLeftButton('body', 50, 75);
- $this->module->see('click, offsetX: 58 - offsetY: 83');
- $this->module->clickWithLeftButton('body div');
- $this->module->see('click, offsetX: 58 - offsetY: 58');
- $this->module->clickWithLeftButton('#element2', 70, 75);
- $this->module->see('click, offsetX: 78 - offsetY: 183');
- }
- public function testRightClick()
- {
- // actually not supported in phantomjs see https://github.com/ariya/phantomjs/issues/14005
- $this->notForPhantomJS();
- $this->module->amOnPage('/form/click');
- $this->module->clickWithRightButton(null, 123, 88);
- $this->module->see('context, offsetX: 123 - offsetY: 88');
- $this->module->clickWithRightButton('body');
- $this->module->see('context, offsetX: 600 - offsetY: 384');
- $this->module->clickWithRightButton('body', 50, 75);
- $this->module->see('context, offsetX: 58 - offsetY: 83');
- $this->module->clickWithRightButton('body div');
- $this->module->see('context, offsetX: 58 - offsetY: 58');
- $this->module->clickWithRightButton('#element2', 70, 75);
- $this->module->see('context, offsetX: 78 - offsetY: 183');
- }
- public function testBrowserTabs()
- {
- $this->notForPhantomJS();
- $this->module->amOnPage('/form/example1');
- $this->module->openNewTab();
- $this->module->amOnPage('/form/example2');
- $this->module->openNewTab();
- $this->module->amOnPage('/form/example3');
- $this->module->openNewTab();
- $this->module->amOnPage('/form/example4');
- $this->module->openNewTab();
- $this->module->amOnPage('/form/example5');
- $this->module->closeTab();
- $this->module->seeInCurrentUrl('example4');
- $this->module->switchToPreviousTab(2);
- $this->module->seeInCurrentUrl('example2');
- $this->module->switchToNextTab();
- $this->module->seeInCurrentUrl('example3');
- $this->module->closeTab();
- $this->module->seeInCurrentUrl('example2');
- $this->module->switchToNextTab(2);
- $this->module->seeInCurrentUrl('example1');
- }
- public function testPerformOnWithArray()
- {
- $asserts = PHPUnit_Framework_Assert::getCount();
- $this->module->amOnPage('/form/example1');
- $this->module->performOn('.rememberMe', [
- 'see' => 'Remember me next time',
- 'seeElement' => '#LoginForm_rememberMe',
- 'dontSee' => 'Login'
- ]);
- $this->assertEquals(3, PHPUnit_Framework_Assert::getCount() - $asserts);
- $this->module->see('Login');
- }
- public function testPerformOnWithCallback()
- {
- $asserts = PHPUnit_Framework_Assert::getCount();
- $this->module->amOnPage('/form/example1');
- $this->module->performOn('.rememberMe', function (\Codeception\Module\WebDriver $I) {
- $I->see('Remember me next time');
- $I->seeElement('#LoginForm_rememberMe');
- $I->dontSee('Login');
- });
- $this->assertEquals(3, PHPUnit_Framework_Assert::getCount() - $asserts);
- $this->module->see('Login');
- }
- public function testPerformOnWithBuiltArray()
- {
- $asserts = PHPUnit_Framework_Assert::getCount();
- $this->module->amOnPage('/form/example1');
- $this->module->performOn('.rememberMe', \Codeception\Util\ActionSequence::build()
- ->see('Remember me next time')
- ->seeElement('#LoginForm_rememberMe')
- ->dontSee('Login')
- );
- $this->assertEquals(3, PHPUnit_Framework_Assert::getCount() - $asserts);
- $this->module->see('Login');
- }
- public function testPerformOnWithArrayAndSimilarActions()
- {
- $asserts = PHPUnit_Framework_Assert::getCount();
- $this->module->amOnPage('/form/example1');
- $this->module->performOn('.rememberMe', \Codeception\Util\ActionSequence::build()
- ->see('Remember me')
- ->see('next time')
- ->dontSee('Login')
- );
- $this->assertEquals(3, PHPUnit_Framework_Assert::getCount() - $asserts);
- $this->module->see('Login');
- }
- public function testPerformOnFail()
- {
- $this->shouldFail();
- $this->module->amOnPage('/form/example1');
- $this->module->performOn('.rememberMe', \Codeception\Util\ActionSequence::build()
- ->seeElement('#LoginForm_rememberMe')
- ->see('Remember me tomorrow')
- );
- }
- public function testPerformOnFail2()
- {
- $this->shouldFail();
- $this->module->amOnPage('/form/example1');
- $this->module->performOn('.rememberMe', ['see' => 'Login']);
- }
- public function testSwitchToIframe()
- {
- $this->module->amOnPage('iframe');
- $this->module->switchToIFrame('content');
- $this->module->see('Lots of valuable data here');
- $this->module->switchToIFrame();
- $this->module->see('Iframe test');
- }
- public function testGrabPageSourceWhenNotOnPage()
- {
- $this->setExpectedException(
- '\Codeception\Exception\ModuleException',
- 'Current url is blank, no page was opened'
- );
- $this->module->grabPageSource();
- }
- public function testGrabPageSourceWhenOnPage()
- {
- $this->module->amOnPage('/minimal');
- $sourceExpected =
- <<<HTML
- <!DOCTYPE html>
- <html>
- <head>
- <title>
- Minimal page
- </title>
- </head>
- <body>
- <h1>
- Minimal page
- </h1>
- </body>
- </html>
- HTML
- ;
- $sourceActualRaw = $this->module->grabPageSource();
- // `Selenium` adds the `xmlns` attribute while `PhantomJS` does not do that.
- $sourceActual = str_replace('xmlns="http://www.w3.org/1999/xhtml"', '', $sourceActualRaw);
- $this->assertXmlStringEqualsXmlString($sourceExpected, $sourceActual);
- }
- public function testChangingCapabilities()
- {
- $this->notForPhantomJS();
- $this->assertNotTrue($this->module->webDriver->getCapabilities()->getCapability('acceptInsecureCerts'));
- $this->module->_closeSession();
- $this->module->_capabilities(function($current) {
- $current['acceptInsecureCerts'] = true;
- return new DesiredCapabilities($current);
- });
- $this->assertNotTrue($this->module->webDriver->getCapabilities()->getCapability('acceptInsecureCerts'));
- $this->module->_initializeSession();
- $this->assertTrue($this->module->webDriver->getCapabilities()->getCapability('acceptInsecureCerts'));
- }
- /**
- * @dataProvider strictBug4846Provider
- **/
- public function testBug4846($selector)
- {
- $this->module->amOnPage('/');
- $this->module->see('Welcome to test app!', $selector);
- $this->module->dontSee('You cannot see that', $selector);
- }
- public function strictBug4846Provider()
- {
- return [
- 'by id' => ['h1'],
- 'by css' => [['css' => 'body h1']],
- 'by xpath' => ['//body/h1'],
- ];
- }
- }
|