Issue
I'm trying to automate the process of setting up a Chrome window opened by Selenium under node.js for some testing, and one requirement is that the window must appear on a different monitor.
It does not seem that Selenium supports this. I also could not find any other automation apps usable from node.js that support this. I looked at node-window-manager and it does not appear to have any way of moving a window to a different monitor. It has the ability to see which monitor a window is on, but only under Windows. I looked at RobotJS, but it does not have multi-monitor support although there are forks which appear to, although as this does not provide a method to simply move a window to a different desktop.
I came upon the idea to simply use AppleScript to invoke the menu option Window > "Move to <monitor model> (2)" but to do this I need to know the process. Selenium does not support finding the PID of the Chrome process just started, so I am trying to get this from AppleScript. Unfortunately, there are other Chrome windows open (not related to Selenium) that are open so I can't just look for the Chrome process. So I started with this code to try to locate the window by title:
tell application "System Events"
set allProcesses to application processes
repeat with i from 1 to count allProcesses
set thisProcess to item i of allProcesses
set allWindows to windows of thisProcess
repeat with j from 1 to count allWindows
set thisWindow to item j of allWindows
if name of thisWindow contains "<my window title>"
tell thisProcess
click menu item "Move to <monitor model> (2)" of menu "Window" of menu bar 1
end tell
exit repeat
...
The first time I ran this I got a permissions error and had to grant permissions through the OS. But after this I did not get this error.
However this did not do anything. I instead tried printing out all the window titles and found a couple of titles that were blank but none that matched my window title. Thinking to identify the window by size and position, I modified the creation of the Chrome window in Selenium to include the following:
let options = new chrome.Options()
options.addArguments("--window-position=0,0")
options.addArguments("--window-size=1000,900")
options.addArguments("--window-name=SELENIUM")
let driver = await new Builder().forBrowser("chrome").setChromeOptions(options).build()
However, none of the window titles, sizes, or positions matched. I noticed that one of the empty named windows disappeared when I closed the Chrome browser opened by Selenium. So I tried clicking the menu on it. But this generates an error:
Error: System Events got an error: Can’t get menu item "Move to <monitor model> (2)" of menu "Window" of menu bar 1 of application process "Google Chrome"., number: -1728
But this window is suspect. It has a very strange size (13x1440) and position (0,-484) which is identical to another reported window. The second window goes away if I close the unrelated Chrome window. So I suspect that Chrome always has an offscreen invisible window for some reason, or appears to to AppleScript.
How can I automate moving the Chrome window to a different monitor from node.js under MacOS?
Solution
As you mentioned, with the robotjs library, you can achieve the desired action by simulating keyboard shortcuts. like (in windows) pressing the combination of 'Windows key + Right arrow key' twice can be used to move a window to the right monitor.
I looked at RobotJS, but it does not have multi-monitor support although there are forks which appear to, although as this does not provide a method to simply move a window to a different desktop.
Answered By - daklay
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.