@@ -739,6 +739,20 @@ public function _saveScreenshot($filename)
739739 }
740740 }
741741
742+ public function _saveElementScreenshot ($ selector , $ filename )
743+ {
744+ if (!isset ($ this ->webDriver )) {
745+ $ this ->debug ('WebDriver::_saveElementScreenshot method has been called when webDriver is not set ' );
746+ return ;
747+ }
748+ try {
749+ $ this ->matchFirstOrFail ($ this ->webDriver , $ selector )->takeElementScreenshot ($ filename );
750+ } catch (\Exception $ e ) {
751+ $ this ->debug ('Unable to retrieve element screenshot from Selenium : ' . $ e ->getMessage ());
752+ return ;
753+ }
754+ }
755+
742756 public function _findElements ($ locator )
743757 {
744758 return $ this ->match ($ this ->webDriver , $ locator );
@@ -789,6 +803,34 @@ public function makeScreenshot($name = null)
789803 $ this ->debugSection ('Screenshot Saved ' , "file:// $ screenName " );
790804 }
791805
806+ /**
807+ * Takes a screenshot of an element of the current window and saves it to `tests/_output/debug`.
808+ *
809+ * ``` php
810+ * <?php
811+ * $I->amOnPage('/user/edit');
812+ * $I->makeElementScreenshot('#dialog', 'edit_page');
813+ * // saved to: tests/_output/debug/edit_page.png
814+ * $I->makeElementScreenshot('#dialog');
815+ * // saved to: tests/_output/debug/2017-05-26_14-24-11_4b3403665fea6.png
816+ * ```
817+ *
818+ * @param $name
819+ */
820+ public function makeElementScreenshot ($ selector , $ name = null )
821+ {
822+ if (empty ($ name )) {
823+ $ name = uniqid (date ("Y-m-d_H-i-s_ " ));
824+ }
825+ $ debugDir = codecept_log_dir () . 'debug ' ;
826+ if (!is_dir ($ debugDir )) {
827+ mkdir ($ debugDir , 0777 );
828+ }
829+ $ screenName = $ debugDir . DIRECTORY_SEPARATOR . $ name . '.png ' ;
830+ $ this ->_saveElementScreenshot ($ selector , $ screenName );
831+ $ this ->debugSection ('Screenshot Saved ' , "file:// $ screenName " );
832+ }
833+
792834 public function makeHtmlSnapshot ($ name = null )
793835 {
794836 if (empty ($ name )) {
0 commit comments