2020年12月25日星期五

How to bring a (child-process) Selenium-spawned chrome browser spawned from another process ('node-schedule') in foreground?

I know how to setup Selenium code to spawn a chrome browser directly via a .js script.

The problem is when I use a scheduler from 'node-schedule' npm package. When my scheduler executes a function that handles Selenium codes, the new spawned chrome is always in the background. This behavior is different from when I execute a .js script directly from the terminal to spawn the automated chrome browser. I want this child process spawned chrome to be in foreground (have focus).

Having the spawned chrome browser be in background causes major errors that breaks my project functionality. My project has parts independent from Selenium that sends physical keyboard values. These causes error when the spawned chrome browser is not the first window on the screen

I know that running a Selenium via a .js script from the terminal will spawn the automated chrome browser in the foreground.

A function call with the same parameter run from a script produces no error, however, the same function call from my scheduler causes error because the spawned chrome browser is in background, causing it to fail to detect certain elements.

The project I am working on is not my personal project, therefore I cannot share too much code. However, if certain parts of code are needed to help me, then please let me know.

I am about 99% sure that the error is caused due to the chrome options or chromeCapabilities. The documentation for javascript API is lacking, so it is hard for me to resolve this issue. I got my current code from a github gist (I modified a little).

My code:

        let chromeCapabilities = Capabilities.chrome();          //setting chrome options to start the browser fully maximized          let chromeOptions = {              'args': [                  '--lang=en',                  '--test-child-process',                  '--test-type',                  '--start-maximized',                  '--app',                  '--no-zygote',                  '--no-sandbox-and-elevated',              ]          };          chromeCapabilities.set('goog:chromeOptions', chromeOptions);          let driver = await new Builder().forBrowser('chrome').withCapabilities(chromeCapabilities).build();  // ...  

I think or hope that there is an args value to always bring the spawned chrome browser to foreground.

Edit: I know that the documentation has this subsection - Selenium Docs - #switching-windows-or-tabs

However, I don't think this works for me. The code driver.getWindowHandle() expects the spawned browser to start as a focused window (in foreground).


Edit: Found a useful reference link to chrome args. '--test-child-process' seems like a useful thing to use. There were too many args values, but I don't seem to find a useful one.


Final Edit: I solved my problem (not really). The initial terminal I was using was VS Code's in-built terminal (git bash). (I use Windows 10)
I decided to test running my server terminal on a new 'cmd' terminal. Now my scheduler spawned the automated chrome browser in focus once. I am currently doing some more tests in different scenarios. This is can be considered solved and closed for now.

https://stackoverflow.com/questions/65403131/how-to-bring-a-child-process-selenium-spawned-chrome-browser-spawned-from-anot December 22, 2020 at 12:05PM

没有评论:

发表评论