PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/test/PHPWebdriverTest.php

http://php-webdriver-bindings.googlecode.com/
PHP | 282 lines | 214 code | 58 blank | 10 comment | 6 complexity | c0ffbb3f16ac1e2ddaebc5787a99e20d MD5 | raw file
  1. <?php
  2. if(is_file('../definedVars.php')) require_once '../definedVars.php';
  3. require_once 'phpwebdriver/WebDriver.php';
  4. /**
  5. *
  6. * @author kolec
  7. * @version 1.0
  8. * @property WebDriver $webdriver
  9. */
  10. class PHPWebDriverTest extends PHPUnit_Framework_TestCase {
  11. protected function setUp() {
  12. $this->webdriver = new WebDriver("localhost", 4444);
  13. $this->webdriver->connect("firefox");
  14. }
  15. protected function tearDown() {
  16. $this->webdriver->close();
  17. }
  18. public function testFileUpload() {
  19. $this->webdriver->get(TEST_URL);
  20. $element = $this->webdriver->findElementBy(LocatorStrategy::id, "file1");
  21. $this->assertNotNull($element);
  22. $element->sendKeys(array(EG_FILE_DIRECTORY . EG_FILE_NAME));
  23. $element->submit();
  24. sleep(1);
  25. $this->assertTrue($this->isTextPresent(EG_FILE_NAME));
  26. }
  27. public function testBackAndForward() {
  28. $this->webdriver->get(TEST_URL);
  29. sleep(1);
  30. $element = $this->webdriver->findElementBy(LocatorStrategy::linkText, "say hello (javascript)");
  31. $this->assertNotNull($element);
  32. $this->webdriver->get('http://www.3e.pl');
  33. sleep(1);
  34. $element = $this->webdriver->findElementBy(LocatorStrategy::linkText, "O Nas");
  35. $this->assertNotNull($element);
  36. $this->webdriver->back();
  37. sleep(1);
  38. $element = $this->webdriver->findElementBy(LocatorStrategy::linkText, "say hello (javascript)");
  39. $this->assertNotNull($element);
  40. $this->webdriver->forward();
  41. sleep(1);
  42. $element = $this->webdriver->findElementBy(LocatorStrategy::linkText, "O Nas");
  43. $this->assertNotNull($element);
  44. }
  45. public function testCssProperty() {
  46. $this->webdriver->get(TEST_URL);
  47. $element = $this->webdriver->findElementBy(LocatorStrategy::id, "prod_name");
  48. $this->assertNotNull($element);
  49. $element->sendKeys(array("selenium 123"));
  50. $this->assertEquals($element->getValue(), "selenium 123");
  51. $element->submit();
  52. $elementResult = $this->webdriver->findElementBy(LocatorStrategy::id, "result1");
  53. $this->assertNotNull($elementResult);
  54. $cssProperty = $elementResult->getCssProperty('background-color');
  55. $this->assertEquals($cssProperty, "#008000");
  56. }
  57. public function testElementIsDisplayedAndItsSize() {
  58. $this->webdriver->get(TEST_URL);
  59. $element = $this->webdriver->findElementBy(LocatorStrategy::id, "prod_name");
  60. $this->assertNotNull($element);
  61. $this->assertTrue($element->isDisplayed());
  62. $elementSize = $element->getSize();
  63. $this->assertNotNull($elementSize);
  64. $this->assertEquals(266, $elementSize->width);
  65. $this->assertEquals(22, $elementSize->height);
  66. }
  67. public function testElementLocations() {
  68. $this->webdriver->get(TEST_URL);
  69. $element = $this->webdriver->findElementBy(LocatorStrategy::id, "prod_name");
  70. $this->assertNotNull($element);
  71. $location = $element->getLocation();
  72. $this->assertNotNull($location);
  73. $this->assertEquals(98, $location->x);
  74. $this->assertEquals(8, $location->y);
  75. $locationInView = $element->getLocationInView();
  76. $this->assertNotNull($locationInView);
  77. $this->assertEquals(102, $locationInView->x);
  78. $this->assertEquals(12, $locationInView->y);
  79. }
  80. public function testIsOtherId() {
  81. $this->webdriver->get(TEST_URL);
  82. $element = $this->webdriver->findElementBy(LocatorStrategy::id, "prod_name");
  83. $this->assertNotNull($element);
  84. $result = $element->isOtherId('sel1');
  85. $this->assertFalse($result);
  86. }
  87. public function testAlerts() {
  88. $this->webdriver->get(TEST_URL);
  89. $element = $this->webdriver->findElementBy(LocatorStrategy::linkText, "say hello (javascript)");
  90. $this->assertNotNull($element);
  91. $element->click();
  92. $this->assertTrue($this->webdriver->getAlertText()=="hello computer !!!");
  93. $this->webdriver->acceptAlert();
  94. sleep(4);
  95. }
  96. public function testCookieSupport() {
  97. $this->webdriver->get(TEST_URL);
  98. $this->webdriver->setCookie('aaa','testvalue');
  99. $cookies = $this->webdriver->getAllCookies();
  100. $this->assertTrue(count($cookies)==1);
  101. $this->assertTrue($cookies[0]->name=='aaa');
  102. $this->assertTrue($cookies[0]->value=='testvalue');
  103. $this->webdriver->deleteCookie('aaa');
  104. $cookies = $this->webdriver->getAllCookies();
  105. $this->assertTrue(count($cookies)==0);
  106. }
  107. public function testFindOptionElementInCombobox() {
  108. $this->webdriver->get(TEST_URL);
  109. $element = $this->webdriver->findElementBy(LocatorStrategy::name, "sel1");
  110. $this->assertNotNull($element);
  111. $option3 = $element->findOptionElementByText("option 3");
  112. $this->assertNotNull($option3);
  113. $this->assertEquals($option3->getText(), "option 3");
  114. $this->assertFalse($option3->isSelected());
  115. $option3->click();
  116. $this->assertTrue($option3->isSelected());
  117. $option2 = $element->findOptionElementByValue("2");
  118. $this->assertNotNull($option2);
  119. $this->assertEquals($option2->getText(), "option 2");
  120. $this->assertFalse($option2->isSelected());
  121. $option2->click();
  122. $this->assertFalse($option3->isSelected());
  123. $this->assertTrue($option2->isSelected());
  124. }
  125. public function testExecute() {
  126. $this->webdriver->get(TEST_URL);
  127. $result = $this->webdriver->executeScript("return sayHello('unitTest')", array());
  128. $this->assertEquals("hello unitTest !!!", $result);
  129. }
  130. public function testScreenShot() {
  131. $this->webdriver->get(TEST_URL);
  132. $tmp_filename = "screenshot".uniqid().".png";
  133. //unlink($tmp_filename);
  134. $result = $this->webdriver->getScreenshotAndSaveToFile($tmp_filename);
  135. $this->assertTrue(file_exists($tmp_filename));
  136. $this->assertTrue(filesize($tmp_filename)>100);
  137. unlink($tmp_filename);
  138. }
  139. /**
  140. * @expectedException WebDriverException
  141. */
  142. public function testHandleError() {
  143. $this->webdriver->get(TEST_URL);
  144. $element = $this->webdriver->findElementBy(LocatorStrategy::name, "12323233233aa");
  145. }
  146. public function testFindElemenInElementAndSelections() {
  147. $this->webdriver->get(TEST_URL);
  148. $element = $this->webdriver->findElementBy(LocatorStrategy::name, "sel1");
  149. $this->assertNotNull($element);
  150. $options = $element->findElementsBy(LocatorStrategy::tagName, "option");
  151. $this->assertNotNull($options);
  152. $this->assertNotNull($options[2]);
  153. $this->assertEquals($options[2]->getText(), "option 3");
  154. $this->assertFalse($options[2]->isSelected());
  155. $options[2]->click();
  156. $this->assertTrue($options[2]->isSelected());
  157. $this->assertFalse($options[0]->isSelected());
  158. }
  159. public function testFindElementByXpath() {
  160. $this->webdriver->get(TEST_URL);
  161. $option3 = $this->webdriver->findElementBy(LocatorStrategy::xpath, '//select[@name="sel1"]/option[normalize-space(text())="option 3"]');
  162. $this->assertNotNull($option3);
  163. $this->assertEquals($option3->getText(), "option 3");
  164. $this->assertFalse($option3->isSelected());
  165. $option3->click();
  166. $this->assertTrue($option3->isSelected());
  167. }
  168. public function testFindElementByAndSubmit() {
  169. $this->webdriver->get(TEST_URL);
  170. $element = $this->webdriver->findElementBy(LocatorStrategy::id, "prod_name");
  171. $this->assertNotNull($element);
  172. $element->sendKeys(array("selenium 123"));
  173. $this->assertEquals($element->getValue(), "selenium 123");
  174. $element->clear();
  175. $this->assertEquals($element->getValue(), "");
  176. $element->sendKeys(array("selenium 123"));
  177. $element->submit();
  178. $element2 = $this->webdriver->findElementBy(LocatorStrategy::id, "result1");
  179. $this->assertNotNull($element2);
  180. }
  181. public function testGetPageAndUrl() {
  182. $this->webdriver->get(TEST_URL);
  183. $this->assertEquals($this->webdriver->getTitle(), "Test page");
  184. $this->assertEquals($this->webdriver->getCurrentUrl(), TEST_URL);
  185. }
  186. public function testGetText() {
  187. $this->webdriver->get(TEST_URL);
  188. $element = $this->webdriver->findElementBy(LocatorStrategy::name, "div1");
  189. $this->assertNotNull($element);
  190. $this->assertEquals($element->getText(), "lorem ipsum");
  191. }
  192. public function testGetName() {
  193. $this->webdriver->get(TEST_URL);
  194. $element = $this->webdriver->findElementBy(LocatorStrategy::name, "div1");
  195. $this->assertNotNull($element);
  196. $this->assertEquals($element->getName(), "div");
  197. }
  198. public function testGetPageSource() {
  199. $this->webdriver->get(TEST_URL);
  200. $src = $this->webdriver->getPageSource();
  201. $this->assertNotNull($src);
  202. $this->assertTrue(strpos($src, "<html>") == 0);
  203. $this->assertTrue(strpos($src, "<body>") > 0);
  204. $this->assertTrue(strpos($src, "div1") > 0);
  205. }
  206. private function isTextPresent($text) {
  207. $waiting_time = 0.5;
  208. $max_waiting_time = 4;
  209. $found = false;
  210. $i = 0;
  211. do {
  212. $html = $this->webdriver->getPageSource();
  213. if (is_string($html)) {
  214. $found = !(strpos($html, $text) === false);
  215. }
  216. if (!$found) {
  217. sleep($waiting_time);
  218. $i += $waiting_time;
  219. }
  220. } while (!$found && $i <= $max_waiting_time);
  221. return $found;
  222. }
  223. }
  224. ?>