Selenium Comparison - java

I am working on a Selenium test using WebDriver in Java. I have a compare that isn't comparing. The code looks like this:
if("20".equals(UserMaintenancePage.UserCount.getText()))
The value of the UserCount element is definitely 20 but it always fails the compare.
I tried slapping a .toString() on the end with the same result.
This one has me scratching my head.
Thanks

Use this
String val = UserMaintenancePage.UserCount.getText();
int intVal = Integer.parseInt(val);
if(intVal == 20) {
// to do your message
}

String str = Integer.toString("20");
if(str.equal(UserMaintenancePage.UserCount.getText()))
{}

I think the code might be failing due to "UserMaintenancePage.UserCount.getText()" is returning "20 " or " 20" instead of "20". So you can use .trim() function to clear blank spaces. I hope below mentioned code works for you:
if("20".equals(UserMaintenancePage.UserCount.getText().trim()))
{
// write your code..
}
Hope this helps :)

There were occasional extra characters getting into the webelement. Placing a .trim() on it everytime solved the problem.
Thanks for bearing with me on here.

Related

Trying to use an assert command in conjuntion with arraylist using selenium, but it breaks if the elements are not in the proper order

I have a string that needs to be compared to the names that are on the website. So the first thing I do is get the number of rows (because some arrays have more or fewer than 2 people in them) and then put that size into an int. String[] names come from the names that selenium is supposed to find when it goes to the website to execute this statement assertTrue(assertion.getText().contains(names[i-1])); The problem is: if the names do not appear in the order in which they appear in the array it breaks. In other words, if Mick Jagger is in li[1] and Keith Richards is in li[2], everything runs as expected. But if Keith Richards appears in li[1] it breaks. Furthermore, I am supposed to use the assertTrue command to do this. I have tried sorting, pushing whats on the web into a new ArrayList and I keep getting errors. Anyone know a good way to ensure the order isn't important and still use the assertTrue command?
Thanks,
Scott
WebElement assertion = null;
List<WebElement> assignees = driver.findElements(By.xpath(".//*[#id='assignee']/li"));
int count = assignees.size();
String[] names = {"Mick", "Keith"};
for (int i = 1; i < count; i++)
{
assertion = driver.findElement(By.xpath(".//*[#id='assignee']/li["+i+"]"));
assertTrue(assertion.getText().contains(names[i-1]));
If names represents the full string, you can just flip it. Make sure the text in your assertion (probably should be named something like assignee instead of assertion) is contained in your collection:
assertTrue(Arrays.asList(names).contains(assertion.getText());
Let me know if this won't work because a name is actually a subset of the text in assertion and I'll adjust the answer.
If they don't exactly match (which you have indicated they don't), you could use linq in c# to match this. Since you're using java you can use an additional loop. There may be a more efficient way to do this in java that I'm not aware of.
String assigneeText = assertion.getText();
boolean nameFound = false;
for(String name: names)
{
nameFound = assigneeText.contains(name);
if(nameFound)
{
break;
}
}
assertTrue(nameFound, "None of the expected names were found in the following assignee text: " + assigneeText);

Codingbat problems -- String

I came across a question on codingbat and the question is:
Given a string, return a new string made of 3 copies of the last 2 chars of the original string. The string length will be at least 2.
I solved the problem and the solution (below) is a better than mine, however, there is a problem with the solution code, when the string length is less than 2, say the length is only 1. str index will be -1. will the code still work? why does the site say the solution is correct?
public String extraEnd(String str) {
String end = str.substring(str.length()-2);
return end + end + end;
The website will try to give you as many tests as possible to make sure it's mostly free of bugs, however some do slip through the crack and make exceptions. The code below is one that works through all tests given if you want to give it a go;
public String extraFront(String str) {
if (str.length() <= 2)
return str+str+str;
return str.substring(0,2)+str.substring(0,2)+str.substring(0,2);
}
It seems that you're correct with the code, the index would be -1. Maybe follow it up with the challenge on String 2, it'll come in handy. Wish you best of luck!

In Java, how do I check if a string begins and ends the same way?

I'm learning Java, and I'm completing some problems tasked to me.
I've come across a specific problem and I feel like the answer is so simple, but I just can't find it.
I need to check if given string ends with the first two characters it begins with. For example, "edited" (begins and ends with "ed")
I've tried using the java endsWith and startsWith, but I keep getting an error
start = text.startsWith(text.substring(0,2));
Yeilds
Error: incompatible types
required: java.lang.String
found: boolean
Any help would be appreciated.
Thankyou.
You're calling startsWith when you don't need to - you know it starts with the first two characters, by definition :)
You could have:
String start = text.substring(0, 2);
boolean valid = text.endsWith(start);
Or just collapse the two:
boolean valid = text.endsWith(text.substring(0, 2));
I'm assuming you already know the string is of length 2 or more... if not, you should check that first. (And change the variable valid to whatever makes sense in your context.)
This is a dynamic code for what you need to do
let's say we have
String testak = "tesHJKLtes"
//index you need to check here 2
int ind = 2;
ind is the index you need to check
if ( testak.substring(0,ind).equals(testak.substring(testak.length()
-ind-1,testak.length()-1))){
System.out.println("yeeaaahhh");
}
and consider that this you are not limited to 2 anymore you can give ind any number you like
and it will work

Java string.equals(string) not behaving as expected

I am getting the occasional odd result from String.equals(String).
here is the code:
boolean equals(OutputHolder other){
boolean result=true;
if( ! this.speciesEng.equals(other.speciesEng))
result=false;
else if( ! this.date.equals(other.date))
result=false;
else if( ! this.gridRef.equals(other.gridRef))
result=false;
else if( ! this.recorder.equals(other.recorder))
result=false;
return result;
}
All pretty straight forward but on some objects .equals() returns false on what appear to be 2 identical strings.
This is a screenshot of the Expressions watchlist when the error occurs. As you can see this.speciesEng.equals(other.speciesEng) is returning false despite the fact that both strings appear the same.
The strings are initially from the same string resource but both have passed over an XMPP connection and back.
EDIT: To pass them over the XMPP connection, they have been concatenated with other strings to represent the whole OutputHolder. They are then separated on return using .substring(start,end). It occurred to me that it might make a difference if I made a new string from the substring but that didn't have any effect. Neither did trimming them.
I am at a loss as to how to proceed with debugging the problem. Any help or suggestions welcome.
There might be some whitespaces in there. Use String#trim method prior to calling equals.
Make sure there are no trailing spaces. So better use trim method on the strings before comparing them using equals method.
I think you should first trim both the strings and get rid of additional spaces.
That way you will be able to equate both the Strings properly.
Example Code:
String yourString = "Your String ";
//Trim the String and get rid of extra spaces before doing any comparisons.
yourString.trim();
//After trimming it, do the comparisons.
if(yourString.equalsIgnoreCase("other trimmed string"))
{
.....
}
I hope this helps.
After god knows how many hours copying and pasting from the debugger to a hex editor I have found the problem and a solution that works.
As suggested the problem was whitespaces but not in the way I or (I think) others suspected.
For some reason that I have failed to get to the bottom of, I am getting non-breaking whitespaces (0x00A0) in my strings instead of normal whitespaces (Ox0020). This appears to be happening more or less at random and I haven't found the section of code responsible yet.
The work around at the moment is to start my equals() method with:
speciesEng=speciesEng.replace((char)0x00a0,(char)0x0020);
other.speciesEng=other.speciesEng.replace((char)0x00a0,(char)0x0020);
speciesEng=speciesEng.trim();
other.speciesEng=other.speciesEng.trim();
Far from elegant but it works for the moment. I'll leave the question open for a couple of days in case anyone has more to add.
Thanks to all for the answers.
boolean equals(OutputHolder other){
boolean t1 = speciesEng.equals(other.speciesEng);
boolean t2 = date.equals(other.date);
boolean t3 = gridRef.equals(other.gridRef);
boolean t4 = recorder.equals(other.recorder);
return t1 && t2 && t3 && t4;
}

How to assert an actual value against 2 or more expected values?

I'm testing a method to see if it returns the correct string. This string is made up of a lot of lines whose order might change, thus usually giving 2 possible combinations. That order is not important for my application.
However, because the order of the lines might change, writing just an Assert statement will not work, since sometimes it will pass the test, and sometimes it will fail the test.
So, is it possible to write a test that will assert an actual string value against 2 or more expected string values and see if it is equal to any of them?
Using the Hamcrest CoreMatcher (included in JUnit 4.4 and later) and assertThat():
assertThat(myString, anyOf(is("value1"), is("value2")));
I would use AssertJ for this:
import static org.assertj.core.api.Assertions.*;
assertThat("hello").isIn("hello", "world");
It's more concise and it will give you a descriptive message when the assertion fails.
I am using the following, I hope this would help:
String expectedTitles[] = {"Expected1","Expected2"};
List<String> expectedTitlesList = Arrays.asList(expectedTitles);
assertTrue(expectedTitlesList.contains((actualTitle)));
You can use Hamcrest for this:
assertThat(testString, anyOf(
containsString("My first string"),
containsString("My other string")));
(I see Joachim just answered very similarly (+1)... i'll add this as another example.)
I read all answers, but the one that seems most compact and expressive to me is using Hamcrest's isOneOf which is already included in JUnit
assertThat(result, isOneOf("value1", "value2"));
which gives you a nice error message when failing.
java.lang.AssertionError:
Expected: one of {"value1", "value2"}
but: was "value"
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
[...]
If your using junit I'd just do something like the following:
assertTrue(myString.equals("Value1") || myString.equals("Value"));
Consider writing a custom hamcrest matcher returned by a method, in this case containsOneOf, i.e.:
assertThat(myString, containsOneOf("value1", "value2"));
In keeping with the "xUnit patterns" you should avoid conditional logic in your matcher, a loop with a break statement should suffice.
Have a look at Hamcrest and xUnit Patterns for more information.
If the content for a line is fixed you can split it at line endings before comparing. Then simply compare each line to a set of expected values.
Something like this:
Set<String> expectedValues = ...
String[] split = text.split("\n");
for(String str : split) {
assert(expectedValues.contains(str));
}
If you want to check that all expected values are present you can assert that expectedValue.remove(str) == true and assert after the loop that the Set is empty. If some lines may occur multiple times you have to use a Bag instead of a Set.
The simplest/most efficient might be
assert str.equals(result1) || str.equals(result2);
This will effectively have no overhead when you don't have assertions turned on.
Assuming the method under test returns an array, you could test using Hamcrest's arrayContainingInAnyOrder.
assertThat(result, is(arrayContainingInAnyOrder("value1", "value2", "value")))
Note: use of is wrapper is purely optional, used only as readability sugar.
I think you can just use assertTrue:
assertTrue(testString.matches("string1|string2"));
Simple solution using AssertTrue. Just create a List.of() the expected values and check is it contains the expected value.
assertTrue(List.of("expected_1", "expected_2").contains("actual))
If you do not have or can't use third-party libraries except junit core, you can use this small helper method:
public static void assertListContains(List<Object> expected, Object actual) {
if (!expected.contains(actual)) {
fail("Expected: " + expected + " Actual: " + actual);
}
}
Advantage, in contrast to other workaround answers, is that you can see what was expected and what was actual:
org.opentest4j.AssertionFailedError: Expected: [1, 2] Actual: 3
It's an old post, but I wanted to have an native easy answer, without having extra libraries to add. :)
So I did :
String e
e = "ear"
j = "jars"
assert (j == "jar" || e == "ear")
or if you want them to be both true
assert (j == "jar" && e == "ear")
This seems to me like the simplest solution ...
assert obtainedValue in [acceptedValue1, acceptedValue2]

Categories