Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Added a bunch of safeguard in tests to avoid fatal errors on failure
Browse files Browse the repository at this point in the history
If the elements used in the tests are not found, most cases are now
reporting a failure instead of triggering a fatal error by calling a
method on null.
  • Loading branch information
stof committed Feb 25, 2014
1 parent b0f7b97 commit 53b48cb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
20 changes: 17 additions & 3 deletions tests/Behat/Mink/Driver/GeneralDriverTest.php
Expand Up @@ -125,6 +125,7 @@ public function testIssue212()
$page = $session->getPage();

$field = $page->findById('poney-button');
$this->assertNotNull($field);
$this->assertEquals('poney', $field->getValue());
}

Expand Down Expand Up @@ -314,7 +315,7 @@ public function testElementsTraversing()
$this->assertFalse($page->find('xpath', '//div/strong[2]')->hasAttribute('class'));

$strongs = $page->findAll('css', 'div#core > strong');
$this->assertEquals(3, count($strongs));
$this->assertCount(3, $strongs);
$this->assertEquals('Lorem', $strongs[0]->getText());
$this->assertEquals('pariatur', $strongs[2]->getText());

Expand Down Expand Up @@ -343,6 +344,7 @@ public function testGetAttribute($attributeName, $attributeValue)

$element = $this->getSession()->getPage()->findById('attr-elem[' . $attributeName . ']');

$this->assertNotNull($element);
$this->assertSame($attributeValue, $element->getAttribute($attributeName));
}

Expand Down Expand Up @@ -403,11 +405,11 @@ public function testDeepTraversing()

$traversDiv = $this->getSession()->getPage()->findAll('css', 'div.travers');

$this->assertEquals(1, count($traversDiv));
$this->assertCount(1, $traversDiv);
$traversDiv = $traversDiv[0];

$subDivs = $traversDiv->findAll('css', 'div.sub');
$this->assertEquals(3, count($subDivs));
$this->assertCount(3, $subDivs);

$this->assertTrue($subDivs[2]->hasLink('some deep url'));
$this->assertFalse($subDivs[2]->hasLink('come deep url'));
Expand All @@ -429,6 +431,7 @@ public function testLinks()
$page = $this->getSession()->getPage();
$link = $page->findLink('Redirect me to');

$this->assertNotNull($link);
$this->assertRegExp('/redirector\.php$/', $link->getAttribute('href'));
$link->click();

Expand All @@ -438,6 +441,7 @@ public function testLinks()
$page = $this->getSession()->getPage();
$link = $page->findLink('basic form image');

$this->assertNotNull($link);
$this->assertRegExp('/basic_form\.php$/', $link->getAttribute('href'));
$link->click();

Expand Down Expand Up @@ -512,6 +516,7 @@ public function testFormSubmitWays($submitVia)
$page = $session->getPage();

$firstname = $page->findField('first_name');
$this->assertNotNull($firstname);
$firstname->setValue('Konstantin');

$page->findButton($submitVia)->click();
Expand Down Expand Up @@ -567,6 +572,7 @@ public function testBasicGetForm()
$this->assertEquals('Basic Get Form Page', $page->find('css', 'h1')->getText());

$search = $page->findField('q');
$this->assertNotNull($search);
$search->setValue('some#query');
$page->pressButton('Find');

Expand Down Expand Up @@ -601,6 +607,7 @@ public function testMultiselect()
$this->assertEquals(array('1', '3'), $multiSelect->getValue());

$button = $page->findButton('Register');
$this->assertNotNull($button);
$button->press();

$space = ' ';
Expand All @@ -627,6 +634,7 @@ public function testElementSelectedStateCheck($selectName, $optionValue, $option

$optionValueEscaped = $session->getSelectorsHandler()->xpathLiteral($optionValue);
$option = $select->find('xpath', 'descendant-or-self::option[@value = ' . $optionValueEscaped . ']');
$this->assertNotNull($option);

$this->assertFalse($option->isSelected());
$select->selectOption($optionText);
Expand Down Expand Up @@ -708,6 +716,7 @@ public function testAdvancedForm()
$about->attachFile($this->mapRemoteFilePath(__DIR__ . '/web-fixtures/some_file.txt'));

$button = $page->findButton('Register');
$this->assertNotNull($button);

$page->fillField('first_name', 'Foo "item"');
$page->fillField('last_name', 'Bar');
Expand Down Expand Up @@ -799,6 +808,7 @@ public function testMultiInput()
$this->assertEquals('tag3', $third->getValue());

$button = $page->findButton('Register');
$this->assertNotNull($button);
$button->press();

$space = ' ';
Expand Down Expand Up @@ -835,6 +845,7 @@ public function testAdvancedFormSecondSubmit()
$page = $this->getSession()->getPage();

$button = $page->findButton('Login');
$this->assertNotNull($button);
$button->press();

$toSearch = array(
Expand Down Expand Up @@ -959,6 +970,9 @@ public function testHtmlDecodingNotPerformed()
$span = $page->find('css', 'span');
$input = $page->find('css', 'input');

$this->assertNotNull($span);
$this->assertNotNull($input);

$expectedHtml = '<span custom-attr="&amp;">some text</span>';
$this->assertContains($expectedHtml, $page->getHtml(), '.innerHTML is returned as-is');
$this->assertContains($expectedHtml, $page->getContent(), '.outerHTML is returned as-is');
Expand Down
25 changes: 23 additions & 2 deletions tests/Behat/Mink/Driver/JavascriptDriverTest.php
Expand Up @@ -110,6 +110,7 @@ public function testClick()
{
$this->getSession()->visit($this->pathTo('/js_test.php'));
$clicker = $this->getSession()->getPage()->find('css', '.elements div#clicker');
$this->assertNotNull($clicker);
$this->assertEquals('not clicked', $clicker->getText());

$clicker->click();
Expand All @@ -123,6 +124,7 @@ public function testDoubleClick()
{
$this->getSession()->visit($this->pathTo('/js_test.php'));
$clicker = $this->getSession()->getPage()->find('css', '.elements div#clicker');
$this->assertNotNull($clicker);
$this->assertEquals('not clicked', $clicker->getText());

$clicker->doubleClick();
Expand All @@ -136,6 +138,7 @@ public function testRightClick()
{
$this->getSession()->visit($this->pathTo('/js_test.php'));
$clicker = $this->getSession()->getPage()->find('css', '.elements div#clicker');
$this->assertNotNull($clicker);
$this->assertEquals('not clicked', $clicker->getText());

$clicker->rightClick();
Expand All @@ -149,6 +152,7 @@ public function testFocus()
{
$this->getSession()->visit($this->pathTo('/js_test.php'));
$focusBlurDetector = $this->getSession()->getPage()->find('css', '.elements input#focus-blur-detector');
$this->assertNotNull($focusBlurDetector);
$this->assertEquals('no action detected', $focusBlurDetector->getValue());

$focusBlurDetector->focus();
Expand All @@ -163,6 +167,7 @@ public function testBlur()
{
$this->getSession()->visit($this->pathTo('/js_test.php'));
$focusBlurDetector = $this->getSession()->getPage()->find('css', '.elements input#focus-blur-detector');
$this->assertNotNull($focusBlurDetector);
$this->assertEquals('no action detected', $focusBlurDetector->getValue());

$focusBlurDetector->blur();
Expand All @@ -176,6 +181,7 @@ public function testMouseOver()
{
$this->getSession()->visit($this->pathTo('/js_test.php'));
$mouseOverDetector = $this->getSession()->getPage()->find('css', '.elements div#mouseover-detector');
$this->assertNotNull($mouseOverDetector);
$this->assertEquals('no mouse action detected', $mouseOverDetector->getText());

$mouseOverDetector->mouseOver();
Expand All @@ -194,6 +200,11 @@ public function testKeyboardEvents($modifier, $eventProperties)
$input3 = $this->getSession()->getPage()->find('css', '.elements input.input.third');
$event = $this->getSession()->getPage()->find('css', '.elements .text-event');

$this->assertNotNull($input1);
$this->assertNotNull($input2);
$this->assertNotNull($input3);
$this->assertNotNull($event);

$input1->keyDown('u', $modifier);
$this->assertEquals('key downed:' . $eventProperties, $event->getText());

Expand All @@ -219,11 +230,14 @@ public function testWait()
{
$this->getSession()->visit($this->pathTo('/js_test.php'));

$this->getSession()->getPage()->findById('waitable')->click();
$waitable = $this->getSession()->getPage()->findById('waitable');
$this->assertNotNull($waitable);

$waitable->click();
$this->getSession()->wait(3000, '$("#waitable").has("div").length > 0');
$this->assertEquals('arrived', $this->getSession()->getPage()->find('css', '#waitable > div')->getText());

$this->getSession()->getPage()->findById('waitable')->click();
$waitable->click();
$this->getSession()->wait(3000, 'false');
$this->assertEquals('timeout', $this->getSession()->getPage()->find('css', '#waitable > div')->getText());
}
Expand All @@ -235,6 +249,9 @@ public function testVisibility()
$clicker = $this->getSession()->getPage()->find('css', '.elements div#clicker');
$invisible = $this->getSession()->getPage()->find('css', '#invisible');

$this->assertNotNull($clicker);
$this->assertNotNull($invisible);

$this->assertFalse($invisible->isVisible());
$this->assertTrue($clicker->isVisible());
}
Expand All @@ -246,6 +263,9 @@ public function testDragDrop()
$draggable = $this->getSession()->getPage()->find('css', '#draggable');
$droppable = $this->getSession()->getPage()->find('css', '#droppable');

$this->assertNotNull($draggable);
$this->assertNotNull($droppable);

$draggable->dragTo($droppable);
$this->assertEquals('Dropped!', $droppable->find('css', 'p')->getText());
}
Expand Down Expand Up @@ -297,6 +317,7 @@ public function testExecuteScript($script)
sleep(1);

$heading = $this->getSession()->getPage()->find('css', 'h1');
$this->assertNotNull($heading);
$this->assertEquals('Hello world', $heading->getText());
}

Expand Down

0 comments on commit 53b48cb

Please sign in to comment.