LocatorTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. use Codeception\Util\Locator;
  3. use Facebook\WebDriver\WebDriverBy;
  4. class LocatorTest extends \PHPUnit\Framework\TestCase
  5. {
  6. public function testCombine()
  7. {
  8. $result = Locator::combine('//button[@value="Click Me"]', '//a[.="Click Me"]');
  9. $this->assertEquals('//button[@value="Click Me"] | //a[.="Click Me"]', $result);
  10. $result = Locator::combine('button[value="Click Me"]', '//a[.="Click Me"]');
  11. $this->assertEquals('descendant-or-self::button[@value = \'Click Me\'] | //a[.="Click Me"]', $result);
  12. $xml = new SimpleXMLElement("<root><button value='Click Me' /></root>");
  13. $this->assertNotEmpty($xml->xpath($result));
  14. $xml = new SimpleXMLElement("<root><a href='#'>Click Me</a></root>");
  15. $this->assertNotEmpty($xml->xpath($result));
  16. }
  17. public function testHref()
  18. {
  19. $xml = new SimpleXMLElement("<root><a href='/logout'>Click Me</a></root>");
  20. $this->assertNotEmpty($xml->xpath(Locator::href('/logout')));
  21. }
  22. public function testTabIndex()
  23. {
  24. $xml = new SimpleXMLElement("<root><a href='#' tabindex='2'>Click Me</a></root>");
  25. $this->assertNotEmpty($xml->xpath(Locator::tabIndex(2)));
  26. }
  27. public function testFind()
  28. {
  29. $xml = new SimpleXMLElement("<root><a href='#' tabindex='2'>Click Me</a></root>");
  30. $this->assertNotEmpty($xml->xpath(Locator::find('a', array('href' => '#'))));
  31. $this->assertNotEmpty($xml->xpath(Locator::find('a', array('href', 'tabindex' => '2'))));
  32. }
  33. public function testIsXPath()
  34. {
  35. $this->assertTrue(Locator::isXPath("//hr[@class='edge' and position()=1]"));
  36. $this->assertFalse(Locator::isXPath("and position()=1]"));
  37. $this->assertTrue(Locator::isXPath('//table[parent::div[@class="pad"] and not(@id)]//a'));
  38. }
  39. public function testIsId()
  40. {
  41. $this->assertTrue(Locator::isID('#username'));
  42. $this->assertTrue(Locator::isID('#user.name'));
  43. $this->assertTrue(Locator::isID('#user-name'));
  44. $this->assertFalse(Locator::isID('#user-name .field'));
  45. $this->assertFalse(Locator::isID('.field'));
  46. $this->assertFalse(Locator::isID('hello'));
  47. }
  48. public function testIsClass()
  49. {
  50. $this->assertTrue(Locator::isClass('.username'));
  51. $this->assertTrue(Locator::isClass('.name'));
  52. $this->assertTrue(Locator::isClass('.user-name'));
  53. $this->assertFalse(Locator::isClass('.user-name .field'));
  54. $this->assertFalse(Locator::isClass('#field'));
  55. $this->assertFalse(Locator::isClass('hello'));
  56. }
  57. public function testContains()
  58. {
  59. $this->assertEquals(
  60. 'descendant-or-self::label[contains(., \'enter a name\')]',
  61. Locator::contains('label', 'enter a name')
  62. );
  63. $this->assertEquals(
  64. 'descendant-or-self::label[@id = \'user\'][contains(., \'enter a name\')]',
  65. Locator::contains('label#user', 'enter a name')
  66. );
  67. $this->assertEquals(
  68. '//label[@for="name"][contains(., \'enter a name\')]',
  69. Locator::contains('//label[@for="name"]', 'enter a name')
  70. );
  71. }
  72. public function testHumanReadableString()
  73. {
  74. $this->assertEquals("'string selector'", Locator::humanReadableString("string selector"));
  75. $this->assertEquals("css '.something'", Locator::humanReadableString(['css' => '.something']));
  76. $this->assertEquals(
  77. "css selector '.something'",
  78. Locator::humanReadableString(WebDriverBy::cssSelector('.something'))
  79. );
  80. try {
  81. Locator::humanReadableString(null);
  82. $this->fail("Expected exception when calling humanReadableString() with invalid selector");
  83. } catch (\InvalidArgumentException $e) {
  84. }
  85. }
  86. public function testLocatingElementPosition()
  87. {
  88. $this->assertEquals('(descendant-or-self::p)[position()=1]', Locator::firstElement('p'));
  89. $this->assertEquals('(descendant-or-self::p)[position()=last()]', Locator::lastElement('p'));
  90. $this->assertEquals('(descendant-or-self::p)[position()=1]', Locator::elementAt('p', 1));
  91. $this->assertEquals('(descendant-or-self::p)[position()=last()-0]', Locator::elementAt('p', -1));
  92. $this->assertEquals('(descendant-or-self::p)[position()=last()-1]', Locator::elementAt('p', -2));
  93. }
  94. }