Wednesday, February 22, 2017

How To Handle Dynamic Web Table In Selenium WebDriver



In this selenium webdriver post, we will learn how to handle dynamic web table. There might occur some scenarios where every time a page loads with different number of rows and columns or different columns for each row.





For that, we have to put two for loops,
1. first loop will iterate all the rows and
2. second loop inside it will iterate the columns of each row



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.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class DynamicTable {
WebDriver driver = new FirefoxDriver();
@BeforeTest
public void setup() throws Exception {
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("http://only-testing-blog.blogspot.in/2014/05/form.html");
}
@AfterTest
public void tearDown() throws Exception {
driver.quit();
}
@Test
public void Handle_Dynamic_Webtable() {
//To locate table.
WebElement mytable = driver.findElement(By.xpath(".//*[@id='post-body-8228718889842861683']/div[1]/table/tbody"));
//To locate rows of table.
List rows_table = mytable.findElements(By.tagName("tr"));
//To calculate no of rows In table.
int rows_count = rows_table.size();
//Loop will execute till the last row of table.
for (int row=0; row
//To locate columns(cells) of that specific row.
List Columns_row = rows_table.get(row).findElements(By.tagName("td"));
//To calculate no of columns(cells) In that specific row.
int columns_count = Columns_row.size();
System.out.println("Number of cells In Row "+row+" are "+columns_count);
//Loop will execute till the last cell of that specific row.
for (int column=0; column
//To retrieve text from that specific cell.
String celtext = Columns_row.get(column).getText();
System.out.println("Cell Value Of row number "+row+" and column number "+column+" Is "+celtext);
}
System.out.println("--------------------------------------------------");
}  
}
}


Feel free to ask queries. Happy Coding :)


@uthor 
Vaneesh Behl
vaneesh.behl@gmail.com

Monday, February 20, 2017

How To Extract Table Data/Read Table Data Using Selenium WebDriver


Following Script will extract table data:

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class table { 
 
 WebDriver driver = new FirefoxDriver();
 @BeforeTest
    public void setup() throws Exception { 
         driver.manage().window().maximize();
         driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
         driver.get("http://only-testing-blog.blogspot.in/2013/09/test.html"); 
    } 
 
  @AfterTest
 public void tearDown() throws Exception { 
   driver.quit();
     } 
  
 @Test
 public void print_data(){
 
 //Get number of rows In table.
 int Row_count = driver.findElements(By.xpath("//*[@id='post-body-6522850981930750493']/div[1]/table/tbody/tr")).size();
 System.out.println("Number Of Rows = "+Row_count);
 
 //Get number of columns In table.
 int Col_count = driver.findElements(By.xpath("//*[@id='post-body-6522850981930750493']/div[1]/table/tbody/tr[1]/td")).size();
 System.out.println("Number Of Columns = "+Col_count);
 
 //divided xpath In three parts to pass Row_count and Col_count values.
 String first_part = "//*[@id='post-body-6522850981930750493']/div[1]/table/tbody/tr[";
 String second_part = "]/td[";
 String third_part = "]";
 
 //Used for loop for number of rows.
 for (int i=1; i<=Row_count; i++){
  //Used for loop for number of columns.
  for(int j=1; j<=Col_count; j++){
   //Prepared final xpath of specific cell as per values of i and j.
   String final_xpath = first_part+i+second_part+j+third_part;
   //Will retrieve value from located cell and print It.
   String Table_data = driver.findElement(By.xpath(final_xpath)).getText();
   System.out.print(Table_data +"  ");   
  }
   System.out.println("");
   System.out.println("");  
 } 
 }
}

Console output of above given example will looks like bellow.
Number Of Rows = 3
Number Of Columns = 6
11  12  13  14  15  16  

21  22  23  24  25  26  

31  32  33  34  35  36

Tuesday, February 7, 2017

Handling Multiple Browser Tabs Using Selenium Webdriver

We can perform a number of actions by combining third party things along with Webdriver, which webdriver is not capable if doing it alone. In this context sharing with you a program of handling multiple tabs. We can make use of Key controls which Robot api of java provides, to handle multiple tabs.








import java.util.concurrent.TimeUnit;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.Keys;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.firefox.FirefoxDriver;  
 public class Switchbetweenbrowsertab {  
   public static void main(String[] a) throws InterruptedException {  
     // Initialize driver  
     WebDriver driver = new FirefoxDriver();  
     //Maximize browser window   
     driver.manage().window().maximize();  
     //Go to URL  
     driver.get("http://www.google.com");  
     //Set timeout  
 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);  
     // Open new tab  
 driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");  
     //Go to URL  
     driver.get("http://www.gmail.com");  
     //Set new tab timeout  
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);  
     // Do some operation      
     driver.findElement(By.id("gmail-sign-in")).click();  
     driver.findElement(By.id("Email")).sendKeys("WebDriver");  
     driver.findElement(By.id("Passwd")).sendKeys("WebDriver");  
     driver.findElement(By.id("signIn")).click();  
     Thread.sleep(2000);  
     // Switch first tab  
 driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "\t");  
     driver.switchTo().defaultContent();  
     Thread.sleep(2000);  
     // Write search String  
     driver.findElement(By.id("gbqfq")).sendKeys("WebDriver");  
     // Click on Search button      
     driver.findElement(By.id("gbqfb")).click();  
     Thread.sleep(2000);  
     // Browser close   
     driver.close();  
   }  
 }  

Feel free to share your views/queries. Happy Learning :)

@uthor  Vaneesh Behl
vaneesh.behl@gmail.com