I found a script on the Webdriver.io website that looks like this (adjusted for testing)
const { remote } = require('webdriverio');
var assert = require('assert');
;(async () => {
const browser = await multiremote({
capabilities: {
browserName: 'chrome',
},
capabilities: {
browserName: 'firefox'
}
})
await browser.url('https://www.google.com');
var title = await browser.getTitle();
assert.equal(title, 'Google');
await browser.deleteSession()
})()
In order to extract capabilities.browserName
from this script, I have been able to do so successfully with the following code:
var browsers = str.match(/\scapabilities\s*?:\s*?\{[^{}]+\}/gi); // find capabilities
console.log(browsers.length); // check length
for(let i=0; i < browsers.length; i++){
let b = browsers[i].replace(/\scapabilities\s*?:\s*?/, '') // remove capabilities: part
.replace(/'/g, '"' ) // convert single quote to double quote
.replace(/\s/g, '') // remove spaces, new lines, tabs etc.
.replace(/(\w+:)|(\w+ :)/g, function(matchedStr) { // wrap words in double quote as keys are not wrapped
return '"' + matchedStr.substring(0, matchedStr.length - 1) + '"';
});
let browser = JSON.parse(b); // parse the string to convert to object
console.log(browser.browserName); // Success !!!
}
While this method works fine, it does encounter issues when capabilities have nested properties, like so:
capabilities: {
browserName: 'chrome',
proxy: {
proxyType: 'PAC',
proxyAutoconfigUrl: 'http://localhost:8888',
}
},
This causes the code to break and only returns the browser name 'firefox'. Additionally, note that await multiremote()
can also be written as await remote()
.
Thank you in advance