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.

How to Clear ISTQB Foundation level exam?

The Best way to prepare and clear ISTQB foundation level is to read the book “Foundations of Software Testing” by Dorothy Graham. Once you have gone through the book then you can attempt solving various online mock test papers and all.
clear istqb exam
Also, the best part of the book is you can be sure that none of the questions in the exam are out of the book.Tips & Tricks from my side:Try to read the Questions thoroughly and don’t assume anything. While going through the questions do concentrate, and always keep in mind that our mind would like to assume the answers and not what is given as option, most of the time that is the root cause of wrong answers.Also, try to take out the options that is Absurd first and then think of the remaining options if you are not aware.

Even though only one option is correct out of 4, there will be lot of questions where they will ask to arrange in orders, match with choices where you would require thorough understanding.

Keep in mind that out of 40, any average person who is working as QA can answer 25 questions correctly.

Usually if you already have testing experience, the foundation level exam should be easy. With little bit of preparation and good study materials, you will not require any kind of costly training – online or offline.

Depending on your experience level, you decide what to do.

1. Read a book
2. Browse online study material
3. Solve the mock tests online or offline.
For more information, go through below article –

Below is the Book Which i was talking about, you can purchase this, which will be more than sufficient to crack ISTQB Foundation Level. Click on the Image below –


ISTQB Dumps – Help guide to clear ISTQB exam

There are no ISTQB dumps as such, One need to prepare deeply, because frequency of repeated questions is very very low in ISTQB Exam & many are scenario based. I have cleared the ISTQB exam. Below words are from my real time exp.

READ BOTH THE TEXT BOOK AND SYLLABUS – NOT ONCE BUT TWICE, THRICE.

Foundation of Software Testing: ISTQB Certification” by Rex Black, Dorothy Graham is the recommended text book. You need not have to read any other text book.

All you need to do is prepare for the exam of the prescribed ISTQB Foundation book.

If anyone is looking for soft-copy of this book, let me know, I can share.

This is the book for the exam. It Contains 6 chapters. Won’t take much time as the testers will be familiar with the concepts. Take a week or 2 to brush up the concepts.

ISTQB syllabus is your bible. Text book explains every concept in detail with elaborated examples. The ISTQB syllabus is like a notes made by summarizing the text book. You might not understand several concepts if you directly read the syllabus. So read the text book first and then the syllabus.

You can download the syllabus here –http://www.istqb.org/downloads/syllabi/foundation-level-syllabus.htmlDo go through the testing GLOSSARY. There are several terminologies used in the text book and syllabus which requires you to go through the glossary.

DO NOT RELY ON DUMPS!

Several ISTQB aspirants are mislead with the concept that they can clear the exam just by solving few dumps that are available online without any preparations.

SOLVE the questions that are available online. They help you to understand the nature of questions that will appear in the exam, it might also boost your confidence a bit but NEVER rely only on question paper dumps! You cannot clear the exams by only solving the question dumps. There are several dumps available online with incorrect answers which might confuse you.

For taking mock tutorials, various sites are having data over internet, I used to prefer:  testpot

A word of caution as far as dumps are concerned, the dumps which are freely available on internet does not contains the actual question which are asked in exam so be careful. It contains much more standard scenarios. This was my personal experience; others may vary on my opinion.