Count number of elements that has a score - selenium

Is there a way to count the number of elements that isn't equal to 0.00?
For example, the code is
<div id="average_2123" style="font-size:20px; ">0.00</div>
<div id="average_2124" style="font-size:20px; ">23.53</div>
<div id="average_2125" style="font-size:20px; ">0.00</div>
How can I count the element so it's only 1 since only one of them has a score?
I want to do this on PHPUnit. I can also do it on Selenium IDE because I can convert it to PHPUnit

you will have to write custom code. I am writing java pseudocode. hope you can understand and convert
List<WebElements> ElemList = Webdriver.FindElements(By.Xpath("//div")
for (i = 0; i < ElemList.size();i++)
{
WebElement Current =List.getElementAt(i);
String ElemName = current.getAttribute("id");
String text =""
int Count = 0;
if( id.Contains("average"))
{
if( !id.getAttribute("value").equals("0.00")
{
count++;
}
}
}

A better approach can be as mentioned below. Im writing the code in Java:
List<WebElement> elemList = driver.findElements(By.cssSelector("div[id^='average']"));
List<WebElement> filteredElements = new ArrayList<WebElement>();
for (WebElement element : elemList) {
if (Long.parseLong(element.getText()) > 0.00)
filteredElements.add(element);
}
This will be find all the elements whose "id" attribute starts with "average".
Also here i am converting the text to long and then comparing whether its grater than 0.00
The filteredEleemnts are the elements which have value greater than 0.00

Related

Limit the element to be stored in List<WebElement> - selenium/java

I need to know how to store the first 10 values in List<WebElement> instead of storing all the elements that is present?
Right now my code stores all the elements:
By mySelector = By.xpath("/html/body/div[1]/div/section/div/div[2]/form[1]/div/ul/li");
List<WebElement> myElements = driver.findElements(mySelector);
for(WebElement e : myElements) {
System.out.println(e.getText());
}
Try using position() with the li.
By.xpath("/html/body/div[1]/div/section/div/div[2]/form[1]/div/ul/li[position() < 11]")

Verify 2 web elements in table are equal or not

List<WebElement> tdata=driver.findElements(By.tagName("tr"));
List<WebElement> tdata1=driver1.findElements(By.tagName("tr"));
for(int i=0,j=0; i<tdata.size() && j<tdata1.size();i++,j++ )
{
WebElement row = tdata.get(i);
WebElement row1 = tdata.get(j);
System.out.print(row1.getText());
System.out.print(row1);
if(row.getText().equals(row1.getText()))
{
System.out.println(row.getText());
}
else if(!(row.getText().equals(row1.getText())))
{
System.out.print("Not matching text");
System.out.println(row1.getText());
}
}
This is my code for comparing 2 web table, I am Unable to verify content equal or not equal. For unmatched text also it is not printing anything. else if part is not capturing if elements are not equal.
try
List<WebElement> tdata= ...
List<WebElement> tdata1= ...
int common= 0;
for (WebElement element: tdata)
if (tdata1.contains(element))
common++;

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

Incrementing value with Selenium IDE

How do I increment value of img path when said path looks like this?
//ab[x]/img
X value increasing by 1 and has a limit of 50.
Trying to write a test case on how to click on several images on website.
Edit: Just wanted to add that I'm just starting with Selenium IDE and using standart commands.
Solution 1: Format your xpath path selector
for(int i=1; i<=numberOfImages; i++) {
String path = String.format("//ab[%d]/img", i);
WebElement image = driver.findElement(By.xpath(path));
if(image != null) {
image.click();
}
}
Solution 2: Select all elements that "//ab/img" returns and iterate over them.
String path = "//ab/img";
List<WebElement> imgElements = driver.findElements(By.xpath(path)); //notice the plural
for(WebElement image : imgElements) {
image.click();
}

How to verify character count of text field?

I want to check the number of characters I can insert in a text field, and was thinking of using 'for loop' but it would not help as Selenium tries to insert more than required character the field will not accept but test goes on without any failure, so is there a way to get character count of the text field?
Would this work?
final String myLongString = "Something horrrrribly looooong";
final int longStringLength = myLongString.length();
// assuming driver is a healthy WebDriver instance
WebElement elem = driver.findElement(By.id("myInput"));
elem.sendKeys(myLongString);
// it's possible that you'll first need to lose focus on elem before the next line
int realLength = elem.getValue().length();
assertEquals(longStringLength, realLength);
Using Protractor I captured the actual text in the field and then did a forloop to count each letter.
element(by.css('elementPATH')).getAttribute('value').then(function(words){
//forloop to count each word
var x = 0
for(var i = 0; i < words.length; i++) {
x = x + 1;
};
//check condition
expect(x).toBe(200);
return true;
});
Let me know if this helps.