Selenium Assert Equals to Value1 or Value2 - java

I want to test in Selenium (Java, Testng)
If my actual value is equal to one of the two values because my value can be Value1 or Value2 and both values will be correct.
If I want to assert only one equality I use this construction:
String expectedPopUpV1 = "Value1";
String expectedPopUpV2 = "Value2";
String actualPopUp = driver.findElement(By.cssSelector(Value)).getText();
Assert.assertEquals(actualPopUp,expectedPopUp);
but what should I do if I want to make something like
Assert.assertEquals(actualPopUp,expectedPopUp1||expectedPopUp1);

You can use assertTrue(boolean condition, String message) for this and give the details in the message
boolean isEqual = actualPopUp.equals(expectedPopUpV1) || actualPopUp.equals(expectedPopUpV2);
Assert.assertTrue(isEqual, "The text " + actualPopUp + " is not " + expectedPopUpV1 + " or " + expectedPopUpV2);

below option should also work using ternary operator:
Assert.assertEquals(expectedPopUpV1.equalsIgnoreCase(actualPopUp )?expectedPopUpV1:expectedPopUpV2 , actualPopUp );

There isn't any such method available. The two closest options I can think of:
if(!actualPopUp.equals(expectedPopUp1) || !actualPopUp.equals(expectedPopUp2) {
Assert.fail("insert failure messsage here");
}
or
if(actualPopUp.equals(expectedPopUp1) || actualPopUp.equals(expectedPopUp2) {
Assert.assertTrue("insert message");
}
The other option would be to really extend the Assert capability by constructing your own with a Builder pattern to be in the tune of:
AssertBuilder.with(actualPopUp).equals(expectedPopUp1).or(expectedPopUp2);
But that may be too complicated for a simple use case.

Related

Conditionally executed blocks should be reachable

sonar complain Ternary operators should not be nested.
Is there any way possible to remove this complaint since I am beginner in java I would like some help in this issue.
object form = null;
if(objs.getForm() != null)
form = objs.getForm();
String getName = form != null
? referenceObjType + form.getName()
: "" + (objs.getType() == null ? ""
: "(" //$NON-NLS-1$
+ objs.Type().getTypeName() + ")"
+ objs.getName());
What about something like this? Nested ternary operators are quite bad practice.
String getName= "";
if (form != null) {
getName = referenceObjType + form.getName();
} else {
getName = objs.getType() == null ? "" : String.format("(%s)%s",objs.Type().getTypeName(), objs.getName());
}
You need to use parenthesis to group the second ternary operator:
String getName = form != null
? referenceObjType + form.getName()
: "" + (objs.getType() == null ? ""
: "(" //$NON-NLS-1$
+ objs.Type().getTypeName() + ")"
+ objs.getName());
In java the additive operator (+) has a higher preference than the equality operator (==).
see: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
So without the parenthesis the statement
"" + objs.getType() == null
The String concatenation is done first and the result then checked against null. Since an empty String that you append something to cannot be null, this statement will always yield false and one part of your ternary operator will never be reachable.
If you are a beginner with Java I would refrain from using ternary operators so you could really understand what each statement is doing.
The issue:
I believe the else statements (the ones starting with ":") are not reachable because form will never be null. Also you are appending a string prior to checking if it is null. This will always return that it is not null.
You also need to group your second ternary operator with a parentheses like so:
String getName = form != null
? referenceObjType + form.getName()
: "" + (objs.getType() == null ? ""
: "(" //$NON-NLS-1$
+ objs.Type().getTypeName() + ")"
+ objs.getName());
It is hard to give you a complete answer since I do not know your original problem and cannot see other code but I hope this helps

Runtime boolean and String error in java?

Hi that println giving me error but it working fine if I just print them separately. Any Idea why this happening?
class StringTesting
{
public static void main(String me[])
{
String s1="Varun";
String s2="varun";
String s3="Varun";
String s4=new String("Varun");
String s5=new String("Varun");
System.out.println(" "+s1==s3+" "+s1==s2+" ");//here its giving me error
}
}
And also thanks in Advance :)
In terms of operators, + has the precedence on ==.
It means that this code :
" "+s1==s3+" "+s1==s2+" "
is translated by the compiler by something like that :
" Varun"=="Varun Varun"=="Varun "
the first part " Varun"=="Varun Varun" produces a boolean
and applying to it the "Varun " String with the == comparator, it results to :
booleanExpression == "Varun "
Comparing boolean with a String is not valid.
Anyway, even if it was, you don't want to as you want to output the boolean result of the == comparison between the objects.
So just put between parentheses the == comparisons to indicate to the compiler that operations in have to be evaluated together :
" "+(s1==s3)+" "+(s1==s2)+" "
It will produce a valid concatenation :
String + boolean + String + boolean + String
According to the Java documentation on String, the proper way to compare two strings is by using the .equals method.
System.out.println("s1 == s2 ?: " + s1.equals(s2))
By using the == operator between two Objects you are comparing memory locations, not the actual primitive value(s) contained in the object.
You can't compare booleans and strings using == and this is what is indeed happening there because of the order of operators being applied.
You can fix that using simple brackets:
System.out.println(" " + (s1 == s3) + " " + (s1 == s2) + " ");
Of course, if you do not combine results of two different operators, that will work just fine:
System.out.println(s1 == s3);
So, it was enough to just read the compilation error.

Comparison of two String return always false java

I have two variable and when i make
system.out.println(var1+" && "+var2)
I found this : "var1" && var2
var1 with quote and var2 without quote so this is why i found always false
what should i do ??
system.out.println(var1+" && "+var2)
won't compile, let alone return or result in a value.
&& can not be used for comparisons, it's used to implement the logical AND.
If you want to compare objects, you'll need to use the '==' operator for a referential comparison, or the equals method if you want to compare values.
For String instances, you have an additional equalsIgnoreCase method you can use.
so: if var1 and var2 are objects, you can try this:
System.out.println((var1 == var2)); // compare references
and
System.out.println(var1.equals(var2)); // compare values
If they are primitives, you'll need to use the '==' operator
If they are booleans you want to check:
System.out.println(var1&&var2);
will do the trick
First of all, you should check if the values you are getting in those variables are correct, and the desired.
If they are correct, then, to delete the quotes you can use the String#replace method to replace all the double quotes with an empty:
var1.replace("\"", "");
Note that here you should escape the double quote \".
Note: This answer supposes that you are comparing the Strings correctly (using String#equals).
String intint = "INT";
String gouc = "GOUc";
String fg2a = "FG2c";
String fg2c = "FG2c";
String goua = "GOUa";
affichageBroadInfos(broadInfos);
Iterator<RncBroadInfo> iter = broadInfos.iterator();
while (iter.hasNext()) {
RncBroadInfo str = iter.next();
// if (!(str.getBroadClass().equals("INT"))) { this return false
if (!(str.getBroadClass().replace("\"", "").equals(intint))) {
System.out.println("ToComprare Twoe Strings " + str.getBroadClass().replace("\"", "") + " && " + intint);
System.out.println("To Remove Interface" + str.toString());
System.out.println("type var 1" + str.getBroadClass().getClass());
System.out.println("type var 2" + intint.getClass());
System.out.println("result of Comparision " + str.getBroadClass().equals(intint));
iter.remove();
}
}

Android replaceAll and string comparison with ==

While auditing an android source code, I found a string comparison bug which used == instead of equals(). However, the app is working well surprisingly!
After some testing, I found that replaceAll() method is hiding the bug.
String description = " ";
description = description.trim();
Result1.setText(description + " == " + "" + ": " + (description == ""));
prints "==:false" as I expected. However,
String description = " ";
description = description.trim().replaceAll("\\s+|\\r+|\\n+", " ");
Result1.setText(description + " == " + "" + ": " + (description == ""));
prints "==:true"! (Android 4.4.2, API 19)
I run the same code in my desktop (javac 1.6.0_45) and it prints "==:false" as I expected.
Is it a bug in Android or is it intended behavior?
No, it's not a bug -- it's just an implementation detail leaking out.
The java compiler creates a pool of strings used in the code. The empty string is surely one of these. Any time you set a variable to the empty string at compile-time, it will point to the same instance of the empty string. So
String a = "";
String b = "";
if (a == b) {
//this will always be true in practice, although I don't know if it's guaranteed
}
Now imagine that trim() and replaceAll() are implemented differently:
String trim() {
byte[] b = getBytes();
...
return new String(b, 0, len);
}
String replaceAll (String needle, String replacement) {
String result = "";
int pos = 0;
while (indexOf(needle, pos) != -1) {
...
result = result + replacement;
pos = ...;
}
return result;
}
Because trim() calls a String constructor, it necessarily creates a new String. But replaceAll starts with an empty String and builds up. And the empty String that it starts with is the same empty string as all the other empty strings in your source code.
These are fake implementations -- it's just a hypothesis that this is how it works. It matches the observed data, but I didn't read the Android code. Still, it demonstrates that different implementations of similar functions could lead to the effect that you're seeing.
In other words, it's not a bug, but it's not a behavior you want to depend on. If you do want to depend on two strings that .equal() also being ==, you can use String.intern().

why does java starts treating everything as a String once it has encountered a string in System out statement?

i am learning java and practicing it daily,i wrote the following code and wondered about the output
class test
{
public static void main(String args[])
{
System.out.println(1+2+ " = " +10+2);
}
}
here the output was 3=102,and wondered about the following "Java starts treating everything as a String once it has encountered a string in System out statement"
can anyone explain this ?i m confused why does it accept it as string?
Java parses program text without regard to the types of expression. As motivation, consider if they were fields written after the method in the class. So, as string concatenation and addition share the same operator, we have
1+2+ " = " +10+2
is equivalent to
((((1+2)+ " = ") +10)+2)
Folding constants, we have
(((3+ " = ") +10)+2)
(("3 = " +10)+2)
("3 = 10"+2)
"3 = 102"
"Java starts treating everything as a String once it has encountered a
string in System.out statement"
It's completely wrong. System.out is a static instance of the class PrintStream.
PrintStream has many overloaded versions of the println() method and the one in your example accepts String as parameter. You are using + operator and it's for concatenation of Strings unless the operands are both numbers.
System.out.println(3+5+"."); // println(String) is invoked.
System.out.println(3+5); // println(int) is invoked.
+ with String becomes String concatenation operator and not a addition operator.
1 + 2 + 10 + 2 will be equal to 15 as a simple addition
while
1 + 2 + "+" + 10 + 2 will be treated as
1. 1 + 2 output will be 3 as it is a simple addition
2. 3 + = (String) output will be 3= because it is String concatenation
3. 3= (String) + 10 + 2 will be String concatenation and not a simple addition so output will be 3=102
(1+2) -- two integres additoin will result int 3
(3 + " = ") -- this will result int + String = String (concatination)
("3=") -- String + any thing (data type) will result String

Categories

Resources