Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to identify the UI when we run the script through command prompt #1980

Open
NivethanSK opened this issue Mar 7, 2024 · 5 comments
Open

Comments

@NivethanSK
Copy link

  1. Launch the application
    System.Diagnostics.Process.Start("Application.Exe Path");

  2. Create a root desktop session as mentioned in the documentation
    DesiredCapabilities appCapabilities = new DesiredCapabilities();
    appCapabilities.SetCapability("app", "Root");
    DesktopSession = new WindowsDriver(new Uri("http://127.0.0.1:4723"), appCapabilities);

var CortanaWindow = DesktopSession.FindElementByName("Cortana");
var CortanaTopLevelWindowHandle = CortanaWindow.GetAttribute("NativeWindowHandle");
CortanaTopLevelWindowHandle = (int.Parse(CortanaTopLevelWindowHandle)).ToString("x"); // Convert to Hex

// Create session by attaching to Cortana top level window
DesiredCapabilities appCapabilities = new DesiredCapabilities();
appCapabilities.SetCapability("appTopLevelWindow", CortanaTopLevelWindowHandle);
CortanaSession = new WindowsDriver(new Uri(WindowsApplicationDriverUrl), appCapabilities);

  1. When I run this particular script through visual studio's developer terminal/powershell, it works fine, even when I trigger the scripts through test explorer it works fine.

  2. But when I try to run the script through an individual command prompt instance outside the visual studio, it fails to identify the application session

  3. The API calls that happen when I run through a command prompt instance

POST /session HTTP/1.1
Accept: application/json, image/png
Connection: Keep-Alive
Content-Length: 63
Content-Type: application/json;charset=utf-8
Host: 127.0.0.1:4723

{"desiredCapabilities":{"app":"Root","platformName":"Windows"}}
HTTP/1.1 200 OK
Content-Length: 111
Content-Type: application/json

{"sessionId":"sessionID","status":0,"value":{"app":"Root","platformName":"Windows"}}

==========================================
POST /session/sessionID/element HTTP/1.1
Accept: application/json, image/png
Connection: Keep-Alive
Content-Length: 41
Content-Type: application/json;charset=utf-8
Host: 127.0.0.1:4723

{"using":"name","value":"appname"}
HTTP/1.1 200 OK
Content-Length: 99
Content-Type: application/json

{"sessionId":"sessionID","status":0,"value":{"ELEMENT":"42.12345.6.78"}}

==========================================
GET /session/sessionId/element/42.12345.6.78/attribute/NativeWindowHandle HTTP/1.1
Accept: application/json, image/png
Connection: Keep-Alive
Host: 127.0.0.1:4723

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

{"sessionId":"sessionId","status":0,"value":"0"}

==========================================
POST /session HTTP/1.1
Accept: application/json, image/png
Connection: Keep-Alive
Content-Length: 74
Content-Type: application/json;charset=utf-8
Host: 127.0.0.1:4723

{"desiredCapabilities":{"appTopLevelWindow":"0","platformName":"Windows"}}
HTTP/1.1 400 Bad Request
Content-Length: 131
Content-Type: application/json

{"status":23,"value":{"error":"no such window","message":"Cannot find active window specified by capabilities: appTopLevelWindow"}}

  1. When I run the same script, through test explorer or developer command prompt or powershell present inside the visual studio, it works fine

POST /session HTTP/1.1
Accept: application/json, image/png
Connection: Keep-Alive
Content-Length: 63
Content-Type: application/json;charset=utf-8
Host: 127.0.0.1:4723

{"desiredCapabilities":{"app":"Root","platformName":"Windows"}}
HTTP/1.1 200 OK
Content-Length: 111
Content-Type: application/json

{"sessionId":"sessionId","status":0,"value":{"app":"Root","platformName":"Windows"}}

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

POST /session/sessionId/element HTTP/1.1
Accept: application/json, image/png
Connection: Keep-Alive
Content-Length: 41
Content-Type: application/json;charset=utf-8
Host: 127.0.0.1:4723

{"using":"name","value":"App name"}
HTTP/1.1 200 OK
Content-Length: 96
Content-Type: application/json

{"sessionId":"sessionId","status":0,"value":{"ELEMENT":"42.1234567"}}

==========================================
GET /session/sessionId/element/42.1234567/attribute/NativeWindowHandle HTTP/1.1
Accept: application/json, image/png
Connection: Keep-Alive
Host: 127.0.0.1:4723

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

{"sessionId":"sessionId","status":0,"value":"1234567"}

==========================================
POST /session HTTP/1.1
Accept: application/json, image/png
Connection: Keep-Alive
Content-Length: 79
Content-Type: application/json;charset=utf-8
Host: 127.0.0.1:4723

{"desiredCapabilities":{"appTopLevelWindow":"12345A","platformName":"Windows"}}
HTTP/1.1 200 OK
Content-Length: 127
Content-Type: application/json

{"sessionId":"sessionId","status":0,"value":{"appTopLevelWindow":"12345A","platformName":"Windows"}}

  1. The issue is when I make the call to this API "POST /session/sessionID/element HTTP/1.1", by using the desktop session, its not bringing out proper ID's

call: desktopsession.FindElementByName("AppName") -> this gives some random results while we trigger the scripts through command prompt

  1. I tried the following call as well, to identify all the window handles instead of the app name, but this as well not bringing out all the winow handles, again this works perfectly fine inside the visual studio's environment

call: var appwindows = desktopsession.FindElementsByClassName("Window);
var test =appwindows .Where(w => w.Text.Equals("App name")).FirstOrDefault();

I am not understanding why this is not happening through commands given outside the VS's environment

I tried even waiting for sometime to identify the elements buts its not happening.

If someone has any clue regarding this, please help

Note: I tried setting the environment variables as well, it did not help

@anunay1
Copy link

anunay1 commented Mar 11, 2024

The error says that it is unable to find the opened window, please try to add some delay before you create a session on the opened window.

@kenzoviski
Copy link

I'm curious, are you using APPIUM?
Cause it seems you're only using Selenium and WinAppDriver to perform automation testing in desktop apps (Windows).

Am I right?

@NivethanSK
Copy link
Author

The error says that it is unable to find the opened window, please try to add some delay before you create a session on the opened window.
Yes, I tried adding delays, its not working. My case however works through the studio's terminal, I have checked the API call's that happens, whenever we run it through the command prompt, the response doesn't capture my application's window

This happens only if I launch and attach my application, otherwise it works perfectly fine when I launch the application through the driver itself

@NivethanSK
Copy link
Author

I'm curious, are you using APPIUM? Cause it seems you're only using Selenium and WinAppDriver to perform automation testing in desktop apps (Windows).

Am I right?

No no, I am using appium's package only - "Microsoft.WinAppDriver.Appium.WebDriver"

@anunay1
Copy link

anunay1 commented Mar 12, 2024

It's weird. Can you schedule a zoom call?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants