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
Related
Now I want to select first value from a drop down then perform some actions on it, then I want to select second value from the same drop down and perform the same action on it.
Here is my code:
WebElement bldgs=Fn_GetWebElement(CreateSSIObject.getProperty("Bldgselect"));
Select Bldg_select=new Select(bldgs);
List<WebElement> dropdownvalues = Bldg_select.getOptions();
int count=dropdownvalues.size();
System.out.println("Total number of values are :"+count);
for(int i=1;i<count;i++) {
if(dropdownvalues.get(i).isEnabled()) {
Bldg_select.selectByIndex(i);
System.out.println("Not Working :"+i);
waitForWebPagetoLoad(2000);
WebElement search_BTN=Fn_GetWebElement(CreateSSIObject.getProperty("search_Btn"));
fn_Click(search_BTN);
WebElement add_VEND=Fn_GetWebElement(CreateSSIObject.getProperty("add_vendors"));
fn_Click(add_VEND);
WebElement vendorName=Fn_GetWebElement(CreateSSIObject.getProperty("vendor_Name"));
fn_Click(vendorName);
vendorName.sendKeys(vendor);
waitForWebPagetoLoad(5000);
WebElement search_BTN1=Fn_GetWebElement(CreateSSIObject.getProperty("search_Btn"));
fn_Click(search_BTN1);
WebElement selectVendor=Fn_GetWebElement(CreateSSIObject.getProperty("select_Vendor"));
fn_Click(selectVendor);
WebElement addToSite=Fn_GetWebElement(CreateSSIObject.getProperty("AddTo_Site"));
fn_Click(addToSite);
}
}
here I am seaching for an element(basically drop down id) and then selecting each value with selectbyindex with i for loop. and then I am clicking on a button and performing some more actions on it. Now it is selecting only first value and doing all above stuff. But it is not going back in for loop to select 2nd value and performs same steps.
I don't quite understand your problem butI can see 2 issues that may be adding to confusion.
Index should 0 based
Your loop is starting with i set to 1. As lists are zero based indexes, you should start at 0
Referencing stale elements??
You are extracting the dropdown values outside of the loop and then referencing these within the loop using index. However, you are performing a lot of actions and events within each iteration.
You may be better extracting the values again within each iteration to ensure all your references are up to date and have not gone stale.
Can you please try below solution? I am not sure what action you are tryig to execute based on selection but I think below code solve your problem.
Select drpCountry = new Select(driver.findElement(By.name("Locator")));
List <WebElement> elementCount = drpCountry.getOptions();
int iSize = elementCount.size();
for(int i =0; i<iSize ; i++)
{
String sValue = elementCount.get(i).getText();
System.out.println(sValue);
drpCountry.selectByIndex(i);
if(sValue.equalsIgnoreCase("Selection1")){
//code to be executed if condition1 is true
}else if(sValue.equalsIgnoreCase("Selection2")){
//code to be executed if condition2 is true
}
else if(sValue.equalsIgnoreCase("Selection3")){
//code to be executed if condition3 is true
}
else{
//code to be executed if all the conditions are false
}
}
Scenario with Example:
10 Different Row in Table (e.g. //a[#class='*******'] ).
Need to Retrieve different Value from each Row
Retrieving it using List
In given example, Temp1 getting executed and iterate as required, As information need to retrieve from the same and got it through getAttribute(" ").
Need to retrieve Temp2 value which is dependent on object findList. findSubInfo is having Following Siblings from mainList. Its only addition on following siblings in to List exa.(findList).
Issue is :
If I execute 1st provision, as independent node, It always retrieve first row's value. (Which is Obvious)
If I execute 2nd provision, with xpath + List object using con-cat it throws xpath Syntax issue.
Question :
How can retrieve all information from object's siblings, where object List<WebElement> findList is pointing ?
Trial :
By mainList = By.xpath("//a[#class='*******']");
By findSubInfo = By.xpath("//a[#class='*****']//following::div[#class='****']");
List<WebElement> findList = driver.findElements(mainList);
for (WebElement webElement : findList ) {
if (webElement.isDisplayed()) {
String temp1= "Info1:" + webElement.getAttribute("ng-href");
String temp2= "Info2: " + driver.findElement(findSubInfo).getText();
}
}
OR
String mainList "//a[#class='*******']";
String findSubInfo = "//following::div[#class='****']";
List<WebElement> findList = driver.findElements(By.xpath(mainList));
for (WebElement webElement : findList ) {
if (webElement.isDisplayed()) {
String temp1= "Info1:" + webElement.getAttribute("ng-href");
String temp2= "Info2: " + driver.findElement(By.xpath(webElement + findSubInfo )).getText();
}
}
Exception Details:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string
'[[ChromeDriver: chrome on XP (3208ef0a2ffd32812c33e159291eebe4)] ->
xpath: //a[#class='noDecoration
addPointer']]//following::div[#class='assignedAccountMasterAddress']'
is not a valid XPath expression.
If you want to search for element with related XPath starting from current element (webelement), try
# Note that it should start with the dot
String findSubInfo = "./following::div[#class='****']";
# use webElement.findElement instead of driver.findElement. No XPath concatenations needed
String temp2= "Info2: " + webElement.findElement(By.xpath(findSubInfo)).getText();
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");
}
Is it possible to test the sorting functionality of a web table in Katalon Studio/Selenium Webdriver?
Does Katalon Studio/Selenium Webdriver have any default method to verify whether the datas within a single column is either in ascending or descending order?
The following is the code that I used to fetch all the values listed in the 1st column of a web table and save them in an array:
WebDriver driver = DriverFactory.getWebDriver()
'To locate table'
WebElement Table = driver.findElement(By.xpath('/html[1]/body[1]/table[1]/tbody[1]'))
'To locate rows of table it will Capture all the rows available in the table'
List<WebElement> rows_table = Table.findElements(By.tagName('tr'))
'To calculate no of rows In table'
int rows_count = rows_table.size()
String[] celltext = new String[rows_count]
for (int row = 0; row < rows_count; row++) {
'To locate columns(cells) of that specific row'
List<WebElement> Columns_row = rows_table.get(row).findElements(By.tagName('td'))
'It will retrieve text from 1st cell'
String celltext_1 = Columns_row.get(0).getText()
celltext[row] = celltext_1
}
For example, celltext = [4,3,2,1]
Now I want to verify that the values saved in celltext is in descending order.
Any help will be highly appreciated.
Neither selenium nor katalon provides sorting functionality. But you can use java Arrays utility class to sort the items and compare it as follows.
String[] celltextBefore = celltext;
Arrays.sort(celltext, Collections.reverseOrder());
if(Arrays.equals(celltextBefore, celltext))
{
System.out.println("Celltext is in descending order");
}
else{
System.out.println("Celltext is not in descending order");
}
Special thanks to Murthi for giving me the splendid idea of comparing arrays.
The following way I was able to solve my problem:
List<Integer> celltext_list = Arrays.asList(celltext);
Collections.sort(celltext_list, Collections.reverseOrder());
int[] celltext_new = celltext_list.toArray();
if(Arrays.equals(celltext_new, celltext)){
System.out.println("Celltext is in descending order")
}
else{
System.out.println("Celltext is in ascending order")
}
In Murthi's solution above, there was an error throwing which I added to his comment. Finally came up with the above solution.
I want to know how to select a specific value from dropdown from a pool of values.
My logic is
1.Open the page
2.Fetch all the values from dropdown in list
3.Use a loop
4.Look for that value
5.If the value is not there,select the value x
For me it is saying No such Element Exception
Is it do we need to focus on that element first
In my code i want to select Nextran Corporation
Below is my code
#Test(dependsOnMethods={"go2personalsettings"})
void setru()
{
driver.switchTo().frame("contentFrame");
Select rudropdown=new Select(driver.findElement(By.id("DefaultOrganisationDropDown")));
List<WebElement> drop=rudropdown.getOptions();
int e=drop.size();
String actual_ru="999425, NEXTRAN CORPORATION - JACKSONVILLE";
for(int i=0;i<e;i++)
{
String expected_ru=drop.get(i).getText();
if(!expected_ru.equals(actual_ru))
{
rudropdown.selectByValue(actual_ru);
}
}
Try This:
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//li[contains(text(),'" + ElementValue + "')]")));
WebElement webElement = driver.findElement(By.xpath("//li[contains(text(),'" + ElementValue + "')]"));
webElement.click();
The logic in your code doesn't match what you stated was your intent. You stated that you want to select a particular value from the dropdown, if it exists. If it doesn't, select value 'x'. Your code doesn't ever select value 'x' so I'm not sure what 'x' is but you can fill that in below. Basically the code attempts to select the expected value. If that expected value does not exist, it will throw an exception which is caught and then 'x' is selected. If it does exist, the value is selected and selecting 'x' is skipped.
driver.switchTo().frame("contentFrame");
Select rudropdown = new Select(driver.findElement(By.id("DefaultOrganisationDropDown")));
String actual_ru = "999425, NEXTRAN CORPORATION - JACKSONVILLE";
try
{
rudropdown.selectByValue(actual_ru);
}
catch (NoSuchElementException e)
{
// expected value does not exist, select the value x instead
rudropdown.selectByValue("x");
}