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. |
url=http://www.rediff.comNow lets read this file from the driver class or base class:
Browser=FF
find=img-link
public static Properties readPropertiesFile() throws IOExceptionNow 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.
{
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;
}
public String getPropertiesFile(String key) throws IOExceptionNow once the reading is done, I need function that will drive the entire stuff like opening browser and navigate to url.It gose like-
{
allprops = readPropertiesFile();
return (String) allprops.get(key);
}
public void startupTest() throws IOException, InterruptedException {As I may have given my browser type as IE,Firefox or chrome. So based on that I need to invoke the drivers.
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
}
private static WebDriver initFirefoxDriver(String sURL) throws InterruptedException {Now,for calling this methods,I need a function with @before annotation as I will be driving this via jnit.
System.out.println("Launching Firefox browser..");
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get(sURL);
Thread.sleep(2000);
return driver;
}
@BeforeWell,now we need one more function to read the specific property files get get the count as per our need:
public void startupTestForBase() {
try {
startupTest();
} catch (Exception e) {
System.out.println("Error....." + e.getStackTrace());
}
}
public int ReturnElementCount(String setType)The whole code looks like-
{
int counterofImage=0;
List AllElements=driver.findElements(By.xpath("//"+setType));
for(int i=0;i<AllElements.size();i++)
counterofImage=counterofImage+1;
return counterofImage;
}
package Base;Now let us come to the actual testcase-
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();
}
}
package Testcase;Thats right!!! you can further encapsulate the things:
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));
}
}
}
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();
}