how to compare a list of names in a table - protractor

My test scenario is to search for last name and expect whether all the names in the table are equal to the search value. I have a different function to search for the last name.
What i want now is to get all the names in the table and to test whether all the names have the same value. I want to use the below function in my page object and use it in the expect in the spec. How to do so?
I am confused how to use getText() and push them into an array and return the array so that i can use it in the expect
this.getAllBorrowerNamesInTable = function () {
element.all(by.binding('row.borrowerName')).then(function (borrowerNames){
});
};

Aside from using map(), you can approach it by simply calling getText() on the ElementArrayFinder - the result of element.all() call:
this.getAllBorrowerNamesInTable = function () {
return element.all(by.binding('row.borrowerName')).getText();
}
Then, you can assert the result to be equal to an array of strings:
expect(page.getAllBorrowerNamesInTable()).toEqual(["Borrower 1", "Borrower 2"]);

I am using the map() function to do the job:
this.getAllBorrowerNamesInTable = function () {
return element.all(by.binding('row.borrowerName')).map(function(elem) {
return elem.getText();
)};
}

You can use javascript 'push' function to add every borrower name and then we can return that array;
this.getAllBorrowerNamesInTable = function () {
var names = [];
element.all(by.binding('row.borrowerName')).then(function (borrowerNames){
borrowerNames.each(function(borrowerName) {
borrowerName.getText().then(function(name) {
names.push(name);
});
});
});
return names;
};

Related

Dart find string value in a list using contains

I have a list of numbers like below -
List contacts = [14169877890, 17781231234, 14161231234];
Now I want to find if one of the above list element would contain the below string value -
String value = '4169877890';
I have used list.any to do the search, but the below print statement inside the if condition is not printing anything.
if (contacts.any((e) => e.contains(value))) {
print(contacts[0]);
}
I am expecting it to print out the first element of the contacts list as it partially contains the string value.
What is it I am doing wrong here?
contacts isn't a List<String>, so your any search can't be true, you need turn element of contracts to string to able to use contains.
void main() {
var contacts = [14169877890, 17781231234, 14161231234];
print(contacts.runtimeType);
var value = '4169877890';
print(value.runtimeType);
var haveAnyValid = contacts.any((element) {
return "$element".contains(value);
});
print(haveAnyValid);
// result
// JSArray<int>
// String
// true
}
Not sure if contacts is an integer and value is a string on purpose or mistake, but this works in dart pad if you convert it to string:
if (contacts.any((e) => e.toString().contains(value))) {
print(contacts[0]);
}
DartPad Link.

Dart / Flutter : Waiting for a loop to be completed before continuing... (Async Await?)

I have a function which creates a sublist from a large(very large list). After creating this list, the function goes on treating it (deleting duplicates, sorting...).
As long as the list was not too big, it worked fine. But now, I get "The Getter length was called on null". I suppose, it's because the second part of the function (after the loop) starts before the sublist is completed... so it doesn't work...
How can we force the function to wait for the loop to be over to continue the rest of the treatment ?
Is it with Async /Await ? Or can we do something like "While... something is not over...", or "As soon as something is done... do that" ? (My suggestions might be naive, but I am a beginner...)
Here is the code :
List themeBankFr() {
List<Map> themeBankFr = [];
for (Word word in wordBank) {
for (Thematique wordTheme in word.theme) {
themeBankFr.add({
'themeFr': wordTheme.themeFr,
'image': wordTheme.image,
});
}
}
// convert each item to a string by using JSON encoding
final jsonList = themeBankFr.map((item) => jsonEncode(item)).toList();
// using toSet - toList strategy
final uniqueJsonList = jsonList.toSet().toList();
// convert each item back to the original form using JSON decoding
final result = uniqueJsonList.map((item) => jsonDecode(item)).toList();
// sort the list of map in alphabetical order
result.sort((m1, m2) {
var r = m1['themeFr'].compareTo(m2['themeFr']);
if (r != 0) return r;
return m1['image'].compareTo(m2['image']);
});
return result;
}
i think i have a good answer that may helps you and it will as following
first create another function to do the work of for loops and this function returns a future of list that you need like below
Future<List<Map>> futureList(List wordBank){
List<Map> themeBankFr = [];
for (Word word in wordBank) {
for (Thematique wordTheme in word.theme) {
themeBankFr.add({
'themeFr': wordTheme.themeFr,
'image': wordTheme.image,
});
}
}
return Future.value(themeBankFr);
}
after that you can use this function inside your code and use it as async await and now you will never run the below lines before you return this array like below
List themeBankFr() async {
List<Map> themeBankFr = await futureList(wordBank);
// convert each item to a string by using JSON encoding
final jsonList = themeBankFr.map((item) => jsonEncode(item)).toList();
// using toSet - toList strategy
final uniqueJsonList = jsonList.toSet().toList();
// convert each item back to the original form using JSON decoding
final result = uniqueJsonList.map((item) => jsonDecode(item)).toList();
// sort the list of map in alphabetical order
result.sort((m1, m2) {
var r = m1['themeFr'].compareTo(m2['themeFr']);
if (r != 0) return r;
return m1['image'].compareTo(m2['image']);
});
return result;
}
i think this will solve your problem and i hope this useful for you

Flutter - Map not working when addAll function call

This the code I have right now (I'm using Mobx):
#observable
ObservableMap dates = ObservableMap();
#action
void getDate() {
final Map obj = {};
final map = item['dates'].map((date) {
DateTime key = DateTime.parse(date['date']);
obj.addAll({
key: ['list']
});
});
// print(map);
dates.addAll(obj);
}
I have function to call query and call getDate function.
At my UI I just call the dates but it won't return any value. The value only return if the print syntax not comment.
Any solutionn?
You are using the map method to do something for each element of item['dates']. That doesn't work because the map operation is lazy and doesn't do anything until you start using the result. You can call .toList() on the result to make it do all the computations, but that's a roundabout way to do it.
Use forEach instead to eagerly do something for each element, or, even better, use a for-in loop:
item['dates'].forEach((date) { ... });
or
for (var date in item['dates']) {
var key = DateTime.parse(date['date']);
obj.addAll({key: ['list']});
// or just:
// obj[key] = ['list'];
}

Promise working without resolving it in protractor

The below is my page object code
this.getRowBasedOnName = function (name) {
return this.tableRows.filter(function (elem, index) {
return elem.element(by.className('ng-binding')).getText().then(function (text) {
return text.toUpperCase().substring(0, 1) === name.toUpperCase().substring(0, 1);
});
});
};
the above function is called in the same page object in another function, which is
this.clickAllProductInProgramTypeBasedOnName = function (name) {
this.getRowBasedOnName(name).then(function (requiredRow) {
requiredRow.all(by.tagName('label')).get(1).click();
});
};
but the above code throws an error in the console as requiredRow.all is not a function
but when i do the following :
this.clickAllProductInProgramTypeBasedOnName = function (name) {
var row = this.getRowBasedOnName(name)
row.all(by.tagName('label')).get(1).click();
};
this works fine and clicks the required element.
But this.getRowBasedOnName() function returns a promise, which should and can be used after resolving it uisng then function. How come it is able to work by just assigning it to a variable?
When you resolve the result of getRowBasedOnName(), which is an ElementArrayFinder, you get a regular array of elements which does not have an all() method.
You don't need to resolve the result of getRowBasedOnName() at all - let it be an ElementArrayFinder which you can chain with all() as in your second sample:
var row = this.getRowBasedOnName(name);
row.all(by.tagName('label')).get(1).click();
In other words, requiredRow is not an ElementArrayFinder, but row is.

Assign mongo selectors in find() dynamically

I have the following problem: I have an interface where a user can filter stuff out based on several inputs. There are 5 inputs. When an input is filled out I want to add it's value to the helper returning the collection. The problem I can't solve is how to do this dynamically. Sometimes the user might fill out one input, sometimes three, sometimes all 5. Within the find() method you can only write down meteor's syntax:
mongoSelector: fieldName,
This means you can only hardcode stuff within find(). But just adding all 5 selectors doesn't work, since if one of the values is empty, the find searches for an empty string instead of nothing.
I thought of doing conditionals or variables but both don't work within find because of the required syntax. What could I do to solve this?
var visitorName;
var visitorAge;
Session.set('visitorName', visitorName);
Session.set('visitorAge', visitorAgee);
Template.web.helpers({
visitors: function() {
return Visitors.find({ visitor_name: Session.get('visitorName'), visitor_age: Session.get('visitorAge') });
}
});
Template.web.events({
"change #visitor_name": function (event, template) {
visitorName = $(event.currentTarget).val();
}
});
Template.web.events({
"click #reset_filter": function (event, template) {
return Visitors.find();
$(input).val('');
}
});
http://jsfiddle.net/m5qxoh3b/
This one works
Template.web.helpers({
visitors: function() {
var query = {};
var visitorName = (Session.get('visitorName') || "").trim();
if (visitorName) {
query["visitor_name"] = visitorName;
}
//same thing for other fields
return Visitors.find(query);
}
});
Template.web.events({
"change #visitor_name": function (event, template) {
var visitorName = $(event.currentTarget).val();
Session.set('visitorName', visitorName);
}
});