Running Chrome ignoring Cross-Origin Chrome policy - selenium

I'm trying to run Chrome using selenium webdriver without any security restrictions as part of an experiment.
Basically, I'm trying to access iframe context from different origin then my page.
Obviously, by default I'm getting the Cross-Origin exception - that's what I want to avoid and I know that I can achieve that using a browser extension, but I want to find alternatives.
Exception that I get when trying to access iframe context:
DOMException: Blocked a frame with origin "file://" from accessing a cross-origin frame.
I've tried to set any of suggested flags that I found online and official documentation when running Chrome/Chromium:
'--allow-file-access-from-files',
'--system-developer-mode',
'-–allow-file-access-from-files',
'--disable-features=IsolateOrigins',
'--disable-web-security',
'--user-data-dir=/home/chrome-dir',
'--disable-features=CrossSiteDocumentBlockingIfIsolating'
My html page:
<html>
<body>
<iframe width="560" height="315" src="https://www.youtube.com></iframe>
</body>
</html>
Script that I'm injecting:
for (let i = 0; i < window.frames.length; i++) {
try {
const frame = window.frames[i];
console.log(frame.name, frame.document);
} catch (e) {
console.log(e);
}
}
My webdriver code:
const { Builder, Capabilities } = require('selenium-webdriver');
const fs = require('fs');
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
function constructChromeOptions() {
const options = {
'args': [
'--allow-file-access-from-files',
'--system-developer-mode',
'--force-dev-mode-highlighting',
'-–allow-file-access-from-files',
'--disable-web-security',
'--user-data-dir=/home/chrome-dir',
]
};
return options;
}
function constructChromeCapabilities(packedExtensionPath) {
const chromeCapabilities = Capabilities.chrome();
const chromeOptions = constructChromeOptions(packedExtensionPath);
const capabilities = chromeCapabilities.set('chromeOptions', chromeOptions);
return capabilities;
}
(async function init() {
let driver = await new Builder()
.withCapabilities(constructChromeCapabilities())
.forBrowser('chrome')
.build();
await driver.get(`file:///${__dirname}/iframes.html`);
const js = fs.readFileSync('scripts/inject.bundle.js', 'utf8');
driver.executeScript(js);
}();

Related

How to capture full page screenshot in Selenium 4 using Bidi API's?

I want to capture a full page screenshot in selenium 4 using Bidi API's/Chrome developer tools.
https://www.selenium.dev/documentation/webdriver/bidirectional/bidi_api/
A sample for generating console logs through Bidi apis. Want to capture the full page screenshot and store it in a local directory.
const { Builder } = require('selenium-webdriver');
(async () => {
try {
let driver = new Builder()
.forBrowser('chrome')
.build();
const cdpConnection = await driver.createCDPConnection('page');
await driver.onLogEvent(cdpConnection, function (event) {
console.log(event['args'][0]['value']);
});
await driver.executeScript('console.log("CDP connection established")');
await driver.quit();
} catch (e) {
console.log(e);
}
})()

Testing stripe.redirectToCheckout using selenium

The problem that I am experiencing is that when I click the checkout button on Stripe domain page (the selenium is losing the session ). I am using JWT tokens, so the question is how should I access localStorage to get the token and later on to pass it to the browser ( I am not sure that this will actually work, so if there is some other way please give me a hand ). I got this code before the execution of the test:
const { Before, After } = require('cucumber');
const { Builder } = require('selenium-webdriver');
const firefox = require('selenium-webdriver/firefox');
const config = require('../config');
let driver;
Before(async () => {
driver = await new Builder().forBrowser('firefox');
if (config.headlessBrowser === 'true') {
const screen = {
width: 1920,
height: 1200,
};
driver = driver
.setFirefoxOptions(new firefox.Options().headless().windowSize(screen));
}
driver = driver
.build();
driver.manage().window().maximize();
exports.driver = driver;
});
After(() => {
driver.quit();
});
And the tests for clicking the submit button and expected URL when redirected:
Then('I press stripe checkout button', {timeout: waitTime}, async () => {
await d.driver.findElement(By.className('SubmitButton')).click();
})
Then(/I should be redirected to "(.*)"/, {timeout: waitTime}, async (path) => {
await d.driver.wait(until.urlIs(config.url + path), waitTime);
});
I can see that it is trying to redirect me to the correct success URL, but unfortunately, it logs me out( I assume that is happening because the session is lost ).

self-signed certificate configuration in Selenium Standalone and webdriverio

How does one set in the configuration to accept insecure self-signed certificates.
I'm using Selenium Standalone and webdriverio.
https://github.com/vvo/selenium-standalone
https://github.com/webdriverio/webdriverio
I cannot read anywhere how to do this.
I'm suing the code below:
const assert = require('assert');
const { promisify } = require('util');
const exec = promisify(require('child_process').exec);
const selenium = require('selenium-standalone');
const webdriverio = require('webdriverio');
selenium.installAsync = promisify(selenium.install);
selenium.startAsync = promisify(selenium.start);
let browser;
let seleniumChild;
before(async function () {
this.timeout(10 * 1000);
try {
// Remove any previous hanging sessions
await exec('pkill -f selenium-standalone');
} catch (error) {
if (error.cmd !== 'pkill -f selenium-standalone') {
console.error(error);
process.exit(1);
}
}
await selenium.installAsync({});
seleniumChild = await selenium.startAsync({});
const options = {
desiredCapabilities: {
browserName: 'chrome',
},
port: 4444,
};
browser = webdriverio.remote(options);
await browser.init();
await browser.url('http://google.com');
const title = await browser.getTitle();
console.log('Title ->', title);
await browser.end();
});
describe('test', function () {
it('test', async function () {
assert.ok(true);
});
});
Since it's starting a Selenium server, I'm expecting to be able to specify this through capabilities:
Did you tried using:
"acceptSslCerts": "true"
More on this topic you can find on the Selenium github page.

How to use basic commands in webdriver.io

I'm using webdriver.io to do some browser automation. Not really testing , just saving time by automating things. I can see examples of how to use some of the functions .. but I can't seem to access them. I'm quite new to node.js
** Important ** I realise you can use browser.getAttribute - but looking at this example: http://webdriver.io/api/property/getAttribute.html
I should be able to execute getAttribute on the element object..?
var allInputs = $$('.loginForm input')
console.log(allInputs.map(function(el) { return el.getAttribute('name'); }))
My code:
var webdriverio = require('webdriverio');
var options = {
desiredCapabilities: {
browserName: 'chrome'
}
};
var browser = webdriverio.remote(options)
async function browserTest(){
await browser
.init()
.url('http://www.google.com')
.getTitle().then(function(title) {
console.log('Title was: ' + title);
})
.catch(function(err) {
console.log(err);
});
var body = await browser.element("//body")
console.log(body)
//the following line fails
console.log(await body.getAttribute("id"))
}
browserTest()

Node Promises with webdriver.io

I am trying to run selenium web scraping and failing. Please look at the code snippet.
var webdriverio = require('webdriverio');
var client = webdriverio
.remote({
desiredCapabilities: {
browserName:'chrome'
}
});
module.exports = function(some_url) {
return new Promise(function(resolve) {
client
.init()
.newWindow(some_url)
.getHTML('html')
.then(function(html) {
var some_data = someFactory(html); // do some data scraping
Promise
.all(
some_data
.some_array
.map(function (data) {
return new Promise(function(resolve) {
client
.newWindow(data.other_link)
.getHTML('html')
.then(function (html) {
var $ = cheerio.load(html); // do some more scraping
resolve(html);
})
.end();
});
})
)
.then(function (data) {
// execution never comes here while I get html in
// the mapped function
resolve(some_data, data);
});
).end();
});
This above execution never completes. It never comes out of the inner block.
Any help would be greatly appreciated.