isEnabled() function not working in Protractor - protractor

I want to select a date in calendar picker using protractor. Below is the snippet of code, that I have written in my script:
if (dat <= 24) {
var us_dat = element(by.xpath("(//a[text()='" + dat + "'])[1]"));
} else {
var us_dat = element(by.xpath("(//a[text()='" + dat + "'])[1]"));
if (us_dat.isEnabled() == false) {
var us_dat = element(by.xpath("(//a[text()='" + dat + "'])[2]"));
}
}
us_dat.click();
browser.sleep(1000);
where dat is a variable, where I have stored the date that needs to be selected in the calendar picker.
During debugging, I found that, when the date is greater than 24, the code is checking the "if" block present under the "else" block. However, it returns a value of undefined and hence it is skipping the action written inside the "if" block. May I know why it is returning a value of "undefined", instead of true or false please?
Please let me know if any further details are required.

Because all Protractor's API is Async and return promise. So isEnabled() is not return Boolean value directly, you can't directly compare it with a boolean value. You should do the comparing in then() or use await.
Another thing, you no need to put xpath into (), like (//a[text()='click me']).
var us_dat = element(by.xpath("//a[text()='" + dat + "'][1]"));
us_dat.isEnabled().then(function(enabled){
if (dat > 24 && enabled=== false) {
return element(by.xpath("//a[text()='" + dat + "'][2]"));
} else {
return us_dat;
}
})
.then(function(ele){
ele.click();
browser.sleep(1000);
});

try to use this method will work
isEnable: function (element) {
return element.isEnabled();
},

Related

If if statement returns true never execute that function

I've a function with if statement inside it, I want to check everytime person clicks, whenever the if statement returns true, I want that function to be removed or never again called, is it possible to achieve in swift?
func checkLevelUp(){
if CookieViewController.moneyLevel >= 3 && CookieViewController.bonusLevel >= 3 && CookieViewController.spinLevel >= 3 {
print("Level up !!!!!!!") // if this is printed, I never want checkLevelUp function to exists
}
}
You need to store this particular state outside the scope of this function.
var didLevelUp = false
func checkLevelUp() {
guard !didLevelUp else {
return // return out of the function if `didLevelUp` is not false
}
if CookieViewController.moneyLevel >= 3 &&
CookieViewController.bonusLevel >= 3 &&
CookieViewController.spinLevel >= 3 {
print("Level up !!!!!!!")
didLevelUp = true
}
}
How you store it outside the scope of the function is up to you but this is the solution you're looking for.

Is there a way to retrieve directly the value returned from a closure in Swift, with type the return type of the closure and not: () -> Type

This is a question that aims merely at elegance, but is there a way to make something to the following code work in Swift? I know the code does not work, what I want is that the result of the code within the closure is stored in a constant. And the underlying theoretical issue is whether or not it is possible to retrieve the returned value from the closure with type Int and not with type () -> Int.
Thanks a lot for any help or comment!
let tableWithBooleans: [Bool] = Array(repeating: false, count: 10)
tableWithBooleans[0] = true
tableWithBooleans[5] = true
let numberOfTrue: Int = {
var result: Int = 0
for i in 0...9 {
if tableWithBooleans[i] {
result += 1
}
}
return result
}
// I want the code to compile and numberOfTrue to be a constant equal to 2
Use a high-order function instead
let numberOfTrue = tableWithBooleans.reduce(0) { $1 ? $0 + 1 : $0 }
Now if you still want to use your closure code then you should add a () after the closing } since you are calling the code inside {} as a function
let numberOfTrue: Int = {
var result: Int = 0
for i in 0...9 {
if tableWithBooleans[i] {
result += 1
}
}
return result
}()

How to return a variable in a function in kotlin

I created a function that recieves input and compare it to a list, when find a match it return the match, in this case this match is the attribute of a class that i created.
I understand that the problem is with the return statement, so in the beginning of the function I declare the return as "Any", further more than that I'm kinda lost.
The error is this: A 'return' expression required in a function with a block body ('{...}')
class Class1(var self: String)
var test_class = Class1("")
fun giver(){
test_class.self = "Anything"
}
class Funciones(){
fun match_finder(texto: String): Any{
var lista = listOf<String>(test_class.self)
var lista_de_listas = listOf<String>("test_class.self")
var count = -1
for (i in lista_de_listas){
count = count + 1
if (texto == i){
lista_de_listas = lista
var variable = lista_de_listas[count]
return variable
}
}
}
}
fun main(){
giver()
var x = "test_class.self"
var funcion = Funciones()
var y = funcion.match_finder(x)
println(y)
}
To explain you what the problem is, let's consider the following code:
class MyClass {
fun doSomething(): String {
val numbers = listOf(1, 2, 3)
for (number in numbers) {
if (number % 2 == 0) {
return "There is at least one even number in the list"
}
}
}
}
If you try compiling it you'll get the same error message as in your question: A 'return' expression required in a function with a block body ('{...}'). Why is that?
Well, we defined a function doSomething returning a String (it could be any other type) but we're returning a result only if the list of numbers contains at least one even number. What should it return if there's no even number? The compiler doesn't know that (how could it know?), so it prompts us that message. We can fix the code by returning a value or by throwing an exception:
class MyClass {
fun doSomething(): String {
val numbers = listOf(1, 2, 3)
for (number in numbers) {
if (number % 2 == 0) {
return "There is at least one even number in the list"
}
}
// return something if the list doesn't contain any even number
return "There is no even number in the list"
}
}
The same logic applies to your original code: what should the function return if there is no i such that texto == i?
Please also note that the solution you proposed may be syntactically correct - meaning it compiles correctly - but will probably do something unexpected. The for loop is useless since the if/else statement will always cause the function to return during the first iteration, so the value "There is no match" could be returned even if a match actually exists later in the list.
I searched online, if someone has the same problem, the correct code is as follows:
class Funciones(){
fun match_finder(texto: String): Any{
var lista = listOf<String>(test_class.self)
var lista_de_listas = listOf<String>("test_class.self")
var count = -1
var variable = " "
for (i in lista_de_listas){
count = count + 1
if (texto == i){
lista_de_listas = lista
var variable = lista_de_listas[count]
return variable
} else {
return "There is no match"
}
}
return variable
}
}

Angular2, TypeError: el.toLowerCase is not a function in Autocomplete Process

When I am try to integrate autocomplete in angular2, I got TypeError i.e
el.toLowerCase is not a function.
filter() {
if (this.query !== ""){
this.filteredList = this.names.filter(function(el){
return el.toLowerCase().indexOf(this.query.toLowerCase()) > -1;
}.bind(this));
}else{
this.filteredList = [];
}
}
Try return el.toString().toLowerCase().indexOf(this.query.toLowerCase()) > -1;. If the argument el is not a string, the toLowerCase() function won't work on it.
try this.. Its working for me..
filter(event: any) {
if (this.query != "") {
this.FilteredApplicationNames = this.ApplicationNames.filter(function (el) {
return el.toString().toLowerCase().indexOf(this.query.toLowerCase()) > -1;
}.bind(this));
} else {
this.FilteredApplicationNames = [];
}
}
You can use like
return !input || (el ? ('' + el).toLowerCase().indexOf(input) !== -1 : false);
This works because the object el is cast to a string using '' + el. Since it's now a string object, we can use the toLowerCase() function.
Try this.el.toLowerCase(). If the argument el being passed in is a property of the class, it might not recognize it without this. in front of it. Let me know if this doesn't work.

Javascript create a function that returns a boolean value based on certain parameters

Thanks for taking the time to look at my problem. What I'm trying to do is create a javascript function that tests whether a sting is a particular length and also whether each element of that string can be found in another string. The function then needs to return a boolean value of either true or false depending on whether the string is valid.
Here's what I have:
N_ALPHA = 6;
N_CHOICES = 4;
ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var alphabet = ALPHABET.substring(0, N_ALPHA);
function isValidGuess(inStr)
{ var valid;
var Str = inStr;
for (i=0; i<Str.length; i++)
{ if (Str.charAt(i) === alphabet.charAt(i) && Str.length == N_CHOICES.length)
{ valid = true;
}
else
{ valid = false;
}
}
return valid;
}
This code is not working at all. It only returns false every time. Any help you could provide would be greatly appreciated. Thank you.
N_CHOICES.length return undefined, because variable N_CHOICES is number.
you have to change your condition to
if (Str.charAt(i) === alphabet.charAt(i) && Str.length == N_CHOICES)