How to write data into excel in a row/column in Sorted Form- ( i am using JXL ), Used Collection.sort(list) but not working - selenium

I am Not getting ---
I need to Pick list items in E commerce application search box . and Have to Print each Line By line in a column or any number of rows and column, in Excel in sorted form
i am able to get the name of items in the Search and print them in eclipse console.
what can be the logic to Print them one after other in Excel in sorted form , do i need to save them all in an array first or something else .
Here in this Code I am Getting -- Items From the Search box in the E commerce Application and printing them in the Excel.
Need the logic that any random number of search items gets printed in the Excel in sorted Form
i used the code below to print all the search list items in the Column(Excel) ,what change can be made to print them in sorted order
// picking list items//
List <WebElement> listItems = driver.findElements(By.xpath(".//form[#class='_1WMLwI']//ul/li"));
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
String[] text = new String[10];
arrylngth=listItems.size();
System.out.println("Length of arrylngth" + arrylngth);
for (int col = 0; col < arrylngth; col++)
{ text[col]=listItems.get(col).getText();
System.out.println("Original List "+" "+ text[col]);
// insert data into Excel sheet
for (int row = 0; row <1 ; row++) {
// create an empty array list with an initial capacity
List<String> arrlist = new ArrayList<String>();
// use add() method to add elements in the list
arrlist.add(text[col]);
Collections.sort(arrlist);
System.out.println("sorted Array "+" "+ arrlist);
for(String counter: arrlist){
System.out.println("After Sorting:"+""+ counter);}
Label label1 = new Label(row,col,text[col]);
try {
shSheet.addCell(label1);
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();

I got the Answer :
Step 1: Need to pick the List Web-Elements -
List <WebElement> listItems = driver.findElements(By.xpath(".//form[#class='_1WMLwI']//ul/li"));
Step 2: Need To Create a String List where i can store the list elements name ( as collection.sort() applied to sting list)
List<String> al = new ArrayList<String>();
Step 3: Apply For Loop to insert data into the String List
for (int i = 0; i <listItems.size(); i++)
{
al.add(listItems.get(i).getText());
System.out.println("Unsorted list " + al);
}
Step 4: Apply Collection.sort() onto the string List , it will Sort the List content
Collections.sort(al);
step 5: Now there is a need to print the sorted list in the Console\
for (String list:al)
{
System.out.println("sorted list"+ " " + al);
}
as we are done with sorted String list , now its an easy task to write the Sorted list into the Excel using JXL or POI using, Write Excel code.

Related

New line "\n" does not work yet no errors are given

I have a program that loads a checked list box, then the user selects the items they want and selects a button to say they are done. The checked items are read in a contiguous string with a newline "\n" at the end of each string added. My problem is everything works ok except the newline "\n", and I don't know why.
Code
private: System::Void bntSelected_Click(System::Object^ sender, System::EventArgs^ e) {
int numSelected = this->checkedListBox1->CheckedItems->Count;
if (numSelected != 0)
{
// Set input focus to the list box.
//SetFocus(lstReceiver);
String^ listBoxStr = "";
// If so, loop through all checked items and print results.
for (int x = 0; x < numSelected; x++)
{
listBoxStr = listBoxStr + (x + 1).ToString() + " = " + this->checkedListBox1->CheckedItems[x]->ToString() + "\n";
}
lstReceiver->Items->Add(listBoxStr);
}
}
The string is being formed correctly, but the ListBox control doesn't show newlines in its list items - each item is pushed onto a single line. You can see this by debugging, or by adding a Label to the form with AutoSize set to false - the label will show the newline(s) in the string properly.
Related (C#): How to put a \n(new line) inside a list box?
Instead of ā€˜\nā€™, try ā€˜\r\nā€™. This may be a windows thing.

How to use Lambda expressions in java for nested if-else and for loop together

I have following Code where i will receive list of names as parameter.In the loop, first i'm assigning index 0 value from list to local variable name. There after comparing next values from list with name. If we receive any non-equal value from list, i'm assigning value of result as 1 and failing the test case.
Below is the Array list
List<String> names= new ArrayList<String>();
names.add("John");
names.add("Mark");
Below is my selenium test method
public void test(List<String> names)
String name=null;
int a=0;
for(String value:names){
if(name==null){
System.out.println("Value is null");
name=value;
}
else if(name.equals(value)){
System.out.println("Received Same name");
name=value;
}
else{
a=1;
Assert.fail("Received different name in between");
}
}
How can i convert above code into lambda expressions?. I'm using cucumber data model, hence i receive data as list from feature file. Since i can't give clear explanation, just posted the example logic i need to convert to lambda expression.
Here's the solution: it cycles all element in your list checking if are all the same.
You can try adding or editing the list so you can have different outputs. I've written the logic, you can easly put it into a JUnit test
List<String> names= new ArrayList<>();
names.add("John");
names.add("Mark");
String firstEntry = names.get(0);
boolean allMatch = names.stream().allMatch(name -> firstEntry.equals(name));
System.out.println("All names are the same: "+allMatch);
Are you looking for duplicates, whenever you have distinct value , set a=1 and say assert to fail. You can achieve this by :
List<String> names= new ArrayList<String>();
names.add("John");
names.add("Mark");
if (names.stream().distinct().limit(2).count() > 1) {
a= 1,
Assert.fail("Received different name in between");
} else {
System.out.println("Received Same name");
}

Dropdown duplicate value automation using Selenium

How to check the duplication of values in a checkbox using Selenium Webdriver
something like the below one will work if both the options have same value
public boolean isSelectOptionsRepeating(WebElement dropdown)
{
Select s = new Select(dropdown);
List<WebElement> list = s.getOptions();
Set<String> listNames = new Hashset<String>(list.size());
for (WebElement w : list) {
listNames.add(w.getText().trim());
}
if(list.size()== listNames.size())
return true;
else
return false;
}
You can store the values of drop down in String array and
traverse string array and use Hashmap for storing the values from the dropdown and if duplicate occurs increement the count by one
voila......you would know the the Values with its count, if count > 1. Duplicate
for reference : Java Beginner - Counting number of words in sentence

How to get the last input ID in a textfile?

Can someone help me in my problem?
Because I'm having a hard time of on how to get the last input ID in a text file. My back end is a text file.
Thanks.
this is the sample content of the text file of my program.
ID|CODE1|CODE2|EXPLAIN|CODE3|DATE|PRICE1|PRICE2|PRICE3|
02|JKDHG|hkjd|Hfdkhgfdkjgh|264|56.46.54|654 654.87|878 643.51|567 468.46|
03|DEJSL|hdsk|Djfglkdfjhdlf|616|46.54.56|654 654.65|465 465.46|546 546.54|
01|JANE|jane|Jane|251|56.46.54|534 654.65|654 642.54|543 468.74|
how would I get the last input id so that the id of the input line wouldn't back to number 1?
Make a function that read file and return list of lines(string) like this:
public static List<string> ReadTextFileReturnListOfLines(string strPath)
{
List<string> MyLineList = new List<string>();
try
{
// Create an instance of StreamReader to read from a file.
StreamReader sr = new StreamReader(strPath);
string line = null;
// Read and display the lines from the file until the end
// of the file is reached.
do
{
line = sr.ReadLine();
if (line != null)
{
MyLineList.Add(line);
}
} while (!(line == null));
sr.Close();
return MyLineList;
}
catch (Exception E)
{
throw E;
}
}
I am not sure if
ID|CODE1|CODE2|EXPLAIN|CODE3|DATE|PRICE1|PRICE2|PRICE3|
is part of the file but you have to adjust the index of the element you want to get
, then get the element in the list.
MyStringList(1).split("|")(0);
If you're looking for the last (highest) number in the ID field, you could do it with a single line in LINQ:
Dim MaxID = (From line in File.ReadAllLines("file.txt")
Skip 1
Select line.Split("|")(0)).Max()
What this code does is gets an array via File.ReadAllLines, skips the first line (which appears to be a header), splits each line on the delimiter (|), takes the first element from that split (which is ID) and selects the maximum value.
In the case of your sample input, the result is "03".

How to setText in a textArea from an ArrayList?

I have an ArrayList ArrayList<String> externalDataList = new ArrayList<>(1600);and I would like to display in a textArea first 3 strings, but I can't succed:
Here is my code
textareaShowPreview.setPrefRowCount(3);
Iterator<String> it = externalDataList.iterator();
int tot = 0;
while(it.hasNext() && tot<3){
String element = it.next();
textareaShowPreview.setText(element + "\n");
System.out.println("elements are: " + element);
tot++;
}
The sout correctly print first 3 strings
element are: 23/05/2007 ,30.9455,31.2545,30.9091,30.9545,7518142
element are: 24/05/2007 ,30.6545,31.0909,30.5364,30.6909,12851606
element are: 25/05/2007 ,30.6636,30.8545,30.4818,30.8091,9392088
but in textArea I have only first one
How do I have to modify my code to show in textArea all three strings, one string per row?
Use appendText instead of setText here is a link.
The setText, delete the previous text and set the text you are giving to it. The append keep the current text in your text area.
Hope it helps!