How to get Total Number of Rows and Columns in the Web table using Selenium WebDriver?

In this article i will talk about how to get Total Number of Rows and Columns in the Web table using Selenium WebDriver? Firstly let us get the basic understanding about web table.

Refer below post –

How to handle Dynamic Web Table using selenium Webdriver?

In above blog post, we learned how to get all data from web table. Now, let us see, how can we get the total number of rows and total number of columns in the web table.

Below is the HTML code for sample table.

Observe above HTML code for sample table. We know that, tagname <tr> is to indicate that, it is a row in the web table.

Tagname <tr> will be under tagname <tbody>

So, we need to get WebElement of <tbody> tag, so that we can get the count of <tr> tags & ultimately total number of rows in the web table.

So, Below is the sample code, with which we can get total number of rows in the web table.

WebElement TogetRows = driver.findElement(By.xpath("//table[@id='users_table']/tbody"));
List<WebElement>TotalRowsList = TogetRows.findElements(By.tagName("tr"));
System.out.println("Total number of Rows in the table are : "+ TotalRowsList.size());
In order to get total number of columns in the web table, what we need to do is, count total number of <td> tags in first <tr> tagname.

Below is the sample code, with which we can get total number of columns easily.

WebElement ToGetColumns = driver.findElement(By.xpath("//table[@id='users_table']/tbody/tr"));

List<WebElement> TotalColsList = ToGetColumns.findElements(By.tagName("td"));

System.out.println("Total Number of Columns in the table are: "+TotalColsList.size());
Hope this helps !!!

How to handle Dynamic Web Table using selenium Webdriver?

In this article, let us talk about how to handle dynamic web table using selenium webdriver. Before start, let us understand what is web table?
There are two types of Web Tables –
            1.       Static Web Table -> where the data is constant on the web page.
            2.       Dynamic Web table -> where the data changes dynamically based on the users request.

In a very simple language, Web table is nothing but the table present on the web page.

Now, since it is a table, it must have rows, columns, cell data, as like how we have in excel sheet.

So, when we deal with web table through selenium Webdriver, we must understand the internal table structure (how the table is created in HTML format?)

This is not very complex to understand. There are few HTML tag which defines the table.

Observe below screen shot. It is a dynamic web table.

If we see the HTML layout for this table, it is as below –

In above HTML structure, we can clearly see that, table has HTML tags like, table, thead, tbody, tr, td etc. Here –

table -> indicate that, it is a web table

thead -> includes the header of the table
tbody -> includes the body of the table.
tr -> indicate that, this is a row of the table.
td -> indicate that, this is the actual table data (as like cell data in excel).

So, this means, actual data of the web table is present under <td> tag. If you want to read this data from web table, then you should read data from <td> tag.

Let us see how can we do this.

Step1: Locate the WebElement where table is.

WebElement is nothing but HTML element, like dropdown, radio button, table, textbox etc. So, in order to get data from webtable, your first task is, to get the webElement of the table.Ex –

WebElement element1  = driver.findElement(By.xpath("//table[@id='users_table']"));

Step2: To get the list of table data

As we saw earlier, table data will be containted within tag <td>.
So, in order to get table data in to the list, we need to find all the elements by tagname <td> on your webelement.

Ex –

List<WebElement> l = element1.findElements(By.tagName("td"));


Step3: Get the data from List

Once you get data in to the List, by using above step, now we need to retrieve the data from list based on index. Here it is –

 for (int i = 0; i<l.size(); i++){
System.out.println("Total elements present in table are: " +l.size());
 System.out.println("table contents: "+l.get(i).getText());

                                }
Now, you can play with the data, as per demand in your test case.

On the similar basis, we can get total number of rows and total number of columns from the webtable. Explained in below post –

What is driver.close(); and driver.quit(); in Selenium WebDriver?

Driver.close and driver.quit are two different methods which are used to close the browser. But there is some difference in between them, as below –

1) WebDriver.Close() This method is used to close the current open window. It closes the current open window on which driver has focus on. If Suppose your Webdriver has opened more than one browser window, if you use driver.close(); then, it will close only 1 browser window on which driver is focused currently.

2) WebDriver.Quit() This method is used to destroy the instance of WebDriver. It closes all Browser Windows associated with that driver and safely ends the session. WebDriver.Quit() calls Dispose. If Suppose your Webdriver has opened more than one browser window, if you use driver.quit(); then, it will close all browser windows which are opened by that driver.

3) WebDriver.Dispose() This method closes all Browser windows and safely ends the session

You should use driver.quit whenever you want to end the program. It will close all opened browser window and terminates the WebDriver session. If you do not use driver.quit at the end of program, WebDriver session will not close properly and files would not be cleared off memory. This may result in memory leak errors.
Conclusion:
Suppose if we are working with only one window, close () and quit () technically do exactly same thing that is closes the browser/window and releases the memory by quitting the driver. But if in our test, it opens another window while execution and we wish to close one of the windows, to continue execution; we can switch the focus to desired window and perform close action on it. On other hand if we are done with the execution and we want all the browser instances to be killed and release the memory, Quit command is recommended.
Example:
package com.aayushCorp.utilities;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.testng.annotations.AfterMethod;

import org.testng.annotations.BeforeMethod;


public class LoginToAayushCorp { 

                WebDriver driver;

               
                @BeforeMethod

                public void Login() throws Exception {

 
                                driver =new FirefoxDriver();
                                 driver.navigate().to("https://www.google.co.i");
                                 driver.manage().window().maximize();
 
                }             

               

                @AfterMethod

                public void TearDown(){

                                driver.close();

                                driver.quit();

                }

 

}

Which is the best wait in selenium WebDriver?

best wait??

To answer which wait to use will be very specific because the answer may change depending on the Web-element you are focusing, Platform and many more.
According to me, Best wait is Fluent Wait because
  1. It works as per customized condition required at run time.
  2. It gives an option to user to change poll time i.e. At which frequency Dom is to be polled for checking element presence.
  3. It also gives option to ignore any specific exception while searching which may not be related to search but hinder our search resulting in exception or unoptimized search.
  4. Provides better support with JavaScript elements which are attached to DOM but are invisible.
  5. Provides better support to Ajax applications​ as it has option to ignore specific exceptions during element search.

What is Fluentwait? How to Achieve Fluent wait in Selenium?

FluentWait in Selenium WebDriverFluentWait is a customized type of Explicit Wait, here we can define polling period according to our need 2 Secs or 5 Secs, We can also configure the wait to ignore some exceptions like NoSuchElementExceptions.

This wait will check for the elements in the DOM for every 5 Secs until the elements found, if the element found within the period it breaks the search for the element and goes for the further action.

Fluent wait = Explicit Wait + frequency with which control need to be checked on application + exceptions to be ignored like  NoSuchElementExceptions.

Syntax: 
 

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
//Wait for the condition
.withTimeout(30, TimeUnit.SECONDS)
// which to check for the condition with interval of 5 seconds.
.pollingEvery(5, TimeUnit.SECONDS)
//Which will ignore the NoSuchElementException
.ignoring(NoSuchElementException.class);

Features of Fluent Wait:

In Fluent Wait we define a Maximum amount of time to wait for a condition. Also, we will be defining a Polling frequency time while declaring the Fluent Wait.Fluent Wait will be polling the web pages every Polling frequency time to verify the given condition.

Fluent Wait has Inbuilt exception handler, which will handle NoSuchElementException until given Maximum amount of time has breached.

Refer below Example for Fluent Wait –

package demoPackage1;

import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;

import javax.lang.model.element.Element;

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.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import org.testng.annotations.Test;

import com.google.common.base.Function;


public class ExplicitWait {

  @Test
  public void FluentWaitTest(){
    
    System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    
    driver.navigate().to("http://demowebshop.tricentis.com");
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    
       // Define fluent wait
       Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
           .withTimeout(30, TimeUnit.SECONDS)
           .pollingEvery(1, TimeUnit.SECONDS)
           .ignoring(NoSuchElementException.class);

       WebElement element = wait.until(new Function<WebDriver, WebElement>() 
       {
         public WebElement apply(WebDriver driver) { 
           return driver.findElement(By.xpath("//a[@class='ico-register']"));
         } } );
       element.click();
    
    driver.close();
    driver.quit();
  }
}

FluentWait in Selenium WebDriver

What is Explicit wait? How to Achieve Explicit wait in Selenium?

What is Explicit wait in Selenium WebDriver?

 

Explicit wait is a type of synchronization in selenium, which is webelement specific and not applied to driver for his life. Different elements on the page takes different times to load on the page, if we use implicit wait for 10 Sec there is no guarantee that all the elements loaded within 10 sec, some elements may take more than 1 minute, in such conditions Explicit waits can be used to wait for that particular element.
Explicit Wait in-Selenium-Webdriver
We can tell the tool to wait only till the Condition satisfy. Once the condition is satisfying, the tool proceed with the next step. This can be done with WebDriverWait in conjunction with ExpectedConditions Class.
Logic: –
It is applicable to the given control and waits to the maximum amount of time or move forward in case the given condition is met.

We are explicitly making the execution of selenium code to wait, for a certain condition to occur before, proceeding further in the code.

Syntax:

WebDriverWait.until(condition-that-finds-the-element(ExpectedCondition))

WebDriverWait by default calls the Expected Condition every 500 milliseconds until it returns successfully. Expected condition can be in the form of like:
button is disabled/enabled
textbox is visible
alertispresentExample for Explicit Wait

package demoPackage1;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;

public class ExplicitWait {

  @Test
  public void ExplicitWaitTest(){
    
    System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    
    driver.navigate().to("http://demowebshop.tricentis.com/register");
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    
    //define webdriver wait statement with time specified
    WebDriverWait wait = new WebDriverWait(driver, 10);
    
    //apply the wait for specific element. Below statement will wait for element to be present till 10 sec as specified by above
    wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy((By.xpath("//a[@class='ico-register']"))));
    
    driver.close();
    driver.quit();
  }
}

 

Explicit Wait in Selenium WebDriver

Hope This Helps !!!!

What is Implicit wait? How to Achieve Implicit wait in Selenium?

What is Implicit wait in Selenium WebDriver?

It means that we can tell Selenium that we would like it to wait for a certain amount of time before throwing an exception (when it cannot find the element on the page).

synchronization in selenium webdriver - Implicit wait

For example:

driver.manage().timeouts().pageLoadTimeout(50,TimeUnit.SECONDS);
 
Above statement will set the navigation timeout as 50. This means that selenium script will wait for maximum 50 seconds for page to load. If page does not load within 50 seconds, it will throw an exception.
The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.
Syntax:
 driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
Implicit waits are used to provide a default waiting time (say 30 seconds) between each consecutive test step/command across the entire test script. Thus, subsequent test step would only execute when the 30 seconds have elapsed after executing the previous test step/command.

Logic: – Selenium checks the control in first go, in case it is not able to find the control it will wait for the given implicit time, and then try again, after the end of the wait time to look for the control before throwing an exception. By default, its value is 0.

Limitation: – implicit wait will be applied to all the elements of the test case by default.

Key Notes

  • Implicit wait is a single line of a code and can be declared in the setup method of the test script.
  • When compared to Explicit wait, Implicit wait is transparent and uncomplicated. The syntax and approach is simpler than explicit wait.
  • Implicit wait will not work all the commands/statements in the application. It will work only for “FindElement” and “FindElements” statements
Being easy and simple to apply, implicit wait introduces a few drawbacks as well. It gives rise to the test script execution time as each of the command would be ceased to wait for a stipulated amount of time before resuming the execution.
Thus, in order to trouble shoot this issue, WebDriver introduces Explicit waits where we can explicitly apply waits whenever the situation arises instead of forcefully waiting while executing each of the test steps.

Example for Implicit Wait

WebDriver driver = new FirefoxDiriver();

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

driver.get(“http://www.google.com”);

 

Refer below video –

How to handle proxy setting using selenium Webdriver?

 In this article, i will talk about How to handle proxy setting using selenium Webdriver?
Suggested Article to read: What is Firefox profile, Need of Firefox profile?

If we try to access some secure application, we get proxy related error many times and manually we need to update the proxy there. But what if we are automating that secure application? Our automation code should handle this.

Some applications have also issues related to SSL certification error. We again do that manually by accepting & confirming the certificate. But again, what if this encounter when we automate application using selenium Webdriver?
In this post, let’s talk about how to handle proxy setting using selenium Webdriver?
While setting up the proxy, note that there are different values for network.proxy.type , which we need to set based on our need. Here are some values –
0 – Direct connection (or) no proxy.
1 – Manual proxy configuration
2 – Proxy auto-configuration (PAC).
4 – Auto-detect proxy settings.
5 – Use system proxy settings.
Below 4 steps explain how can we handle proxy setting via selenium
Step1: Create the FirefoxProfile object.
Step2: Set preference for network.proxy.type
Step3: Set preference for network.proxy.http
Step4: network.proxy.http_port
    FirefoxProfile profile = new FirefoxProfile();

    profile.setPreference("network.proxy.type", 1);

    profile.setPreference("network.proxy.http", "proxy.domain.example.com");

    profile.setPreference("network.proxy.http_port", 8080);

    profile.setPreference("network.proxy.ssl", "proxy.domain.example.com");

    profile.setPreference("network.proxy.ssl_port", 8080);

    WebDriver driver = new FirefoxDriver(profile);

That’s it, this should handle the updated proxy in code.

Hope this helps. Drop your comments.

How to Handle SSL Untrusted Connection in Selenium WebDriver ?

In this article, i will talk about how to handle SSL Untrusted connection in selenium webdriver.
Suggested Article to read: What is Firefox profile, Need of Firefox profile?

Handling SSL Untrusted Connection in Selenium WebDriver

Some applications have issues related to SSL certification error. We again do that manually by accepting & confirming the certificate. But again, what if this encounter when we automate application using selenium Webdriver?
Using profiles in Firefox we can handle accept the SSL untrusted connection certificate. Profiles are basically set of user preferences stored in a file.
Let’s consider the Firefox browser to create a new profile and set the following:
Handle Untrusted certificate (SSL Certificate error) in Firefox
FirefoxProfile profile = new FirefoxProfile();

profile.setAcceptUntrustedCertificates(true); 

profile.setAssumeUntrustedCertificateIssuer(false);

WebDriver driver = new FirefoxDriver(profile);

Handle Untrusted certificate (SSL Certificate error) in Chrome
To Handle SSL certification error in chrome, we need to Create object of DesiredCapabilities class. Basically, the DesiredCapabilities help to set properties for the WebDriver. The DesiredCapabilities class tells the Webdriver which environment we are going to use for test execution.
DesiredCapabilities cap=DesiredCapabilities.chrome();

// Set ACCEPT_SSL_CERTS  variable to true

cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);

// Set the driver path

System.setProperty("webdriver.chrome.driver","Chrome driver path");

// Open browser with capability

WebDriver driver=new ChromeDriver(cap);

Handle Untrusted certificate (SSL Certificate error) in IE

// Create object of DesiredCapabilities class

DesiredCapabilities cap=DesiredCapabilities.internetExplorer();

// Set ACCEPT_SSL_CERTS  variable to true

cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);

// Set the driver path

System.setProperty("webdriver.ie.driver","IE driver path");

// Open browser with capability

WebDriver driver=newInternetExplorerDriver(cap);

Hope this helps ….

How to Configure Log4j in your project (Using Log4j.properties file) ?

Configure Log4j in your project with the help of Log4j.properties file – 

To start with using logger, below steps explain, how can we use Log4j for logging the required messages which can be used later on, might be for debugging propose or is informative

Step1: Download Log4j from below link (Official site)
Log4j is a logging library used in java development. Firstly download Log4j.jar from its official site (Apache)   http://logging.apache.org/log4j/1.2/download.html
Step2: Add Log4j.jar to project library
We need to add this jar file in to the build path. To do this, right click on the project -> Build path -> Configure Build path -> Libraries tab -> Add external Jar -> Select Log4j.jar

Now Log4j is added in to your library and we are good use it.
Step3: Create Log4j.properties file (Note: Create Under src folder)
In order to configure Log4j in project, we need to specify various parameters in properties file.
The parameters we need to provide are logger, appenders, layout etc.
These parameters need to create in properties file.
To do this, right click on src folder -> New -> Other -> File -> give the name as “Log4j.properties”.

Now we can see the blank text file.
Add the blow lines of code in to the properties file. Save the file.
To write this properties file, one should know about 3 Main components of Log4j, different logging levels in Log4j.

Refer the blow links –
Main Components of Log4j

Different Logging levels in Log4j

// Here we have defined root logger

log4j.rootLogger=ALL,CONSOLE,R


// Here we define the appender

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender

log4j.appender.R=org.apache.log4j.RollingFileAppender

#log4j.appender.TTCC=org.apache.log4j.RollingFileAppender

#log4j.appender.HTML=org.apache.log4j.FileAppender


// Here we define log file location

log4j.appender.R.File=./log/testlog.log

#log4j.appender.TTCC.File=./log/testlog1.log

#log4j.appender.HTML.File=./log/application.html


// Here we define the layout and pattern

log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout

log4j.appender.CONSOLE.layout.ConversionPattern= %5p [%t] (%F:%L)- %m%n

log4j.appender.R.layout=org.apache.log4j.PatternLayout

log4j.appender.R.layout.ConversionPattern=%d - %c -%p - %m%n

#log4j.appender.TTCC.layout=org.apache.log4j.TTCCLayout

#log4j.appender.TTCC.layout.DateFormat=ISO8601

#log4j.appender.HTML.layout=org.apache.log4j.HTMLLayout

#log4j.appender.HTML.layout.Title=Application log

#log4j.appender.HTML.layout.LocationInfo=true

Step4: Create Sample class where we will use Logger

Once Log4j.properties file is created, all is set to write log in your java program.
To achieve this, create sample class.
To define Logger class, use blow syntax
                                Logger log = Logger.getLogger(“your_class_name”);
Import suitable respective jar.
Below sample code shows how can we log the information for various logging level.
Sample Program:
package com.aayushCorp.appData.tests;

import org.apache.log4j.Logger;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

 

public class LoggerExampleTest {

 

                public static void main(String[] args) {

                               

                                Logger log = Logger.getLogger("LoggerExampleTest");

                                WebDriver driver = new FirefoxDriver();

                                driver.navigate().to("https://www.google.co.in");





                                log.info("This is Info message - Launched Google");

                                log.debug("This is debug message");

                                log.error("This is error message");

                                log.fatal("This is fatal mesage");

                                log.trace("This is trace message");

                               

                                driver.close();

                }

 

}
Run the program, you will see below output.
Output:
INFO [main] (LoggerExampleTest.java:16)- This is Info message - Launched Google

DEBUG [main] (LoggerExampleTest.java:17)- This is debug message

ERROR [main] (LoggerExampleTest.java:18)- This is error message

FATAL [main] (LoggerExampleTest.java:19)- This is fatal mesage

TRACE [main] (LoggerExampleTest.java:20)- This is trace message
Based on Log4j.properties file, log messages will be stored in .log file (text file) at the defined path on properties file.
Below screenshot shows the folder location and respective log file.
Hope this helps.