Drop down list values are in sorted order or not in Selenium WebDriver
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class DropDownListSortedOrNot {
@Test
public void dropDownListSortedOrNot() throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.ebay.in/");
Thread.sleep(3000);
WebElement element = driver.findElement(By.id("gh-cat"));
element.click();
List<WebElement> dropDownvalues = element.findElements(By.tagName("option"));
ArrayList<String> listValues = new ArrayList<String>();
for(WebElement value : dropDownvalues) {
System.out.println("values are"+ value.getText());
listValues.add(value.getText());
}
boolean sortedOrNot = sortedOrNot(listValues);
assertEquals(true, sortedOrNot);
driver.close();
}
public boolean sortedOrNot(ArrayList<String> dropDownValues) {
System.out.println("number of values "+ dropDownValues.size());
for(int i=0; i<dropDownValues.size();i++) {
int temp = dropDownValues.get(i).compareTo(dropDownValues.get(i+1));
if(temp>1) {
System.out.println("i value"+i);
return false;
}
}
return true;
}
}
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class DropDownListSortedOrNot {
@Test
public void dropDownListSortedOrNot() throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.ebay.in/");
Thread.sleep(3000);
WebElement element = driver.findElement(By.id("gh-cat"));
element.click();
List<WebElement> dropDownvalues = element.findElements(By.tagName("option"));
ArrayList<String> listValues = new ArrayList<String>();
for(WebElement value : dropDownvalues) {
System.out.println("values are"+ value.getText());
listValues.add(value.getText());
}
boolean sortedOrNot = sortedOrNot(listValues);
assertEquals(true, sortedOrNot);
driver.close();
}
public boolean sortedOrNot(ArrayList<String> dropDownValues) {
System.out.println("number of values "+ dropDownValues.size());
for(int i=0; i<dropDownValues.size();i++) {
int temp = dropDownValues.get(i).compareTo(dropDownValues.get(i+1));
if(temp>1) {
System.out.println("i value"+i);
return false;
}
}
return true;
}
}
public boolean sortedOrNot(ArrayList dropDownValues) {
ReplyDeleteSystem.out.println("number of values "+ dropDownValues.size());
for(int i=0; i1) {
System.out.println("i value"+i);
return false;
}
}
return true;
}
Can you explian anyone above code please ...
Hi Venkat ,
ReplyDeletePlease help me understand this:
boolean sortedOrNot=sortedOrNot(listValues);
assertEquals(false,sortedOrNot );
driver.close();
If above assert is false test case is passing, what actually its checking.
Also what are the below statements doing in the code. Not able to understand.
int temp=dropdownValues.get(i).compareTo(dropdownValues.get(i+1));
if (temp>1){
System.out.println("i value is : "+i);
return false;
}}
return true;