Receiving error in selenium (javascript) when opening shadowRoot - javascript

I am setting up the first automated tests for a web app I'm working on, and have hit a state I don't understand.
It is a browser app, so I start a very simple static server:
import http from 'http';
let serve = serveStatic(path);
server = http.createServer(function(req, res) {
var done = finalhandler(req, res);
serve(req, res, done);
});
During my testing, I receive an error message HTTP method not allowed
let options = new firefox.Options();
options.headless();
let capabilities = webdriver.Capabilities.firefox().set('acceptInsecureCerts', true);
let driver = new webdriver.Builder()
.forBrowser('firefox')
.setFirefoxOptions(options)
.withCapabilities(capabilities)
.build();
await driver.get('http://127.0.0.1:3030/index.html');
let tab = await driver.findElement(state.By.css('ps-tabpanel'));
tab = await tab.getShadowRoot(); // HTTP method not allowed
On a hunch, I changed this to an HTTPS connection
import http from 'https';
In this case I receive a very different error
await driver.get('https://127.0.0.1:3030/index.html');
// Reached error page: about:neterror?e=nssFailure2&u=https%3A//127.0.0.1%3A3030/index.html&c=UTF-8&d=%20
So my main question is, what am I doing wrong to access the shadowRoot using Javascript Selenium?
For reference
mocha + selenium + firefox
gitpod environment
have an earlier test that simply verifies I can connect to example.com just to prove the connection is working.

In an attempt to work around the error, I switched to the javascript executor. This raised a different error message (cyclical object).
This led me to a different stackoverflow question
https://stackoverflow.com/a/67223939/1961413
According to that answer, this is a known defect in the GeckoDriver/Firefox.
Based on that I switched to the ChromeDriver/Chrome, and was able to find the ShadowRoot.
https://www.npmjs.com/package/chromedriver
let driver = new webdriver.Builder()
.forBrowser('chrome')
.build();
await driver.get('http://127.0.0.1:3030/index.html');
let tab = await driver.findElement(state.By.css('ps-tabpanel'));
tab = await tab.getShadowRoot();

Related

Selenium-webdriver Proxy Authentication

Currently, I need to use proxy with authentication, on selenium-webdriver on Firefox.
I successfully connect to Tor using :
var option = new Options();
option.headless()
option.setPreference('network.proxy.type', 1)
.setPreference('network.proxy.socks', '127.0.0.1')
.setPreference('network.proxy.socks_port', 9050)
.setPreference('network.proxy.socks_remote_dns', true)
.setPreference('network.proxy.socks_version', 5)
let driver = await new Builder()
.forBrowser(Browser.FIREFOX)
.setFirefoxOptions(option)
.build();
But I don't find how to pass username and password for other proxy.
I looked on Selenium's github, but nothing worked. This is the exemple on the file's comment but not working:
let capabilities = new Capabilities();
capabilities.setProxy(proxy.socks('username:pass#host:port'))
// for tor
capabilities.setProxy(proxy.socks('host:port'))
let driver = await new Builder()
.withCapabilities(capabilities)
.forBrowser(Browser.FIREFOX)
.setFirefoxOptions(option)
.build();
Please help me :(
You could try using the following with setPreference itself:
.set_preference("network.proxy.socks_username", USERNAME)
.set_preference("network.proxy.socks_password", PASSWORD)

TypeError: Assignment to constant variable ,After Changing it to let/var

Im trying to set the binary path of a binary of chrome with selenium, in javascript language.
unfortunately, my knowledge in javascript is limited, and Im getting an error while trying to do so, in which I cannot solve, despite my efforts.
so without further ado, I will now share my problem, with the hope that someone with a better knowledge in javascript then me, will help me
some background:
Im triggering a function in the firebase could functions,
inside this function , I'm trying to create a selenium webdriver.
in order to do so:
I need to do those things:
chromedriver --> that work on a linux system(located inside the functions project folder)✅
chrome browser binary that is located on this machine ✅
3.then, I need to create a a chrome Options object.
a. adding an Argument so it will be headless.✅
b. setting it with a path to the chrome binary.❌
and at last, create a chrome driver with options, that I have created
currently, I'm at stage 3.b
the error that rise coming from my poor knowledge in javascript
this is the error :
TypeError: Assignment to constant variable.
here what's lead to this error
this is my code :
exports.initializedChromeDriver = functions.https.onRequest((request, response) => {
async function start_chrome_driver() {
try {
functions.logger.info('Hello logs!', {structuredData: true});
console.log("did enter the function")
const google_site = "https://www.gooogle.com";
const { WebDriver } = require('selenium-webdriver');
const {Builder, By} = require('selenium-webdriver');
console.log("will try to initialzed chrome");
let chrome = require('selenium-webdriver/chrome');
console.log("did initialzed chrome");
var chrome_options = new chrome.Options()
console.log("will try to set the chrome binary Path");
functions.logger.info('new chrome.Options()', {structuredData: true});
chrome_options = chrome_options.setChromeBinaryPath(path="/usr/bin/google-chrome");// <------- THIS IS THE LINE THAT RISE THE ERROR!
console.log("did setChromeBinaryPath");
chrome_options.addArguments("--headless");
let buillder = new Builder().forBrowser('chrome');
functions.logger.info(' did new Builder().forBrowser(chrome)', {structuredData: true});
const google_site = 'https://wwww.google.com'
await driver.get(google_site);
functions.logger.info('driver did open google site', {structuredData: true});
return "succ loading google"
}
catch (err) {
console.log('did catch')
console.error(err);
return "error loading google";
}
}
const p = start_chrome_driver().then((value,reject) => {
const dic = {};
dic['status'] = 200;
dic['data'] = {"message": value};
response.send(dic);
});
and here's the error that follows this code in the firebase functions logs:
I tried to change the chrome_options object into var/let, and looking for answers in the web , but after deploying again, and again, and again.. I feel like its time to get another perspective, any help will do.
You have an unnecessary assignment here (path=...)
chrome_options = chrome_options.setChromeBinaryPath(path="/usr/bin/google-chrome");
Just remove the assignment to path
chrome_options = chrome_options.setChromeBinaryPath("/usr/bin/google-chrome");

uploading a file using puppeteer browserWSEndpoint

I am trying to upload a file using puppeteer and browserWSEndpoint, the error message I am getting is
"Uncaught (in promise) Error: File chooser handling does not work with multiple connections to the same page".
Here is my code:
const puppeteer = require('puppeteer');
async function getTest() {
const browser = await puppeteer.connect({
browserWSEndpoint: 'wss://chrome.browserless.io'
});
const page = (await browser.pages())[0];
await page.goto('https://someWebSite');
//DO STUFF
console.log("before upload"); //code runs until here
const [fileChooser] = await Promise.all([page.waitForFileChooser(),page.click('#uploadTrigger'),]);
await fileChooser.accept(['C:\\myProgram\\pic.jpg']);
await page.click('#edit-submit');
}
getTest().then(console.log);
I must mention that if I don't use browserWSEndpoint, and use this code at the beginning instead, everything works fine.
const browser = await puppeteer.launch({headless: false, defaultViewport:null});
Honnestly I am pretty lost with browserWSEndpoint, I used info from this post How to run Puppeteer code in any web browser?
which led me to browserless.io, copied the code and it works.
Now this is my precise question, my error indicates does not work with multiple connections to the same page. How exactly am I connecting with multiple connections? Maybe I can resolve this issue and then I could use const [fileChooser].
My main issue is that I need to upload a file, using browserless
Others seem to have the same problem according to https://github.com/GoogleChrome/puppeteer/issues/4783, but using chromuim is not an option if I want to use browserless
If you are the only client connected to that browser you must be connected to a browser that doesn't support the fileChooser. You should connect to a Chromium 77.0.3844.0 (r674921) or higher.

web-bluetooth get stored data from device GATT

My goal is to get data stored in device.
Like device which is measuring temp or whatever and store it to its memory. i need to query all this data device has via Record Access Control Point (RACP).
First thought, to achieve it, was
get characteristic
start notifications
write code to descriptor
get all data via eventListener
result: throws error on starting notifications
examples used:
https://googlechrome.github.io/samples/web-bluetooth/notifications-async-await.html
https://bugs.chromium.org/p/chromium/issues/detail?id=664863
Next thought was to not starting notification since characteristic is
INDICATE, WRITE type.
So was thinking about add listener and write to descriptor code from device docs which states:
OP Code:
1 – Report stored records
even with deleted startNotifications line is throwing error
so my code example is:
const mainService = 'my correct service uuid';
const characteristicUUID1 = 'my correct char uuid';
const characteristicUUID2 = 'my correct char uuid';
const descriptorUUID = '00002902-0000-1000-8000-00805f9b34fb';
let deviceCache = null;
let serverCache = null;
let serviceCache = null;
let characteristicCacheA = null;
let characteristicCacheB = null;
let descriptorCache = null;
try {
deviceCache = await navigator.bluetooth.requestDevice({ filters: [{ name: 'my device' }] });
console.log('Connecting to GATT Server...');
serverCache = await deviceCache.gatt.connect();
console.log('Getting Services...');
serviceCache = await serverCache.getPrimaryService(mainService);
console.log('Getting Characteristics A...');
characteristicCacheA = await serviceCache.getCharacteristic(characteristicUUID1);
console.log('Start Notifications A...');
await characteristicCacheA.startNotifications();
console.log('Getting Characteristics B...');
characteristicCacheB = await serviceCache.getCharacteristic(characteristicUUID2);
console.log('Start Notifications B...');
await characteristicCacheB.startNotifications();
console.log('Add event listener...');
characteristicCacheA.addEventListener('characteristicvaluechanged', this.handleNotifications);
console.log('Getting Descriptor...');
descriptorCache = await characteristicCacheA.getDescriptor(descriptorUUID);
console.log('Write value to descr...');
await descriptorCache.writeValue(new Uint8Array([1]));
} catch (error) {
console.log(error.message, 'error');
}
Error with notifications is(with experimental chrome features it doesn't throw error):
error: GATT operation failed for unknown reason.
Error with descriptor is:
writeValue() called on blocklisted object marked exclude-writes.
Also my device is asking for pin but web is connecting without prompting anything. And so maybe it says that writing to descriptor is blocked.
How to handle pin input - no clue(once i got prompt to enter pin after enabling chrome experimental features not sure if its related).
Is my logic correct? - dont think so.
Any suggestions?
What i have investigated so far?
https://googlechrome.github.io/samples/web-bluetooth/
https://www.oreilly.com/library/view/getting-started-with/9781491900550/ch04.html
https://webbluetoothcg.github.io/web-bluetooth/
Edit: After reading this article - https://medium.com/#devdevcharlie/experimenting-with-web-bluetooth-1f1176047ddd
I think correct logic should be, write to command characteristic commands you need(like get all data). After that find correct characteristic responsible for this data from device docs, and start notifications and add eventListener and receive data.
The call to writeValue() is failing because access to the CCCD is on the blocklist. The call to startNotifications() will write to the descriptor as necessary to enable notifications.
We need to investigate this "unknown reason" for startNotifications() failing. What operating system are you using? Please follow the instructions for reporting Web Bluetooth bugs and file an issue in the Chromium project's issue tracker.
As for now, chrome is not able or got issues with communicating with protected characteristics on windows 10, on macOS it is all working perfectly. I have posted issue on chromium bug tracker if someone is in to watching it.
https://bugs.chromium.org/p/chromium/issues/detail?id=960258#c6

tor browser selenium javascript

I'm using selenium with Tor but it's not working , i saw that there is a library for doing that but only with python . Can this be done with javascript ? I tried that but it doesn't work.
const {Builder, By, Key, until} = require('selenium-webdriver');
var driver = new Builder()
.forBrowser('tor')
.build();
driver.get('https://www.google.com')
As far as I remember, this can be done only from Java and Python.
I had trouble getting the latest geckodriver (0.21.0) and Selenium (3.13.0) to fetch a web page after launching the Tor Browser Bundle. I think it may be incompatibilities with the older Firefox version Tor uses and the geckodriver but am not sure.
If you're just trying to use selenium-webdriver to use the Tor network, try this:
const webdriver = require('selenium-webdriver');
const firefox = require('selenium-webdriver/firefox');
var options = new firefox.Options();
options.setPreference('network.proxy.type', 1) // manual proxy config
.setPreference('network.proxy.socks', '127.0.0.1')
.setPreference('network.proxy.socks_port', 9050)
.setPreference('network.proxy.socks_remote_dns', true) // resolve DNS over Tor
.setPreference('network.proxy.socks_version', 5)
let driver = new webdriver.Builder()
.forBrowser('firefox')
.setFirefoxOptions(options)
.build();
driver.get('https://example.com/')
You will need to run Tor using the expert bundle or install and run it natively.
Here's what I tried for actually getting Tor Browser to automate. It launches everything correctly but never navigates to the page.
const webdriver = require('selenium-webdriver');
const firefox = require('selenium-webdriver/firefox');
var options = new firefox.Options();
options.setBinary('/home/me/Desktop/tor-browser_en-US/Browser/start-tor-browser');
options.addArguments('--detach');
(async function run() {
let driver = await new webdriver.Builder()
.forBrowser('firefox')
.setFirefoxOptions(options)
.build();
await driver.get('https://example.com/')
})();
Just to re-iterate, this second example isn't working. On Mint 18 and Tor Browser 7.5.6 (FF ESR 52.9.0) it launches Tor and the browser just fine, but will not navigate to a page.

Categories