For loop not executing in protractor - protractor

I am able to get the count value with the following:
element.all(by.options('type as type for type in types')).then(function(elems){
return elems.length;
})
.then(function(count){
cnt = count;
});
Then later in the code I want to use cnt in a for loop where I also use closure:
for(var x = 1;x < cnt; x++){
search_options(x);
}
function test(y){
console.log('input'+y);
}
function search_options(input){
it('tess', function(){
test(input);
});
}
The problem is that the for does not execute.
Any tips or suggestions, guidance is appreciated or point out any errors.
I have read about IIFE but I find most samples use arrays, I believe 'cnt' has resolved
Unfortunately, I have to use the for loop. 'each' is not suitable.

The problem is that cnt would only be set when the promise would be resolved by the control flow mechanism. The for loop would be executed earlier.
Instead, define cnt as a promise and resolve it in your test:
cnt = element.all(by.options('type as type for type in types')).count();
cnt.then(function (actualCount) {
for(var x = 1; x < actualCount; x++){
search_options(x);
}
});
Also see: Using protractor with loops.
Also, I'm not exactly sure if dynamically creating its this way would actually work, here are some relevant threads:
https://github.com/jasmine/jasmine/issues/830
Can I dynamically create a test spec within a callback?

Related

ranges::views::generate have generator function signal end of range

I'd like to have a generator that terminates, like python, but I can't tell from ranges::views::generate's interface if this is supported.
You can roll it by hand easily enough:
https://godbolt.org/z/xcGz6657r although it's probably better to use a coroutine generator if you have one available.
You can return an optional in the generator, and stop taking elements when a std::nullopt is generated with views::take_while
auto out = ranges::views::generate(
[i = 0]() mutable -> std::optional<int>
{
if (i > 3)
return std::nullopt;
return { i++ };
})
| ranges::views::take_while([](auto opt){ return opt.has_value();})
;

I found this example of a function that accepts a callback but it's not working?

This code is similar to our previous mean() function, except in the
following if block where we check to see if a callback has been
provided. If it has, then the callback is applied to each value
before being added to the total; otherwise, the total is calculated
using just the values from the array given as the first argument
mean([2,5,7,11,4]); // this should just calculate the mean << 5.8
mean([2,5,7,11,4],x => 2*x); << 11.6
function mean(array ,callback) {
if (callback) {
array.map( callback );
}
const total = array.reduce((a, b) => a + b);
return total/array.length;
}
console.log(mean([2,5,7,11,4,5],x => 2*x));
function mean(array,callback) {
if (callback) {
array = array.map( callback ); // <--- note changes here
}
const total = array.reduce((a, b) => a + b);
return total/array.length;
}
console.log(mean([2,5,7,11,4])); //5.8 fine
console.log(mean([2,5,7,11,4],x => 2*x)) // 11.6
You weren't off by much. Check out the exact definition of Array.prototype.map() The return value from that function is a new array with each element being the result of the callback function.
I will say, your question got me to review Array.prototype.map() and passing callback functions. Thanks!
Almost forgot.. See how simple my code is formatted? That makes it easy for someone to see what's going on quickly. Recommend you do the same when posting questions here in the future.

element.all always returning count as 0

I am trying to test a dynamic web table using protractor and trying to find the count of headers, rows and cols but always getting 0 as the count
var x = element.all(by.xpath('//table//thead//tr//th'))
x.count().then(function(c){
console.log(c);
});
I tried using element.all(by.css ) as well and it returns the same , can anyone help?
I used selenium and able to retrieve the value, so xpath is not wrong, but I have to use protractor to fetch the same.
Selenium script which is working
List col = driver.findElements(By.xpath("//div[#class='table-wrapper']//table//thead//tr/th"));
System.out.println(col.size());
html
Try the below code
var x = await element.all(by.css('table[title="status"]'))
//Add wait if the table take more time to load
x.count().then(function(c){
console.log(c);
});
In general, you should avoid xpath since it's very inefficient.
This should work for you:
var table = element(by.css('table.table'));
table
.element(by.css('thead'))
.all(by.css('tr th'))
.count()
.then(function(count) {
console.log('count:',count);
});

Protractor- ElementFinder returning unexpected values

I am writing a protractor test case to compare the name(s) of the displayed data is same as the searched name.
Even though my test case works fine, I am not able to understand what is happening. Because when i expect the name to compare, it compares as expected, but when i print the elementFinder's(rowData)(i have attached the output screen shot here) value in console.log, it show a huge list of some values which i am not able to understand?
PS: I am a newbie to protractor`
This is the testCase:
it('should show proper data for given last name', function () {
var searchValue='manning';
searchBox.sendKeys(searchValue);
search.click();
element.all(by.binding('row.loanNumber')).count().then(function(value) {
var loanCount = value;
for (var i = 0; i < loanCount; i++) {
var rowData = element.all(by.binding('row.borrowerName')).get(i).getText();
expect(rowData).toMatch(searchValue.toUpperCase());
console.log(rowData,'*****',searchValue.toUpperCase());
}
});
});`
And give me valuable suggestions about my style of code
rowData is a promise (not a value), so when you console.log it, you get the promise object
Protractor patches Jasmine to automatically resolve promises within the expect(), so that's how it knows to resolve the value and compare to the expected result.
If you want to console.log the value, you need to resolve the promise with .then() to get the value.
rowData.then(function(rowDataText) {
console.log(rowDataText);
)}
This is pretty much everyone's first question when they start using protractor. You will want to learn how promises work if you want a good understanding of how to manipulate them.

What does for (;;) mean in Perl?

I was looking though a fellow developers code when I saw this ..
for (;;){
....
....
....
}
I have never seen ";;" used in a loop. What does this do exactly?
It loops forever. ';;' equates to no start value, no stop condition and no increment condition.
It is equivalent to
while (true)
{
...
}
There would usually be a conditional break; statement somewhere in the body of the loop unless it is something like a system idle loop or message pump.
All 3 parts are optional. An empty loop initialization and update is a noop. An empty terminating condition is an implicit true. It's essentially the same as
while (true) {
//...
}
Note that you it doesn't have to be all-or-nothing; you can have some part and not others.
for (init; cond; ) {
//...
}
for (; cond; update) {
//...
}
for (init; ; update) {
//...
}
Just like in C, the for loop has three sections:
a pre-loop section, which executes before the loop starts.
a continuing condition section which, while true, will keep the loop going.
a post-iteration section which is executed after each iteration of the loop body.
For example:
for (i = 1, acc = 0; i <= 10; i++)
acc += i;
will add up the numbers from 1 to 10 inclusive (in C and, assuming you use Perl syntax like $i and braces, in Perl as well).
However, nothing requires that the sections actually contain anything and, if the condition is missing, it's assumed to be true.
So the for(;;) loop basically just means: don't do any loop setup, loop forever (breaks notwithstanding) and don't do any iteration-specific processing. In other words, it's an infinite loop.
Infinite loop. A lot of the time it will be used in a thread to do some work.
boolean exit = false;
for(;;) {
if(exit) {
break;
}
// do some work
}
Infinite Loop (until you break out of it).
It's often used in place of:
while(true) { // Do something }
It's the same as
while(true) {
...
}
It loops forever.
You don't need to specify all of the parts of a for loop. For example the following loop (which contains no body, or update token) will perform a linear search of myArray
for($index = -1; $myArray[++$index] != $target;);