Parsing data in javascript with selenium - javascript

I am trying to read data from a json file in javascript. I can read succesfully read from it. When passing in the data in the code it throws errors.
Any help will be appriciated.
My code is as follows :
module.exports = {
'#tags': ['TC2'],
"LAUNCHURL" : function (browser) {
var fs = require('fs');
fs.readFile('C:/NightWatch_Automation/credentials.json', 'utf8', function (err, data) {
if (err) throw err; // we'll not consider error handling for now
var mydata = JSON.parse(data);
var url_get = mydata.credentials[0]['url'] //Passing data works here
browser
//opens salesforce and checks title to match 'Login | Salesforce'
.url(url_get)
.waitForElementVisible('//body', 1000)
//.assert.title('Login | Salesforce')
});
},
"Login": function(browser) {
var fs = require('fs');
fs.readFile('C:/NightWatch_Automation/credentials.json', 'utf8', function (err, data) {
if (err) throw err; // we'll not consider error handling for now
var mydata = JSON.parse(data);
var email = mydata.credentials[0]['email']
browser
.useXpath()
.click("//a[contains(text(),'Sign In')]")
.waitForElementVisible('//body',1000)
.setValue("//input[#aria-label='Enter email address']", email )//Passing data does NOT work here
.click("//button[#type='button']")
.waitForElementVisible("//input[#id='password']")
.setValue("//input[#id='password']","password12345")
.click("//button[#type='button']")
});
}
};
My json file is as follows
{ "credentials": [ {"url": "https://www.walmart.com", "search": "bandaids", "email" : "test#yahoo.com", "password" : "password12345"}] }
Further info
When the values are hard coded, the script works. As soon as a attempt to pass in data in breaks the code and wont complete. Using nightwatch as a test tool.

The paths are different which might be the problem:
LAUNCHURL: fs.readFile('C:/NightWatch_Automation/credentials.json'
Login: fs.readFile('C:/Users/NightWatch_Automation/credentials.json'

Related

Redis stream command passing message with parsed value

I am creating the client using Redis and node js, I want to add a message to stream with the below format. I am facing an issue when trying the parse the message with the request body value.
async function userRegister (request) {
var userID = request.body.userID
var username = request.body.userName
redisClient.xadd(
'user:create',
'*',
'module',
`userRegister`,
'request_id',
'2312432434',
'message',
`{"module":"*","user_id":"1","username":"fffff"}`, // this works bcoz its string.
`{"module":"*","userID":"1","username":"here i need pass the above request body value"}`,
function (err, resp) {
if (err) {
console.log(err)
} else {
console.log(resp)
}
}
)
}

Extract text from multiple tweets in JSON format

I have extracted 1000 tweets that mention my internship company. So, I received the data in JSON format. Now, I am trying to extract the text for each 1000 tweets of the JSON
Since the JSON is stored as notepad, so I used require('fs') and also readFileSync because of synchronous actions
const fs=require('fs')
var lineTNB = fs.readFileSync('mentionTNB.json','utf8',function(err,data) {
if (err) throw err;
obj = JSON.parse(data);
});
var i;
for (i=0;i<lineTNB.length;i++){
console.log(lineTNB[i].text);
}
The expected values would be
"text": "string1234"
"text": "string7682"
The actual values:
undefined
undefined
Assuming you still want it to be sync:
var res = JSON.parse(fs.readFileSync('mentionTNB.json', 'utf8'));
console.log(res)
If you actually meant you want it async with callback:
var res = fs.readFile('mentionTNB.json', 'utf8', function (err, data) {
if (err) throw err;
return JSON.parse(data);
});

Node.js - Get a single XML value

I am simply trying to output a single value (thumbnail) of an XML file in Node.js. I feel like I am so close but can't figure it out.
var request = require('request');
request('https://boardgamegeek.com/xmlapi/game/1', (error, response, body) => {
if (error) { return console.log(error); }
console.log(body.thumbnail);
});
You need a XML parser, for example xml2js :
var request = require('request');
var parseString = require('xml2js').parseString;
request('https://boardgamegeek.com/xmlapi/game/1', (error, response, body) => {
if (error) { return console.log(error); }
parseString(body, function (err, result) {
console.dir(result);
});
});
Double check by using the console to see all of body I.e:
console.log(body)
Then you will see the options you have available. Show us what you get and we could be more specific or it may be enough for you to work out at a glance. You are on the right track. It just depends on the data structure that is there for you.

NodeJs: can't write a file

I'm new in node, for practice i thought to develop a weather commandline application, but i found a problem with ajax request, i'm usually to use $.ajax of jquery but it doesn't works, ( I've tried to require jquery ). I've solved this problem with another module.
Now the problem is: when i try to print json information on the coords.json and next read it with read-json module there are some "\" & "\n" everywhere in the string, i've tried to replace it with regex and fs module but it doesn't re-write the file... why?
Here the full code:
// index.js
// modules
const program = require('commander');
const clear = require('clear');
const chalk = require('chalk');
const request = require('ajax-request');
const fs = require('fs');
const json = require('read-data').json;
const writeJson = require('write-json');
// Forecast.io Key
const key = "*************";
const freegeoip = "http://freegeoip.net/json/";
let latitude = 0,
longitude = 0 ;
// forecast.io api url
const url = `https://api.darksky.net/forecast/${key}/${latitude},${longitude}`;
// initialize myData with the freegeoip datas
let myData = request({
url: 'http://freegeoip.net/json/',
method: 'GET',
data: {
format: 'json'
},
}, function(err, res, body) {
writeJson('test.json', body, function(err) {
if (err) console.log(err);
});
});
fs.readFile('test.json', 'utf8', function (err,data) {
let result = data.replace(/[\\~#%&*<>?|\-]/g, '');
fs.writeFile('test.json', result, 'utf8', function (err) {
if (err) return console.log(err);
// if i do this is normal json
// console.log(result)
});
});
and the output in the file is:
// coords.json
"{\"ip\":\"**.**.**.**\",\"country_code\":\"IT\",\"country_name\":\"Italy\",\"region_code\":\"62\",\"region_name\":\"Latium\",\"city\":\"Rome\",\"zip_code\":\"00119\",\"time_zone\":\"Europe/Rome\",\"latitude\":**.*,\"longitude\":**.**\"metro_code\":0}\n"
but if i print it in console it's normal...
I really recommend that you use JSON.parse. It will parse your json and put it into a variable you can use:
fs.readFile('test.json', 'utf8', function (err,data) {
data = JSON.parse(data); // Yay you can use anything from the JSON
}
The \ are there to escape the quotes so that they don't end the string. They shouldn't affect anything, and are actually necessary. Have you tried it without the regex? That could be breaking things if it actually removes the .

Node.js - howto block around async call. Or non-blocking xmltojs lib

I'm over my head at the moment.
I'm new to node and writing a passportjs module for Freshbooks. There's a Passport function I'm trying to implement that get's a user's profile.
This code uses Passport's OAuth foo to make a request.
this._oauth.post(url, token, tokenSecret, post_body, post_content_type, function (err, body, res) {
if (err) { return done(new InternalOAuthError('failed to fetch user profile', err)); }
try {
var parser = require('xml2json');
var json = parser.toJson(body); //returns a string containing the JSON structure by default
var util = require('util');
console.log(util.inspect(json));
var profile = { provider: 'freshbooks' };
profile.id = json.response.staff.staff_id;
profile.displayName = json.response.staff.first_name + ' ' + json.response.staff.last_name;
profile.name = { familyName: json.response.staff.last_name,
givenName: json.response.staff.first_name };
if (json.response.staff.email) { profile.emails = [{ value: json.response.staff.email }]; }
profile._raw = body;
profile._json = json;
console.log(util.inspect(json));
done(null, profile);
} catch(e) {
done(e);
}
});
I get a response. It's xml. I'm converting it to JSON, but I don't want that actually. I want a plain-old javascript object.
I looked at https://github.com/Leonidas-from-XIV/node-xml2js but the examples don't show how to get the result out.
var parseString = require('xml2js').parseString;
var xml = "<root>Hello xml2js!</root>"
parseString(xml, function (err, result) {
console.dir(result);
});
What do I do to block around this code till the call is complete and get result out? I'm not sure how to merge these two callbacks together.
you can ask xml2json to return object:
var json = parser.toJson(body, {object: true});
if you decide to use async parser then just put your done callback inside json result handler. There is no need to "block" async function:
var parseString = require('xml2js').parseString;
parseString(body, function(err, json) {
// handle error: return done(err)
// do your logic if no error
// ...
// profile._json = json;
// ...
//
// 'return' result
done(null, profile);
});

Categories

Resources