Nightwatch tests to click download link and verify contents of csv - javascript

I have a download link on a webpage which downloads a csv file. This csv file contains 100+ columns with data (numeric, text, decimal etc). The values in csv are actually taken from the webpage entered by users, when user clicks save and clicks download link these values are downloaded to csv.
How do i write a nightwatch test which can validate all of these values from web page are downloaded to csv correctly?
I have mapped all the values (that go into csv) from webpage into UI properties (such as cityName:'input[elementId]', stateName:'input[elementId]' ..so on).
I am looking for a way to perform below steps in my nightwatch tests:
Get all the values from the UI for ex: browser.getValue(cityName,function(textBoxValue){var city=textBoxValue})
Trigger Click download link
Read the downloaded file from CSV and compare the value in csv with that of the value in UI - pass if the csv value and UI value match.

Finally I was able to parse values from a csv using fs and papa parse
const papa = require('papaparse');
const fs = require('fs');
..
..
fs.stat(localFilePath, function(err, stats){
if (!err) {
console.log("FileSize: "+stats['size']);
var fileContents = fs.readFileSync(localFilePath, 'utf8');
papa.parse(fileContents,{
complete:function(results){
firstName = results.data[1][1];
}
}
}
}

Related

Get GTFS data from ZIP file in Javascript

I need to get gtfs data and show raw data on screen. I tried to use gtfs-stream module, but not sure, how to get data from it.
var gtfs_source = 'https://www.bkk.hu/gtfs/budapest_gtfs.zip'
request.get(gtfs_source).pipe(gtfs.enhanced()).on('data', (entity) => {
console.log(entity)
})
gtfs-utils has a rich set of functions, but it is unable to get data from a compressed file.

How to write to js file at specific location on key value pairs?

I am writing to a js file using protractor as follows :
index.js
var outputFile = '../Actions/data_write.js';
var username = "someusername";
var password = "somepassword";
var fs = require('fs');
var text = "userCredentials : {username : '"+username+"', password : '"+password+"'};";
fs.writeFile(outputFile, text, function(error){
if(error){
console.log(error);
}else{
console.log('data saved to '+outputFile);
}
});
My issue is I am not able to figure out how to write this data at specific location. So like right now the text is written to the entire file by replacing the old content. I want to write it at say at specific location, say I have multiple data with username and password defined in array in this file. I want to write to that specific array.
FileSystem APIs do not provide support for editing a file, rather focus on file operations.
You can read the older content into an object or a string, then navigate through it to figure out the right position for insertion, add your new content, and then write the transformed data back into the file.
If your app involves intensive file content modification with seeking to and fro, you may consider writing a native add-on with memory mapped file, for efficiency and performance.
Hope this helps.

what languages would be required to import a csv file and display the data in a bootstrap website?

I have a csv file containing data I want to report on, and I'm trying to work out what the workflow would be in terms of what programming languages I would need to implement the following scenario:
scenario first then (language) in brackets.
As an example; data extracted in step 2 might be count of number of records in a particular category: clothing!
I am not too sure of the languages but I think the workflow would need to be:
press button on bootstrap website, brings up file manager: pick file 2 import (javascript) - javascript calls python script in step 2
upon pressing ok and importing a file, the file would be opened/manipulated using (python) - results passed back from python to the website dashboard using javascript?
use either (html 5) and or (javascript) and or a visualisation language like d3 to pass the manipulated data to one of the bootstrap admin dashboards below, and display the number of sales of clothing in my report:
bootstrap admin dashboards
You can do it in the browser with Javascript.
You can catch the change event with a listener on the upload button, then use in the callback the FileReader api (https://developer.mozilla.org/en-US/docs/Web/API/FileReader).
No need to send the file to your python server.
HTML:
<input type="file" id="upload" />
Javascript:
var readCSV = function (event) {
var reader = new FileReader();
reader.onload = (function(theFile) {
var lines = theFile.target.result.split(/\n|\r/);
//code here
}
}
document.getElementById("upload").addEventListener('change', readCSV);

Upload csv file using javascript and d3

I am new to JavaScript and D3 and cannot figure out how to allow users to upload a csv file and displaying a scatterplot using d3. I am using the tag to allow user to select file. But I am not sure on what the next step should be. Is there a way to read the csv file and store it's contents in a d3 array and then displaying a graph using that array ??
Thanks in advance
Look into the d3.csv function (https://github.com/mbostock/d3/wiki/CSV). Here is a simple example
//load up the example.csv file
d3.csv('example.csv',
function(data){
//this is an object, the contents of which should
//match your example.csv input file.
console.log(data);
// do more stuff with 'data' related to
// drawing the scatterplots.
//
//-----------------------
},
function(error, rows) {
console.log(rows);
};
);
There are a number of examples online showing you how to go from a data array to a scatterplot...it's a matter of modifying those examples to fit your specific data format.

Local HTML 5 database usable in Mac Dashboard wigdets?

I'm trying to use HTML 5's local database feature on a Mac Dashboard widget.
I'm programming in Dashcode the following javascript:
if (window.openDatabase)
{
database = openDatabase("MyDB", "1.0", "Sample DB", 1000);
if (database)
{
...database code here...
}
}
Unfortunately the database-variable remains always null after the call to openDatabase-method. I'm starting to think that local databases are not supported in Widgets...
Any ideas?
/pom
No you will not be able to do the above. And even if you could then you would not be able to distribute the widget without distributing the database assuming it was a MySQL or SGLite. (not sure what you mean by HTML 5's local Db.
here are a number of ways round this:-
You can add a data source which can be a JSON file, or an XML file or and RSS feed. So to do this with JSON for example you would write a page on a server in PHP or something that accessed a database so that when the URL was called the result was a JSON string. Take the JSON string and parse it and use it in the Widget. This will let you get data but not save it.
Another way would be to use the user preferences. This allows you to save and retrieve data in the individual widget.
So
var preferenceKey = "key"; // replace with the key for a preference
var preferenceValue = "value"; // replace with a preference to save
// Preference code
widget.setPreferenceForKey(preferenceValue, preferenceKey);
You can then retrieve it with
var preferenceForKey = "key"; // replace with the key for a preference
// Preference code
preferenceForKey = widget.preferenceForKey(preferenceForKey);
The external call, you could also use REST will let you read any amount of data in and the preferences will let you save data for later reuse that will survive log out's and shut downs.
The Apple site has a lot of information about Widgets and tutorials as well thjat are worth working through.
Hope this helps.

Categories