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