TraversingTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace Behat\Mink\Tests\Driver\Basic;
  3. use Behat\Mink\Tests\Driver\TestCase;
  4. class TraversingTest extends TestCase
  5. {
  6. /**
  7. * find by label.
  8. *
  9. * @group issue211
  10. */
  11. public function testIssue211()
  12. {
  13. $this->getSession()->visit($this->pathTo('/issue211.html'));
  14. $field = $this->getSession()->getPage()->findField('Téléphone');
  15. $this->assertNotNull($field);
  16. }
  17. public function testElementsTraversing()
  18. {
  19. $this->getSession()->visit($this->pathTo('/index.html'));
  20. $page = $this->getSession()->getPage();
  21. $title = $page->find('css', 'h1');
  22. $this->assertNotNull($title);
  23. $this->assertEquals('Extremely useless page', $title->getText());
  24. $this->assertEquals('h1', $title->getTagName());
  25. $strong = $page->find('xpath', '//div/strong[3]');
  26. $this->assertNotNull($strong);
  27. $this->assertEquals('pariatur', $strong->getText());
  28. $this->assertEquals('super-duper', $strong->getAttribute('class'));
  29. $this->assertTrue($strong->hasAttribute('class'));
  30. $strong2 = $page->find('xpath', '//div/strong[2]');
  31. $this->assertNotNull($strong2);
  32. $this->assertEquals('veniam', $strong2->getText());
  33. $this->assertEquals('strong', $strong2->getTagName());
  34. $this->assertNull($strong2->getAttribute('class'));
  35. $this->assertFalse($strong2->hasAttribute('class'));
  36. $strongs = $page->findAll('css', 'div#core > strong');
  37. $this->assertCount(3, $strongs);
  38. $this->assertEquals('Lorem', $strongs[0]->getText());
  39. $this->assertEquals('pariatur', $strongs[2]->getText());
  40. $element = $page->find('css', '#some-element');
  41. $this->assertNotNull($element);
  42. $this->assertEquals('some very interesting text', $element->getText());
  43. $this->assertEquals(
  44. "\n some <div>very\n </div>\n".
  45. "<em>interesting</em> text\n ",
  46. $element->getHtml()
  47. );
  48. $this->assertTrue($element->hasAttribute('data-href'));
  49. $this->assertFalse($element->hasAttribute('data-url'));
  50. $this->assertEquals('http://mink.behat.org', $element->getAttribute('data-href'));
  51. $this->assertNull($element->getAttribute('data-url'));
  52. $this->assertEquals('div', $element->getTagName());
  53. }
  54. public function testVeryDeepElementsTraversing()
  55. {
  56. $this->getSession()->visit($this->pathTo('/index.html'));
  57. $page = $this->getSession()->getPage();
  58. $footer = $page->find('css', 'footer');
  59. $this->assertNotNull($footer);
  60. $searchForm = $footer->find('css', 'form#search-form');
  61. $this->assertNotNull($searchForm);
  62. $this->assertEquals('search-form', $searchForm->getAttribute('id'));
  63. $searchInput = $searchForm->findField('Search site...');
  64. $this->assertNotNull($searchInput);
  65. $this->assertEquals('text', $searchInput->getAttribute('type'));
  66. $searchInput = $searchForm->findField('Search site...');
  67. $this->assertNotNull($searchInput);
  68. $this->assertEquals('text', $searchInput->getAttribute('type'));
  69. $profileForm = $footer->find('css', '#profile');
  70. $this->assertNotNull($profileForm);
  71. $profileFormDiv = $profileForm->find('css', 'div');
  72. $this->assertNotNull($profileFormDiv);
  73. $profileFormDivLabel = $profileFormDiv->find('css', 'label');
  74. $this->assertNotNull($profileFormDivLabel);
  75. $profileFormDivParent = $profileFormDivLabel->getParent();
  76. $this->assertNotNull($profileFormDivParent);
  77. $profileFormDivParent = $profileFormDivLabel->getParent();
  78. $this->assertEquals('something', $profileFormDivParent->getAttribute('data-custom'));
  79. $profileFormInput = $profileFormDivLabel->findField('user-name');
  80. $this->assertNotNull($profileFormInput);
  81. $this->assertEquals('username', $profileFormInput->getAttribute('name'));
  82. }
  83. public function testDeepTraversing()
  84. {
  85. $this->getSession()->visit($this->pathTo('/index.html'));
  86. $traversDivs = $this->getSession()->getPage()->findAll('css', 'div.travers');
  87. $this->assertCount(1, $traversDivs);
  88. $subDivs = $traversDivs[0]->findAll('css', 'div.sub');
  89. $this->assertCount(3, $subDivs);
  90. $this->assertTrue($subDivs[2]->hasLink('some deep url'));
  91. $this->assertFalse($subDivs[2]->hasLink('come deep url'));
  92. $subUrl = $subDivs[2]->findLink('some deep url');
  93. $this->assertNotNull($subUrl);
  94. $this->assertRegExp('/some_url$/', $subUrl->getAttribute('href'));
  95. $this->assertEquals('some deep url', $subUrl->getText());
  96. $this->assertEquals('some <strong>deep</strong> url', $subUrl->getHtml());
  97. $this->assertTrue($subUrl->has('css', 'strong'));
  98. $this->assertFalse($subUrl->has('css', 'em'));
  99. $this->assertEquals('deep', $subUrl->find('css', 'strong')->getText());
  100. }
  101. public function testFindingChild()
  102. {
  103. $this->getSession()->visit($this->pathTo('/index.html'));
  104. $form = $this->getSession()->getPage()->find('css', 'footer form');
  105. $this->assertNotNull($form);
  106. $this->assertCount(1, $form->findAll('css', 'input'), 'Elements are searched only in the element, not in all previous matches');
  107. }
  108. }