1

I'm using NodeJS selenium within my electron desktop application.

"electron-chromedriver": "^4.0.0-beta.1",
"selenium-webdriver": "^4.0.0-alpha.1",

I followed electron documentation about using selenium inside electron app and I have code like this:

let chromeCapabilities = webdriver.Capabilities.chrome();
let chromeOptions = { 'args': ['--disable-infobars'] };
chromeCapabilities.set('chromeOptions', chromeOptions);
capabilities.setPageLoadStrategy('eager');

this.driver = new webdriver.Builder()
    .withCapabilities(chromeCapabilities)
    .usingServer('http://localhost:9515')
    .forBrowser('chrome')
    .build();

after above code there is chain of promises which are doing many different things.

Except that I'm runing chromedriver command in terminal within ./node_modules/.bin/ directory.

Starting ChromeDriver 69.0.3497.106 (857b284701ddf7bef0f14fa76416cf7ca786b411) on port 9515
Only local connections are allowed.

Now when I run entire code it sometimes works correctly but occasionally it print error in terminal in which I npm run dev electron app:

Error: ECONNREFUSED connect ECONNREFUSED 127.0.0.1:9515

I read several similiar SO questions but none of them was helpful. I don't really understand how it's possible that some piece of code is sometimes working and sometimes causing error.

I also checked ports 9515 (chromedriver) and 9080 (electron nodeJS) after getting error and everything looks fine:

alt


I've done some more debugging and I found out that probably this:

webElement.getAttribute('style')

is sometimes causing my error. When I delete above (and also .then() and .catch after it ofcourse) error stop occuring. However I don't know how geting inline styles of webElement might be relevant to connection refused error.

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
BT101
  • 2,674
  • 3
  • 26
  • 61
  • Where is your server hosted? – Rohit Dalal Mar 16 '19 at 08:46
  • I'm runing code locally. I've created my electron app using electron-vue cli boilerplate. `npm run dev` which I call from terminal is runing script `"dev": "node .electron-vue/dev-runner.js",` which open local server. The file is: https://gist.github.com/BorysTyminski/0dd37249b274c12796b030b565671564 – BT101 Mar 16 '19 at 08:52
  • whats the output of command - "lsof -i :9515" when the connection is refused? – Rohit Dalal Mar 16 '19 at 08:56
  • Well I can't really run any command until I stop server with ctrl+C. Except that I'm using windows10. I don't know if command `lsof` is recognized command in windows. – BT101 Mar 16 '19 at 09:04
  • My bad. Poweshell and cmd are both available to run commands. See https://stackoverflow.com/questions/48198/how-can-you-find-out-which-process-is-listening-on-a-port-on-windows for seeing whats running on your port – Rohit Dalal Mar 16 '19 at 09:08
  • No matter if i run command before or after connection refused the outup is: https://i.imgur.com/01qEZ8V.png – BT101 Mar 16 '19 at 09:20
  • The above screenshot must be when your server is running perfectly, server is running on the port 9515. Whats the output when server doesn't work. – Rohit Dalal Mar 16 '19 at 09:47
  • Chromedriver is running on the port 9515. Node side of electron is running on `server.listen(9080)` as you can see in file which I post gist above in comment. Anyway I'm not lying, I execute command after the error occur. Take a look: https://i.imgur.com/pcfx1MI.png – BT101 Mar 16 '19 at 11:46
  • Running that stuff in your personal user folder may cause some problems with antivirus/host intrusion software. Also, anonymous access to your users folder isn’t a great idea. Might not be your issue for this, but moving that kind of stuff outside of your users folder is just some general feedback. –  Mar 23 '19 at 19:16
  • No anonymous user should be allowed to write to your user folder. –  Mar 23 '19 at 19:22

1 Answers1

3

You have used the following capability:

capabilities.setPageLoadStrategy('eager');

Earlier, in a couple of QAs we have discussed about PageLoadStrategy as eager for ChromeDriver and Chrome:

The conclusion was, as per WebDriver Status you will find the list of all WebDriver commands and their current support in ChromeDriver based on what is in the WebDriver Specification. Once the implementation are completed from all aspects PageLoadStrategy.EAGER is bound to be functionally present within Chrome Driver.

Similar discussions were also active on several threads within bugs.chromium.org and groups.google.com as follows:

If you look at page_load_strategy.cc PageLoadStrategy as eager is yet to be implemented:

#include "chrome/test/chromedriver/chrome/page_load_strategy.h"
#include "base/logging.h"
#include "chrome/test/chromedriver/chrome/navigation_tracker.h"
#include "chrome/test/chromedriver/chrome/non_blocking_navigation_tracker.h"
const char PageLoadStrategy::kNormal[] = "normal";
const char PageLoadStrategy::kNone[] = "none";
const char PageLoadStrategy::kEager[] = "eager";
PageLoadStrategy* PageLoadStrategy::Create(
    std::string strategy,
    DevToolsClient* client,
    const BrowserInfo* browser_info,
    const JavaScriptDialogManager* dialog_manager) {
  if (strategy == kNone) {
    return new NonBlockingNavigationTracker();
  } else if (strategy == kNormal) {
    return new NavigationTracker(client, browser_info, dialog_manager);
  } else {
    NOTREACHED() << "invalid strategy '" << strategy << "'";
    return nullptr;
  }
}

This may be root cause of the Error: ECONNREFUSED which you are seeing as the dummy elements/iframes may not have generated/created/rendered completely as per the Chrome Browser startup logs:

[1517231304.270][DEBUG]: DEVTOOLS COMMAND Runtime.evaluate (id=11) {
   "expression": "var isLoaded = document.readyState == 'complete' ||    document.readyState == 'interactive';if (isLoaded) {  var frame = document.createElement('iframe');  frame.name = 'chromedriver dummy frame'; ..."
}

This issue inturn causes the line:

webElement.getAttribute('style')

to cause the actual error.


Solution

For a cleaner test you may need to use PageLoadStrategy as normal instead as:

capabilities.setPageLoadStrategy('normal');

Outro

Some relevant discussions:

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • I don't know who -1 but this answer is correct. Eager is sometimes causing error. Anyway I switch to puppeteer because selenium makes some things hard - like changing `window.navigator`. Also I feel like puppeteer is performing better. – BT101 Mar 24 '19 at 13:26