Saturday, November 7, 2020

How to handle Dynamic table

 Q)How to handle Dynamic table and Create Re-usable method and add in framework.

package com.seleniumpractice;


import java.util.List;

import java.util.concurrent.TimeUnit;


import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;


import com.businesshelper.Helper;


public class Dyamictable extends Helper{


public static void main(String[] args) {

/*

System.setProperty("webdriver.chrome.driver","D:\\driver1016\\chromedriver.exe");

WebDriver driver=new ChromeDriver();

driver.get("https://www.redbus.in/");

driver.manage().window().maximize();

driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);

*/

Helper.launchApp();

driver.findElement(By.xpath("//button[contains(text(),'Search Buses')]/preceding::span[1]")).click();

Helper.selectDate(driver,"10");

/*

List<WebElement> list=driver.findElements(By.xpath("//table[@class='rb-monthTable first last']/tbody//td"));

for(int i=0;i<list.size();i++){

String strlist=list.get(i).getText();

System.out.println("strlist-->" + strlist);

if(strlist.equals("10")){

list.get(i).click();

break;

}

*/


}


}

=========================================================================

package com.businesshelper;


import java.util.List;


import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.interactions.Actions;


public class Helper {

public static WebDriver driver;

public static String driverPath="D:\\driver1016\\chromedriver.exe";

public static String url="https://www.redbus.in/";

public static List<WebElement> listofCheckbox;

public static void launchApp(){

System.setProperty("webdriver.chrome.driver",driverPath);

driver=new ChromeDriver();

driver.get(url);

driver.manage().window().maximize();

}

public static boolean verifyisSeleted(WebElement ele){

boolean status= ele.isSelected();

if(status){

System.out.println("CheckBox is selected");

}

else{

System.out.println("CheckBox is not selected");

}

return status;

}

/*

public static void multipleCheckbox(){

listofCheckbox=driver.findElements(By.xpath("//input[@type='checkbox']"));

for(WebElement listele:listofCheckbox){

listele.click();

}

}

*/

public static boolean isDisplayedRadiobtn(WebElement ele){

boolean eleisisplaye=ele.isDisplayed();

if(eleisisplaye){

System.out.println("Radio Button is Displayed ");

}

else{

System.out.println("Radio Button is not Displayed ");

}

return eleisisplaye;

}

public static boolean isEnabledRadioButton(WebElement ele){

boolean eleisEnabled=ele.isEnabled(); 

if(eleisEnabled){

System.out.println("Radio Button is Enabled ");

}

else{

System.out.println("Radio Button is not Enabled");

}

return eleisEnabled;

}

public static void ClickWithActions(WebElement ele){

Actions act=new Actions(driver);

act.moveToElement(ele).click().perform();

}

public static void selectDate(WebDriver driver,String str){

List<WebElement> list=driver.findElements(By.xpath("//table[@class='rb-monthTable first last']/tbody//td"));

for(int i=0;i<list.size();i++){

String strlist=list.get(i).getText();

if(strlist.equals("10")){

list.get(i).click();

break;


}

}

}


}

====================================================================

Q) How to handle Dynamic table in Selenium


package Selenium.SeleniumTest;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class WebTable {

public static void main(String[] args) {
    System.setProperty("webdriver.chrome.driver","D:\\driver1016\\chromedriver.exe\\");
    WebDriver driver=new ChromeDriver();
    driver.get("https://www.w3schools.com/html/html_tables.asp");
  //*[@id="customers"]/tbody/tr[2]/td[1]
  //*[@id="customers"]/tbody/tr[3]/td[1]
  //*[@id="customers"]/tbody/tr[4]/td[1]
  //*[@id="customers"]/tbody/tr[6]/td[1]
    
  //*[@id="customers"]/tbody/tr[2]/td[2]
  //*[@id="customers"]/tbody/tr[3]/td[2]
  //*[@id="customers"]/tbody/tr[4]/td[2]
  //*[@id="customers"]/tbody/tr[5]/td[2]
  //*[@id="customers"]/tbody/tr[2]/td[3]
    
    String beforeXpath_compnay="//*[@id='customers']/tbody/tr[";
    String afterXpath_company="]/td[1]";
    
    String beforeXpath_contact="//*[@id='customers']/tbody/tr[";
    String afterXpath_contact="]/td[2]";
    
    String beforeXpath_country="//*[@id='customers']/tbody/tr[";
    String afterXpath_country="]//td[3]";
    
    List<WebElement> rows=driver.findElements(By.xpath("//table[@id='customers']//tr"));
    System.out.println("Total number of rows = " + (rows.size()-1));
     int rowCount=rows.size();
     
     //Xls_Reader reader=new XlsReader();
     
     
    for(int i=2;i<=rowCount;i++){
    String actualXpath_comapnayName=beforeXpath_compnay+i+afterXpath_company;
    String companyName=driver.findElement(By.xpath(actualXpath_comapnayName)).getText();
    System.out.println(companyName);
   
    String actualXpathcoustomer=beforeXpath_contact+i+afterXpath_contact;
    String contactName=driver.findElement(By.xpath(actualXpathcoustomer)).getText();
    System.out.println(contactName);
   
    String actualXpath_country=beforeXpath_country+i+afterXpath_country;
    String countryName=driver.findElement(By.xpath(actualXpath_country)).getText();
    System.out.println(countryName);
   
    }
}

}






No comments:

Post a Comment

Q) How To Find Duplicate Characters In A String In Java?

Step1:- Creating a HashMap containing char as key and it's occurrences as value. Step2:- Converting given string to char array. Step3:- ...