How to remove white spaces while comparing array lists - java

//View Company-View Project
driver.manage().timeouts().implicitlyWait(100000, TimeUnit.SECONDS);
List<WebElement> rows10 = driver.findElements(By.xpath("//*[#class='content-
wrapper']/div[2]/div[1]/div[1]/div/p|//*[#class='content-
wrapper']/div[2]/div[1]/div[4]/div/p|//*[#class='content-
wrapper']/div[2]/div[1]/div[5]/div/p|//*[#class='content-
wrapper']/div[2]/div[1]/div[7]/div/p|//*[#class='content-
wrapper']/div[2]/div[1]/div[8]/div/p|//*[#class='content-
wrapper']/div[2]/div[2]/div[6]/div/p"));
List<String> all_elements_text10 = new ArrayList<>();
for(int i=0; i<rows10.size(); i++) {
all_elements_text10.add(rows10.get(i).getText());
System.out.println(rows10.get(i).getText());
}
List<WebElement> rows11 = driver.findElements(By.xpath ("/html/body/div/div[2]/div[2]/div[1]/div[2]/div/p|/html/body/div/div[2]/div[2]/div[1]/div[3]/div/p|/html/body/div/div[2]/div[2]/div[1]/div[9]/div/p|/html/body/div/div[2]/div[2]/div[2]/div[1]/div/p|/html/body/div/div[2]/div[2]/div[2]/div[2]/div/p|/html/body/div/div[2]/div[2]/div[2]/div[3]/div/p|/html/body/div/div[2]/div[2]/div[2]/div[4]/div/p|/html/body/div/div[2]/div[2]/div[2]/div[5]/div/p"));
List<String> all_elements_text11 = new ArrayList<>();
for(int i=0; i<rows11.size(); i++) {
all_elements_text11.add(rows11.get(i).getText());
System.out.println(rows11.get(i).getText());
}
Assert.assertEquals(all_elements_text10, all_elements_text11);
System.out.println("All Dropdown Fields matched in View Company-Project");
}
Here i am comparing the two array lists. By executing the above code i got the below error.The second array list is having one space.How can i remove that white space and compare the lists.
java.lang.AssertionError: Lists differ at element [3]: Selenium != Selenium expected [Selenium] but found [Selenium ]

Try this.
rows10.get(i).getText().replaceAll("\\s+", "")
rows11.get(i).getText().replaceAll("\\s+", "")
Hope this help!

Related

how to build an string array from a string and integers in java

I have to put in a string array some values resulting from several parsed html pages. So the first value it's a name and all the others are numbers. After I must return the array to main to print. Obviously I make something wrong .
this is part of my newbie code...
String[] ret = null;
int y = 0;
for (Element h1 : h1s) {
// Using Jsoup to scrape the html file and find H1 text
h1_id = h1.className();
// I put here the text of H1
h1_text = h1.text();
if (h1_id.equals("ezomat-logo-text ezCSS")) {
// jump to the next h1
} else {
// I want to put the txt as the first array place
ret[y] = "'" + h1_text + "'";
}
i = 0;
// found the number values single integers with comma
for (Element image : images) {
Imm[i] = "," + imageName;
i++;
}
i = 0;
y = 1;
// y = 1 because I want to start from the second position.
for (Element image : images) {
ret[y] = Imm[i];
i++;
y++;
}
}
return ret;
You can't dynamicly resize an array, you have to initialize it with a fixed size.
So, you have to initialize it with
String[] ret = new String[size];
where size have to be the number of elements you are going to put into your array.
Or the better approach: Use ArrayList<String>instead. Initialize it with
ArrayList<String> ret = new ArrayList<String>();
and add your Items with ret.add("whatever");.
On the first line of your code you attempt to define an array without a size, but you don't actually define it, you just assign null.
Also, it's impossible to dynamically add elements to such array.
For these scenarios we have List.
To define a List that stores Strings use the following code:
List<String> ret = new ArrayList<String> ();
And then proceed to add elements to this array like so:
ret.add ("," + imageName);
To retrieve a value from an index in the list do the following:
ret.get(index);
Java does not allow arrays with variable length. I think that this is your main problem.
There are two choiches:
Obtain the array length first and instantiate the array accordingly
String[] ret = new String[100];
Use an ArrayList
ArrayList<String> ret = new ArrayList<String>();
You can add elements to the ArrayList like this: ret.add(value);
The Java Tutorial: Arrays
java.util.ArrayList reference

Selenium code to check duplicate value

I have a list of countries where the product is available. I am looking for the code to check whether any duplicate value exists in the list of countries.
They are giving like this: AZ,CA,GB, AU,AR,AT,MX,NL,NZ.
How do I check in Selenium if a duplicate value exists with a loop?
As others have pointed out, this has very little to do with Selenium, other than where your values come from.
I will assume that your list of countries comes from a pulldown menu. If not you will need to adjust the code below to match.
import java.util.*;
...
Select slctCountry = new Select(driver.findElement(By.id("select_id")));
// create an empty List
List<String> optionsList = new ArrayList<String>();
// a Set naturally removes duplicates!
Set<String> optionsSet = Collections.emptySet();
for (WebElement option : slctCountry.getOptions()) {
// fill both from the same source
optionsList.add(option.getText());
optionsSet.add(option.getText());
}
// compare the two
Assert.assertEquals("The List contains duplicates!", optionsSet.size(), optionsList.size());
A modified version of this. If the array of String contains duplicates it will simply return true.
String[] array = {"a", "b", "c", "a", "b"};
ArrayList<String> str = new ArrayList<String>();
for (String s:array){
str.add(s);
}
boolean ind = false;
for (int i = 0; i < array.length; i++) {
str.remove(array[i]);
for (int j = 0; j < str.size(); j++) {
if (array[j].equals(str.get(j))){
System.out.println(str.get(j) +" "+ array[j] );
ind = true;
}
}
}
Note:This probably has nothing to do with Selenium, at least the way you showed in the question.

How to check the order of breadcrumbs in selenium

I have a header defined as First / Second / Third as a breadcrumbs. I would like to check if the elements are displayed in correct order using selenium with optimal way of coding.
<ol class = "breadcrumb"
<li class="break-all">
First
<span class="divider">/</span>
</li>
<li class="break-all">
Second
<span class="divider">/</span>
</li>
<li class="break-all">
Third
<span class="divider">/</span>
</li>
</ol>
Now when I do
findBy("//ol[#class='breadcrumb']")
, I get the whole elements.
First you need to find all breadcrumb elements, for example, via a cssSelector(). Then, for every WebElement in the list call getText() to get the actual text:
List<String> expected = Arrays.asList("First", "Second", "Third");
List<WebElement> breadcrumbs = driver.findElements(By.cssSelector("ol.breadcrumb li a"));
for (int i = 0; i < expected.length; i++) {
String breadcrumb = breadcrumbs.get(i).getText();
if (breadcrumb.equals(expected[i])) {
System.out.println("passed on: " + breadcrumb);
} else {
System.out.println("failed on: " + breadcrumb);
}
}
findBy("//ol[#class='breadcrumb']").getText().equals("First / Second / Third");
it should work.
To solve your problem, you can follow the below process:
1- Create an "ArrayList" and add all the items that needs to be compared with.
2- Retrieve the link texts and put it in a new ArrayList.
3- Assert that the two ArrayList matches.
Below code shall work for you:
//Adding all the list items to compare in an ArrayList
ArrayList<String> alist = new ArrayList<String>();
alist.add("First");
alist.add("Second");
alist.add("Third");
//Checking the Arraylist's data
System.out.println("The list values are as under: ");
for(String list_item: alist)
System.out.println(list_item);
//Creating an ArrayList to store the retrieved link texts
ArrayList<String> List_Compare = new ArrayList<String>();
//Retrieving the link texts and putting them into the Arraylist so created
List<WebElement> New_List = driver.findElements(By.xpath("//a[#class='break-all']"));
for(WebElement list_item: New_List){
List_Compare.add(list_item.getText());
}
//Checking the new Arraylist's data
System.out.println("The Retrieved list values are as under: ");
for(String list_item: List_Compare)
System.out.println(list_item);
//Asserting the original Arraylist matches to the Arraylist with retrieved Link Texts
try{
Assert.assertEquals(alist, List_Compare);
System.out.println("Equal lists");
}catch(Throwable e){
System.err.println("Lists are not equal. "+e.getMessage());
}
NOTE: Do import the Assert class using import junit.framework.Assert; for the last part of the code to work.
You need to create a list of known values to compare with. And then use findElements() to find all the elements to match your target. In that you also need to carefully write the selector so that it grabs the list of expected elements.
//a[#class='break-all'] can be used to grab the list of elements you want
String[] expected = {"First", "Second", "Third"};
List<WebElement> allOptions = select.findElements(By.xpath("//a[#class='break-all']"));
// make sure you found the right number of elements
if (expected.length != allOptions.size()) {
System.out.println("fail, wrong number of elements found");
}
// make sure that the value of every <option> element equals the expected value
for (int i = 0; i < expected.length; i++) {
String optionValue = allOptions.get(i).getText();
if (optionValue.equals(expected[i])) {
System.out.println("passed on: " + optionValue);
} else {
System.out.println("failed on: " + optionValue);
}
}
Implementation taken from here

The method split(String) is undefined for the type List<String>

I am making a small program to split sentences when detecting a dot
I am struggling to print the result
// print result list
for(int i = 0; i < fileContent.size(); i++) {
String[] fileContent1 = (fileContent).split("\\.");
}
the function split is not working and on eclipse I got this error message :
The method split(String) is undefined for the type List
I understand the error message, I tried to cast the result but not working.
Do you have any ideas ?
Thank you for your explanations and help :)
Update 1
Thank for your reply,
I think it is better to put the full loop to understand better
Indeed I made a mistake its a list of String
// print result list
for(int i = 0; i < fileContent.size(); i++)
{
List<String> fileContent1 = fileContent.split("\\.");
System.out.println(fileContent.get(i));
PrintWriter out = new PrintWriter(new FileWriter("C:/Users/Jer/Desktop/outputfile.txt"));
//out.print("Hello ");
out.print(fileContent);
out.close();
}
Update 2 :
Well I got any error message :) thank you very much I will continue to debug my code on my side now
Change your code to
for(String singleFileContent: fileContent) {
String[] fileContent1 = singleFileContent.split("\\.");
}
Seems like the fileContent is a List and not a String.
You code also do it like (as mention in an other answer)
for(int i = 0; i < fileContent.size(); i++) {
String[] fileContent1 = fileContent.get(i).split("\\.");
}
Judging from your error message, I'm guessing fileContent is of type List<String>. If so, you probably want to do something like:
for (String fc : fileContent) {
String[] fileContent1 = fc.split("\\.");
// ...
}
fileContent is of type List and not String, as indicated in the compiler message. The split method should be called against a String object. If the list contains String elements, and you want to split each element in the list, you need to get the element first:
for(int i = 0; i < fileContent.size(); i++) {
String[] fileContent1 = fileContent.get(i).split("\\.");
}

adding elements from list to string array

I have a String array like,
String[] abc= new String[]{};
and my List has some values. I iterate the list and add each list element to string array.
for(int i=0; i<errList.size(); i++)
{
abc[i] = errList.get(i).getSrceCd();
}
errList.size() has 6 values. But when the for loops executed I get java.lang.ArrayIndexOutOfBoundsException. Any inputs?
You're creating a String[] object of zero length; so, when you try to assign an item to abc[i], it is accessing an index not within your bounds 0 <= i < 0.
You should allocate abc with a length instead:
String[] abc= new String[errList.size()];
for(int i=0; i<errList.size(); i++)
{
abc[i] = errList.get(i).getSrceCd();
}
You need to craete the string array with the same size as the list. It is not dynamic. Perhaps you can tell what you are trying to achieve with this exercise.
Did you try to use for each loop which is widely used in collection framework?
String[] abc = errList.toArray(new String[0]);
Or:
String[] abc = new String[errList.size()];
errList.toArray(abc);
I would just do this
String[] abc= errList.toArray(new String[errList.size()]);

Categories

Resources