How to work with Edge browser using Selenium webdriver-js - javascript

I am trying to Launch edge using selenium-webdriver-js ( javascript, not Java). i am facing issues in launching the edge browser, all other browsers work fine for me.
var wd = require('selenium-webdriver');
var driver = new wd.Builder().forBrowser('MicrosoftEdge').build();
driver.get('http://www.google.com/ncr');
driver.quit();
I am getting the following error.
WebDriverError: Unknown error
at parseHttpResponse (D:\selenium-js\node_modules\selenium-webdriver\lib\http.js:536:11)
at doSend.then.response (D:\selenium-js\node_modules\selenium-webdriver\lib\http.js:441:30)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
From: Task: WebDriver.createSession()
at Function.createSession (D:\selenium-js\node_modules\selenium-webdriver\lib\webdriver.js:769:24)
at Function.createSession (D:\selenium-js\node_modules\selenium-webdriver\edge.js:281:41)
at createDriver (D:\selenium-js\node_modules\selenium-webdriver\index.js:170:33)
at Builder.build (D:\selenium-js\node_modules\selenium-webdriver\index.js:651:16)
at Object.<anonymous> (D:\selenium-js\sampleScripts\yourProduct\features\stepdefinition\DifferentBrowser.js:23:59)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
Can you help me in getting the solution or working code? I don't know what i am missing

You should make sure to PATH to MicrosoftWebDriver.msi should be valid and your webdriver should support your browser's version.
Your code doesn't contain setup to access to selenium server and webdriver. If selenium server isn't defined, It connects to "localhost:4444" by default.

const { Builder, By, Key } = require("selenium-webdriver");
async function example() {
const driver = await new Builder().forBrowser("MicrosoftEdge").build();
const URL = "https://www.google.com/";
driver.get(URL);
driver.manage().window().maximize();
driver.close();
driver.quit();
}
example();

you can download the edge webdriver fromhttps://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
Then you can unzip it and copy the msedgedriver.exe file into your project root
or you can configure it in environment variables.
This should be fine

Related

Session not created Selenium/webdriver when using Safari 12

Since upgrading to Safari 12, my automated scripts are now getting this error:
SessionNotCreatedError: Request body does not contain required parameter 'capabilities'.
(The error does not occur for other browsers).
I'm using the javascript webdriver bindings and, when I build webdriver, I use the withCapability key value pairs:
var capabs = {
'browserName' : 'Safari',
'version' : '12.0'
}
browserUnderTest = new webdriver.Builder().
withCapabilities(capabs)
.forBrowser('safari')
.build();
I think the problem is with the safari.js file itself, but I don't know enough about how it operates to pinpoint anything. Here is the full text of the error:
SessionNotCreatedError: Request body does not contain required parameter 'capabilities'.
at Object.throwDecodedError (/Users/qualit/Documents/autotests/node_modules/selenium-webdriver/lib/error.js:514:15)
at parseHttpResponse (/Users/qualit/Documents/autotests/node_modules/selenium-webdriver/lib/http.js:519:13)
at doSend.then.response (/Users/qualit/Documents/autotests/node_modules/selenium-webdriver/lib/http.js:441:30)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
From: Task: WebDriver.createSession()
at Function.createSession (/Users/qualit/Documents/autotests/node_modules/selenium-webdriver/lib/webdriver.js:769:24)
at Function.createSession (/Users/qualit/Documents/autotests/node_modules/selenium-webdriver/safari.js:253:41)
at createDriver (/Users/qualit/Documents/autotests/node_modules/selenium-webdriver/index.js:170:33)
at Builder.build (/Users/qualit/Documents/autotests/node_modules/selenium-webdriver/index.js:660:16)
at Object.<anonymous> (/Users/qualit/Documents/autotests/K8_autotest.js:354:6)
at Module._compile (module.js:643:30)
at Object.Module._extensions..js (module.js:654:10)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
Does anyone have any ideas about the cause of this or a fix?
This issue happens because Safari 12 uses a new W3C webdriver protocol (source) which appears to be incompatible with the latest stable selenium-webdriver package (v3.6)
safaridriver can be passed a --legacy flag to use the old protocol. Directly on the command line this would be done like: /usr/bin/safaridriver --legacy
This flag can be set on the driver in your node program as follows:
const webdriver = require('selenium-webdriver');
const safari = require('selenium-webdriver/safari');
new webdriver.Builder()
.usingServer(await new safari.ServiceBuilder().addArguments('--legacy').build().start())
.forBrowser('safari')
.build();
Here's documentation on the ServiceBuilder API - https://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/safari_exports_ServiceBuilder.html
A couple GitHub tickets cover this as well:
https://github.com/SeleniumHQ/selenium/issues/6431
https://github.com/SeleniumHQ/selenium/issues/6026
This will also work if you get an error for 'await' when trying #mjdease solution above.
new webdriver.Builder()
.usingServer(new safari.ServiceBuilder().addArguments('--legacy').build().start())
.forBrowser('safari')
.build();

Plain text SASL authentication in gremlin-javascript

I'm trying to connect to a Gremlin server with the JavaScript driver variant.
Up to package version 2.7.0, this is done easily by passing options to Gremlin.createClient() as in this example for Azure Cosmos DB:
const client = Gremlin.createClient(
config.port,
config.endpoint,
{
"session": false,
"ssl": true,
"user": `/dbs/${config.database}/colls/${config.collection}`,
"password": config.primaryKey
}
);
In newer versions of the package I can't get it done. The official docs suggest using gremlin.driver.auth.PlainTextSaslAuthenticator. However, that method seems to be not implemented in the package and returns TypeError: Cannot read property 'PlainTextSaslAuthenticator' of undefined
My test code (same config.js as in the working example):
const gremlin = require("gremlin");
const config = require("./config");
const Graph = gremlin.structure.Graph;
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;
const graph = new Graph();
const authenticator = new gremlin.driver.auth.PlainTextSaslAuthenticator(
`/dbs/${config.database}/colls/${config.collection}`,
config.primaryKey
);
const g = graph.traversal().withRemote(new DriverRemoteConnection(`ws://${config.endpoint}:${config.port}`, { authenticator });
Return:
C:\repos\gremlin-test\index.js:9
const authenticator = new gremlin.driver.auth.PlainTextSaslAuthenticator(
^
TypeError: Cannot read property 'PlainTextSaslAuthenticator' of undefined
at Object.<anonymous> (C:\repos\gremlin-test\index.js:9:47)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:191:16)
at bootstrap_node.js:612:3
Anyone know a solution to this?
I've not got too much experience with Gremlin.js, but I just downloaded it and have manually searched through all of its files - I can't find any trace of a PlainTextSaslAuthenticator function or its declaration.
This could mean one of two three -
Its function has been (accidentally) removed
It uses a third party package that has been (accidentally) removed
It has not been added to the package yet
Upon a swift Google search, I found this link which seems to show it being added to /lib/driver/auth, but that directory doesn't seem to exist in the package I got through npm install gremlin. Perhaps it is yet to be released?
I would therefore suggest you raise an issue on Github, but it seems that repository you linked doesn't allow for issues to be raised. So perhaps email/contact the author?
EDIT:
Thanks to Stephen for the link - the code hasn't been merged to the package yet. Keep track of it here.

remote function returns undefined or null in electron renderer process

I'm currently very interested in the comprehensive opportunites granted by electron.js and its modules. Unfortunately, I keep getting the same error in my renderer process (named 'connector.js') when trying to start my application.
Here is the error:
App threw an error during load
TypeError: Cannot match against 'undefined' or 'null'.
at Object.<anonymous> (D:\Eigene Dateien\Desktop\Coding\DesktopApps\EVT\extFunctions\connector\connector.js:2:44)
at Object.<anonymous> (D:\Eigene Dateien\Desktop\Coding\DesktopApps\EVT\extFunctions\connector\connector.js:22:3)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (D:\Eigene Dateien\Desktop\Coding\DesktopApps\EVT\main.js:9:1)
And here is my connector.js:
const $ = require('jquery');
const {BrowserWindow} = require('electron').remote;
let Remotewin = remote.getFocusedWindow();
$("#minimize").click(function(){
Remotewin.minimize();
});
$("#maximize").click(function(){
if(!Remotewin.isMaximized()){
Remotewin.maximize();
}else{
Remotewin.unmaximize();
}
});
$("#close").click(function(){
Remotewin.close();
});
As you can see clearly, I wanted to create my own menubar at the top frame of the window, but the functionality is getting seemingly demolished by this error. I already searched half of the internet and stackoverflow, but the every answer I found refered to a webpack and/or electron bug they couldn't directly influence.
That's why I want to clearly point out, that I am NOT using webpack in this project. Only external module I added is jquery, as you can see in the code.
So my question; Have you experiended this error in this context and do you maybe even know a solution? Or can you refer to someone with similiar problems?
Thank you in advance, J0nny
Since getFocusedWindow() is a a static method of BrowserWindow,
let Remotewin = remote.getFocusedWindow();
should be:
let Remotewin = BrowserWindow.getFocusedWindow();

Unable to run appjs sample app using node.js

I am trying to run one of the sample projects using appjs which is present over here https://github.com/appjs/appjs/tree/master/examples. I am using the latest version of node.js (v4.1.0
) on Windows (64 bit machine)
When I try and run the example using the below command on Command Prompt
node --harmony index.js
I get an error as follows,
Error: AppJS requires Node is run with the --harmony command line flag
at Object.<anonymous> (F:\programs\appjs_examples\node_modules\appjs\lib\ind
ex.js:2:9)
at Module._compile (module.js:434:26)
at Object.Module._extensions..js (module.js:452:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (F:\programs\appjs_examples\octosocial\index.js:1:73)
at Module._compile (module.js:434:26)
at Object.Module._extensions..js (module.js:452:10)
I tried searching for this issue but I couldn't find a solution. Can anyone tell me how to use node.js with the harmony flag?
UPDATE
My index.js looks like this
var app = require('appjs'),
github = new (require('github'))({ version: '3.0.0' }),
KEY_F12 = process.platform === 'darwin' ? 63247 : 123;
app.serveFilesFrom(__dirname + '/assets');
var window = app.createWindow({
width: 460,
height: 640,
resizable: false,
disableSecurity: true,
icons: __dirname + '/assets/icons'
});
window.on('create', function(){
window.frame.show();
window.frame.center();
});
window.on('ready', function(){
var $ = window.$,
$username = $('input[name=username]'),
$password = $('input[name=password]'),
$info = $('#info-login'),
$label = $info.find('span'),
$buttons = $('input, button');
$(window).on('keydown', function(e){
if (e.keyCode === KEY_F12) {
window.frame.openDevTools();
}
});
$username.focus();
$('#login-form').submit(function(e){
e.preventDefault();
$info.removeClass('error').addClass('success');
$label.text('Logging in...');
$buttons.attr('disabled', true);
github.authenticate({
type: 'basic',
username: $username.val(),
password: $password.val()
});
github.user.get({}, function(err, result) {
if (err) {
$info.removeClass('success').addClass('error');
$label.text('Login Failed. Try Again.');
$buttons.removeAttr('disabled');
} else {
loggedIn(result);
}
});
});
function loggedIn(result){
$label.text('Logged in!');
$('#user-avatar').append('<img src="'+result.avatar_url+'" width="64" height="64">');
$('#user-name').text(result.name);
$('#login-section').hide();
$('#profile-section').show();
['Followers', 'Following'].forEach(function(type){
github.user['get'+type]({ user: result.login }, populate.bind(null, type.toLowerCase()));
});
}
Now with v0.12 of Node.js I get below error
F:\softwares\Node.js_v0.12\node_modules\appjs\lib\index.js:2
throw new Error ('AppJS requires Node is run with the --harmony command line
Error: AppJS requires Node is run with the --harmony command line flag
at Object.<anonymous> (F:\softwares\Node.js_v0.12\node_modules\appjs\lib\ind
ex.js:2:9)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (F:\softwares\Node.js_v0.12\index.js:1:73)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
Just tested your code out locally with node v0.12.7 and v4.0.0. Looks like the node_modules/appjs/lib/index.js check makes sure that proxy is enabled no matter what.
By default the --harmony flag does not enable proxies. However you can use --harmony_proxies.
To help you understand what is happening:
Open node in your terminal, Then type Proxy. You will get 'Proxy is not defined'.
Now, open node --harmony in your terminal and do the same. You will get the same output.
Now, with node --harmony-proxies. Bam, you get an empty object.
You should be able to run this with v4.x.x however, you will still need the proxies flag for harmony.
When the merge happened with node.js and io.js for v4 they released a page of ES6 features that are shipped if you are using 4.x.x. https://nodejs.org/en/docs/es6/
https://github.com/appjs/appjs is deprecated btw, but once you pass the module's test of features, it will require 32bit ;)
Edit:
To properly run your app use the following:
node --harmony-proxies index.js
Here is a screenshot to show the expected output from step 3 above.

Unable to run selenium WebDriver JavaScript bindings

I'm currently using the selenium binding for python and would like to evaluate the JavaScript binding but am stuck with the sample application!
As I cannot seem to understand, what's wrong in this example, any help would be appreciated.
my package.json file looks like this:
{
"name": "selenium_tests",
"version": "0.0.0",
"repository": {},
"devDependencies": {
"mocha": "^1.21.4",
"selenium-webdriver": "^2.43.5"
}
}
my script looks like this:
var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder().
withCapabilities(webdriver.Capabilities.firefox()).
build();
driver.get('http://www.google.com');
driver.findElement(webdriver.By.name('q')).sendKeys('webdriver');
driver.findElement(webdriver.By.name('btnG')).click();
driver.wait(function () {
'use strict';
return driver.getTitle().then(function (title) {
return title === 'webdriver - Google Search';
});
}, 1000);
driver.quit();
and the error stack when running it is as follows:
D:\selenium_node\node_modules\selenium-webdriver\lib\webdriver\promise.js:1745
throw error;
^
Error: Wait timed out after 1039ms
at D:\selenium_node\node_modules\selenium-webdriver\lib\webdriver\promise.js:1412:29
at D:\selenium_node\node_modules\selenium-webdriver\lib\goog\base.js:1582:15
at webdriver.promise.ControlFlow.runInNewFrame_ (D:\selenium_node\node_modules\selenium-webdriver\lib\webdriver\promise.js:1640:20)
at notify (D:\selenium_node\node_modules\selenium-webdriver\lib\webdriver\promise.js:444:12)
at then (D:\selenium_node\node_modules\selenium-webdriver\lib\webdriver\promise.js:497:7)
at Object.webdriver.promise.asap (D:\selenium_node\node_modules\selenium-webdriver\lib\webdriver\promise.js:749:11)
at newFrame.then.e (D:\selenium_node\node_modules\selenium-webdriver\lib\webdriver\promise.js:1656:25)
at D:\selenium_node\node_modules\selenium-webdriver\lib\goog\base.js:1582:15
at webdriver.promise.ControlFlow.runInNewFrame_ (D:\selenium_node\node_modules\selenium-webdriver\lib\webdriver\promise.js:1640:20)
at notify (D:\selenium_node\node_modules\selenium-webdriver\lib\webdriver\promise.js:444:12)
==== async task ====
at webdriver.promise.ControlFlow.wait (D:\selenium_node\node_modules\selenium-webdriver\lib\webdriver\promise.js:1397:15)
at webdriver.WebDriver.wait (D:\selenium_node\node_modules\selenium-webdriver\lib\webdriver\webdriver.js:589:21)
at Object.<anonymous> (D:\selenium_node\original_sample.js:12:8)
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)
With the help of the friendly people supporting selenium, I solved the mystery and I'm just plain stupid.
Because www.google.com gets automatically redirected depending on where your ip address is located, I've landed on a localized google page and the title was no longer 'webdriver - Google Search'.
Just using the proper localized title or prevent google not to localize using
driver.get('http://www.google.com/ncr');
solved the problem.

Categories