WinAppDriver icon indicating copy to clipboard operation
WinAppDriver copied to clipboard

500 Internal Error when i try to capture screenshot of element

Open AnatolyRyzhakov opened this issue 1 year ago • 3 comments

Hello everyone!

I try to make in-house framework for GUI testing and this the problem I have encountered:

When i try to capture screenshot of the element (or group of elements), server sends 500 internal error

==========================================
GET /session/53C6FE05-D076-48C7-9499-DDDF337DAC38/element/42.7149364/screenshot HTTP/1.1
Accept: application/json
Accept-Encoding: identity
Content-Type: application/json;charset=UTF-8
Host: 127.0.0.1:4723
User-Agent: selenium/3.141.0 (python windows)


HTTP/1.1 500 Internal Error
Content-Length: 133
Content-Type: application/json

{"status":13,"value":{"error":"unknown error","message":"An unknown error occurred in the remote end while processing the command."}}
==========================================

But! Look at this server responce

==========================================
GET /session/499FA856-097B-451C-BC2B-B56898F06A6B/element/42.2889676.4.-2147483562/screenshot HTTP/1.1
Accept: application/json
Accept-Encoding: identity
Content-Type: application/json;charset=UTF-8
Host: 127.0.0.1:4723
User-Agent: selenium/3.141.0 (python windows)


HTTP/1.1 200 OK
Content-Length: 1818
Content-Type: application/json

{"sessionId":"499FA856-097B-451C-BC2B-B56898F06A6B","status":0,"value":"iVBORw0KGgoAAAANSUhEUgAAAXEAAAAYCAIAAACDaVrVAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAATESURBVHhe7dxdSKpnHADw0tQ+WB8z08w6asVqsOiDFcEuNqqbg220Ilqcm4qyj0NFN6273WwQ0ceFO1At2E0rYTcFFSxbrJsOq7MF5ywvoiZqMjQzSyVNc3/04cCx0td4Paj8fxdvz8f/oXzw/b/P+1ViSUlJAkII0YRBfiKEEB0wpyCE6IQ55X3j+pAKQnHnnusp19fXbDabwaAh3TCZzNTUVFJ5l8Ph8Hg8pEJBWloah8O5vb29vLyELbTAX5ieng5bp9Npt9v9YdGvvr4etiq [...]

==========================================

This request works!

I have researched the issue and what I found:

  • request 1 (with 500 error) have elementID = 42.7149364
  • request 2 (with 200 OK) have elementID = 42.2889676.4.-2147483562 For example: in Appium Inspector it calls RuntimeId: image

UPD. Both of these elements can be find in page_source and can interact with it (click, move_to, etc.)

This my pseudo code example:

driverDesiredCaps = {
    'platformName': 'Windows',
    'app': 'C:\\Path\\To\\MyApp.exe',
    'automationName': 'Windows',
}

winAppDriverServerUrl = 'http://127.0.0.1:4723'

driverSession = webdriver.Remote(command_executor=winAppDriverServerUrl, desired_capabilities=driverDesiredCaps)

## ELEMENT LOCATORS ##
myAppPanel_classname = "myAppPanel"
myAppPanel_xpath = f"//*[@ClassName='{myAppPanel_classname}']"

myAppPanel = driverSession.find_element(By.XPATH, myAppPanel_xpath)
myAppPanel.screenshot('myAppPanel.png')

driverSession.quit()

Why does request work in one case (with long ID) and not in another (with short ID)?

Please, help me to solve this problem

My stack:

  • Python (+ PyTest but not implemented yet)
  • Selenium==3.141.0 (because JWP protocol needed)
  • WinAppDriver (1.2.2009.2003)

AnatolyRyzhakov avatar Jan 23 '24 17:01 AnatolyRyzhakov

Hi @AnatolyRyzhakov , Win app driver does not support direct element image capture, therefore what we do is we capture the whole screen shot and then we crop out the element image using its dimensions. look at the code below:

public void highlightElement(WebElement element) throws ErrorMessage { try { // Get the bounds of the element Point location = element.getLocation(); Dimension size = element.getSize();

        DesktopParamClass paramClass = new DesktopParamClass();
        //appending screenshot
        WindowsDriver  driver = ((WindowsDriver )(paramClass.getExecutionParamValue(DesktopParamKeyName.WIN_APP_DRIVER)));
        BufferedImage fullImage = ImageIO.read(new ByteArrayInputStream((
				(TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES)));
        
        new ImageProcessing().highlightElementOnOnImage(fullImage, new DesktopImageProcessing().getHighlightColor(), 
        		location.getX(), location.getY(), size.getWidth(), size.getHeight());
        
        paramClass.putExecutionParamValue(DesktopParamKeyName.BUFFERED_SCREENSHOT, fullImage);
        
    } catch (Exception e) {
        e.printStackTrace();
        throw new ErrorMessage("Unable to get element rect for highlights :" + e.getMessage());
    }
}

Also we have developed a tool regarding the same you can look into that as a solution over here 'https://soliterata.com/'

RakeshDangi-slt avatar Jan 24 '24 06:01 RakeshDangi-slt

Instead of using myAppPanel.screenshot('myAppPanel.png') just open binary new file and save content of element.screenshot_as_png. Dont listen to comment above because It is wrong.

HowTurnRight avatar Feb 07 '24 08:02 HowTurnRight

@HowTurnRight Hello! Sounds interesting. Thanks for the answer I will try it later

AnatolyRyzhakov avatar Feb 07 '24 09:02 AnatolyRyzhakov