Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. class WebTestCase extends PHPUnit_Extensions_Selenium2TestCase
  2. {
  3. protected $captureScreenshotOnFailure = TRUE;
  4. protected $screenshotPath = '/../../screenshots';
  5. protected $screenshotUrl = 'http://localhost:8080/screenshots';
  6.  
  7. protected function setUp() {
  8. $this->setBrowser('phantomjs');
  9. $this->setBrowserUrl("http://localhost:8080");
  10. $this->setHost('localhost');
  11. }
  12. }
  13.  
  14. class Test extends WebTestCase
  15. {
  16.  
  17. public function testTitle()
  18. {
  19. $this->url('');
  20. assertEquals($this->title(), "My App");
  21. }
  22. }
  23.  
  24. $ vendor/bin/phpunit
  25. PHPUnit 4.6-ge85198b by Sebastian Bergmann and contributors.
  26.  
  27. Configuration read from /MyApp/phpunit.xml
  28.  
  29. F
  30.  
  31. Time: 231 ms, Memory: 5.50Mb
  32.  
  33. There was 1 failure:
  34.  
  35. 1) Test::testTitle
  36. Failed asserting that two strings are equal.
  37. --- Expected
  38. +++ Actual
  39. @@ @@
  40. -''
  41. +'My App'
  42.  
  43. /MyApp/tests/functional/Test.php:7
  44.  
  45. FAILURES!
  46. Tests: 1, Assertions: 1, Failures: 1.
  47.  
  48. <?php
  49. class Test extends PHPUnit_Extensions_Selenium2TestCase
  50. {
  51.  
  52. protected function setUp() {
  53. $this->setBrowser('phantomjs');
  54. $this->setBrowserUrl("http://localhost:8080");
  55. $this->setHost('localhost');
  56. }
  57.  
  58. public function testEnterText()
  59. {
  60. $this->url("/");
  61.  
  62. try {
  63.  
  64. $this->assertEquals($this->title(), "My App");
  65.  
  66. } catch (Exception $e) {
  67.  
  68. $this->screenshot( __DIR__.'/'.$this->getName().'-'.time(). '.png');
  69. }
  70. }
  71.  
  72. public function screenshot($file)
  73. {
  74. $filedata = $this->currentScreenshot();
  75. file_put_contents($file, $filedata);
  76. }
  77. }
  78.  
  79. <?php
  80.  
  81. class homepageTest extends PHPUnit_Extensions_Selenium2TestCase {
  82.  
  83. private $listener;
  84.  
  85. public function setUp() {
  86.  
  87. // Your screenshots will be saved in '/var/www/vhosts/screenshots/'
  88. $screenshots_dir = '/var/www/vhosts/screenshots/';
  89.  
  90. $this->listener = new PHPUnit_Extensions_Selenium2TestCase_ScreenshotListener($screenshots_dir);
  91.  
  92. $this->setBrowser('firefox');
  93. $this->setBrowserUrl('https://netbeans.org');
  94. }
  95.  
  96. public function tearDown() {
  97. $this->stop();
  98. }
  99.  
  100. public function testNetbeansContainsHorses() {
  101. $this->url('https://netbeans.org');
  102. $this->assertContains('Equestrian', $this->title()); // Will fail on NetBeans page.
  103. }
  104.  
  105.  
  106. public function onNotSuccessfulTest($e) {
  107. $this->listener->addError($this, $e, null);
  108. parent::onNotSuccessfulTest($e);
  109. }
  110. }
  111.  
  112. class MyBaseWebTests
  113. {
  114.  
  115. $this->directory = '/some_path_to_put_screenshots_in/';
  116.  
  117. // Override PHPUnit_Extensions_Selenium2TestCase::onNotSuccessfulTest
  118. public function onNotSuccessfulTest(Exception $e)
  119. {
  120. $filedata = $this->currentScreenshot();
  121. $file = $this->directory . get_class($this) . '.png';
  122. file_put_contents($file, $filedata);
  123.  
  124. parent::onNotSuccessfulTest($e);
  125. }
  126. }
  127.  
  128. $fp = fopen('path/35.png', 'wb');
  129. fwrite($fp, $this->currentScreenshot());
  130. fclose($fp);
  131. sleep(1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement