10289

How to break out of for loop in protractor?

This is my code -

formElements[0].findElements(by.repeater(repeater)).then(function(items){ console.log(i, '>>>>>>>>>.No of items in the list --- '+items.length); (function(items){ for(var x1=0; x1<itemsToBeSelected.length; x1++){ console.log(i, '>>>>>>.Looking for item --- '+itemsToBeSelected[x1]); skip = false; (function(items, x1){ for(var x2=0; x2<items.length; x2++){ (function(items, x2){ items[x2].getText().then(function(itemName){ console.log(i, '>>>>..Verifying '+itemsToBeSelected[x1]+' with '+itemName); if(itemName == itemsToBeSelected[x1]){ console.log(i, '>>>>>.Selecting the item --- '+itemName); items[x2].findElement(by.css('.regular-checkbox')).click(); } }); }(items, x2)); } }(items, x1)); } }(items)); });

I want to break out of inner for loop when the condition itemName == itemsToBeSelected[x1] is satisfied. Tried using flag, return statements, but unable to break out from the loop.

Please suggest corrections in the code.

Answer1:

ptor.element.all(by.repeater(repeater)).then(function(products){ console.log(i, '>>>>>>>>>.Products length --- '+products.length); async.each(products, verifyName, function(err){ console.log('>>>>>>>>>>>err value --- '+err); expect(err).toBe(true); if(err){ console.log(i, '>>>>>>>>.Element is present'); }else{ console.log(i, '>>>>>>>>.Element is not present'); } }); function verifyName(product, callback){ console.log(i, '>>>>>>>>.Inside function verifyName'); product.getText().then(function(name){ console.log('>>>>>>>>>>Looking for product --- '+name); if(name==entityName){ console.log(i, '>>>>>>>>Verified the name - '+name); callback(true); } }); } });

We can achieve the same results with the help of async.each module as well. For e.g. I've posted a code in which i'm trying to find a single value.

So in relation to my question, we can click or perform any action on the element before setting callback(true). Say for e.g. here we could do - product.click();

Answer2:

Nevermind what I said before the edit, you could use caolan's async module to iterate over your array, with the detect or detectSeries function.

It should look something like this :

formElements[0].findElements(by.repeater(repeater)).then(function(items) { console.log(i, '>>>>>>>>>.No of items in the list --- ' + items.length); itemsToBeSelected.forEach(function(itemToBeSelected) { async.detect(items, function(item, next) { item.getText().then(function(itemName) { // This function will be called on each item in items, until next(true) is called console.log('Verifying ' + itemToBeSelected + ' with ' + itemName); // Here you call the callback with the truth value : return next(itemName === itemToBeSelected); }); }, function(item) { // This function is called with the first item that resulted in a true // callback value. console.log('Selecting the item --- ' + item); item.findElement(by.css('.regular-checkbox')).click(); }); }); });

Recommend

  • Sign a XML file with .pfx certificate using PHP
  • How do I include a single-quote in MSBuild item transformation seperator?
  • How to do WebDriverWait correctly with two conditions
  • Try/Catch not handling exceptions from Selenium2 FindElement
  • How can I click on a button using Selenium WebDriver with Java?
  • Selenium Parallel testing with extension methods
  • c# parsing xml with and apostrophe throws exception
  • SQLite HAVING comparison error
  • Get all stl vector elements greater than a value
  • Listen to the 'change' even of an element in the shadow DOM
  • Notepad++ - delete all lines with certain text
  • Recursion in ASP.NET Core Razor views
  • How to name a 'group' check box in Adobe Reader when wanting to fill form by FDF / XFDF
  • Raphael.js function getBBox give back NAN/NAN/NAN in IE8
  • change color of jstree node
  • matching similar elements in between two lists
  • How to populate html table with info from list in django
  • Prevent page break in text block with iText, XMLWorker
  • IE10 strips out hashtag from the URL
  • Create a link to a web page that runs a Javascript function on the page
  • Add reference to ASP.NET 5 Class Library from Framework 4.5 Class Library Project
  • Shouldn't else be indented in the below code
  • Angularjs pass function from Controller to Directive (or call controller function from directive) -
  • Adjust width of select element according to selected option's width
  • Tamper-proof configuration files in .NET?
  • ViewController With Transparent Background Entering Current ViewController With Push Transition
  • Grails calculated field in SQL
  • WPF - CanExecute dosn't fire when raising Commands from a UserControl
  • Mysterious problem with floating point in LISP - time axis generation
  • How to know when stdin is empty if it contains EOF?
  • Lost migrations and Azure database is now out of sync
  • Why ng-show works with ng-repeat but ng-if doesn't? [duplicate]
  • Modifying destination and filename of gulp-svg-sprite
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • jquery mobile loadPage not working
  • Data Validation Drop Down Box Arrow Disappearing
  • CSS Applying specific rule for a specific monitor resolution with only CSS is posible?
  • How do you join a server to an Active Directory (domain)?
  • Change div Background jquery
  • Reading document lines to the user (python)