How to check the value of a dropdown? - angularjs

I am trying to read (using protractor and BDD) the default values of certain dropdowns. To do this, I have tried several lines of code but none of them works.
Here is the code that I am using, the console.log is not being executed for some reason. Why?
checkDropdownAssignment: function(dropdown,defaultValue) //Function to check the value assigned to a dropdown
{
let texto=[];
$$('* option:checked').each(function(element, index) { // Execute this operation for each element of the website
element.getText().then(function (text) { //Get text of the element in order to compare it with the endpoint afterwards.
texto.push(index.toString()); // Add one more component to the vector
texto[index]=text; // Save the text in a variable visible outside of this function.
console.log(texto[index]);
});
});
},
Thanks in advance.

Try this:
$$('*option:checked').all(by.tagName('option')).getAttribute('label').then(function(value){
for (var i=0; i<value.length; i++){
expect(value).toContain(arayOfExpectedResults[i]);
}
});
Here you get an array with all the labels inside the dropdown and compare them with the expected result, or use the array the way you want.

Related

Form Submit Iterating for Existing Username React

I'm trying to create a sign up page where it first iterates through existing accounts and submits when input account is available; otherwise, it returns an error message if just one element matches.
I first tried .map, but it iterates through the entire array and still submits if one value is false. I then tried .find, but still produces the same result. Afterwards, I tried switch, case and could only return the proper outcome with ==. Lastly, I tried .find and .map using .includes but, again, no luck.
function handleSubmit(e) {
e.preventDefault();
accounts.find(acc => {
if (acc.username.includes(formData.username)) {
console.log("taken");
} else {
some post request code
}
How can I create a function that only produces one outcome if one of many elements meets the condition? Thanks for the help
You should assign the function that finds or filters the username to a variable and then create an if statement using that variable.
To return the first matching object in the array of accounts
const matchingAccount = accounts.find(account => account.username.includes(formData.username);
To return an array of matching account objects
const matchingAccounts = accounts.filter(account => account.username.includes(formData.username);

JSON object update giving strange result

Hi I am updating a json object which has internal array and each object of array has id element. Based on id element I update one of the element which matches the inout id, but it is modifying more than one object. My source code and console logs are given below which will help in understanding the issue.
function
updateUserData(event,id){
var elementName=event.target.name;
console.log('id='+id+', element name='+elementName);
var userData=this.state.user_data;
console.log('User Data before change ='+JSON.stringify(userData));
for(var i=0;i<userData.sports.length;i++){
for(var j=0; j<userData.sports[i].ticket_detail.length;j++){
if(userData.sports[i].ticket_detail[j].id==id){
for(var k=0;k<userData.sports[i].ticket_detail[j].ticket.length;k++){
if(userData.sports[i].ticket_detail[j].ticket[k].paramname==event.target.name){
userData.sports[i].ticket_detail[j].ticket[k].value=event.target.value;
console.log('user data after change ='+JSON.stringify(userData));
this.setState({user_data:userData});
return;
}
}
}
}
}
}
console logs
value=h
id=0, element name=name
User Data before change =
{"total_tickets":3,"total_amount":2124,"htmlid_counter":3,"sports":[{"name":"Badminton","ticket_detail":[{"cat":"Men Singles","formid":102,"ticket":[{"label":"Name:","paramname":"name","type":"text","value":""},{"label":"Email:","paramname":"email","type":"text","value":""},{"label":"Phone:","paramname":"phone","type":"text","value":""}],"id":0},{"cat":"Men
Singles","formid":102,"ticket":[{"label":"Name:","paramname":"name","type":"text","value":""},{"label":"Email:","paramname":"email","type":"text","value":""},{"label":"Phone:","paramname":"phone","type":"text","value":""}],"id":1},{"cat":"Men
Singles","formid":102,"ticket":[{"label":"Name:","paramname":"name","type":"text","value":""},{"label":"Email:","paramname":"email","type":"text","value":""},{"label":"Phone:","paramname":"phone","type":"text","value":""}],"id":2}]},{"name":"Carrom","ticket_detail":[]}],"tournament_id":1}
user data after change =
{"total_tickets":3,"total_amount":2124,"htmlid_counter":3,"sports":[{"name":"Badminton","ticket_detail":[{"cat":"Men Singles","formid":102,"ticket":[{"label":"Name:","paramname":"name","type":"text","value":"h"},{"label":"Email:","paramname":"email","type":"text","value":""},{"label":"Phone:","paramname":"phone","type":"text","value":""}],"id":0},{"cat":"Men
Singles","formid":102,"ticket":[{"label":"Name:","paramname":"name","type":"text","value":"h"},{"label":"Email:","paramname":"email","type":"text","value":""},{"label":"Phone:","paramname":"phone","type":"text","value":""}],"id":1},{"cat":"Men
Singles","formid":102,"ticket":[{"label":"Name:","paramname":"name","type":"text","value":"h"},{"label":"Email:","paramname":"email","type":"text","value":""},{"label":"Phone:","paramname":"phone","type":"text","value":""}],"id":2}]},{"name":"Carrom","ticket_detail":[]}],"tournament_id":1}
You can see from the abobe json , even though input id=0, but json object with id 0 , 1 and 2 are modified. Can someone help me to resolve this issue.
You are doing a asynchronous setState at each iterations. that's probably why you are getting a weird result.
You have to build a new array, then, after your array is what you finally want in your state, you can do:
this.setState({ user_data: newArray })
I found the issue. This issue was happening because each element of array was havinv reference to a common element. That was the reason when I was modifying any one of them, all were been modified.

There is issue $scope value doesn't bind to ng-model at first time load

I want to display some default template in text area at very first loading.I tried to get that output but my code doesn't work.I put all breakpoints that value change.that also doesn't work. When second time data will display on text area.
<textarea meditor style="height: 100%;margin-top: 10px;border: 1px solid lightgray;" ng-model="activity.GoCode" name="gocode" required>{{activity.GoCode}}</textarea>
function getGoCode(data) {
if (data.GoCode == undefined) {
$scope.activity.GoCode = "test";
} else {
$scope.activity.GoCode = data.GoCode;
};
}
I Found solution for this
I set a value initially for this variable (activity.GoCode) before that I create
'$scope.activity' object and then assign value for when data is loaded.
$scope.ReadSampleGoFile = function () {
$http.get('settings/sampleGO.txt').then(function (data) {
//use the file data here
// console.log(data.data);
$scope.activity=[];
$scope.activity.GoCode=data.data;
});
}
When does your function getGoCode() is called, upon page load ?
I see one issue with your code even if it is not probably the problem here, having:
ng-model="activity.GoCode"
and
{{activity.GoCode}}
is redundant as explain here and will cause an exception in Internet explorer (Error: Invalid Argument) as explain here
Also what is the meditor directive doing, it is possible that this directive is the source of your problem as I don't see why your code would not work

TypeError: push is not a function

Here is my code for factories.html
af.factory("PurchaseFactory",function(){
var productlist={products:[]};
return{
getpurchaseCart:function(){
return productlist;
},
addPurchaseCart:function(products){
productlist.products.push(products);
}
}
})
For Services.html
as.service("PurchaseService",function(PurchaseFactory){
this.getAllPurchase=function(){
return PurchaseFactory.getpurchaseCart();
}
this.addPurchase=function(products)
{
PurchaseFactory.addPurchaseCart(products);
}
})
For Controller.html
ac.controller("PurchaseController",function($scope,PurchaseService){
$scope.savepurchase=function(products){
if($scope.products._id==undefined){
$scope.products=angular.extend($scope.products,$scope.sizes)
PurchaseService.addPurchase($scope.products);
}
}
}
Here i have an another function in the same controller, as
$scope.saveorder=function(cartorder){
$scope.Mainpurchaselist=angular.extend($scope.cartorder,$scope.getpurchaseList)
CartService.addPurchaseCart($scope.Mainpurchaselist);
$scope.getpurchaseList.products={}
$scope.cartorder={}
$scope.products={}
$rootScope.isLogin=false;
CartService.deletecartyPurchase(idx);
Notification.success({message: 'your Cart Saving Successfully', delay: 1000});
}
In my HTML file, i have a button with function as savepurchase(products).
For first time its saving data but from second time its showing me error as TypeError:productlist.products.push is not a function. If I refresh the page its again saving the data, but continuously its not working.Let me know where the code goes wrong.
Here $scope.getpurchaselist.products={} is used to make a data null for every new purchase. If I remove the $scope.getpurchaselist.products={}, its working fine with no error but the problem is that the list is not getting null.
SO finally i have two ways
1. either to make it null by writing $scope.getpurchaselist.products={} but error as productlist.products.push is not a function let me know to overcome that error
2. Or to remove that line and let me know, how to make it null
I think, $scope.getpurchaseList.products={} is effecting you. As it passing an empty object instead of array as soon as the controller load. So, try removing it.
The condition if($scope.products._id==undefined) is breaking you. please check the logic you wrote. keep in mind $scope.products and products both are different variable in savepurchase function.
After extending $scope.products first time, condition return false.
change the $scope.products into just products. because u pass the products as paramater to the savepurchase() function. so no need to define as scope.products in ur controller

How to get a selected object within other stuff?

I have a multiselect grid where I can get schools.getSelectionModel().getSelection();
there is an object called data, I want to get a field within the data; lets say school_name
How I'll do it?
I've tried
schools.getSelectionModel().getSelection().data
schools.getSelectionModel().getSelection(data)
schools.datagetSelectionModel().getSelection()
they did not work.
You have to use Ext.each to iterate over the array of records..
Ext.each(schools.getSelectionModel().getSelection(), function(record, index, allRecords) {
console.log(record.get('school_name');
});
This:
schools.getSelectionModel().getSelection()[0].get('school_name')
should give you a 'school_name' field from first row selected (which is also a first record in selection).
To iterate over all selected rows do:
var selectedSchools = schools.getSelectionModel().getSelection();
for (i in selectedSchools) {
console.log(schools[i].get('school_name')); //this will log school name to firebug console - you can do whatever you need
}

Resources