Nathan Cooper Nathan Cooper - 2 months ago 18
TypeScript Question

Turn javascript webdriver.Capabilities into string

Using protractor (selenium) in typescript I can call

to get an object that looks something like this:

Capabilities {
'acceptSslCerts' => true,
'applicationCacheEnabled' => false,
'browserConnectionEnabled' => false,
'browserName' => 'chrome',
'chrome' => { chromedriverVersion: '2.24.417431 (9aea000394714d2fbb20850021f6204f2256b9cf)',
userDataDir: 'C:\\Users\\***\\AppData\\Local\\Temp\\scoped_dir12788_29679' },
'cssSelectorsEnabled' => true,
'databaseEnabled' => false,
'handlesAlerts' => true,
'hasTouchScreen' => false,
'javascriptEnabled' => true,
'locationContextEnabled' => true,
'mobileEmulationEnabled' => false,
'nativeEvents' => true,
'networkConnectionEnabled' => false,
'pageLoadStrategy' => 'normal',
'platform' => 'Windows NT',
'rotatable' => false,
'takesHeapSnapshot' => true,
'takesScreenshot' => true,
'version' => '54.0.2840.71',
'webStorageEnabled' => true }

That what it looks like when feed through console.log. But how can I get this string representation in my code?

returns nothing.
, also nothing.
for(let foo in caps)
doesn't yield anything either. the object looks like some kind of map. I want to save this object to a file, what special magic does console.log do and how can I do it?

If someone knows what kind of object this is that also would be helpful. I thought it might be an ES6 map, but unlike the example in Converting ES6 Maps to and from JSON, it's not an iterator.


The method getCapabilities returns an ES6 MAP object. It doesn't seem possible to directly serialize this object. So your best chance is probably to serialize the entries:

browser.driver.getCapabilities().then(caps => {
  var entries = Array.from(caps.entries());

Or to convert the entries to an Object and then to serialize it:

browser.driver.getCapabilities().then(caps => {
  var obj = Array.from(caps.entries()).reduce((o, v) => (o[v[0]] = v[1], o), {});