Created
May 21, 2015 14:30
-
-
Save omeraslam-vteams/949e5ebfccf2d45ba603 to your computer and use it in GitHub Desktop.
Taking Screen shots when the test fails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AnyTest extends TestConfigs | |
{ | |
/** | |
* @BeforeMethod | |
* SETUP METHOD TO ADD TEST DATA AND INITALIZE THE REMOTEWEBDRIVERS | |
*/ | |
protected function setUp() | |
{ | |
try{ | |
/*INITI BROWSER*/ | |
$this->setupBrowser(); | |
} catch (Exception $ex) { | |
echo $e->getMessage(); | |
} | |
} | |
/** | |
* Function : dependencyManagement | |
* LOAD ALL HELPERS AND MODELS REQUIRED TO ADD | |
* THE TEST DATA INTO DATABASE & RUN THE TESTS | |
*/ | |
protected function dependencyManagement() | |
{ | |
/*LOAD DATABASE*/ | |
$this->CI->load->database(); | |
/*LOAD USER MODEL*/ | |
$this->CI->load->model('user_model'); | |
/*LOAD ACADEMY MODEL */ | |
} | |
/** | |
* FUNCTION:testImageUpload | |
* POSTER UPLOAD TEST FOR COURSE TEASER VIDEO | |
*/ | |
public function testRegistrationCaptcha() | |
{ | |
$errorSnaps = ''; | |
$myBrowserDriver = $this->driver; | |
//open the base url for the site | |
$myBrowserDriver->get(base_url()); | |
//maximize window | |
$myBrowserDriver->manage()->window()->maximize(); | |
//assert if element not found | |
$this->assertElementNotFound(WebDriverBy::id('register_link'), 'Registration Link'); | |
//get the registration link | |
$registerLink = $myBrowserDriver->findElement(WebDriverBy::id('register_link')); | |
//check password field is displayed | |
$this->elementDisplaying($registerLink, 'Registration Link', $myBrowserDriver); | |
$registerLink->click(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*THIS IS THE BASE CLASS I HAVE CREATED FOR MY TESTS*/ | |
abstract class TestConfigs extends PHPUnit_Framework_testCase { | |
function __construct(){ | |
/*GET INSTANCE OF CI*/ | |
$this->CI = &get_instance(); | |
/*LOAD NECESSARY DATABSE MODELS*/ | |
$this->dependencyManagement(); | |
if($this->cleanUpScreenshotDirectory){ | |
$this->clearDirectory(); | |
} | |
} | |
/** | |
* Function : | |
* GET BROWSER FACTORY OBJECT FOR SELENIUM | |
*/ | |
protected function setupBrowser(){ | |
/*INIT REMOTE WEBDRIVER FOR SELENIUM*/ | |
$this->driver = BrowserFactory::factory(); | |
} | |
/** | |
* Function : | |
* CREATE A SCREEN SHOT OF A PAGE OR ELEMENT | |
*/ | |
public function TakeScreenshot($element = null) | |
{ | |
// Change the Path to your own settings | |
$screenshot = dirname(__DIR__) . $this->screenShotDirectoryPath . time() . ".png"; | |
// Change the driver instance | |
$this->driver->takeScreenshot($screenshot); | |
if (!file_exists($screenshot)) { | |
throw new Exception('Could not save screen-shot'); | |
} | |
if (!(bool)$element) { | |
return $screenshot; | |
} | |
$element_screenshot = dirname(__DIR__) . $this->screenShotDirectoryPath . time() . ".png"; // Change the path here as well | |
$element_width = $element->getSize()->getWidth(); | |
$element_height = $element->getSize()->getHeight(); | |
$element_src_x = $element->getLocation()->getX(); | |
$element_src_y = $element->getLocation()->getY(); | |
// Create image instances | |
$src = imagecreatefrompng($screenshot); | |
$dest = imagecreatetruecolor($element_width, $element_height); | |
// Copy | |
imagecopy($dest, $src, 0, 0, $element_src_x, $element_src_y, $element_width, $element_height); | |
imagepng($dest, $element_screenshot); | |
// unlink($screenshot); // unlink function might be restricted in mac os x. | |
if (!file_exists($element_screenshot)) { | |
throw new Exception('Could not save element screenshot'); | |
} | |
return $element_screenshot; | |
} | |
/** | |
* Function : assertElementNotFound | |
* ASSERT ELEMENT NOT FOUND | |
* params $by instance of WebDriverByElement | |
* params $element name of the element used to show in the error message | |
*/ | |
protected function assertElementNotFound($by, $element) | |
{ | |
$errorSnaps = ''; | |
if (!$this->driver->findElements($by)) { | |
$errorSnaps = $this->TakeScreenshot(); | |
$this->fail("The $element was not found on the page" . $this->driver->getCurrentURL()); | |
} | |
// increment assertion counter | |
$this->assertTrue(true); | |
} | |
/** | |
* Function : elementDisplaying | |
* Description: WAITS UNTIL THE ELEMENT IS FOUND | |
* params $myBrowserDriver Instance RemoteWebDriver | |
* params $element dom element object | |
* params $element_label element title | |
*/ | |
public function elementDisplaying($element,$element_label,$myBrowserDriver){ | |
if (!$element->isDisplayed()) { | |
try { | |
$errorSnaps = $this->TakeScreenshot(); | |
$this->fail("The ".$element_label." on " . $myBrowserDriver->getCurrentURL() . " is not displayed , a screen-shot for the error has been placed here -->" . $errorSnaps); | |
} catch (Exception $ex) { | |
$this->fail($ex->getMessage()); | |
} | |
} | |
} | |
/** | |
* Function : waitForElement | |
* Description: WAITS UNTIL THE ELEMENT IS FOUND | |
* params $myBrowserDriver Instance RemoteWebDriver | |
* params $webDriverBy webDriverBy | |
* params string element title | |
*/ | |
public function waitForElement($myBrowserDriver,$webDriverBy,$element_title){ | |
try { | |
$myBrowserDriver->wait(10, 500)->until( | |
WebDriverExpectedCondition::presenceOfAllElementsLocatedBy( | |
$webDriverBy | |
) | |
); | |
} catch (TimeOutException $toe) { | |
try { | |
$errorSnaps = $this->TakeScreenshot(); | |
$this->fail('The element '.$element_title.' on ' . $myBrowserDriver->getCurrentURL() . ' took too much time to load, a screenshot has been placed here -->' . $errorSnaps); | |
} catch (Exception $ex) { | |
$this->fail($ex->getMessage()); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment