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.
Related
//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!
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("\\.");
}
I'm scanning through an array of String objects, each string object is going to be broken down into a regex.
When going through a an enhanced for-loop I'm wondering, is it possible to put the retval into an array?
For example if I have String regex = new String[3];
Where regex[0] = "EVEN_BIN_NUM (0|1)*0"
The enhanced for-loop can break my String object up into EVEN_BIN_NUM and (0|1)*0
I want to be able to put EVEN_BIN_NUM in one array, and (0|1)*0 in another array. Here is the code I have that scans through the String array with the string objects
/*
* Run through each String object and appropriately place them in the kind,
* and explicit.
*/
for (int j = 0; j < regex.length; j++)
{
for (String retval: regex[j].split(" ", 2))
{
System.out.println(retval);
}
}
For regex[0].split(" ", 2) I get EVEN_BIN_NUM and (0|1)*0 returned separately.
Alternatively, if you know how to break this up in a better way, let me know:
EVEN_BIN_NUM (0|1)*0
ODD_BIN_NUM (0|1)*1
PET (cat|dog)
The parts in capital letters are to be put in the "kind" array, and the rest is to be put in another array.
So the kind array would have three strings, and the other array would have three strings.
Hopefully this isn't too confusing....
It might be a good idea to use a Map object to store your information, however, if you wanted to return your analysis as an array, you could return an array of arrays and do the following.
String[] regex = {"EVEN_BIN_NUM (0|1)*0", "ODD_BIN_NUM (0|1)*1", "PET (cat|dog)"} ;
String[][] split = new String[regex.length][];
for(int i = 0; i < regex.length; i++) {
split[i] = regex[i].split(" ", 2);
}
You can then access the data as follows
String firstProperty = split[0][0]; //EVEN_BIN_NUM
String firstRegex = split[0][1]; //(0|1)*0
String secondProperty = split[1][0]; //ODD_BIN_NUM
String secondRegex = split[1][1]; //(0|1)*1
etcetera.
Or using a map:
Map<String, Pattern> map = new HashMap<>();
for(int i = 0; i < regex.length; i++) {
String[] splitLine = regex[i].split(" ", 2);
map.put(splitLine[0], Pattern.compile(splitLine[1]));
}
This way your properties would map straight to your Patterns.
For example:
Pattern petPattern = map.get("PET");
I believe that we can use a for loop to reverse a string in Java. Just like below:
String[] name = new String[10];
for(int i = name.length; i >0; i--){
System.out.print(name[i-1]);
}
But another implementation is using LinkedList. So my understanding is to use LinkedList when the client is not sure how long the string array might increase dynamically. Is that correct?
A linked list of characters can be used to do this.
In this instance, think of an array list as an array with unlimited length. For this, instead of adding values to the END of the array, we will add them to the BEGINNING of the linked list
LinkedList<Character> linkedList = new LinkedList<Character>();
String str = "Hello World";
for (int i = 0; i < str.length(); i++) {
linkedList.addFirst(str.charAt(i));
}
//whenever it is time to print the value....
for (char c : linkedList) {
//Print it out, one character at a time
System.out.print(c);
}
Whenever you have to add more characters to it, just use linkedList.addFirst() to append it to the beginning.
I have two arrays "tags" and "selected" the tags array is static and depending on what the user selects the "select" array values will be set to true or false.
I want to compare the tags array and the select array and the "select" array positions that have true I want to pull out the correspoding position of the "tags" array and bulid a new array.
private String[] tags = new String[] { "Bob", "Tom", "Mike", "Smith" };
private boolean[] selected = new boolean[tags.length];
public String[] selected_tags;
for (int i = 0; i < tags.length; i++) {
if (selected[i] == true){
selected_tags[i] = tags[i];
}
I am not sure if I am doing this correctly because I feel like I would have empty spots in my selected_tag array. If there is a better way of doing this I am open to suggestions.
thanks!
public String[] selected_tags = new String[tags.length]
Everything you say and you did is fine and reasonable. Just make sure all of them are of the same size. (see above)