<s> import os <EOL> import traceback <EOL> from io import BytesIO <EOL> from . import config <EOL> from . exceptions import ScreenshotError <EOL> from . plogger import preporter <EOL> '''<STR_LIT>''' <EOL> from struct import pack <EOL> from platform import system <EOL> from zlib import compress , crc32 <EOL> if system ( ) == '<STR_LIT>' : <EOL> try : <EOL> import Quartz <EOL> from LaunchServices import kUTTypePNG <EOL> pyobjc_installed = True <EOL> except ImportError : <EOL> pyobjc_installed = False <EOL> elif system ( ) == '<STR_LIT>' : <EOL> from ctypes import c_void_p , create_string_buffer , sizeof , windll , Structure , POINTER , WINFUNCTYPE <EOL> from ctypes . wintypes import BOOL , DOUBLE , DWORD , HBITMAP , HDC , HGDIOBJ , HWND , INT , LPARAM , LONG , RECT , UINT , WORD <EOL> class BITMAPINFOHEADER ( Structure ) : <EOL> _fields_ = [ ( '<STR_LIT>' , DWORD ) , ( '<STR_LIT>' , LONG ) , ( '<STR_LIT>' , LONG ) , <EOL> ( '<STR_LIT>' , WORD ) , ( '<STR_LIT>' , WORD ) , <EOL> ( '<STR_LIT>' , DWORD ) , ( '<STR_LIT>' , DWORD ) , <EOL> ( '<STR_LIT>' , LONG ) , ( '<STR_LIT>' , LONG ) , <EOL> ( '<STR_LIT>' , DWORD ) , ( '<STR_LIT>' , DWORD ) ] <EOL> class BITMAPINFO ( Structure ) : <EOL> _fields_ = [ ( '<STR_LIT>' , BITMAPINFOHEADER ) , ( '<STR_LIT>' , DWORD * <NUM_LIT:3> ) ] <EOL> class MSS ( object ) : <EOL> def enum_display_monitors ( self , screen = <NUM_LIT:0> ) : <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> def get_pixels ( self , monitor ) : <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> def save ( self , output , screen = <NUM_LIT:0> ) : <EOL> for i , monitor in enumerate ( self . enum_display_monitors ( screen ) ) : <EOL> if screen <= <NUM_LIT:0> or ( screen > <NUM_LIT:0> and i + <NUM_LIT:1> == screen ) : <EOL> self . save_img ( data = self . get_pixels ( monitor ) , <EOL> width = monitor [ b'<STR_LIT:width>' ] , <EOL> height = monitor [ b'<STR_LIT>' ] , <EOL> output = output ) <EOL> def save_img ( self , data , width , height , output ) : <EOL> zcrc32 = crc32 <EOL> zcompr = compress <EOL> len_sl = width * <NUM_LIT:3> <EOL> scanlines = b'<STR_LIT>' . join ( <EOL> [ b'<STR_LIT:0>' + data [ y * len_sl : y * len_sl + len_sl ] <EOL> for y in range ( height ) ] ) <EOL> magic = pack ( b'<STR_LIT>' , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:10> , <NUM_LIT> , <NUM_LIT:10> ) <EOL> ihdr = [ b'<STR_LIT>' , b'<STR_LIT>' , b'<STR_LIT>' , b'<STR_LIT>' ] <EOL> ihdr [ <NUM_LIT:2> ] = pack ( b'<STR_LIT>' , width , height , <NUM_LIT:8> , <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> ihdr [ <NUM_LIT:3> ] = pack ( b'<STR_LIT>' , zcrc32 ( b'<STR_LIT>' . join ( ihdr [ <NUM_LIT:1> : <NUM_LIT:3> ] ) ) & <NUM_LIT> ) <EOL> ihdr [ <NUM_LIT:0> ] = pack ( b'<STR_LIT>' , len ( ihdr [ <NUM_LIT:2> ] ) ) <EOL> idat = [ b'<STR_LIT>' , b'<STR_LIT>' , b'<STR_LIT>' , b'<STR_LIT>' ] <EOL> idat [ <NUM_LIT:2> ] = zcompr ( scanlines , <NUM_LIT:9> ) <EOL> idat [ <NUM_LIT:3> ] = pack ( b'<STR_LIT>' , zcrc32 ( b'<STR_LIT>' . join ( idat [ <NUM_LIT:1> : <NUM_LIT:3> ] ) ) & <NUM_LIT> ) <EOL> idat [ <NUM_LIT:0> ] = pack ( b'<STR_LIT>' , len ( idat [ <NUM_LIT:2> ] ) ) <EOL> iend = [ b'<STR_LIT>' , b'<STR_LIT>' , b'<STR_LIT>' , b'<STR_LIT>' ] <EOL> iend [ <NUM_LIT:3> ] = pack ( b'<STR_LIT>' , zcrc32 ( iend [ <NUM_LIT:1> ] ) & <NUM_LIT> ) <EOL> iend [ <NUM_LIT:0> ] = pack ( b'<STR_LIT>' , len ( iend [ <NUM_LIT:2> ] ) ) <EOL> try : <EOL> output . write ( magic + b'<STR_LIT>' . join ( ihdr ) + b'<STR_LIT>' . join ( idat ) + b'<STR_LIT>' . join ( iend ) ) <EOL> except : <EOL> err = '<STR_LIT>' . format ( output ) <EOL> raise ScreenshotError ( err ) <EOL> class MSSMac ( MSS ) : <EOL> def enum_display_monitors ( self , screen = <NUM_LIT:0> ) : <EOL> if screen == - <NUM_LIT:1> : <EOL> rect = Quartz . CGRectInfinite <EOL> yield ( { <EOL> b'<STR_LIT:left>' : int ( rect . origin . x ) , <EOL> b'<STR_LIT>' : int ( rect . origin . y ) , <EOL> b'<STR_LIT:width>' : int ( rect . size . width ) , <EOL> b'<STR_LIT>' : int ( rect . size . height ) <EOL> } ) <EOL> else : <EOL> max_displays = <NUM_LIT:32> <EOL> rotations = { <NUM_LIT:0.0> : '<STR_LIT>' , <NUM_LIT> : '<STR_LIT:right>' , - <NUM_LIT> : '<STR_LIT:left>' } <EOL> _ , ids , _ = Quartz . CGGetActiveDisplayList ( max_displays , None , None ) <EOL> for display in ids : <EOL> rect = Quartz . CGRectStandardize ( Quartz . CGDisplayBounds ( display ) ) <EOL> left , top = rect . origin . x , rect . origin . y <EOL> width , height = rect . size . width , rect . size . height <EOL> rot = Quartz . CGDisplayRotation ( display ) <EOL> if rotations [ rot ] in [ '<STR_LIT:left>' , '<STR_LIT:right>' ] : <EOL> width , height = height , width <EOL> yield ( { <EOL> b'<STR_LIT:left>' : int ( left ) , <EOL> b'<STR_LIT>' : int ( top ) , <EOL> b'<STR_LIT:width>' : int ( width ) , <EOL> b'<STR_LIT>' : int ( height ) <EOL> } ) <EOL> def get_pixels ( self , monitor ) : <EOL> width , height = monitor [ b'<STR_LIT:width>' ] , monitor [ b'<STR_LIT>' ] <EOL> left , top = monitor [ b'<STR_LIT:left>' ] , monitor [ b'<STR_LIT>' ] <EOL> rect = Quartz . CGRect ( ( left , top ) , ( width , height ) ) <EOL> options = Quartz . kCGWindowListOptionOnScreenOnly <EOL> winid = Quartz . kCGNullWindowID <EOL> default = Quartz . kCGWindowImageDefault <EOL> self . image = Quartz . CGWindowListCreateImage ( rect , options , winid , default ) <EOL> if not self . image : <EOL> raise ScreenshotError ( '<STR_LIT>' ) <EOL> return self . image <EOL> def save_img ( self , data , width , height , output ) : <EOL> cf_data = Quartz . CFDataCreateMutable ( Quartz . kCFAllocatorDefault , <NUM_LIT:0> ) <EOL> dest = Quartz . CGImageDestinationCreateWithData ( cf_data , kUTTypePNG , <NUM_LIT:1> , None ) <EOL> if not dest : <EOL> err = '<STR_LIT>' <EOL> raise ScreenshotError ( err ) <EOL> Quartz . CGImageDestinationAddImage ( dest , data , None ) <EOL> if not Quartz . CGImageDestinationFinalize ( dest ) : <EOL> raise ScreenshotError ( '<STR_LIT>' ) <EOL> cf_data_bytes = Quartz . CFDataGetBytes ( cf_data , Quartz . CFRangeMake ( <NUM_LIT:0> , Quartz . CFDataGetLength ( cf_data ) ) , None ) <EOL> output . write ( cf_data_bytes ) <EOL> class MSSWindows ( MSS ) : <EOL> def __init__ ( self ) : <EOL> self . _set_argtypes ( ) <EOL> self . _set_restypes ( ) <EOL> def _set_argtypes ( self ) : <EOL> self . MONITORENUMPROC = WINFUNCTYPE ( INT , DWORD , DWORD , POINTER ( RECT ) , <EOL> DOUBLE ) <EOL> windll . user32 . GetSystemMetrics . argtypes = [ INT ] <EOL> windll . user32 . EnumDisplayMonitors . argtypes = [ HDC , c_void_p , <EOL> self . MONITORENUMPROC , <EOL> LPARAM ] <EOL> windll . user32 . GetWindowDC . argtypes = [ HWND ] <EOL> windll . gdi32 . CreateCompatibleDC . argtypes = [ HDC ] <EOL> windll . gdi32 . CreateCompatibleBitmap . argtypes = [ HDC , INT , INT ] <EOL> windll . gdi32 . SelectObject . argtypes = [ HDC , HGDIOBJ ] <EOL> windll . gdi32 . BitBlt . argtypes = [ HDC , INT , INT , INT , INT , HDC , INT , INT , <EOL> DWORD ] <EOL> windll . gdi32 . DeleteObject . argtypes = [ HGDIOBJ ] <EOL> windll . gdi32 . GetDIBits . argtypes = [ HDC , HBITMAP , UINT , UINT , c_void_p , <EOL> POINTER ( BITMAPINFO ) , UINT ] <EOL> def _set_restypes ( self ) : <EOL> windll . user32 . GetSystemMetrics . restypes = INT <EOL> windll . user32 . EnumDisplayMonitors . restypes = BOOL <EOL> windll . user32 . GetWindowDC . restypes = HDC <EOL> windll . gdi32 . CreateCompatibleDC . restypes = HDC <EOL> windll . gdi32 . CreateCompatibleBitmap . restypes = HBITMAP <EOL> windll . gdi32 . SelectObject . restypes = HGDIOBJ <EOL> windll . gdi32 . BitBlt . restypes = BOOL <EOL> windll . gdi32 . GetDIBits . restypes = INT <EOL> windll . gdi32 . DeleteObject . restypes = BOOL <EOL> def enum_display_monitors ( self , screen = - <NUM_LIT:1> ) : <EOL> if screen == - <NUM_LIT:1> : <EOL> SM_XVIRTUALSCREEN , SM_YVIRTUALSCREEN = <NUM_LIT> , <NUM_LIT> <EOL> SM_CXVIRTUALSCREEN , SM_CYVIRTUALSCREEN = <NUM_LIT> , <NUM_LIT> <EOL> left = windll . user32 . GetSystemMetrics ( SM_XVIRTUALSCREEN ) <EOL> right = windll . user32 . GetSystemMetrics ( SM_CXVIRTUALSCREEN ) <EOL> top = windll . user32 . GetSystemMetrics ( SM_YVIRTUALSCREEN ) <EOL> bottom = windll . user32 . GetSystemMetrics ( SM_CYVIRTUALSCREEN ) <EOL> yield ( { <EOL> b'<STR_LIT:left>' : int ( left ) , <EOL> b'<STR_LIT>' : int ( top ) , <EOL> b'<STR_LIT:width>' : int ( right ) , <EOL> b'<STR_LIT>' : int ( bottom ) <EOL> } ) <EOL> else : <EOL> def _callback ( monitor , dc , rect , data ) : <EOL> rct = rect . contents <EOL> monitors . append ( { <EOL> b'<STR_LIT:left>' : int ( rct . left ) , <EOL> b'<STR_LIT>' : int ( rct . top ) , <EOL> b'<STR_LIT:width>' : int ( rct . right - rct . left ) , <EOL> b'<STR_LIT>' : int ( rct . bottom - rct . top ) <EOL> } ) <EOL> return <NUM_LIT:1> <EOL> monitors = [ ] <EOL> callback = self . MONITORENUMPROC ( _callback ) <EOL> windll . user32 . EnumDisplayMonitors ( <NUM_LIT:0> , <NUM_LIT:0> , callback , <NUM_LIT:0> ) <EOL> for mon in monitors : <EOL> yield mon <EOL> def get_pixels ( self , monitor ) : <EOL> width , height = monitor [ b'<STR_LIT:width>' ] , monitor [ b'<STR_LIT>' ] <EOL> left , top = monitor [ b'<STR_LIT:left>' ] , monitor [ b'<STR_LIT>' ] <EOL> SRCCOPY = <NUM_LIT> <EOL> DIB_RGB_COLORS = BI_RGB = <NUM_LIT:0> <EOL> srcdc = memdc = bmp = None <EOL> try : <EOL> bmi = BITMAPINFO ( ) <EOL> bmi . bmiHeader . biSize = sizeof ( BITMAPINFOHEADER ) <EOL> bmi . bmiHeader . biWidth = width <EOL> bmi . bmiHeader . biHeight = - height <EOL> bmi . bmiHeader . biPlanes = <NUM_LIT:1> <EOL> bmi . bmiHeader . biBitCount = <NUM_LIT> <EOL> bmi . bmiHeader . biCompression = BI_RGB <EOL> buffer_len = height * width * <NUM_LIT:3> <EOL> self . image = create_string_buffer ( buffer_len ) <EOL> srcdc = windll . user32 . GetWindowDC ( <NUM_LIT:0> ) <EOL> memdc = windll . gdi32 . CreateCompatibleDC ( srcdc ) <EOL> bmp = windll . gdi32 . CreateCompatibleBitmap ( srcdc , width , height ) <EOL> windll . gdi32 . SelectObject ( memdc , bmp ) <EOL> windll . gdi32 . BitBlt ( memdc , <NUM_LIT:0> , <NUM_LIT:0> , width , height , srcdc , left , top , <EOL> SRCCOPY ) <EOL> bits = windll . gdi32 . GetDIBits ( memdc , bmp , <NUM_LIT:0> , height , self . image , <EOL> bmi , DIB_RGB_COLORS ) <EOL> if bits != height : <EOL> raise ScreenshotError ( '<STR_LIT>' ) <EOL> finally : <EOL> if srcdc : <EOL> windll . gdi32 . DeleteObject ( srcdc ) <EOL> if memdc : <EOL> windll . gdi32 . DeleteObject ( memdc ) <EOL> if bmp : <EOL> windll . gdi32 . DeleteObject ( bmp ) <EOL> self . image [ <NUM_LIT:2> : buffer_len : <NUM_LIT:3> ] , self . image [ <NUM_LIT:0> : buffer_len : <NUM_LIT:3> ] = self . image [ <NUM_LIT:0> : buffer_len : <NUM_LIT:3> ] , self . image [ <NUM_LIT:2> : buffer_len : <NUM_LIT:3> ] <EOL> return self . image <EOL> def mss ( * args , ** kwargs ) : <EOL> mss_class = { <EOL> '<STR_LIT>' : MSSMac , <EOL> '<STR_LIT>' : MSSWindows <EOL> } [ system ( ) ] <EOL> return mss_class ( * args , ** kwargs ) <EOL> def take_screenshot ( ) : <EOL> if config . get_option ( "<STR_LIT>" ) : <EOL> return <EOL> from . import testexecutor <EOL> web_drivers = testexecutor . current_executor ( ) . get_property ( "<STR_LIT>" ) <EOL> running_test_fixture = testexecutor . current_executor ( ) . get_property ( "<STR_LIT>" ) <EOL> if web_drivers : <EOL> for index , web_driver in enumerate ( web_drivers ) : <EOL> screenshot = { <EOL> "<STR_LIT:source>" : "<STR_LIT>" , <EOL> "<STR_LIT:path>" : "<STR_LIT>" % ( running_test_fixture . full_name , index + <NUM_LIT:1> ) <EOL> } <EOL> running_test_fixture . screenshots . append ( screenshot ) <EOL> try : <EOL> screenshot [ "<STR_LIT>" ] = web_driver . switch_to . alert . text <EOL> except Exception : <EOL> pass <EOL> while True : <EOL> try : <EOL> web_driver . switch_to . alert . dismiss ( ) <EOL> except Exception : <EOL> break <EOL> try : <EOL> screenshot [ "<STR_LIT:url>" ] = web_driver . current_url <EOL> except Exception : <EOL> pass <EOL> try : <EOL> screenshot [ "<STR_LIT:title>" ] = web_driver . title <EOL> except Exception : <EOL> pass <EOL> try : <EOL> with open ( os . path . join ( config . get_option ( "<STR_LIT>" ) , screenshot [ "<STR_LIT:path>" ] ) , mode = "<STR_LIT:wb>" ) as f : <EOL> f . write ( web_driver . get_screenshot_as_png ( ) ) <EOL> except Exception : <EOL> preporter . warn ( "<STR_LIT>" % traceback . format_exc ( ) ) <EOL> else : <EOL> if system ( ) == '<STR_LIT>' and not pyobjc_installed : <EOL> preporter . warn ( "<STR_LIT>" ) <EOL> return <EOL> screenshot = { <EOL> "<STR_LIT:source>" : "<STR_LIT>" , <EOL> "<STR_LIT:path>" : "<STR_LIT>" % running_test_fixture . full_name <EOL> } <EOL> running_test_fixture . screenshots . append ( screenshot ) <EOL> output = BytesIO ( ) <EOL> try : <EOL> mss ( ) . save ( output = output , screen = - <NUM_LIT:1> ) <EOL> with open ( os . path . join ( config . get_option ( "<STR_LIT>" ) , screenshot [ "<STR_LIT:path>" ] ) , mode = "<STR_LIT:wb>" ) as f : <EOL> f . write ( output . getvalue ( ) ) <EOL> except Exception : <EOL> preporter . warn ( "<STR_LIT>" % traceback . format_exc ( ) ) </s>
