Permalink
Browse files

Replaced most null safeguards in tests with usages of WebAssert

The assertNotNull calls are kept in 2 cases:
- in the TraversingTest as they are the real assertions of the tests
- in case findByID() or findButton() are used, as these shortcut are more
  readable than calling elementExists() with a named selector, especially
  considering the fact that it would require applying the XPath escaping
  manually in these places.
  • Loading branch information...
1 parent 8d8b852 commit 4f16fa08ee435551dca2bf4e5a309cf82cf1f4bd @stof stof committed May 19, 2014
@@ -10,10 +10,7 @@ public function testOuterHtml()
{
$this->getSession()->visit($this->pathTo('/index.html'));
- $page = $this->getSession()->getPage();
- $element = $page->find('css', '.travers');
-
- $this->assertNotNull($element);
+ $element = $this->getAssertSession()->elementExists('css', '.travers');
$this->assertEquals(
"<div class=\"travers\">\n <div class=\"sub\">el1</div>\n".
@@ -59,14 +56,12 @@ public function testJson()
public function testHtmlDecodingNotPerformed()
{
$session = $this->getSession();
+ $webAssert = $this->getAssertSession();
$session->visit($this->pathTo('/html_decoding.html'));
$page = $session->getPage();
- $span = $page->find('css', 'span');
- $input = $page->find('css', 'input');
-
- $this->assertNotNull($span);
- $this->assertNotNull($input);
+ $span = $webAssert->elementExists('css', 'span');
+ $input = $webAssert->elementExists('css', 'input');
$expectedHtml = '<span custom-attr="&amp;">some text</span>';
$this->assertContains($expectedHtml, $page->getHtml(), '.innerHTML is returned as-is');
@@ -148,19 +148,20 @@ public function testHttpOnlyCookieIsDeleted()
public function testSessionPersistsBetweenRequests()
{
$this->getSession()->visit($this->pathTo('/session_test.php'));
- $this->assertNotNull($node = $this->getSession()->getPage()->find('css', '#session-id'));
+ $webAssert = $this->getAssertSession();
+ $node = $webAssert->elementExists('css', '#session-id');
$sessionId = $node->getText();
$this->getSession()->visit($this->pathTo('/session_test.php'));
- $this->assertNotNull($node = $this->getSession()->getPage()->find('css', '#session-id'));
+ $node = $webAssert->elementExists('css', '#session-id');
$this->assertEquals($sessionId, $node->getText());
$this->getSession()->visit($this->pathTo('/session_test.php?login'));
- $this->assertNotNull($node = $this->getSession()->getPage()->find('css', '#session-id'));
+ $node = $webAssert->elementExists('css', '#session-id');
$this->assertNotEquals($sessionId, $newSessionId = $node->getText());
$this->getSession()->visit($this->pathTo('/session_test.php'));
- $this->assertNotNull($node = $this->getSession()->getPage()->find('css', '#session-id'));
+ $node = $webAssert->elementExists('css', '#session-id');
$this->assertEquals($newSessionId, $node->getText());
}
}
@@ -9,22 +9,19 @@ class IFrameTest extends TestCase
public function testIFrame()
{
$this->getSession()->visit($this->pathTo('/iframe.html'));
- $page = $this->getSession()->getPage();
+ $webAssert = $this->getAssertSession();
- $el = $page->find('css', '#text');
- $this->assertNotNull($el);
+ $el = $webAssert->elementExists('css', '#text');
$this->assertSame('Main window div text', $el->getText());
$this->getSession()->switchToIFrame('subframe');
- $el = $page->find('css', '#text');
- $this->assertNotNull($el);
+ $el = $webAssert->elementExists('css', '#text');
$this->assertSame('iFrame div text', $el->getText());
$this->getSession()->switchToIFrame();
- $el = $page->find('css', '#text');
- $this->assertNotNull($el);
+ $el = $webAssert->elementExists('css', '#text');
$this->assertSame('Main window div text', $el->getText());
}
}
@@ -9,12 +9,10 @@ class VisibilityTest extends TestCase
public function testVisibility()
{
$this->getSession()->visit($this->pathTo('/js_test.html'));
+ $webAssert = $this->getAssertSession();
- $clicker = $this->getSession()->getPage()->find('css', '.elements div#clicker');
- $invisible = $this->getSession()->getPage()->find('css', '#invisible');
-
- $this->assertNotNull($clicker);
- $this->assertNotNull($invisible);
+ $clicker = $webAssert->elementExists('css', '.elements div#clicker');
+ $invisible = $webAssert->elementExists('css', '#invisible');
$this->assertFalse($invisible->isVisible());
$this->assertTrue($clicker->isVisible());
@@ -49,15 +49,12 @@ public function testSetValue()
public function testCheckboxMultiple()
{
$this->getSession()->visit($this->pathTo('/multicheckbox_form.html'));
+ $webAssert = $this->getAssertSession();
- $page = $this->getSession()->getPage();
- $this->assertEquals('Multicheckbox Test', $page->find('css', 'h1')->getText());
+ $this->assertEquals('Multicheckbox Test', $webAssert->elementExists('css', 'h1')->getText());
- $updateMail = $page->find('css', '[name="mail_types[]"][value="update"]');
- $spamMail = $page->find('css', '[name="mail_types[]"][value="spam"]');
-
- $this->assertNotNull($updateMail);
- $this->assertNotNull($spamMail);
+ $updateMail = $webAssert->elementExists('css', '[name="mail_types[]"][value="update"]');
+ $spamMail = $webAssert->elementExists('css', '[name="mail_types[]"][value="spam"]');
$this->assertTrue($updateMail->getValue());
$this->assertFalse($spamMail->getValue());
@@ -23,14 +23,12 @@ public function testBasicForm()
{
$this->getSession()->visit($this->pathTo('/basic_form.html'));
+ $webAssert = $this->getAssertSession();
$page = $this->getSession()->getPage();
- $this->assertEquals('Basic Form Page', $page->find('css', 'h1')->getText());
+ $this->assertEquals('Basic Form Page', $webAssert->elementExists('css', 'h1')->getText());
- $firstname = $page->findField('first_name');
- $lastname = $page->findField('lastn');
-
- $this->assertNotNull($firstname);
- $this->assertNotNull($lastname);
+ $firstname = $webAssert->fieldExists('first_name');
+ $lastname = $webAssert->fieldExists('lastn');
$this->assertEquals('Firstname', $firstname->getValue());
$this->assertEquals('Lastname', $lastname->getValue());
@@ -52,9 +50,9 @@ public function testBasicForm()
$page->findButton('Save')->click();
if ($this->safePageWait(5000, 'document.getElementById("first") !== null')) {
- $this->assertEquals('Anket for Konstantin', $page->find('css', 'h1')->getText());
- $this->assertEquals('Firstname: Konstantin', $page->find('css', '#first')->getText());
- $this->assertEquals('Lastname: Kudryashov', $page->find('css', '#last')->getText());
+ $this->assertEquals('Anket for Konstantin', $webAssert->elementExists('css', 'h1')->getText());
+ $this->assertEquals('Firstname: Konstantin', $webAssert->elementExists('css', '#first')->getText());
+ $this->assertEquals('Lastname: Kudryashov', $webAssert->elementExists('css', '#last')->getText());
}
}
@@ -66,15 +64,15 @@ public function testFormSubmitWays($submitVia)
$session = $this->getSession();
$session->visit($this->pathTo('/basic_form.html'));
$page = $session->getPage();
+ $webAssert = $this->getAssertSession();
- $firstname = $page->findField('first_name');
- $this->assertNotNull($firstname);
+ $firstname = $webAssert->fieldExists('first_name');
$firstname->setValue('Konstantin');
$page->findButton($submitVia)->click();
if ($this->safePageWait(5000, 'document.getElementById("first") !== null')) {
- $this->assertEquals('Firstname: Konstantin', $page->find('css', '#first')->getText());
+ $this->assertEquals('Firstname: Konstantin', $webAssert->elementExists('css', '#first')->getText());
} else {
$this->fail('Form was never submitted');
}
@@ -95,13 +93,13 @@ public function testFormSubmit()
$session = $this->getSession();
$session->visit($this->pathTo('/basic_form.html'));
- $page = $session->getPage();
- $page->findField('first_name')->setValue('Konstantin');
+ $webAssert = $this->getAssertSession();
+ $webAssert->fieldExists('first_name')->setValue('Konstantin');
- $page->find('xpath', 'descendant-or-self::form[1]')->submit();
+ $webAssert->elementExists('xpath', 'descendant-or-self::form[1]')->submit();
if ($this->safePageWait(5000, 'document.getElementById("first") !== null')) {
- $this->assertEquals('Firstname: Konstantin', $page->find('css', '#first')->getText());
+ $this->assertEquals('Firstname: Konstantin', $webAssert->elementExists('css', '#first')->getText());
};
}
@@ -110,29 +108,29 @@ public function testFormSubmitWithoutButton()
$session = $this->getSession();
$session->visit($this->pathTo('/form_without_button.html'));
- $page = $session->getPage();
- $page->findField('first_name')->setValue('Konstantin');
+ $webAssert = $this->getAssertSession();
+ $webAssert->fieldExists('first_name')->setValue('Konstantin');
- $page->find('xpath', 'descendant-or-self::form[1]')->submit();
+ $webAssert->elementExists('xpath', 'descendant-or-self::form[1]')->submit();
if ($this->safePageWait(5000, 'document.getElementById("first") !== null')) {
- $this->assertEquals('Firstname: Konstantin', $page->find('css', '#first')->getText());
+ $this->assertEquals('Firstname: Konstantin', $webAssert->elementExists('css', '#first')->getText());
};
}
public function testBasicGetForm()
{
$this->getSession()->visit($this->pathTo('/basic_get_form.php'));
+ $webAssert = $this->getAssertSession();
$page = $this->getSession()->getPage();
- $this->assertEquals('Basic Get Form Page', $page->find('css', 'h1')->getText());
+ $this->assertEquals('Basic Get Form Page', $webAssert->elementExists('css', 'h1')->getText());
- $search = $page->findField('q');
- $this->assertNotNull($search);
+ $search = $webAssert->fieldExists('q');
$search->setValue('some#query');
$page->pressButton('Find');
- $this->assertNotNull($div = $page->find('css', 'div'));
+ $div = $webAssert->elementExists('css', 'div');
$this->assertEquals('some#query', $div->getText());
}
@@ -150,27 +148,19 @@ public function testAdvancedForm()
$this->getSession()->visit($this->pathTo('/advanced_form.html'));
+ $webAssert = $this->getAssertSession();
$page = $this->getSession()->getPage();
- $this->assertEquals('ADvanced Form Page', $page->find('css', 'h1')->getText());
-
- $firstname = $page->findField('first_name');
- $lastname = $page->findField('lastn');
- $email = $page->findField('Your email:');
- $select = $page->findField('select_number');
- $sex = $page->findField('sex');
- $maillist = $page->findField('mail_list');
- $agreement = $page->findField('agreement');
- $notes = $page->findField('notes');
- $about = $page->findField('about');
-
- $this->assertNotNull($firstname);
- $this->assertNotNull($lastname);
- $this->assertNotNull($email);
- $this->assertNotNull($select);
- $this->assertNotNull($sex);
- $this->assertNotNull($maillist);
- $this->assertNotNull($agreement);
- $this->assertNotNull($notes);
+ $this->assertEquals('ADvanced Form Page', $webAssert->elementExists('css', 'h1')->getText());
+
+ $firstname = $webAssert->fieldExists('first_name');
+ $lastname = $webAssert->fieldExists('lastn');
+ $email = $webAssert->fieldExists('Your email:');
+ $select = $webAssert->fieldExists('select_number');
+ $sex = $webAssert->fieldExists('sex');
+ $maillist = $webAssert->fieldExists('mail_list');
+ $agreement = $webAssert->fieldExists('agreement');
+ $notes = $webAssert->fieldExists('notes');
+ $about = $webAssert->fieldExists('about');
$this->assertEquals('Firstname', $firstname->getValue());
$this->assertEquals('Lastname', $lastname->getValue());
@@ -236,15 +226,12 @@ public function testMultiInput()
{
$this->getSession()->visit($this->pathTo('/multi_input_form.html'));
$page = $this->getSession()->getPage();
- $this->assertEquals('Multi input Test', $page->find('css', 'h1')->getText());
-
- $first = $page->findField('First');
- $second = $page->findField('Second');
- $third = $page->findField('Third');
+ $webAssert = $this->getAssertSession();
+ $this->assertEquals('Multi input Test', $webAssert->elementExists('css', 'h1')->getText());
- $this->assertNotNull($first);
- $this->assertNotNull($second);
- $this->assertNotNull($third);
+ $first = $webAssert->fieldExists('First');
+ $second = $webAssert->fieldExists('Second');
+ $third = $webAssert->fieldExists('Third');
$this->assertEquals('tag1', $first->getValue());
$this->assertSame('tag2', $second->getValue());
@@ -10,12 +10,10 @@ public function testHtml5FormInputAttribute()
{
$this->getSession()->visit($this->pathTo('/html5_form.html'));
$page = $this->getSession()->getPage();
+ $webAssert = $this->getAssertSession();
- $firstName = $page->findField('first_name');
- $lastName = $page->findField('last_name');
-
- $this->assertNotNull($firstName);
- $this->assertNotNull($lastName);
+ $firstName = $webAssert->fieldExists('first_name');
+ $lastName = $webAssert->fieldExists('last_name');
$this->assertEquals('not set', $lastName->getValue());
$firstName->setValue('John');
@@ -63,12 +61,10 @@ public function testHtml5FormButtonAttribute()
{
$this->getSession()->visit($this->pathTo('/html5_form.html'));
$page = $this->getSession()->getPage();
+ $webAssert = $this->getAssertSession();
- $firstName = $page->findField('first_name');
- $lastName = $page->findField('last_name');
-
- $this->assertNotNull($firstName);
- $this->assertNotNull($lastName);
+ $firstName = $webAssert->fieldExists('first_name');
+ $lastName = $webAssert->fieldExists('last_name');
$firstName->setValue('John');
$lastName->setValue('Doe');
@@ -90,11 +86,7 @@ public function testHtml5FormOutside()
$this->getSession()->visit($this->pathTo('/html5_form.html'));
$page = $this->getSession()->getPage();
- $field = $page->findField('other_field');
-
- $this->assertNotNull($field);
-
- $field->setValue('hello');
+ $page->fillField('other_field', 'hello');
$page->pressButton('Submit separate form');
@@ -9,16 +9,13 @@ class SelectTest extends TestCase
public function testMultiselect()
{
$this->getSession()->visit($this->pathTo('/multiselect_form.html'));
+ $webAssert = $this->getAssertSession();
$page = $this->getSession()->getPage();
- $this->assertEquals('Multiselect Test', $page->find('css', 'h1')->getText());
+ $this->assertEquals('Multiselect Test', $webAssert->elementExists('css', 'h1')->getText());
- $select = $page->findField('select_number');
- $multiSelect = $page->findField('select_multiple_numbers[]');
- $secondMultiSelect = $page->findField('select_multiple_values[]');
-
- $this->assertNotNull($select);
- $this->assertNotNull($multiSelect);
- $this->assertNotNull($secondMultiSelect);
+ $select = $webAssert->fieldExists('select_number');
+ $multiSelect = $webAssert->fieldExists('select_multiple_numbers[]');
+ $secondMultiSelect = $webAssert->fieldExists('select_multiple_values[]');
$this->assertEquals('20', $select->getValue());
$this->assertSame(array(), $multiSelect->getValue());
@@ -65,13 +62,12 @@ public function testMultiselect()
public function testElementSelectedStateCheck($selectName, $optionValue, $optionText)
{
$session = $this->getSession();
+ $webAssert = $this->getAssertSession();
$session->visit($this->pathTo('/multiselect_form.html'));
- $select = $session->getPage()->findField($selectName);
- $this->assertNotNull($select);
+ $select = $webAssert->fieldExists($selectName);
$optionValueEscaped = $session->getSelectorsHandler()->xpathLiteral($optionValue);
- $option = $select->find('named', array('option', $optionValueEscaped));
- $this->assertNotNull($option);
+ $option = $webAssert->elementExists('named', array('option', $optionValueEscaped));
$this->assertFalse($option->isSelected());
$select->selectOption($optionText);
@@ -90,8 +86,7 @@ public function testSetValueSingleSelect()
{
$session = $this->getSession();
$session->visit($this->pathTo('/multiselect_form.html'));
- $select = $session->getPage()->findField('select_number');
- $this->assertNotNull($select);
+ $select = $this->getAssertSession()->fieldExists('select_number');
$select->setValue('10');
$this->assertEquals('10', $select->getValue());
@@ -101,8 +96,7 @@ public function testSetValueMultiSelect()
{
$session = $this->getSession();
$session->visit($this->pathTo('/multiselect_form.html'));
- $select = $session->getPage()->findField('select_multiple_values[]');
- $this->assertNotNull($select);
+ $select = $this->getAssertSession()->fieldExists('select_multiple_values[]');
$select->setValue(array('1', '2'));
$this->assertEquals(array('1', '2'), $select->getValue());
Oops, something went wrong.

0 comments on commit 4f16fa0

Please sign in to comment.