Sikuli: if else statement not working correctly - sikuli

if (Checkbox is Unchecked)
{
clickImage("Chechbox")
}
else
{
clickImage("OkButton")
}

What is the error you are getting?
I use Python in my Sikuli script, and I have something like:
if (name == 'Ente'):
print('Name is Ente')
elif (name == 'Jamie'):
print('Name is Jamie')
else:
print('Name Unknown')

Correct code must be:
if (Checkbox is Unchecked):
clickImage("Chechbox")
else:
clickImage("OkButton")
Pay attention to the ":".

Related

If I type out the code of this function in the interactive shell it works, but as a function it doesn't, why?

Given a string, I want to check if it is a palindrome. A palindrome is a string that reads the same left-to-right and right-to-left. What is wrong with my code? It does not give a true or false when I checkPalindrome('stringhere'). However, when I type in the code in the interactive shell I can a true or false result.
def checkPalindrome(inputString):
inputStringlist = list(inputString)
for i in range(len(inputStringlist)):
inputStringlist[len(inputStringlist) - 1 - i] = inputString[i]
inputStringlist == list(inputString)
You have to return the result from the function. Change the last line of your code to:
return inputStringlist == list(inputString)
If you want to optimize this, you can do:
def checkPalindrome(inputString):
if inputString == inputString[::-1]:
return(True)
else:
return(False)

How do we test for elements present using || operator in selenium webdriver

I am new to webdriver, need some guidance with the following
What I am looking for is:
if (Login || Password) webelement is not present then a message for elements not present will be present
Error what is displayed
The operator || is undefined for the argument type(s) org.openqa.selenium.WebElement, org.openqa.selenium.WebElement
Using Junit at my end
Do you just want:
if(Login.isDisplayed() == false || Password.isDisplayed() == false )
{
...
}
If not, look at the examples here, here, here, here.

what is the replacement of long nested if else construct?

I have a nested if else construct like below and i wanted to replace this
with the right type of programming statement.
if($provider[0][0]=='A' && $provider[1][0]=='B'){
return 'O';
}elseif($provider[0][0]=='B' && $provider[1][0]=='A'){
return 'O';
}elseif($provider[0][0] == 'A' && $provider[1][0] == '' ){
return 'A';
}elseif($provider[0][0] == 'B' && $provider[1][0] == '' ){
return 'B';
} else{
return 'Return nothing';
}
Not really avoiding nesting, but simplifying the reading:
<?php
function isOprovider($provider) {
return $provider[0][0]=='A' && $provider[1][0]=='B'
|| $provider[0][0]=='B' && $provider[1][0]=='A';
}
function isAprovider($provider) {
return $provider[0][0] == 'A' && $provider[1][0] == '';
}
function isBprovider($provider) {
return $provider[0][0] == 'B' && $provider[1][0] == '';
}
if (isOprovider($provider)) {
return '0';
} else if (isAprovider($provider)) {
return 'A';
} else if (isBprovider($provider)) {
return 'B';
} else {
return 'Return nothing';
}
One of the options to make it more readable-
if($provider[0][0]=='A') {
// other condition(s)
} else if($provider[0][0]=='B') {
// other condition(s)
} else {
// return nothing
}
You may try switch as well. In any case you would need nested conditions.
I would do something like this:
function IsCombinationOf($first, $second, $provider) {
return ($provider[0][0]==$first && $provider[1][0]==$second) ||
($provider[0][0]==$second && $provider[1][0]==$first);
}
if(IsCombinationOf('A', 'B', $provider)){
return 'O';
}
elseif(IsCombinationOf('', '', $provider)){
return 'Return Nothing';
}
elseif(IsCombinationOf('A', '', $provider)){
return 'A';
}
elseif(IsCombinationOf('B', '', $provider)){
return 'B';
}
I would replace most of this code with data and then use a loop to select the correct answer from the data. The idea here is to separate policy and implementation. The policy is: Given different combinations of providers, what do I return? The implementation is.... well, the implementation.
The example is in Ruby, but the idea applies to any language.
The data representing the policy might look like this:
PROVIDER_COMBOS = [
['A', 'B', 'O'],
['B', 'A', 'O'],
['A', '', 'A'],
['B', '', 'B'],
]
and the code that uses it might look like this:
def lookup_provider_combo(provider1, provider2)
PROVIDER_COMBOS.each do |key1, key2, result|
if provider1[0] == key1 && provider2[0] == key2
return result
end
end
return 'Return nothing'
end
Technically speaking that if..else statement is not nested. It is flat. Also, it is technically already at the state of minimum complexity. It is fairly simple code. It does "look" messy though. And the thing that makes it look messy is it's verbosity (don't confuse verbosity/messiness for complexity).
But you are right in complaining about verbosity. Verbose code, especially one with a lot of repeated bits, harms readibility. For the code in the example, one of the first obvious thing you can do to make it more readable is to factor out the array syntax:
p1 = $provider[0][0];
p2 = $provider[1][0];
if (p1 == 'A' && p2 == 'B') {
return 'O';
} elseif (p1 == 'B' && p2 == 'A') {
return 'O';
} elseif (p1 == 'A' && p2 == '' ) {
return 'A';
} elseif (p1 == 'B' && p2 == '' ) {
return 'B';
} else {
return 'Return nothing';
}
This alone removes cruft from the code making the logic clearer. There are other things you can do to remove more repeated bits from the code above to make it even clearer but they all boil down to what the code above does: basically a table of conditions and results.
In languages where the switch statement accept strings as input you can simply concatenate the two conditions into a single string as the switch condition:
switch (join([p1,p2],',')) {
'A,B' : return 'O'; break;
'B,A' : return 'O'; break;
'A,' : return 'A'; break;
'B,' : return 'B'; break;
default : return 'Return nothing'; break;
}
This makes it even more obvious that what you're doing is consulting a table. Alternatively you can achieve a similar layout using the ternary operator:
cond = join([p1,p2],',');
return cond == 'A,B' ? 'O' :
cond == 'B,A' ? 'O' :
cond == 'A,' ? 'A' :
cond == 'B,' ? 'B' :
'Return nothing';
Admittedly that still has the cond == bit repeated. But it is slightly easier to see the table compared to the original if..else statement.
In languages that has a dictionary/associative array/hash, you can simply encode the logic in a data structure and simply read that data structure:
conditions = {
'A' : {
'B' : 'O',
'' : 'A'
},
'B' : {
'A' : 'O',
'' : 'B'
}
}
result = conditions[p1][p2];
return result ? result : 'Return nothing';
Alternatively you can also use the following data structure:
conditions = {
'A,B' : 'O',
'B,A' : 'O',
'A,' : 'A',
'B,' : 'B'
}
result = conditions[join([p1,p2],',')];
return result ? result : 'Return nothing';
Keeping the conditional logic as pure data instead of code makes it even more obvious that what we're doing is looking up a table. Another advantage of keeping the conditions as pure data is that you can potentially create the data structure at runtime by reading it from a file (or from a socket over the internet). For example, the logic can potentially be encoded in a JSON or YAML formatted file and you can make the logic programmable.
As you can see, there are many ways to do this but it depends on what features are available in your programming language. They are all of the same complexity (and most compile to the same thing). The only difference is in terms of readability and maintainability.

Whats wrong with these beginner style codes?

I'm a beginner and I was reading https://stackoverflow.com/questions/237241/what-coding-mistakes-are-a-telltale-giveaway-of-an-inexperienced-programmer. I didn't get few things.
1.Why writing something like this is frowned up on?
if (IsAManMan == true) {
Console.WriteLine("It's a MAN, man!");
}
2.what about this?
if (test) {
return true;
}
else {
return false;
}
I don't write code like above. Should it be written this way: return test? or for readability I sometimes write it like return test?true:false or return (test==true)? true:false
In this example:
if (IsAManMan == true) {
Console.WriteLine("It's a MAN, man!");
}
In most languages, if conditions always evaluate a Boolean expression, which means IsAManMan can only either be true or false. Comparing to true or false is thus unnecessary because it's implied. So we just write this instead:
if (IsAManMan) {
Console.WriteLine("It's a MAN, man!");
}
And in this example:
if (test) {
return true;
}
else {
return false;
}
Following the above example, this means test can only either be true or false, which is like saying:
if (true) {
return true;
}
else { // If it's not true, it's false
return false;
}
That's again unnecessarily verbose code, so we just
return test;
instead.
In the end, it's still a matter of preference and readability. For example, if you think if (IsAManMan == true) is easier to interpret than if (IsAManMan), you can still write it that way and the compiler won't mind.
It's just a tautology. If it rains and If it's true that it rains is exactly the same and therefore, you can (or should) leave out superfluous comparisons, checks and returns.
In both cases you're testing the value of a boolean variable. Because your value is already a boolean, there's no need to explicitly test against true because it's implied in the if statement (and similar constructs that contain a boolean test, like loops)
For example
if(isAManMan == true){
//...
}
could evaluate to
if(true == true){ //answer: true
//...
}
or
if(false == true){ //answer: false
//...
}
which is the same as just writing
if(isAManMan){
//...
}
In your 2nd example, you're examining the value of another boolean value before deciding what boolean to return.
Again,
if (test) {
return true;
}else {
return false;
}
could evaluate to
if (true == true) { //returns true
return true;
}else {
return false;
}
or
if (false == true) { //returns false
return true;
}else {
return false;
}
The if statement is redundant, because the variable already holds the value you want to return, hence this is equivalent, and preferred:
return test;
It's important to note though, that your examples are completely valid, however a good compiler will optimise them into the improved versions I gave, so ultimately it comes down to readability and convention. I believe the versions I gave are more readable and closer to the generally accepted conventions.
These codes do same things. In my opinion, its just readability.
Due to this being tagged language-agnostic:
There are languages where keywords can be used for variable names. In FORTRAN, this is a valid statement: IF IF THEN THEN ELSE ELSE.
If true is defined to be a boolean variable with value false, the proposed shorthand solutions are not the same as the more verbose ones. Neither if test, IsAMan, true, and false are not boolean values in the first place.
A real world example in Python 2.0
>>>True = False
>>>True
False
Tongue in cheek.
Say you want to return a boolean, and not the actual value of test for the 2nd case.
You could do
return !!test
in most languages, or something like
return bool(test)
which is more explicit and readable.

How do I write an if else statement in Reporting Services expression language?

I would like to write a Reporting Services "Expression" that basically behaves as the following (pseudo code)...
if ([Fields!StateProvinceId.Value] == 1)
{
return "Ontario";
}
else if ([Fields!StateProvinceId.Value] == 2)
{
return "Quebec";
}
else if ([Fields!StateProvinceId.Value] == 3)
{
return "Manitoba";
}
// ...
// more cases same pattern
I don't see this type of logic do I have to nest a bunch of IIF?
=IIF(Fields!StateProvinceId.Value = 1, "Ontario", IIF(Fields!StateProvinceId.Value = 2, "Quebec", IFF(Fields!StateProvinceId.Value = 3, "Manitoba", "Unknown Province")))
Have you tried a switch statement?
= Switch( Fields!StateProvinceId.value=1,"Ontario", Fields!StateProvinceId.value=2,"Quebec", Fields!StateProvinceId.value=3,"Manitoba")
See "decision functions" on this page for example:
http://msdn.microsoft.com/en-us/library/ms157328.aspx