Protractor test not executing - angularjs

I have a protractor test. I want to provide some data for a test so i can generate tests automatically.
My function is as below. The problem is that i can console log something after the opening of describe. But this is'nt the case after the it function.
The code:
bigTestFunction = function(testElements) {
testElements = JSON.parse(testElements);
for (i = 0; i < testElements.length; i++) {
var title = testElements[i].title;
var shouldText = testElements[i].should
var url = testElements[i].url;
var actions = testElements[i].action;
describe(title, function() {
it(shouldText, function() {
goToUrl(url);
for (x = 0; x < actions.length; x++) {
var action = actions[x].action;
var value = actions[x].value;
var element = actions[x].element;
var notEmpty = actions[x].notEmpty;
var nested = actions[x].nested;
if (action === 'sendKeys') {
sendKey(element, value);
}
if (action === 'click') {
click(element, notEmpty);
if (nested) {
for (x = 0; x < nested.length; x++) {
if (nested[x]['action'] === 'sendKeys') {
sendKey(nested[x]['element'], nested[x]['value']);
}
if (nested[x]['action'] === 'click') {
click(nested[x]['element'], nested[x]['notEmpty']);
}
}
}
}
}
});
});
}
}
testElements = JSON.parse(testElements);
the json:
[
{
"id": 1,
"title": "Small test one",
"should": "should start training",
"url": "https://ledmagazine.nl/home",
"actions": [
{
"id": 1,
"test_id": 1,
"element": "/html/body/div[1]/div/div/header/div/div[2]/div[2]/div/div/div/div/nav/section/ul/li[3]/a",
"action": "click",
"status": "notEmpty",
"value": "/html/body/div[1]/div/div/div[2]/div/div/div[1]/div/section",
"nested": {
"id": 1,
"action_id": 1,
"action": "sendKeys",
"element": "//*[#id=\"mce-EMAIL\"]",
"value": "dennisageffen#hotmail.com",
"created_at": null,
"updated_at": null
}
}
]
}
]
I think i'm really close but the function stops after 'describe(title, function() {...'

Propably you are missing beforeEach(function() {...} to get data.

Related

Get data from complex Json structure using NodeJs

json Structure:
{
"id" : "1",
"Data" : [
{
"name" : "abc",
},
{
"name" : "option1",
"position" : [
{
"name" : "option1",
"status" : [
{
"code" : "0",
"value" : "OFF"
},
{
"code" : "1",
"value" : "ON"
}
]
}]
} ]
}
Here,I want to get the data from above complex Json structure.How to do that,
Have tried below code but giving error like;
error: uncaughtException: Cannot read property 'status' of undefined
function myfunc(req,res){
var collectionname = db.collection("col1");
collectionname.find({}).each(function(err, doc) {
if(doc != null)
{
var fdata = [];
for(var i =0;i<doc.Data.length;i++){
fdata.push(doc.Data[i].position.status);
}
console.log("fdata............",fdata);
}
});
}
Please help with the same.
You can use foreach for prevent length undefined.
function myfunc(req,res)
{
let collectionname = db.collection("col1");
collectionname.find({}).each(function(err, doc)
{
if(doc != null)
{
let fdata = [];
for(let i in doc.Data)
{
for(let j in doc.Data[i].position)
{
fdata.push(doc.Data[i].position[j].status);
}
}
console.log("fdata............", fdata);
}
});
}
#MikaelLennholm have right, for(let i in doc.Data) works but not recommanded, be careful not to use it in prototyped or object-built arrays.
EDIT:
function myfunc(req,res)
{
db.collection('col1').find({}).each(function(err, doc)
{
if(err)
{
console.log('[INFOS] Request fail, more details:\n', err);
}
else if(doc)
{
let fdata = [];
if(doc.Data && doc.Data.length)
{
for(let i = doc.Data.length-1; i >= 0; i--)
{
if(doc.Data[i].position && doc.Data[i].position.length)
{
for(let j = doc.Data[i].position.length-1; j >= 0; j--)
{
if(doc.Data[i].position[j].status)
{
fdata = fdata.concat(doc.Data[i].position[j].status);
}
}
}
}
}
console.log("[INFOS] Datas:\n", fdata);
}
});
}
im a newbie at nodejs, hope this's correctly
//I assume this is a object or you can convert from string to object
var data = {
"id": "1",
"Data": [
{
"name": "option1",
"position": [
{
"name": "option1",
"status": [
{
"code": "0",
"value": "OFF"
},
{
"code": "1",
"value": "ON"
}
]
}]
}]
}
var statusArr = data.Data[0].position[0].status;
console.log(...statusArr);
Result: { code: '0', value: 'OFF' } { code: '1', value: 'ON' }
Parse through position array as well
function myfunc(req,res){
var collectionname = db.collection("col1");
collectionname.find({}).each(function(err, doc) {
if(doc != null)
{
var fdata = [];
for(var i =0;i<doc.Data.length;i++){
for(var j =0;j<doc.Data[i].position.length;j++){
fdata.push(doc.Data[i].position[j].status);
}
}
console.log("fdata............",fdata);
}
});
}

Convert JSON.STRINGIFY to json array length is not calculating

Hi i have excel upload functionality when i upload the excel that should save in to the mongodb(database). I have taken the excel cell value as json stringify and also i converted that as parse. now i don't how to get that json length value.
here i have attached my code
function to_json(workbook) {
debugger;
console.log(workbook);
var result = {};
workbook.SheetNames.forEach(function(sheetName) {
$scope.Sheetname=sheetName; // here sheet name is excel sheet name
MainSheetName=sheetName;
var roa = X.utils.sheet_to_json(workbook.Sheets[sheetName]);
if(roa.length > 0){
result[sheetName] = roa;
}
});
return result;
}
var HTMLOUT = document.getElementById('htmlout');
function to_html(workbook) {
HTMLOUT.innerHTML = "";
workbook.SheetNames.forEach(function(sheetName) {
var htmlstr = X.write(workbook, {sheet:sheetName, type:'binary', bookType:'html'});
HTMLOUT.innerHTML += htmlstr;
});
}
var tarea = document.getElementById('b64data');
function b64it() {
if(typeof console !== 'undefined') console.log("onload", new Date());
var wb = X.read(tarea.value, {type: 'base64',WTF:wtf_mode});
process_wb(wb);
}
window.b64it = b64it;
var OUT = document.getElementById('out');
var global_wb;
function process_wb(wb) {
global_wb = wb;
var output = "";
var output1 = "";
switch(get_radio_value("format")) {
case "json":
output = JSON.stringify(to_json(wb), 2, 2);
output1=JSON.parse(output);
break;
case "form":
output = to_formulae(wb);
break;
case "html": return to_html(wb);
default:
output = to_csv(wb);
}
if(OUT.innerText === undefined) OUT.textContent = output;
else OUT.innerText = output;
debugger;
$scope.MainInfo=output1; // this return whole data
console.log(output1);
$scope.jsondata=output1.Sheet1[0].length;// this returns undefined. Here sheet name is excel file sheetName it will taken from excel.
console.log($scope.jsondata);
my output is showing like this
{
"Sheet1": [
{
"Name": "xxx",
"Age": "22",
"Salary": "222222"
},
{
"Name": "yyy",
"Age": "23",
"Salary": "232323"
},
{
"Name": "zzz",
"Age": "23",
"Salary": "232323"
}
]
}
now i want length of Sheet1 how can i get that. can any one tell me
If you were to assign your output to obj getting the length of Sheet1 would be as simple as going obj.Sheet1.length
Try this :
var output = {
"Sheet1": [{
"Name": "xxx",
"Age": "22",
"Salary": "222222"
}, {
"Name": "yyy",
"Age": "23",
"Salary": "232323"
}, {
"Name": "zzz",
"Age": "23",
"Salary": "232323"
}]
};
var newArr = $.grep(output1.Sheet1, function( value, index ) {
if(n.Name == "xxx") {
return n;
}
});
console.log(newArr)
You can use angular.fromJson(yourJson).length functionality.
In your case, it should be angular.fromJson($scope.jsondata).length.
As per my understanding from above code, your are getting response into JSON string format so you can't perform JSON operation over it. try to convert json string to json object using JSON.parse method.Below is sample code in node js.
var parseData = JSON.parse(output1);
var parsedata1 = parseData.Sheet1;
for(var namevalue=0;namevalue<parsedata.length;namevalue++){
console.log("Name value: "+parsedata1[namevalue].Name);
}

Parse Objects and Arrays in Json data in AngularJs

I am trying to build an array of objects using the data I get from backend service using angularjs. Here is how I get the data
"mylist": [
{
"name": "Test1",
"moreInfo": {
"moreInfoText": "More test",
},
"companyInfo": {
"companyNameInfo": "ABC",
"url": "http://www.google.com",
}
},
{
"name": "Test2",
"moreInfo": {
"moreInfoText": "More test2",
},
"companyInfo": {
"companyNameInfo": "ABC2",
"url": "http://www.yahoo.com",
}
},
]
I want to parse it so I can combine it all in one array of objects like
[{"name": "Test1", "moreInfoText": "More test","companyNameInfo": "ABC", "url": "http://www.google.com"},{ "name": "Test2", "moreInfoText": "More test2","companyNameInfo": "ABC2", "url": ""}]
Try this:
var flatten = function(object) {
var newObj = {};
for (var key in object) {
var item = object[key];
if (typeof item !== 'object') {
newObj[key] = item;
}
else {
var flattened = flatten(item);
for (var k in flattened) {
newObj[k] = flattened[k];
}
}
}
return newObj;
};
var newList = [];
myList.forEach(function(object) {
newList.push(flatten(object);
});
console.log(newList) //this should be what you want

Issue with the object references in Angularjs

I have a simple function below using which am trying create dynamic div containing a d3 chart for each row from the given input "json.res_full_sk"
However, when I use apply in Angularjs, am losing the historic data that is being assigned to dyndata object.
<script>
var app = angular.module('rpPlotExampleApp', ['ui.rpplot']);
app.controller('rpPlotCtrl', function ($scope) {
$scope.dyndata={};
$scope.assign_sk = function (testjsobj) {
alert('From Angular JS'+ JSON.stringify(testjsobj));
$scope.dyndata=testjsobj;
};
});
</script>
<script type="text/javascript">
jsonList = [];
jsonList.res_full_sk = [
{ "tid": 20, "sk": [{ "name": "Banking", "value": 40, "id": 0 }, { "name": "Housing", "value": 4, "id": 1 }, { "name": "Home", "value": 4, "id": 2 }, { "name": "NET", "value": 4, "id": 3 }] },
{ "tid": 22, "sk": [{ "name": "Movie", "value": 12, "id": 0 }, { "name": "Movie2", "value": 4, "id": 1 }, { "name": "Housing", "value": 4, "id": 2 }, { "name": "Banking", "value": 4, "id": 3 }, { "name": "C", "value": 4, "id": 4 }] },
{ "tid": 24, "sk": [{ "name": "Housing", "value": 4, "id": 0 }, { "name": "Home", "value": 4, "id": 1 }, { "name": "Banking", "value": 4, "id": 2 }] }
];
arr = [];
function getObjContents(i) {
arr = $.grep(jsonList.res_full_sk, function (e) {
return e.tid == i;
});
var str = "";
for (var i = 0; i < arr[0].sk.length; i++) {
str += ","+ JSON.stringify(arr[0].sk[i]);
}
str=str.substring(1);
str = "[" + str + "]";
str_obj=JSON.parse(str);
str_fin = {};
str_obj.forEach(function (e, i) {
str_fin['d' + i] = e;
});
return str_fin;
}
function insertDiv(Num) {
array = [];
var ar1 = Num.replace('[', '');
array = JSON.parse("[" + ar1 + "]");
var i = 0;
for (i; i < array.length; i++) {
js_data={};
js_data=getObjContents(array[i]);
testjsobj=js_data;
var scope = angular.element(document.getElementById("rpPlotCtrl")).scope();
scope.$watch("dyndata", function (oldval, newval) {
}, true);
scope.$apply(function () {
scope.dyndata = js_data;
});
var id_num;
id_num = array[i];
var rChart = angular.element(document.getElementById("sktemp"));
rChart.injector().invoke(function ($compile) {
$('<div obj></div>').insertAfter("#sktemp")
var obj = $('[obj]'); // get wrapper
var scope1 = $('[obj]').scope();
// generate dynamic content
alert(JSON.stringify(scope1.dyndata));
obj.html("<div class='skpro' id='skpro" + id_num + "' name='cn'><div class='skprovis' id='skprovis" + id_num + "' >Hello " + id_num + "<div class='half-plot' style='height:95%;width:95%;margin-top:0px;margin-left:5px;'><rp-plot dsn='dyndata' point-radius='1.5' scale='log' labelled='true' class='plot-rp-plot'></rp-plot></div></div></div>");
// compile!!!
console.log(obj.contents());
$compile(obj.contents())(scope1);
scope1.$apply();
});
}
}
</script>
The problem is dyndata is being changed for each value of i in the for loop, and thus the d3 chart is updated with the lastly assigned dyndata object.
As I see the dyndata object is being assigned with different values each time for loop executes, but it is not remaining but changes with the next value of dyndata to all the charts that were created.
How can I get the historic data to persist for dyndata that is already rendered for each for loop execution?
Am new to Angularjs and not very sure how I can make use of Angular.copy() for this scenario.
Problem seems to be with below code -
var scope = angular.element(document.getElementById("rpPlotCtrl")).scope();
scope.$watch("dyndata", function (oldval, newval) {
}, true);
scope.$apply(function () {
scope.dyndata = js_data;
});
as soon as I assign new js_data value to dyndata, all the existing charts gets updated with new value.
I tried using angular.copy(scope.dyndata)=js_data.. But it didn't work out.
Can you please suggest me?

AngularJS forEach issue

I already have this foreach that sets the selected value for the state in statesList correctly for a _searchParam[key] that == the state.id.
However, if the _searchParam[key] contains more than one value in this format" "NH,TX" then the if statement fails and the match never occurs so all items get selected set to false.
var _states = [];
angular.forEach($scope.statesList, function (state, key2) {
if(_searchParams[key].indexOf(state.id) > -1)
if(state.id == _searchParams[key]) {
state.selected = true;
} else {
state.selected = false;
}
this.push(state);
if(state.selected)
$scope.states.push(state);
}, _states);
So, I tried to split the _searchParams[key] value and loop through them but it is not working. See error in comment
var _states = [];
angular.forEach($scope.statesList, function (state, key2) {
if(_searchParams[key].indexOf(state.id) > -1)
var sp = _searchParams[key].split(',');
for(i = 0; i < sp.length; i++) {
if(state.id == sp[i]) {
state.selected = true;
} else {
state.selected = false;
}
}
}
this.push(state); // get unexpected token this
if(state.selected)
$scope.states.push(state);
}, _states);
So, I moved the this.push(state) line into the if (where it really should be anyway), and get the error below.
var _states = [];
angular.forEach($scope.statesList, function (state, key2) {
if(_searchParams[key].indexOf(state.id) > -1)
var sp = _searchParams[key].split(',');
for(i = 0; i < sp.length; i++) {
if(state.id == sp[i]) {
state.selected = true;
} else {
state.selected = false;
}
}
this.push(state);
}
if(state.selected) // get unexpected token if
$scope.states.push(state);
}, _states);
Now how can I fix this as I'm not sure what is going on in this forEach() that would be causing these errors?
The statesList is defined like this:
$scope.statesList = [
{ "id": "AK", "name": "Alaska", "selected": false},
{ "id": "AL", "name": "Alabama", "selected": false},
// SNIPPED most of states
{ "id": "WY", "name": "Wyoming", "selected": false},
{ "id": "DC", "name": "District of Columbia", "selected": false},
{ "id": "MX", "name": "Mexico", "selected": false}];
It appears that you are missing an opening curley brace {. Add it, like this:
if(_searchParams[key].indexOf(state.id) > -1) {
And then take out the split part you added. Otherwise, in some cases you'd set state.selected to true then to false.

Resources