Header Ads

How to Get All The Web Elements and Print Each Web Element Count In Selenium



 
In this post we will how to get all the webElements and how to get their numbers for each webelements.
I will be writing this as per very basic framework.So the precondition is a very basic framework is in place.Lets see how to code step by step to get the count of each type of elements.
Well,to start with let us make entry to a file from where my test will read the basic inputs like the browser,what to find[separated by "-"],url
url=http://www.rediff.com
Browser=FF
find=img-link

Now lets read this file from the driver class or base class:
public static  Properties readPropertiesFile() throws IOException
 {
  FileInputStream fs = new FileInputStream("D:\\My_projects\\Projects\\TestBase\\src\\Base\\Path.txt");
                 //need to change the path based on your file location
   properties= new Properties(System.getProperties());
  //Loading all the properties from Object Repository property file in to OR object
  properties.load(fs);
  return properties;
 }
Now we need to read properties one by one-so I will send the key to the getPropertiesFile function and it will return me the required value.
public String getPropertiesFile(String key) throws IOException
{
 allprops = readPropertiesFile();
 return (String) allprops.get(key);
}
Now once the reading is done, I need function that will drive the entire stuff like opening browser and navigate to url.It gose like-
public  void startupTest() throws IOException, InterruptedException {
 allprops = readPropertiesFile();
 String browserType=(String) allprops.get("Browser");
 String sURL=(String)allprops.getProperty("url");
 switch(browserType){
 case "FF":
 driver=initFirefoxDriver(sURL);
        //you may give IE,Chrome etc
 
 } 
As I may have given my browser type as IE,Firefox or chrome. So based on that I need to invoke the drivers.
private static WebDriver initFirefoxDriver(String sURL) throws InterruptedException {
  System.out.println("Launching Firefox browser..");
  WebDriver driver = new FirefoxDriver();
  driver.manage().window().maximize();
  driver.get(sURL);
  Thread.sleep(2000);
  
  return driver;
 }
Now,for calling this methods,I need a function with @before annotation as I will be driving this via jnit.
@Before
 public void startupTestForBase() {
  try {
   startupTest();

  } catch (Exception e) {
   System.out.println("Error....." + e.getStackTrace());
  }
 }

Well,now we need one more function to read the specific property files get get the count as per our need:

public int  ReturnElementCount(String setType)
 {   
  
  int counterofImage=0;
  List AllElements=driver.findElements(By.xpath("//"+setType));
  for(int i=0;i<AllElements.size();i++)
  counterofImage=counterofImage+1;
  return counterofImage;
 }
 
The whole code looks like-
package Base;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Properties;

import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class BaseTest {
 public static Properties properties;
 private WebDriver driver;
 static Properties allprops;
 
  protected WebDriver getDriver() {
  return driver;
 }
public String getPropertiesFile(String key) throws IOException
{
 allprops = readPropertiesFile();
 return (String) allprops.get(key);
}
 
 public  void startupTest() throws IOException, InterruptedException {
 allprops = readPropertiesFile();
 String browserType=(String) allprops.get("Browser");
 String sURL=(String)allprops.getProperty("url");
 switch(browserType){
 case "FF":
 driver=initFirefoxDriver(sURL);
 
 }
 
 }
 @Before
 public void startupTestForBase() {
  try {
   startupTest();

  } catch (Exception e) {
   System.out.println("Error....." + e.getStackTrace());
  }
 }


 private static WebDriver initFirefoxDriver(String sURL) throws InterruptedException {
  System.out.println("Launching Firefox browser..");
  WebDriver driver = new FirefoxDriver();
  driver.manage().window().maximize();
  driver.get(sURL);
  Thread.sleep(2000);
  
  return driver;
 }
 public int  ReturnElementCount(String setType)
 {   
  
  int counterofImage=0;
  List AllElements=driver.findElements(By.xpath("//"+setType));
  for(int i=0;i<AllElements.size();i++)
  counterofImage=counterofImage+1;
  return counterofImage;
 }
 public static  Properties readPropertiesFile() throws IOException
 {
  FileInputStream fs = new FileInputStream("D:\\My_projects\\Projects\\TestBase\\src\\Base\\Path.txt");
  properties= new Properties(System.getProperties());
  //Loading all the properties from Object Repository property file in to OR object
  properties.load(fs);
  return properties;
 }
 @After
 public void teardown()
 {
  driver.quit();
 }
}
Now let us come to the actual testcase-
package Testcase;

import java.io.IOException;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import Base.BaseTest;

public class ReadingElements extends BaseTest {
 @Test
 public void getCountType() throws IOException
 {
  String type=getPropertiesFile("find");
  String[] set=type.split("-");
  for(String setType:set)
  {
   System.out.println("Count of "+setType+" is "+ReturnElementCount(setType));
  }
 }
 
}


Thats right!!! you can further encapsulate the things:
The output:
Launching Firefox browser..
Count of img is 128
Count of link is 3
There is a shortcut to get count. I have written it below:
public int  ReturnElementCount(String setType)
 {   
  
  
  return AllElements=driver.findElements(By.xpath("//"+setType)).size();
  
 }
 
Powered by Blogger.