Synology NodeJS Selenium - Server terminated early with status 127 - node.js

I read a lot of similar issue but nothing indicate works ...
I'm on Synology - DSM 7.1 (Debian) and my code is
const chrome = require('selenium-webdriver/chrome');
const chromedriver = require('chromedriver');
const webdriver = require('selenium-webdriver');
//const path = require('chromedriver').path;
const {By, until, Builder} = require('selenium-webdriver');
exports.getInfoFromUrl = async(url) => {
// Lancement du webdriver pour scrapper Bet Assistant
//let service = new chrome.ServiceBuilder().build();
//chrome.setDefaultService(service);
//var driver = new webdriver.Builder(path).withCapabilities(webdriver.Capabilities.chrome()).build();
const options = new chrome.Options();
options.addArguments(
'--no-sandbox',
'headless',
'disable-gpu',
'--disable-dev-shm-usage'
);
var driver = new webdriver.Builder(chromedriver.path)
//.forBrowser('chrome')
.withCapabilities(webdriver.Capabilities.chrome())
.setChromeOptions(options)
.build();
/*chrome.setDefaultService(new chrome.ServiceBuilder(chromedriver.path).build());
var driver = new webdriver.Builder(chromedriver.path)
.setChromeOptions(new chrome.Options().addArguments(['--no-sandbox','-headless', '--disable-dev-shm-usage']))
.build();
*/
driver.get(url);
}
When I execute this code with "node script.js" I get this error :
/volume1/web/betassistant/node_modules/selenium-webdriver/remote/index.js:248
reject(Error(e.message))
^
Error: Server terminated early with status 127
at /volume1/web/betassistant/node_modules/selenium-webdriver/remote/index.js:248:24
at processTicksAndRejections (node:internal/process/task_queues:96:5)
I try several sample or code to run webdriver but nothing works. I see some of user install "default-jre" (How do I solve "Server terminated early with status 127" when running node.js on Linux?) but I don't have "apt-get" and I think JRE don't be need on DSM.
Some help will be appreciate :)

Related

Failed to create session. unknown error: Chrome failed to start: was killed. (unknown error: DevToolsActivePort file doesn't exist)

I'm executing an automated test with Electron by using Selenium by using the following code:
const webdriver = require('selenium-webdriver');
var Application = require('spectron').Application;
var assert = require('assert');
const driver = new webdriver.Builder()
// The "9515" is the port opened by chrome driver.
.usingServer('http://localhost:9515')
.withCapabilities({
'goog:chromeOptions': {
// Here is the path to your Electron binary.
binary: 'C:/Program Files/Google/Chrome/Application/chrome.exe',
},
})
.forBrowser('electron') // note: use .forBrowser('electron') for selenium-webdriver <= 3.6.0
.build();
describe('Application launch', function () {
var app;
jest.setTimeout(30000);
beforeEach(function () {
app = new Application({
path: 'node_modules/.bin/electron',
args: ['--headless', '--no-sandbox', '--disable-dev-shm-usage'],
});
return app.start();
});
afterEach(function () {
if (app && app.isRunning()) {
return app.stop();
}
});
it('click a button', function* () {
yield driver.sleep(5000);
});
});
But I'm obtaining the following error:
yarn run v1.22.19
warning package.json: License should be a valid SPDX license expression
warning ..\..\..\package.json: No license field
$ jest --NODE_ENV=test
FAIL src/__test__/App.spec.jsx (10.623 s)
Application launch
× click a button (6564 ms)
● Application launch › click a button
Failed to create session.
unknown error: Chrome failed to start: was killed.
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location C:\Users\vipul\appname\src\node_modules\spectron\lib\launcher.bat is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
at startWebDriverSession (src/node_modules/webdriver/build/utils.js:72:15)
at Function.newSession (src/node_modules/webdriver/build/index.js:58:45)
at Object.remote (src/node_modules/webdriverio/build/index.js:73:22)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 11.878 s
Ran all test suites.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
I've tried to add even the args suggested by several answers in other stackoverflow questions like --headless or --no-sandbox but even those not worked.
Try this file
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("--no-sandbox");
options.add_argument("--headless")
options.add_argument("--disable-setuid-sandbox")
options.add_argument("--remote-debugging-port=9222") # this
options.add_argument("--disable-extensions")
#options.add_argument("--headless")
options.add_argument("--disable-gpu")
options.add_argument("enable-automation")
options.add_argument("--disable-infos")
options.add_argument("--disable-dev-shm-usage")
driver = webdriver.Chrome(chrome_options=options)
driver.get("https://www.google.com/")
element_text = driver.page_source
print(element_text)

Selenium webdriver timeouts don't set when usingServer

I try a lot to set Timeout but it still uses the 300-second default timeout
const { Builder } = require('selenium-webdriver');
let driver = await new Builder()
.forBrowser('firefox')
.usingServer('http://selenium-server:4000/wd/hub')
.build();
const capabilities = await driver.getCapabilities();
capabilities['map_'].set('timeouts', { implicit: 0, pageLoad: 60000, script: 30000 });
await driver.get(url);
I also try driver.manage().timeouts() but got error: driver.manage(...).timeouts is not a function
"selenium-webdriver": "^4.0.0"
There is no stable version of selenium 4. At least I tried 4.0.0-alpha-6 and this version has a lot of problems.
Let's try 3.6.0 instead.
Then this worked:
driver.manage().timeouts().pageLoadTimeout(60000);

AWS Lambda Nodejs 12.x using Selenium: Chrome Driver not found

When I run my nodejs 12.x code calling selenium, I am getting the error below.
At the top level of my directory structure, I have my index.js file, a lib folder and a node_modules folder.
I am preparing the code on a Mac and then zipping it, uploading to S3 and running it in Lambda.
Where should my chrome and chromedriver executables be? I don't think I can use npm to install them as I think I need the linux versions for running in Lambda?
This is the error I am getting:
{
"errorType": "Error",
"errorMessage": "The ChromeDriver could not be found on the current PATH. Please download the latest version of the ChromeDriver from http://chromedriver.storage.googleapis.com/index.html and ensure it can be found on your PATH.",
"trace": [
"Error: The ChromeDriver could not be found on the current PATH. Please download the latest version of the ChromeDriver from http://chromedriver.storage.googleapis.com/index.html and ensure it can be found on your PATH.",
" at new ServiceBuilder (/var/task/node_modules/selenium-webdriver/chrome.js:232:13)",
" at getDefaultService (/var/task/node_modules/selenium-webdriver/chrome.js:321:22)",
" at Function.createSession (/var/task/node_modules/selenium-webdriver/chrome.js:695:44)",
" at createDriver (/var/task/node_modules/selenium-webdriver/index.js:155:33)",
" at Builder.build (/var/task/node_modules/selenium-webdriver/index.js:662:16)",
" at Runtime.exports.handler (/var/task/index.js:38:26)",
" at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"
]
}
This is my code
'use strict';
exports.handler = async (event, context, callback) => {
var webdriver = require('selenium-webdriver');
var chrome = require('selenium-webdriver/chrome');
var builder = new webdriver.Builder().forBrowser('chrome');
var chromeOptions = new chrome.Options();
const defaultChromeFlags = [
'--headless',
'--disable-gpu',
'--window-size=1280x1696', // Letter size
'--no-sandbox',
'--user-data-dir=/tmp/user-data',
'--hide-scrollbars',
'--enable-logging',
'--log-level=0',
'--v=99',
'--single-process',
'--data-path=/tmp/data-path',
'--ignore-certificate-errors',
'--homedir=/tmp',
'--disk-cache-dir=/tmp/cache-dir'
];
chromeOptions.setChromeBinaryPath("/var/task/lib/chrome");
chromeOptions.addArguments(defaultChromeFlags);
builder.setChromeOptions(chromeOptions);
//*****this is the problem line****//
var driver = builder.build();
driver.get(event.url);
driver.getTitle().then(function(title) {
console.log("Page title for " + event.url + " is " + title)
callback(null, 'Page title for ' + event.url + ' is ' + title);
});
driver.quit();
};
Check this:
https://github.com/blackboard/lambda-selenium/blob/master/README.md
Welcome to the lambda-selenium project! The purpose of this project is to show how to use Selenium Webdriver with Amazon Web Services (AWS) Lambda compute service.
or
https://github.com/smithclay/lambdium/blob/master/README.md
Lambdium uses Selenium Webdriver with Headless Chromium to run Webdriver scripts written in JavaScript on AWS Lambda.
You can use chrome-aws-lambda package for running it headless in your lambda function , As per Documentation 512 MB of ram will be used https://www.npmjs.com/package/chrome-aws-lambda

ChromeOptions causes reference error using Selenium ChromeDriver for node.js

I am trying to use the ChromeDriver driver for Selenium to run some tests using Chrome, but I'm getting a reference error when I use ChromeOptions.
My Code
I want to force the use of certain options, such as testing it against a particular user profile. Based on the Selenium and ChromeDriver documentation, this is my file test.js:
opt = new chromeOptions(); // ERROR OCCURS HERE!
opt.setBinary("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
opt.addArguments("--user-data-dir=C:\\Users\\MyUserAccount\\AppData\\Local\\Google\\Chrome\\User Data");
driver = new ChromeDriver(opt);
// rest of my script goes here
The error
I am executing this using the command node test.js. This throws the following error on the first line:
\path\to\test.js:1
ction (exports, require, module, __filename, __dirname) { opt = new chromeOpti
^
ReferenceError: chromeOptions is not defined
at Object.<anonymous> (\path\to\test.js:1:73)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3
For what it's worth, if I skip setting options and replace the first four lines of the script with this, it works, but I can't set the options I need to set:
var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder().
withCapabilities(webdriver.Capabilities.chrome()).
build();
I'm sure that I'm missing something really basic, but I can't figure this one out. How can I set options for Chrome using Selenium and node.js?
Edited to remove some obviously invalid syntax from the samples in some of the documentation I found.
The following works for me:
var webdriver = require("selenium-webdriver");
var chrome = require("selenium-webdriver/chrome");
// Make sure the PATH is set to find ChromeDriver. I'm on a Unix
// system. You'll need to adapt to whatever is needed for
// Windows. Actually, since you say that you can get a browser to show
// up if you don't try to specify options, your ChromeDriver is
// probably already on your PATH, so you can probably skip this.
process.env["PATH"] += ":/home/user/src/selenium/";
var options = new chrome.Options();
// Commented out because they are obviously not what you want.
// Uncomment and adapt as needed:
//
// options.setChromeBinaryPath("/tmp/foo");
// options.addArguments(["--blah"]);
var driver = new webdriver.Builder().
withCapabilities(options.toCapabilities()).build();
driver.get("http://www.google.com")
I've tested the code above with various values and found that it works.
If you want to see what else you can do with the Options object, you can open node_modules/selenium_webdriver/chrome.js and read the source. This is how I figured out the above method.
could you please just try below solution
var webdriver = require("selenium-webdriver");
var chrome = require("selenium-webdriver/chrome");
/**
* Set chrome command line options/switches
*/
var chromeOptions = new chrome.Options();
chromeOptions.addArguments("test-type");
chromeOptions.addArguments("start-maximized");
chromeOptions.addArguments("--js-flags=--expose-gc");
chromeOptions.addArguments("--enable-precise-memory-info");
chromeOptions.addArguments("--disable-popup-blocking");
chromeOptions.addArguments("--disable-default-apps");
chromeOptions.addArguments("--disable-infobars");
driver = new webdriver.Builder()
.forBrowser("chrome")
.setChromeOptions(chromeOptions)
.build();
driver.get("http://www.google.com")
This works for me (from this gist)
//import the selenium web driver
var webdriver = require('selenium-webdriver');
var chromeCapabilities = webdriver.Capabilities.chrome();
//setting chrome options to start the browser fully maximized
var chromeOptions = {
'args': ['--test-type', '--start-maximized']
};
chromeCapabilities.set('chromeOptions', chromeOptions);
var driver = new webdriver.Builder().withCapabilities(chromeCapabilities).build();
This seems to be a fundamental OOP JavaScript misunderstanding!
Your problem is:
ChromeOptions opts = new chromeOptions();
You don't instantiate objects this way. Try:
var opt = new ChromeOptions();
Here's proof:
http://jsfiddle.net/q5Etk/
If you run that, we get "Unexpected Identifier".
Uncomment the var bit and comment the Cat cat bit and see it work.
EDIT After your edit:
You are specifying chromeOptions(). It's ChromeOptions() capped C
var m_Options = new chrome.Options();
m_Options.addArguments("--user-data-dir=C:/Users/israfil/AppData/Local/Google/Chrome/Profile 2");
m_Options.addArguments("--profile-directory=Default");
m_Options.addArguments("--disable-extensions");
m_Options.addArguments(['user-agent="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.11 Safari/537.36"']);
m_Options.setBinaryPath("H://project//nodejs//chrome//chrome.exe");
m_Options.directConnect = false; //setBinaryPath true;
//m_Options.addExtensions();
//m_Options.setProxy();
//m_Options.headless();// Options.headless()}.#headless Options.headless()} Gpu Support ok
//m_Options.addArguments(" --blah"); // not use this codes Importent
var driver = new webdriver.Builder().withCapabilities(webdriver.Capabilities.chrome()).setChromeOptions(m_Options).build();

How can I control a browser ( ala Selenium ) with node.js?

I've heard of soda, but it seems like it requires you to signup and there's a limit on the # of minutes ( free acct / 200 minutes ).
Does anyone know if there's some alternative way to control a browser, or more specifically invoke JS on a web page?
https://github.com/LearnBoost/soda/raw/master/examples/google.js
/**
* Module dependencies.
*/
var soda = require('../')
, assert = require('assert');
var browser = soda.createClient({
host: 'localhost'
, port: 4444
, url: 'http://www.google.com'
, browser: 'firefox'
});
browser.on('command', function(cmd, args){
console.log(' \x1b[33m%s\x1b[0m: %s', cmd, args.join(', '));
});
browser
.chain
.session()
.open('/')
.type('q', 'Hello World')
.clickAndWait('btnG')
.getTitle(function(title){
assert.ok(~title.indexOf('Hello World'), 'Title did not include the query');
})
.clickAndWait('link=Advanced search')
.waitForPageToLoad(2000)
.assertText('css=#gen-query', 'Hello World')
.assertAttribute('as_q#value', 'Hello World')
.testComplete()
.end(function(err){
if (err) throw err;
console.log('done');
});
Zombie.js might work for you. It is headless and seems really cool.
There are actually now Selenium bindings for JavaScript that work with Node.js.
Here are some basic steps to get started:
1 Install Node.js, you can find the download here.
Make sure you
have the latest Chrome driver and put it in your path.
Use npm install selenium-webdriver to get the module added to your project.
Write a test, for example:
var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder().
withCapabilities(webdriver.Capabilities.chrome()).
build();
driver.get('http://www.google.com');
driver.findElement(webdriver.By.name('q')).sendKeys('simple programmer');
driver.findElement(webdriver.By.name('btnG')).click();
driver.quit();</code>
I cover how to do this with some screenshots and how to use Mocha as a test driver in my blog post here.
Here's a pure node.js wrapper around the java API for selenium's webdriver:
https://npmjs.org/package/webdriver-sync
Here's an example:
var webdriverModule = require("webdriver-sync");
var driver = new webdriverModule.ChromeDriver;
var By = webdriverModule.By;
var element = driver.findElement(By.name("q"));
element.sendKeys("Cheese!");
element.submit();
element = driver.findElement(By.name("q"));
assert.equal(element.getAttribute('value'), "Cheese!");
Save that in a .js file and run it with node.
The module is a pure wrapper, so things like sleep or synchronous calls are entirely possible. Here's the current interface of the module:
module.exports={
ChromeDriver:ChromeDriver,
FirefoxDriver:FirefoxDriver,
HtmlUnitDriver:HtmlUnitDriver,
By:new By(),
ExpectedConditions:new ExpectedConditions(),
WebDriverWait:WebDriverWait,
Credentials:UserAndPassword,
Cookie:Cookie,
TimeUnits:TimeUnits,
/**
* #param {number} amount in mills to sleep for.
*/
sleep:function(amount){
java.callStaticMethodSync(
"java.lang.Thread",
"sleep",
new Long(amount)
);
}
};
You can see an integration test that tests the full capabilities here:
https://github.com/jsdevel/webdriver-sync/blob/master/test/integrations/SmokeIT.js
wd is "A node.js javascript client for webdriver/selenium 2"

Resources