Selenium Interview Questions

Just spend just 10 minutes every day to master the top Selenium interview questions. This ultimate list of Selenium interview questions and answers will help you to answer on the topics like different Locators supported in Selenium, ways to upload a file using AutoIt tool, the difference between driver.findElement() and driver.findElements() commands, Selenese and different types of Selenese, Page Object Model in Selenium, etc. Start preparing these Selenium technical interview questions and answers, which are for freshers as well as for experienced, and this will land you a good job as a Quality Assurance Engineer or Senior Quality Analyst.

  • 4.7 Rating
  • 101 Question(s)
  • 60 Mins of Read
  • 7651 Reader(s)

Beginner

Selenium can only be used to test/automate web-based applications. It cannot be used to automate desktop applications.

Selenium WebDriver doesn’t have an inbuilt data-driven testing mechanism. This means the user needs to manually connect automated tests to external data sources.

Selenium doesn't support testing on images. It needs to be integrated with Sikuli for image-based testing

There is no native reporting capability. This can be resolved by integrating it with frameworks like TestNG /JUnit

A Windows-based popup cannot be tested using Selenium alone. AutoIT needs to be used to handle the windows based popups.

Selenium is an open source so there is no dedicated support available for it.

Selenium doesn’t have built-in object repository.

Selenium web driver uses below 8 locators to find the elements on the web page:

  • id – used to select the element with a specified @id attribute.
  • name – used to Select the first element with the specified @name attribute.
  • linkText - used to select link /anchor tag element which contains the matching text
  • PartialLinkText – used to select link (anchor tag) element which contains text matching the specified partial link text.
  • tagName – used to locate the element using its HTML Tag
  • className – used to locate the Element using a class Name
  • cssSelector - used to locate the element using CSS selectors
  • xpath – used to locate the element using an XPath expression.

WebDriver is an Interface. We need to create an Object of the required driver class such as FirefoxDriver, ChromeDriver, InternetExplorerDriver etc.

Syntax to launch Firefox Driver: 

WebDriver driver = 
new FirefoxDriver();
Also to use geckodriver with Selenium, upgrade to Selenium 3.x or higher version  and set the property as follows:
System.setProperty("webdriver.gecko.driver", 
"D:\\Selenium Environment\\Drivers\\geckodriver.exe");

Syntax to launch Chrome Driver: 

WebDriver driver = new 
ChromeDriver();

Syntax to launch Internet Explorer Driver: 

WebDriver driver = new 
InternetExplorerDriver();

Syntax to launch Safari Driver: 

WebDriver driver = new 
SafariDriver();

driver.navigate().forward(); – used to navigate to the next web page with reference to the browser’s history

driver.navigate().back(); – used to take back to the previous web page with reference to the browser’s history

driver.navigate().refresh(); – used to to refresh the current web page i.e. reloading all the web elements

driver.navigate().to(“url”); – used to launch a new web browser window and navigate to the specified URL .

Selenium IDE (Integrated Development Environment) previously known as Selenium recorder is the simplest tool. Selenium IDE is an automated testing tool that supports record and playback actions.

This firefox plugin has immense benefits for test automation.

Advantages are -

  • It is an Open-Source tool
  • very easy to get started with for functional testing of web applications.
  • It enables to record and playback for testing web applications.
  • Capability to convert the test to different programming languages
  • Experience in a programming language is not required
  • Can debug and set breakpoints
  • Available as google chrome IDE

Disadvantages are -

  • Being a firefox plugin, the scope is limited to firefox browser
  • Does not support iteration
  • Error handling is not supported
  • Database testing is not supported
  • Grouping of scripts not supported
  • Does export WebDriver script
  • Does not support data-driven scripts.

Selenium is a very widely used reliable software testing tool across the world. Many IT companies are using Selenium to meet their fast delivery schedules. Selenium is a super-duper hit tool in the software testing field. Selenium has its own advantages but still, there are some limitations which we should know.

Let us discuss Selenium limitations one by one -

  • It only supports web applications and does not support non-web based applications such as desktop applications or mobile applications.
  • Being open-source, for technical issues need to rely on the selenium community forum which may not give you quick and reliable solutions.
  • Some user-specific pages load different elements depending on the user, or sometimes elements appear depending on the previous action taken, for example, list of cities in the dropdown corresponding to the country selected in previous action loads when you select a country. Selenium may not identify elements on run time. We need to ads explicit waits in the script so the element loads properly and handle these type of scenarios.
  • Some web elements properties changes on page load. Handling these dynamic elements is a bit tricky in selenium. To handle dynamic elements, use  XPath or CSS with functions like starts-with, contains, ends with, etc.
  • Good hold on the programming language is essential for successful automation testing.
  • To work with selenium you need to have programming skills. A high skill level is expected from Selenium automation engineer.
  • No inbuilt reporting capability. You have to rely on plug-ins like JUnit and TestNG for test reports.
  • Image testing is not supported in selenium. Selenium standard API interacts with browser and it difficult to test image-based applications.
  • Test environment preparation in Selenium takes more time compared to other tools like UFT, RFT, Silk test, etc
  • No test tool integration for Test Management. Neither Selenium supports test management nor it could be integrated with any test management tool.
  • Selenium does not handle window based pop-ups. You need to use JavaScript Robot class a java based utility or third-party open-source tool AutoIt.
  • There is a limitation on the automating of captcha in selenium. You need to use a third-party tool to automate captcha.

Selenium IDE was created with the intention to increase the speed of test case creation. It records user action and playback which helps you create simple tests quickly. The interface is user-friendly and also supports multiple extensions.

Selenium IDE(Integrated Development Environment) is an open-source software testing tool for web applications and Firefox Add-on and chrome extension. Without paying a single penny Testers and web developers can use and download it. The purpose of behind Selenium IDE is to increase the speed of test case creation and it allows to edit, record and debug the tests. It helps the users to record tests quickly and playback tests in the actual environment that it will run in. Depending on your requirement for test creation, test environment and test execution you can use Selenium IDE.

Selenium is not a single tool but below tools comes under selenium umbrella -

  • Selenium IDE
  • Selenium RC
  • Selenium WebDriver
  • Selenium Grid

As Selenium Grid is just a set of configurations and Selenium RC is deprecated, let us discuss different operating systems supported by below Selenium Components only-

  • Selenium IDE
  • Selenium WebDriver

Operating Systems supported by Selenium IDE

Latest Selenium IDE  supports below mentioned operating systems which support Firefox and chrome runs on the Chrome and Firefox Browsers:

  • Microsoft Windows
  • Linux
  • Mac OS X
  • Solaris
  • Other operating systems which support Firefox and Chrome Browsers

Operating Systems supported by Selenium WebDriver are

  • Microsoft Windows
  • Mac OS X
  • Linux

To conclude, selenium at a high level is supported by the following Operating Systems:

  • Microsoft Windows
  • Linux
  • Mac OS X

Below is the list of selenium components -

  • Selenium IDE
  • Selenium client API
  • Selenium WebDriver
  • Selenium Remote Control
  • Selenium Grid

Selenium IDE -

Selenium IDE was created with the intention to increase the speed of test case creation. It records user action and playback which helps you create simple tests quickly. The interface is user-friendly and also supports multiple extensions.

Selenium IDE(Integrated Development Environment) is an open-source software testing tool for web applications and Firefox Add-on and chrome extension. Without paying a single penny Testers and web developers can use and download it. The purpose behind Selenium IDE is to increase the speed of test case creation and it allows to edit, record and debug the tests. It helps the users to record tests quickly and playback tests in the actual environment that it will run in.

Selenium client API

Selenium IDE has its very own scripting language called Selenese, which provides options for clicking a link, selecting something, retrieving data from opened pages. Instead of writing tests in Selenese, these tests can also be written in different programming languages. Tests communicate with selenium by calling methods in Selenium Client API. Selenium provides client APIs for Java, C#, Ruby, JavaScript, R, and Python. With Selenium 2, a new Client API is introduced with WebDriver as its central component.

Selenium WebDriver

Selenium WebDriver is a collection of APIs used to automate web application testing. It is a framework to automate a browser that accepts commands and sends it to the browser through browser-specific drivers. WebDriver control browser by communicating with it. Selenium WebDriver supports Firefox, Chrome, IE and Safari browsers and Java, C#, PHP, Python, Perl, Ruby programming languages. It is designed to provide a simple, concise programming interface. It enables a user to use a programming language of their choice to write test scripts and enables them to use conditional operations, looping and other programming concepts which helps to make test script robust.

Selenium Remote Control

Selenium RC(Remote Control) is a tool that allows creating automated web application UI tests in any programming language.

Selenium RC is composed of two components

1. Selenium Server -

It launches and kills browsers and interprets and runs the Selenese commands passed from the test program. For browser request, Selenium server acts as an HTTP proxy by intercepting and verifying HTTP messages passed between the browser and the AUT. In short Selenium Server receives Selenium commands from your program, Server interprets commands and reports back results of running test to your program.

2. Selenium Client -

Client libraries provide the interface between a programming language and the RC Server. Client libraries of different programming languages, instructs Selenium Server how to test the AUT bypassing server test script's Selenium commands.

The client libraries allow you to run Selenium commands from a program. Each supported language has a different client library. The selenium client library provides a programming interface (API) that runs Selenium commands from your program. Each interface contains a programming function that supports each Selenese command.

The client library gets Selenese command and passes it to the Selenium Server for processing a specific action on the application under test (AUT) and receives the result of that command and sends it to your program. Your program stores received result into program variable and report is a success or failure.

Selenium RC is used to execute scripts on different browsers as RC supports multiple browsers like IE, Firefox, Chrome, Safari, Opera, etc.

Synchronization is a mechanism that involves two or more components that work parallel with Each other.

In Test Automation, Application Under Test and Test Automation tool are the two components.

Both these components have their own speed,  and to make these two components should move with the same and desired speed, we should write our scripts in such a way that we will not encounter "Element Not Found" errors which will consume time again in debugging.

Unconditional and Conditional Synchronization are the two categories of synchronization.

Unconditional Synchronization:

In Unconditional synchronization, we specify the timeout value only. The tool will wait until a certain amount of time and then proceed further.

Examples: Wait() and Thread.Sleep(),

The disadvantage in the above statements is, even though the application is ready there is a chance of unnecessary waiting time.

When we interact with third-party systems like interfaces, it is not possible to write a condition or check for a condition. In such scenarios, we have to make applications wait for a certain amount of time by specifying timeout value and the unconditional wait comes as help here.

Conditional Synchronization:

In conditional synchronization, we specify a condition along with timeout value, so the tool waits and checks for the condition and then it come out if nothing happens.

It is important to set the timeout value in conditional synchronization because the tool should proceed further instead of making the tool to wait for a particular condition to satisfy.

In Selenium implicit Wait and Explicit Wait are two components of conditional statements.

There are two types of Conditional Synchronization:

  1. Implicit Wait
  2. Explicit Wait

Implicit Wait: Implicit Wait tells the WebDriver to before throwing exception NoSuchElement/ElementNotVisible, wait until the stated time. Waiting time between each consecutive step is taken by default across the test script. So, the next test Step will be executed only when the specified time is elapsed.

Syntax:

driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(30));

FromMilliSecondsThe Implicit wait takes two arguments: TimeSpan (timeToWait) and time measure.

  • FromSeconds
  • FromMinutes
  • FromHours
  • FromDays  

Explicit Wait:

When page load dynamically explicit waits are good to use. Before throwing NoSuchElement (or) ElementNotVisible Exceptions, explicit Wait tells the WebDriver to Wait till the specified condition is met or maximum time elapses. Explicit waits are applied for the specified test Step in the test script.

We must first create an instance for the “WebDriverWait” class to use Explicit wait.

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
wait.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.Id(element_ID)));

The reference variable “wait” informs the WebDriver to wait until the Expected condition to occur (or) Wait for the specified time of 30seconds, whichever shows in first place before throwing an exception.

Below are the ExpectedConditions:

  • AlertIsPresent()
  • ElementSelectionStateToBe()
  • VisibilityOfAllElementsLocatedBy()
  • ElementToBeSelected()
  • FrameToBeAvaliableAndSwitchToIt()
  • InvisibilityOfTheElementLocated()
  • TextToBePresentInElementLocated()
  • TextToBePresentInElementValue()
  • TitleIs()
  • TitleContains()
  • VisibilityOf()
  • VisibilityOfAllElements()
  • VisibilityOfElementLocated()
  • InvisibilityOfElementWithText()
  • ElementToBeClickable()
  • PresenceOfAllElementsLocatedBy()
  • PresenceOfElementLocated()
  • TextToBePresentInElement()

The Implicit wait is simple as compared to Explicit wait and can be declared in setup method of the test script in single line of code. But, despite being easy and simple to apply and hence rising scope of Explicit Waits as these waits can be applied for a specified test Step in test script.

When two or more components work together to perform any action, we expect these components to work together at the same pace, and the coordination between these components to run parallel is called Synchronization. We require synchronization so that the Application Under Test(AUT) and testing tool are running in sync with each other.

Most of the web applications are developed using JavaScript and Ajax and it might happen that some of the elements may load at distant time intervals, because of which we may see “ElementNotVisibleException” or “'NoSuchElementException” exceptions. To handle these scenarios, we use Synchronization/wait to overcome these exceptions.

Two components are involved in Automation Testing is Application under test and Test Automation Tool.

While writing scripts we must ensure these two components move with the same desired pace, because both have their own agility and therefore, avoid Errors which will exhaust time in debugging.

There are two types of Conditional Synchronization:

  1. Implicit Wait
  2. Explicit Wait

Implicit Wait: Implicit Wait tells the WebDriver before throwing exception NoSuchElement/ElementNotVisible, to wait until the stated time. Waiting time between each consecutive step is taken by default across the test script. So, the next test Step will be executed only when the specified time is elapsed.

Syntax:

driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(30));

The Implicit wait takes two arguments: TimeSpan (timeToWait) and time measure.

  • FromMilliSeconds
  • FromSeconds
  • FromMinutes
  • FromHours
  • FromDays

Explicit Wait:

When page load dynamically explicit waits are good to use. Before throwing NoSuchElement (or) ElementNotVisible Exceptions, explicit Wait tells the WebDriver to Wait till the specified condition is met or maximum time elapses. Explicit waits are applied for the specified test Step in the test script.

We must first create an instance for the “WebDriverWait” class to use Explicit wait.

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
wait.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.Id(element_ID)));

The reference variable “wait” informs the WebDriver to wait until the Expected condition to occur (or) Wait for the specified time of 30seconds, whichever shows in the first place before throwing an exception.

Below are the ExpectedConditions:

  • AlertIsPresent()
  • ElementSelectionStateToBe()
  • VisibilityOfAllElementsLocatedBy()
  • ElementToBeSelected()
  • FrameToBeAvaliableAndSwitchToIt()
  • InvisibilityOfTheElementLocated()
  • TextToBePresentInElementLocated()
  • TextToBePresentInElementValue()
  • TitleIs()
  • TitleContains()
  • VisibilityOf()
  • VisibilityOfAllElements()
  • VisibilityOfElementLocated()
  • InvisibilityOfElementWithText()
  • ElementToBeClickable()
  • PresenceOfAllElementsLocatedBy()
  • PresenceOfElementLocated()
  • TextToBePresentInElement()

The Implicit wait is simple as compared to Explicit wait and can be declared in setup method of the test script in a single line of code. But, despite being easy and simple to apply and hence rising scope of Explicit Waits as these waits can be applied for a specified test Step in test script.

Id Locator :

The ID locator searches for an element having an id attribute corresponding to the specified pattern on the page.

Syntax is - driver.findElement(By.id("id value"));

PROS:

  • Each id is expected to be unique, so no chance of matching several elements

CONS:

  • Works on elements with fixed ids and not generated ones

Name Locator :

The Name locator searches for an element having a name attribute corresponding to the specified pattern on the page. You can specify a filter to refine your locator.

Syntax is - driver.findElement(By.name("name-value"));

PROS:

  • Fits well with a fixed list of similar elements

CONS:

  • It is difficult to use with data-bound lists

Link Locator:

The link locator is intended to select links only and selects the anchor element containing the specified text: link=The text of the link

Syntax is - driver.findElement(By.linkText("link text"));

PROS:

  • Selects only anchor elements
  • Useful in testing navigation

CONS:

  • You should know the text of the link in advance

DOM Locator :

The DOM locator works by locating elements that match the javascript expression referring to an element in the DOM.

Locating by getElementById

Syntax is - document.getElementById("id of the element");

Locating by getElementsByName

Syntax is - document.getElementsByName("name")[index];

Locating by DOM - dom: name

Syntax is - document.forms["name of the form"].elements["name of the element"]

  • name of the form = the value of the name attribute of the element you want to access of the form tag
  • name of the element = the value of the name attribute of the element you want to access

Locating by DOM - dom: index

Syntax is - document.forms[index of the form].elements[index of the element]

  • index of the form = With respect to the whole page, the index number (starts at 0) of the form
  • index of the element = With respect to the whole form, the index number (starts at 0) of the element

PROS:

  • Javascript allows building dynamic locators

CONS:

  • Depends on the structure of the page

XPath Locator:

XPath is the navigation tool for XML. XPath can be used everywhere where there is XML. Xpath Locate element by using an XPath expression.

Syntax is -  

//button[@value="value text"];
 //div[@id="id value"]/button[0];

PROS:

  • Very precise locators

CONS:

  • Slower compared to CSS
  • Depends on the browser’s XPath implementation which is not always complete and not recommended for cross-browser testing

CSS Locator

To find the elements on the page, the CSS locator uses CSS selectors.

Syntax is - div[id="id value"] > button[value="value text"];

selects the button with its value property set at value text if children of the id value are div

PROS:

  • Faster than XPath
  • good balance between structure and attributes
  • Allows selection of elements by their surrounding context

CONS:

  • More complex and require a steeper learning curve

Structure-Dependent Or Not?

Locators are classified into two categories ie Structure-based locators and Attributes-based locators.

  • Locators that depends on the structure of the page to find elements are structured based locator. Below locators are structure-based locators.
    • XPath
    • DOM
    • CSS
  • Locators that depend on the attributes of the elements to locate are Attribute-based locators. Below locators are the Attribute-based locators.
    • Identifier
    • Id
    • Name
    • Link
    • CSS
  • Consider all these points pertaining to CSS before choosing a locator strategy
  • CSS is used widely because it is flexible and gives a good balance between using structure and attributes to find the elements.

To find the web element on the page, the CSS locator can be used.

Syntax is - div[id="id value"] > button[value="value text"];

selects the button with its value property set at value text if children of the id value are div

PROS:

  • Faster than XPath
  • good balance between structure and attributes
  • Allows selection of elements by their surrounding context

CONS:

  • More complex and require a steeper learning curve

Structure-Dependent Or Not?

Locators are classified into two categories ie Structure-based locators and Attributes-based locators.

  • Locators that depends on the structure of the page to find elements are structured based locator. Below locators are structure-based locators.
    •  XPath
    • DOM
    • CSS
  •  Locators that depend on the attributes of the elements to locate are Attribute-based locators. Below locators are the Attribute-based locators.
    • Identifier
    • Id
    • Name
    • Link
    • CSS
  • Consider all these points pertaining to CSS before choosing a locator strategy
  • CSS is used widely because it is flexible and gives a good balance between using structure and attributes to find the elements.

XPath stands for XMLPath. Using XPath you can navigate to any element in an XML document. As  XML is a component of HTML, XPath can be used to find web elements on any web page. Relative XPath and absolute XPath are two types of XPath.

Absolute XPath: Absolute XPath is the direct way to find the element. The disadvantage of the absolute XPath is that for any changes made in the path of the element then that XPath will fail. 

Relative XPath: Relative XPath is a URL that contains a portion of the full path and it is alternatively referred to as a partial path or non-absolute path. A relative path is used to specify the location of a directory relative to another directory. A relative path starting from the element you want to refer to and go from there.

Absolute XPath is a direct way to finding an element and it starts with the root node or a single forward-slash (/). It is similar to what we do in our system when a particular file path is to be specified [eg: D:\photos\new album\myimage.JPG ] and in that case if you move the file to another location [eg: D:\photos\old album\myimage.JPG ] it will not be found correctly by the path, the same applies to the Absolute Xpath concept too, so for any changes made in the application hierarchy which have an impact on that particular element then the XPath of the element will definitely fail. Hence, absolute Xpath is not considered to be a reliable technique to locate an element. 

  • Syntax: //html/body/tag1[index]/tag2[index]/.../tagN[index]
  • Example: //html/body/div[2]/div/div[2]/div/div/div/fieldset/form/div[1]/input[1]

Relative XPath is a URL that contains a portion of the full path and it is alternatively referred to as a partial path or non-absolute path. A relative path is used to specify the location of a directory relative to another directory. A relative path starting from the element you want to refer to and go from there. Relative XPaths are preferred as they are not complete path and chances of changes are less compared to an absolute path. Its complete location is based on its relation to the directory to which it is linked.

Relative XPath starts with Double forward slash(//). For example, for google login page //*[@title=’google’]

It directly interacts with the current node of the element.

It is the shortest way to navigate to an element on a webpage.

With the use of relative Xpath, you can handle dynamic elements too.

In Relative XPath, we have various functions with which we can create a more robust and customized Xpath for better element handling.

Intermediate

driver.get("URL") and driver.navigate().to("URL")  both these commands are used to navigate to a URL passed as parameter.

There is a very little difference between the two commands-

driver.navigate() command maintains browser history or cookies to navigate back and forward and allows you moving back and forward in browser history using driver.navigate().forward() and driver.navigate().back() commands. driver.navigate() command used to navigate to the particular URL and it does not wait to page load.

In the case of a single page application, where the URL is appended by '#' to navigate to a different section of the page, driver.navigate().to() navigates to a particular page by changing the URL without refreshing the page whereas the driver.get() refreshes the page also and waits for the page to load. This refreshing of the page is also the primary reason because of which history is not maintained in the case of the driver.get() command.

Some of the commonly used expected conditions of an element that can be used with explicit waits are-

  • elementToBeClickable(WebElement element or By locator) - An expectation for checking an element is visible or not and enabled so that you can click on it.
  • stalenessOf(WebElement element) - will Wait until an element is no longer attached to the DOM or timeout expires.
  • visibilityOf(WebElement element) - visibilityOf(WebElement element) is used for checking that an element is present on the DOM of a page and visible.
  • visibilityOfElementLocated(By locator) - Used to check if the element is present on the DOM of a page and it is visible.
  • invisibilityOfElementLocated(By locator) - invisibilityOfElementLocated(By locator) used for checking an element is invisible or not present in the DOM.
  • attributeContains(WebElement element, String attribute, String value)  - Waits for a certain text fragment to appear in the attribute.
  • alertIsPresent() - alertIsPresent(), accepts any present alert. and returns true if there is an alert to click
  • titleContains(String title) -  titleContains(String title) used for checking that the title of a page contains a case-sensitive substring.
  • titleIs(String title) - wait till the title of the page is the same as mentioned in the attribute. 
  • textToBePresentInElementLocated(By, String) - wait till expected text appears on targeted element.

Some of the commonly seen exceptions in selenium are-

  1. NoSuchElementException -  You get this exception when the locator mentioned in the Selenium Program code is unable to find the web element on the web page.
  2. ElementNotVisibleException - You get this exception when a WebDriver is able to locate web elements on the current webpage but it is not visible on the screen.
  3. NoAlertPresentException - You get this exception when we try to switch to an alert but the targeted alert is not present.
  4. InvalidElementStateException - You get this exception when the state of an element is not appropriate for the desired action.
  5. NoSuchAttributeException - You get this exception when you are trying to fetch an attribute's value but the attribute is not correct.
  6. WebDriverException - You get this exception when there is an issue with driver instance preventing it from getting launched.
  7. NoSuchFrameException - You get this exception when you try to switch to a frame but the targeted frame is not present.
  8. NoSuchWindowException - You get this exception when you try to switch to a window but the targeted window is not present.
  9. UnexpectedAlertPresentException - You get this exception when an unexpected alert blocks the normal interaction of the driver.
  10. TimeoutException - You get this exception when a command execution gets a timeout.

POM that is Page Object Model is a design pattern popular in selenium to create object repository for web UI elements. It is a widely used design pattern for enhancing test maintenance and to avoid code duplication. It can be used in a type of framework such as modular, data-driven, keyword-driven, etc. POM is an object-oriented class works as an interface to the page for application under test. Under the page object model, for each web page in the application, there must be a corresponding page class. Web element of that web page will be identified by page class and this page class also contains Page methods which perform operations on those WebElements. As per the task performed by these methods, the name of these methods is given.

POM allows Operations. POM concept makes the test scripts cleaner and easy to understand. With the help of POM, the object repository is created independent of test cases, so you can use the same object repository for a different purpose with different tools. We can integrate POM with TestNG/JUnit for functional testing and as well as with JBehave/Cucumber for acceptance testing.

Using POM an Object Repository can be created and can be maintained in the separate file.

  • Code reusability – You can achieve code reusability by writing the code once and use it in different tests.
  • Code maintainability – There is a clean separation between test code and page specific code such as locators and layout which makes it very easy to maintain code. Code changes happen only on Page Object Classes when a UI change occurs and enhances test maintenance and reduces code duplication.
  • Object Repository – Each page is defined as a java class and all the fields on the page will be defined in an interface as members, and the class will then implement the interface.
  • Readability – Due to a clear separation between test code and page-specific code, there is an enhancement in readability.

To read and write an excel file, the Apache POI library is capable to read write both xls and xlsx file format of excel.

Below are the steps to write to excel sheet -

  1. Create an xlsx file and save it at the desired location( d:testdata.xlsx). Add some data to read by selenium and rename sheet (ex. mysheet). 
  2. By default excel cell format is general, make it text.
  3. Close that excel file before the execution of the script.
  4. You need to download POI jars and unzip it. POI jars allow to create, modify, and display MS Office files using Java programs. Go to the official website of Apache POI download section http://poi.apache.org/download.html 
  5. In your editor Select Project
  6. Right-click on the project
  7. Go to build path
  8. Go to 'Configure build path;
  9. Click on the 'lib' tab and, 
  10. add POI external jar files.
  11. Below is the code to write excel file
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.testng.annotations.Test;
public class WriteExcel {
 public static void main(String []args){
  try {
  // To specify the file path which you want to create or write
  File src=new File("./test/testdata.xlsx");
  // To Load the file
  FileInputStream fis=new FileInputStream(src);
   // To load the workbook
   XSSFWorkbook wb=new XSSFWorkbook(fis);
  // To get the sheet to modify or create
   XSSFSheet sh1= wb.getSheetAt(0);
 // here createCell will create column and setCellvalue will set the value
 sh1.getRow(0).createCell(2).setCellValue("2.31.0");
 sh1.getRow(1).createCell(2).setCellValue("3.5");
 sh1.getRow(2).createCell(2).setCellValue("4.66");
// to specify where you want to save file
 FileOutputStream fout=new FileOutputStream(new File("location of file/filename.xlsx"));
// finally write content 
 wb.write(fout);
// close the file
 fout.close();
  } catch (Exception e) {
   System.out.println(e.getMessage());
  }
  }
 }

To read and write an excel file, the Apache POI library is capable to read and write both xls and xlsx file format of excel.

Below are the steps to write to excel sheet -

  1. Create an xlsx file and save it at the desired location( d:/test/testdata.xlsx). Add some data to read by selenium and rename sheet (ex. mysheet). 
  2. By default excel cell format is general, make it text.
  3. Close that excel file before the execution of the script.
  4. You need to download POI jars and unzip it. POI jars allow to create, modify, and display MS Office files using Java programs. Go to the official website of Apache POI download section http://poi.apache.org/download.html
  5. In your editor Select Project
  6. Right-click on the project
  7. Go to build path
  8. Go to 'Configure build path;
  9. Click on the 'lib' tab and, 
  10. add POI external jar files.
  11. Below is the code to read excel file
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.testng.annotations.Test;
 public class ReadExcel {
 public static void main(String []args){
  try {
  // To specify the path of file
  File src=new File("filepath/excelsheetname.xlsx");
   // To load file
   FileInputStream fis = new FileInputStream(src);
   // To load workbook
   XSSFWorkbook wb=new XSSFWorkbook(fis);
   // To load sheet- Here loading the first sheet
      XSSFSheet sh1= wb.getSheetAt(0);
  // getRow() to specify which row we want to read.
  // and getCell() to specify which column to read.
  // getStringCellValue() to specify that we are reading String data.
 System.out.println(sh1.getRow(0).getCell(0).getStringCellValue());
 System.out.println(sh1.getRow(0).getCell(1).getStringCellValue());
 System.out.println(sh1.getRow(1).getCell(0).getStringCellValue());
 System.out.println(sh1.getRow(1).getCell(1).getStringCellValue());
 System.out.println(sh1.getRow(2).getCell(0).getStringCellValue());
 System.out.println(sh1.getRow(2).getCell(1).getStringCellValue());
  } catch (Exception e) {
   System.out.println(e.getMessage());
  }
  }
 }

A headless browser is a browser that does not have a GUI. It is used to simulate programs even though there is no browser installed on the local system. You will not see the browser in your system but will get the result in the console. The headless browser does not have GUI means program runs in the background without GUI.

Two headless browsers widely used are HtmlUnitDriver and PhantomJSDriver.

Advantages of performing Headless browser testing -

  1. It is faster. Compared to browser automation, performance is better. Running regression scripts using browser takes lots of time, but with headless browsers, time is saved.
  2. It allows you to run scripts on a system that does not have a browser.
  3. It helps in executing scripts on different browser versions on the local system even though that browser version is not available on the system. Most of the headless browsers support browser versions.

Disadvantages of Headless browsers testing -

  1. Debugging is difficult using headless browsers as the browser is not available. The only way is to capture a screenshot.

We know Selenium WebDriver is a tool to automate User Interface and We could only interact with Browser using Selenium WebDriver.

We may face a situation where we want to get the data from the database or to update or delete the data from the Database.  If we want to automate anything outside the browser, we need to use other tools to achieve the task. For Database connection and work on it, use JDBC  API Driver. 

JDBC API lets the user connect and interact with the Database and fetch the data based on the queries used in the automation script. JDBC is a SQL level API that allows executing SQL statements. It creates connectivity between Java Programming Language and the database.

To make a connection to the database the syntax is -

DriverManager.getConnection(URL, "userid", "password" )

where the user ID is the username in the database

Password of the configured user

URL format is 'jdbc:< dbtype>://ipaddress:portnumber/db_name'

<dbtype> is the driver for the database to be connected. 

Code to create a connection is -

Connection con = DriverManager.getConnection(dbUrl,username,password);

Code to load JDBC Driver -

Class.forName("com.mysql.jdbc.Driver");

Once connection is set, use Statement Object to send queries -

Statement stmt = con.createStatement();

Once the statement object is created use executeQuery method

stmt.executeQuery(select *  from employee;);

In selenium mostly we click on an element using click() method.

Many times web controls don’t react against selenium commands and we face issues with the Click() statement. To overcome such a situation, in selenium we use the JavaScriptExecutor interface.

JavaScriptExecutor executes Javascript using Selenium driver and provides “executescript” & “executeAsyncScript” methods, to run JavaScript in the context of the currently selected frame or window.

To execute JavaScript within the browser using the Selenium WebDriver script, writing a separate script is not required. Just use a predefined interface named ‘Java Script Executor’ and need to import the JavascriptExecutor package in the script.

Package:

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.JavascriptExecutor;

Syntax:

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(Script,Arguments);

where Script is the JavaScript to be executed.

Arguments are the arguments to the script and are optional and can be empty.

JavascriptExecutor returns either Boolean, or Long, or String, or List, or WebElement, or null.

Let us discuss some scenarios we could handle using JavascriptExecutor :

  1. Without using the sendKeys() method, you can use this interface to type Text in Selenium WebDriver.
  2. Using JavascriptExecutor, you can click on a Button.
  3. You can use it to handle the Checkboxes.
  4. It can be used to generate the Alert Pop window.
  5. You can Refresh browser window using Javascript
  6. You can get the inner text of the entire webpage in Selenium
  7. Get the Title of webpage
  8. Get the domain
  9. Get the URL of a webpage
  10. Perform Scroll on an application using Selenium
  11. Click on a SubMenu which is only visible on mouse hover on Menu
  12. Navigate to a different page using Javascript.

Using Selenium WebDriver, handling of AJAX calls is one of the common issues. Users will not be aware when the AJAX call would be completed and the page has been updated. 

AJAX stands for Asynchronous JavaScript and XML and AJAX allows the web page to retrieve small amounts of data from the server without reloading the entire page. From the client to server AJAX sends HTTP requests and then process the server’s response without reloading the entire page. Wait commands may not work to handle AJAX control in such a scenario and it is just because the actual page is not going to refresh.

For example, When you click on the submit button, the required information may appear on the web page without refreshing the browser or sometimes it may load in a second or sometimes it may take longer. And you do not have any control overloading time. The best way to handle this kind of situation in selenium is to use dynamic waits that are WebDriverWait in combination with ExpectedCondition.

Some of the methods available are as follows:

  1. titleIs() – titleIs() waits for a page to load with a specific title. wait.until(ExpectedConditions.titleIs(“Google”));
  2. elementToBeClickable() – elementToBeClickable() waits for an element to be clickable i.e. element should be present/displayed/visible on the screen as well as enabled.
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath")));
  3. alertIsPresent() – alertIsPresent () waits for an alert box to appear.
    wait.until(ExpectedConditions.alertIsPresent()) !=null);
  4. textToBePresentInElement() – textToBePresentInElement() waits for an element having a certain string pattern.
    wait.until(ExpectedConditions.textToBePresentInElement(By.id(“title’”), “text to be found”));

Selenium has the ability to handle various types of keyboard and mouse events. In order to perform action events, use org.openqa.selenium.interactions Actions in class. This API includes actions such as clicking multiple elements, drag and drop.

Let us create an object action of Action class

Actions action = new Actions(driver);

Now to focus on an element using WebDriver use action -

actions.moveToElement(element).perform();

Here perform() method is used to execute the action.

Let us see mouse events using selenium action class API -

  • click() : It Clicks on an element.
  • doubleClick() : Double clicks onElement.
  • contextClick() : Performs a context-click on an element. 
  • clickAndHold() : It Clicks on an element without releasing.
  • dragAndDrop(source, target) : Invokes click-and-hold at the location and moves to the target location of the element before releasing the mouse. where the source is an element to grab and the target is the element to release. 
  • dragAndDropBy(source, xOffset, yOffset) : Performs click-and-hold at the source location and shifts by a given offset, then frees the mouse. Here xOffset is to shift horizontally and yOffset is to shift vertically.
  • moveByOffset(x-offset, y-offset) : Shifts the mouse from its current position (or 0,0) by the given offset. 
  • moveToElement(toElement) : Shifts the mouse to the centre of the element. 
  • release() : Releases left mouse button at the existing mouse location

Both setSpeed() and sleep() will delay the speed of script execution.

Thread.sleep(): It will stop the current execution of script thread for a specified period of time. It is done only once and it takes a single argument in integer format

For example: thread.sleep(2000)- Execution of the script will be stopped for 2 seconds

It waits only once at the command given at sleep.

SetSpeed(): It will stop the execution of each and every selenium command for a specific amount of time and it takes a single argument in integer format

For example selenium.setSpeed(“2000”)- It will wait for 2 seconds for each and every command before the execution of the next command.

It runs each command after setSpeed delay by the number of milliseconds mentioned in set Speed.

Page Factory class in selenium is an extension of the Page object design pattern. 

Page Factory is an inbuilt concept that provides an optimized way to implement the Page Object Model. Here optimization is, it refers to the fact that the memory utilization is very good and also the implementation is done in an object-oriented manner.

It is used to initialize the elements on the Page Object or instantiate the Page Objects. Annotations for elements can be created and recommended as the describing properties may not always be descriptive enough to differentiate one object with the other.

Here the concept of separating the Page Object Repository and Test Methods is followed. Instead of using ‘FindElements’, here we use annotations like @FindBy to find WebElement, and the initElements method to initialize web elements from the Page Factory class. It makes handling "Page Objects" easier and optimized by providing the  @FindBy annotation and initElements method.

@FindBy annotation - can accept attributes tagName, partialLinkText, name, linkText, id, CSS, className & XPath and is used to locate and declare web elements using different locators.

Example - @FindBy(id="elementId") WebElement element;

initElements() - is a static method of PageFactory class used in conjunction with @FindBy annotation. Web elements located by @FindBy annotation can be initialized using the initElements method. 

Example - initElements(WebDriver driver, java.lang.Class pageObjectClass)

Questions on the dropdown and selecting a value from the dropdown are common Selenium interview questions.

You should know that to work with a dropdown in Selenium, always make use of HTML tag: ‘select’. Without using ‘select’, you cannot handle dropdowns. 

So the steps to select an element from the drop-down are -

  1. Identify the ‘select’ HTML tag as dropdowns must have the ‘select’ tag)
  2. Select an option from that dropdown element

To identify the ‘select’ html element from the web page, use findElement() method. Look at the below piece of code:

WebElement mySelectElement = driver.findElement(By.id("id value"));

Select dropdown = new Select(mySelectElement);

Now to select an option from that dropdown can be done in three ways:

    dropdown.selectByVisibleText(“text1”); → Select an option by the visible text.

    dropdown.selectByIndex(“1”); → Select, by choosing the Index number of a option.

    dropdown.selectByValue(“value”); → Select by choosing the value.

These are the different ways to select a value from a dropdown.

There are different frameworks available in selenium, but below three frameworks are popular and you can create these three different types of frameworks using selenium are -

Data-Driven Framework:- Currently Data Driven Framework is one of the popular automation testing frameworks. 

The entire test data is generated from some external files like Excel, CSV, XML or some database table,  and it is called Data Driven framework. The data-driven test performs the same functionality with multiple input values.

Keyword Driven Framework:- Keyword driven framework is also known as Table driven testing or action word based testing.

When the instructions and operations are written in a different external file like an Excel worksheet, it is called Keyword Driven framework. It is used to speed up automation testing by using the keywords for a common set of actions.

Hybrid Framework:- It is a combination of both the Data-Driven framework and the Keyword Driven framework is called the Hybrid framework. Keywords are written in external files like excel file and code will call this file and execute test cases.

WebElement is HTML DOM(document object model) element. When a browser loads HTML document, WebElement converts into the document object. WebElement is a class in selenium WebDriver and converts an object into the document object. Text Box, text, button, link table, radio button, checkbox, etc, present on the webpage is webelement. Selenium needs to identify these elements uniquely before performing any action on webelement. WebElement commands will apply to almost all DOM elements on the page and used to interact with visible as well as invisible elements present on-page.

Below are some important WebElement Methods -

1. Clear(): Clear() command will clear the value in the text box if the element is text entry element. No parameters required for this method.

//Create WebElement
WebElement clearElement = driver.findElement(By.id("id value"));
// Clear text
clearElement.clear();
// Or
driver.findElement(By.id("id value")).clear()

2. SendKeys: This mimics typing and sets the value into an element. Character sequence is the parameter required for this method and returns nothing. This method used for text entry elements like INPUT and Text Area elements.

//Create WebElement
WebElement sendkeyElement = driver.findElement(By.id("id value"));
// Send text
sendkeyElement.sendKeys("Sample Text");
//OR
driver.findElement(By.id("id value").sendKeys("Sample Text"));

3. Click():  To click any element on a web page, Click() command is used and is the most common way of interacting with web elements like link, radio buttons, checkboxes, etc. No parameters required for this method.

//Create WebElement
WebElement clickElement = driver.findElement(By.id("id value"));
//Click on an element
clickElement.click();
//OR
driver.findElement(By.id("id value")).click();

4. isDisplayed(): To check if an element is currently displayed or not? isDisplayed() command is used. No parameter is required with this command and returns a boolean value. If an element is present on the page, it will return true else throws 'NoSuchElementFound exception' exception.

//Create WebElement
WebElement  displayElement = driver.findElement(Bt.id("id value"));
//perform operation
displayElement.isDisplayed();
//Or
driver.findElement(By.id("id value")).isDisplayed();

5. isEnabled():  To check if an element is currently enabled or not? isEnabled() command is used. No parameter is required with this command and returns a boolean value. If an element is enabled on the page, it will return true.

//Create WebElement
WebElement enabledElement = driver.findElement(By.id("id value"));
//perform operation e.g. on radio button or checkbox
enabledElement.isEnabled();
//Or
driver.findElement(By.id("id value")).isEnabled();

6. isSelected(): To determine whether the element is selected or not, isSelected() method is used. No parameter is required with this command and returns a boolean value. If an element is currently selected or checked, it will return true. This method applies to input elements like Checkboxes, radio buttons, etc.

//Create WebElement
WebElement selectedElement = driver.findElement(By.id("id value"));
selectedElement.isSelected();
//Or
driver.findElement(By.id("id value")).isSelected();

7. Submit(): If an element is a form or an element within a form, Submit() method works well. No parameter is required with this command and returns nothing. As an impact of Submit(), current web page changes, Submit() method will wait till new page loads.

//Create WebElement
WebElement submitElement = driver.findElement(By.id("id value"));
//Perform submit
submitElement.Submit();
//Or
driver.findElement(By.id("id text").Submit();

8. getText(): getText() method fetch visible inner text of the element. No parameter is required to pass but returns string value.

//Create WebElement
WebElement gettextElement = driver.findElement(By.id("id text"));
//perform getText operation
gettextElement.getText();
//Or
driver.findElement(By.id("id value")).getText();

9. getTagName(): This method returns the tag name of the web element. No parameter is required to pass.

//Create WebElement
WebElement tagnameElement = driver.findElement(By.id("id value"));
//perform operation
tagnameElement.getTagName();
//Or
driver.findElement(By.id("id value")).getTagName();

10. getAttribute(): This method returns the value of the given attribute of the element. A string is accepted as a parameter and returns String value.

//Create WebElement
WebElement attributeElement = driver.findElement(By.id("id value"));
//perform operation
attributeElement.getAttribute("Attribute");
//Or
driver.findElement(By.id("id value")).getAttribute("Attribute");

11. getCssValue(): This method returns CSS property value of the given element. No parameter is required to pass and return a string value.

//create webelement
WebElement cssElement = driver.findElement(By.cssSelector("css value"));
cssElement.getCssValue();

12. getSize(): This method returns the width and height of the element. No parameter is required to pass and return the size of the element on the page.

//Create WebElement
WebElement sizeElement = driver.findElement(By.id("id value"));
Dimension dimensions = element.getSize();
System.out.println("Height :" + dimensions.height + "Width : "+ dimensions.width);

13. getLocation(): To locate location of the element getLocation() method is used. No parameter is required to pass but returns the Point object, from which we get X and Y coordinates.

//create WebElement
WebElement LocationElement = driver.findElement(By.id("Btn"));
Point pnt = element.getLocation();
System.out.println("X coordinate : " + point.x + "Y coordinate: " + point.y);

Let us see, the example of creating WebElement on facebook.com -

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class WebElement_Create {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
// Wait to load page
  driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
// Set driver path
System.setProperty("webdriver.gecko.driver", "C:\\geckodriver.exe");
// Redirect to url
driver.get("https://facebook.com");
//============ clear() ====================
// Create WebElement
WebElement clElement = driver.findElement(By.id("email"));
// Perform clear operation
clElement.clear();
// ========== sendKeys() =================
// Create WebElement
WebElement keyElement = driver.findElement(By.id("email"));
// Perform sendKeys operation
keyElement.sendKeys("Anderson@gmail.com");
//============ click()=====================
// Create WebElement
WebElement clickElement = driver.findElement(By.id("u_0_2"));
// Perform click operation
clickElement.click();
//============ isDisplayed() =============
// Create WebElement
WebElement displayElement = driver.findElement(By.id("u_0_2"));
// Perform isDisplayed operation
String disp = displayElement.isDisplayed();
System.out.println(disp);
//========== isEnabled() ===============
// Create WebElement
WebElement enabledElement = driver.findElement(By.id("u_0_2"));
// Perform isEnabled operation
String ena = enabledElement.isEnabled();
 System.out.println(ena);
//=========== isSelected() ===========
// Create WebElement
WebElement selectedElement = driver.findElement(By.id("u_0_2"));
// Perform eleSelected operation
String tSelect = eleSelected.isSelected();
System.out.println(tSelect );
// ============ submit() ==========
// Create WebElement
WebElement submitElement = driver.findElement(By.id("u_0_15"));
// Perform submit operation
submitElement .submit();
// =========== getText() ===============
// Create WebElement
WebElement textElement = driver.findElement(By.id("u_0_2"));
// Perform getText operation
textElement.getText();
//=========== getTagName() ==============
// Create WebElement
WebElement tagnameElement = driver.findElement(By.id("u_0_2"));
// Perform getTagName operation
String tName = tagnameElement.getTagName();
System.out.println(tName);
//========== getAttribute() ===============
// Create WebElement
WebElement attributeElement = driver.findElement(By.id("u_0_2"));
// Perform getgetAttribute operation
String attri = attributeElement.getAttribute("Attribute");
System.out.println(attri);
// ============ cssSelector() =================
// Locating textBox element using CSS Selector
WebElement signButton = driver.findElement(By.cssSelector("#u_0_15 "));
System.out.println(signButton);
// =============== getSize() ===================
WebElement dimensionsElement = driver.findElement(By.id("u_0_15"));
Dimension dimensions = dimensionsElement.getSize();
System.out.println("Height :" + dimensions.height + "Width : " + dimensions.width);
// ======= getLocation() ========================
WebElement locationElement = driver.findElement(By.id("u_0_15"));
Point point = locationElement.getLocation();
System.out.println("X coordinate : " + point.x + "Y coordinate: " + point.y);
        }
}

In automation, there are cases where you want to get the values of the attribute and then perform any action

The attribute value will be null for the tag that does not exist.

To add extra meaning to a web element, a web developer defines the attribute. Attributes have additional information about an HTML element and come in name="value" pairs. Attributes are defined by HTML. We retrieve the values of attributes of web elements to verify test cases. Let us consider a movie ticket booking application. Where available seat colour will be green and booked seat colour will be red, so, to check whether a seat is available or already booked, we need to fetch attribute (colour) value through the script and based on the value we need to perform operations. To achieve this Selenium WebDriver provides a method called getAttribute() which is declared in WebElement interface. It returns the value of the web element a String of the Web element. getAttribute() method returns either “true” or null for attributes whose value is Boolean. getAttribute() method work on specific web elements, so first you need to locate a web element and then call getAttribute() method on it by specifying the attribute for which value you require.

To get the attribute value using selenium WebDriver, we can use

'element.getAttribute(attributeName)'.
WebElement searchTextBox= driver.findElement(By.id("lst-ib"));
// retrieving html attribute value using getAttribute() method
String titleValue=searchTextBox.getAttribute("title");

The tagName property returns the tag name of the element.

A tagName is a part of a DOM structure. Every element on a page is defined with a tag like input tag, button tag, anchor tag, etc. Each tag can have multiple attributes like ID, name, value class, etc. In the case of the tagName locator in Selenium, we are simply using the tag name to identify an element.

See below example of DOM structure of the login page where tag names are highlighted

-
Email Field: < input type="email" name="email" value="" placeholder="Email" required="required" autofocus="autofocus" class="form-control md-5 form-control-lg">
Password Field: < input type="password" name="password" placeholder="Password" class="form-control md-5 form-control-lg" >
Login Button: < button type="submit" class="btn btn-primary btn-lg btn-block md-5">LOGIN< /button >
Forgot Password Link: < button type="submit" class="btn btn-primary btn-lg btn-block md-5">LOGIN< /button >

When to use this tagName locator in Selenium? where you do not have attribute values like ID, class, or name and you want to locate an element, you can use the tagName locator in Selenium. For example, to retrieve data from a table, you can use < td > tag or < tr > tag.

Similarly, in a scenario where you wish to verify the number of links on a page and verify whether links are working or not, you can locate all such links through the anchor tag.

The command to identify an element via tagName is:

driver.findElement(By.tagName("input"));

This method is not reliable while locating individual elements and the page might have multiple instances of these elements.

Alerts is a popup box that takes focus away from the current browser and forces you to read the alert message. Some activities such as accepting or dismissing the alert box need to be performed to resume your task on the browser.

To handle alerts popup window, we need to call the Selenium WebDriver Alert API method to switch to the alert window. Window-based and Web/Browser based alert are the two types of alert.

To handle Browser-based Alerts (alert popup), we use Alert Interface which provides some methods to handle the popup.

During the execution of the WebDriver script, the driver control will be on the browser. Even when after the alert generated control will be on a browser that means the driver control will be behind the alert pop up. In order to move the control to alert pop up, use the command –

driver.switchTo().alert();

After switching control from browser to alert window, we can use the Alert Interface methods to perform actions such as accepting the alert, dismissing the alert, get the text from the alert window, writing some text on the alert window, etc.,

Syntax send the value to the alert is -

alert.sendkeys(String tringToSend);

A small message/dialogue box that appears on the top of a web application and it provides some information/warning to the user is an alert. It may expect input information from the user.

In web applications, there are three kinds of popup boxes,

  • Alert box
  • Confirm box
  • Prompt box

Alert box-

To display some information to the user an alert box is used. The user needs to click the OK button displayed on an alert box that shows up to proceed.

Alert box

Confirm box -

When an application wants to get confirmation from the user, a confirmation box is used. The user needs to click on either Yes/Ok button or No/Cancel button on the confirm box to proceed further.

Confirm box

Prompt box -

When an application expects input from the user immediately after opening it, a prompt box is used. After Entering the input, the user needs to click on either Yes/Ok button or No/Cancel button when the confirm box shows up to proceed further.

Prompt box

You can’t inspect any button or textbox present on any popup boxes using the Developer tool. Pop-up boxes block screen and appear on top of the screen. Users cannot perform any action on screen unless you handle these pop-ups.

User can perform below operations on these pop-ups using selenium are,

  1. Accept - to click on the OK button
  2. Dismiss - to click on Cancel button
  3. SendKeys - to enter text in the text box available on the pop-up
  4. GetText - to get label text on pop-up

Below is the code to handle alert pop-up. Please note element and test site is a dummy –

String alertBoxOutput, popupText; 
    System.setProperty("webdriver.chrome.driver", "./Resources/chromedriver.exe"); 
WebDriver driver = new ChromeDriver(); 
driver.manage().window().maximize(); 
    driver.manage().timeouts().setScriptTimeout(20, TimeUnit.SECONDS); 
    driver.get("https://www.testtutorial.com"); 
Thread.sleep(2000); 
  // Alert popups 
    driver.findElement(By.id("alertBox")).click(); 
popupText = driver.switchTo().alert().getText(); 
Assert.assertEquals(popupText, "I am an alert box!"); 
driver.switchTo().alert().accept(); 
alertBoxOutput = driver.findElement(By.id("output")).getText(); 
Assert.assertEquals(alertBoxOutput, "You selected alert box"); 
// Confirmation popups 
    driver.findElement(By.id("confirmBox")).click(); 
popupText = driver.switchTo().alert().getText(); 
    Assert.assertEquals(popupText, "Press a button!"); 
driver.switchTo().alert().accept(); 
alertBoxOutput = driver.findElement(By.id("output")).getText(); 
Assert.assertEquals(alertBoxOutput, "You pressed OK in confirm box"); 
driver.findElement(By.id("confirmBox")).click(); 
driver.switchTo().alert().dismiss(); 
alertBoxOutput = driver.findElement(By.id("output")).getText(); 
Assert.assertEquals(alertBoxOutput, "You pressed Cancel in confirm box"); 
// Prompt pop ups 
    driver.findElement(By.id("promptBox")).click(); 
popupText = driver.switchTo().alert().getText(); 
Assert.assertEquals(popupText, "Please enter your name:"); 
    driver.switchTo().alert().sendKeys("Reddy"); 
driver.switchTo().alert().accept(); 
alertBoxOutput = driver.findElement(By.id("output")).getText(); 
Assert.assertEquals(alertBoxOutput, "You entered text Reddy in prompt box"); 
    driver.findElement(By.id("promptBox")).click(); 
driver.switchTo().alert().dismiss(); 
alertBoxOutput = driver.findElement(By.id("output")).getText(); 
Assert.assertEquals(alertBoxOutput, "You pressed Cancel in prompt box."); 
   driver.quit();

Sometimes while automating, we come across scenarios where we need to handle pop-ups generated by windows like a print pop up or a browser window while uploading a file.

We know selenium is an automation testing tool that supports only web application testing. Selenium does not support windows based applications and automating window alert is not possible. Handling these pop-ups in selenium is always a bit tricky. However, along with third-party intervention, selenium can handle windows alert.

Many third-party tools are available for handling window-based pop-ups along with the selenium.

Let’s handle a window-based pop-up using the Robot class.

Robot class is a java based utility to emulate the keyboard and mouse actions.

We will automate one scenario

  1. Launch the browser and open “gmail.com”
  2. Enter username and password and click on the Sign button(Note script has dummy username and password)
  3. Click on the compose icon
  4. Click on the attachment icon and select the file to upload with the window based pop up.

WebDriver Code using Robot Class

Step 1: Create a new java class named as “DemoAlertWindow” under project “Learning_Selenium”
Step 2: Import package import java.awt.Robot prior to the script creation.
Step 3: Import package java.awt.event.KeyEvent allows the user to use keyPress and keyRelease events of a keyboard.

Below is the test script that is equivalent to the above-mentioned scenario.

  import java.awt.Robot;
    import java.awt.event.KeyEvent;
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    public class DemoAlertWindow {
    WebDriver driver;
    @Before
    public void setUp()
    {
    driver=new FirefoxDriver();
    driver.get("https://gmail.com");
    driver.manage().window().maximize();
    }
    @Test
    public void testAlertWindow() throws Exception{
    // enter a valid email address
    driver.findElement(By.id("Email")).sendKeys("TestSelenium1@gmail.com");
    // enter a valid password
    driver.findElement(By.id("Passwd")).sendKeys("TestSelenium");
    // click on the sign in button
    driver.findElement(By.id("signIn")).click();
    Thread.sleep(30000);
    // click on compose button
    driver.findElement(By.xpath("//div[@class='z0']//div[contains(text(),'COMPOSE')]")).click();
    // click on attach files icon
    driver.findElement(By.xpath("//div[contains(@command,'Files')]//div[contains(@class,'aaA')]")).click();
    // creating instance of Robot class (A java based utility)
    Robot rb =new Robot();
    // pressing keys with the help of keyPress and keyRelease events
    rb.keyPress(KeyEvent.VK_D);
    rb.keyRelease(KeyEvent.VK_D);
    Thread.sleep(2000);
    rb.keyPress(KeyEvent.VK_SHIFT);
    rb.keyPress(KeyEvent.VK_SEMICOLON);
    rb.keyRelease(KeyEvent.VK_SEMICOLON);
    rb.keyRelease(KeyEvent.VK_SHIFT);
    rb.keyPress(KeyEvent.VK_BACK_SLASH);
    rb.keyRelease(KeyEvent.VK_BACK_SLASH);
    Thread.sleep(2000);
    rb.keyPress(KeyEvent.VK_P);
    rb.keyRelease(KeyEvent.VK_P);
    rb.keyPress(KeyEvent.VK_I);
    rb.keyRelease(KeyEvent.VK_I);
    rb.keyPress(KeyEvent.VK_C);
    rb.keyRelease(KeyEvent.VK_C);
    Thread.sleep(2000);
    rb.keyPress(KeyEvent.VK_ENTER);
    rb.keyRelease(KeyEvent.VK_ENTER);
    Thread.sleep(2000);
    }
    @After
    public void tearDown()
    {
    driver.quit();
    }
    }

In many scenarios, when you open a web site, the application displays multiple windows. Those can be advertisements or a kind of information showing on popup windows. Using Windows Handlers in selenium WebDriver, we can handle multiple.

Step 1: Open website and get the main window handle by using driver.getWindowHandle();
Step 2: Now get all the window handles by using driver.getWindowHandles();
Step 3: Compare all the window handles with the main Window handles and perform the operation on the window which you need.

In the below example we will see how to handle multiple windows and close all the child windows which are not required. Compare the main window handle to all the other window handles and close them.

package multialert;
import java.util.Set;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
 public class WindowExamples {
         static WebDriver driver;
         public void test_CloseWindowsExceptMainWindow() {
                     driver = new FirefoxDriver();
                     // Opens Naukri.com website with multiple windows
                        driver.get("http://www.naukri.com/"); 
                  // get main window handle
                     String winTitle= getCurrentWindowTitle();
                     String mainWindow = getMainWindowHandle(driver);
                        Assert.assertTrue(closeAllOtherWindows(mainWindow));
                     Assert.assertTrue(winTitle.contains("Jobs - Recruitment-Job Search"), "window title is not matching");
         }
                        public String getMainWindowHandle(WebDriver driver) {
                     return driver.getWindowHandle();
         }
         public String getCurrentWindowTitle() {
                     String winTitle = driver.getTitle();
                     return winTitle;
         }
         //Except main window, close all windows.
         public static boolean closeAllOtherWindows(String openWindowHandle) {
                     Set<String> allWindowHandles = driver.getWinHandle();
                     for (String currentWindowHandle : allWindowHandles) {
                                 if (!currentWindowHandle.equals(openWindowHandle)) {
                                                driver.switchTo().window(currentWindowHandle);
                                                driver.close();
                                }
                     }
                        driver.switchTo().window(openWindowHandle);
                     if (driver.getWindowHandles().size() == 1)
                                 return true;
                     else
                                 return false;
         }
}

The below image shows you the multiple windows that open in the application. Three windows are open. One is the main window and the other two are child windows.

How To Handle Multiple Popup Windows In Selenium?

isSelected() is the method used to verify whether the desired web element is selected or not. The best way to do this is to use the selenium methods isSelected(): This method determines if an element is selected or not. isSelected() returns true if the element is selected and false if it is not. It is widely used on checkboxes, radio buttons, and options in a select.

While running selenium tests, visibility conditions are necessary, because they enable you to check if an element is selected/displayed/enabled. For example, a tester may want to check if a checkbox or radio button has been selected by default before it is click. To ensure whether the checkbox or radio button is selected or not use method isSelected() as below.

Boolean isSelected() method determines whether an element is selected or not. It returns the Boolean value true if the element is selected and false if it is not. isSelected() method is predominantly used with radio buttons, dropdowns, and checkbox.

Ex. on flight booking sight you can select radio button one way or round trip

tripRadioBtn = driver.findElement (By.id("oneway"));
tripRadioBtn.isSelected();

This will return a Boolean value, if it returns true then said radio button is selected, or it returns False

The frame is a web page embedded in another web page or an HTML document embedded in another HTML document. The IFrame is frequently used to insert content from another source into a Web page. The <iframe> tag specifies an inline frame.

The frame inside the frame is called nested frames. To switch from one frame to another first switch to the outer frame by either index or ID of the iframe. Once you switch to the outer frame you can find the total number of iframes inside the outer frame and then switch to the inner frame by any known method. You should follow the same order as we entered into the frame while exiting that is an exit from the inner frame first and then the outer frame.

Syntax to switch frame are:

driver.switchTo().frame(0); (switch to outer frame)
size = driver.findElements(By.tagName("iframe")).size(); (get iframe count)
driver.switchTo().frame(0); (Switching to inner frame)
driver.switchTo().parentFrame(); (to move back to parent frame)
driver.switchTo().defaultContent(); (to move back to main frame, mostly parent frame)

Code to switch to a frame whose ID or web element is not available using index-

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class IndexOfIframe {
public static void main(String[] args) {
    WebDriver driver = new FirefoxDriver();
    driver.get("https://seleniumautomationpractice.blogspot.com/");  
    driver.manage().window().maximize();
    //driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
    int size = driver.findElements(By.tagName("iframe")).size();
    for(int i=0; i<=size; i++){
driver.switchTo().frame(i);
int total=driver.findElements(By.tagName("iframe")).size();
System.out.println(total);
    driver.switchTo().defaultContent();
}
}
}

An HTML document embedded inside the current HTML document on a web page is iFrame. An iFrame is used to insert content from another source into a web page like an advertisement. A single web page can have multiple iFrames and iFrame can have an inner frame as well. The frame inside the frame is called nested frames. Content of iFrame can be changed without reloading the complete website. The IFrame is frequently used to insert content from another source into a Web page. The <iframe> tag specifies an inline frame.

Syntax to get number of frames on a page is -

size = driver.findElements(By.tagName("iframe")).size();
System.out.println("Total Frames --" + size); //print the number of frames on page

Here is the code to find the number of frames in a web page -

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class CountFrame {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("https://seleniumautomationpractice.blogspot.com/2019/07/example-of-html-iframe-alternative.html");
System.out.print("No. of Frames:");
System.out.println(driver.findElements(By.tagName("frame")).size());
System.out.print(“No. of inline Frames:”);
System.out.println(d.findElements(By.tagName("iframe")).size());
}
}

There are two types of HTML tables on the web that are Static table and Dynamic table.

In the Static table, data is static ie.rows and columns are fixed whereas in Dynamic table rows and columns are not fixed.

Follow the steps below to get cell data –

  1. First, locate the table.
  2. Determine the total number of rows and columns
  3. From the rows and columns count locate required cells and perform ‘getText()’ method of the WebDriver element on the element.

Syntax -

  1. WebElement baseTable = driver.findElement(By.tagName("table"));
  2. List  col = driver.findElements(By.xpath(“XPath"]/table/thead/tr/th"));
    System.out.println("No of cols are : " +col.size());
  3. List  rows = driver.findElements(By.xpath("Xpath”]/table/tbody/tr/td[1]"));
    System.out.println("No of rows are : " + rows.size());
  4. To find 3rd row
    WebElement tableRow = baseTable.findElement(By.xpath("XPath"]/table/tbody/tr[3]"));
    String rowtext = tableRow.getText();
  5. To get 3rd row's 2nd column data
    WebElement cellIneed = tableRow.findElement(By.xpath("XPath"]/table/tbody/tr[3]/td[2]"));
    String valueIneed = cellIneed.getText();
    System.out.println("Cell value is : " + valueIneed);

There are 8 types of locators in selenium WebDriver.It is very important to decide on which locator we should use.

To find web element Selenium web driver takes time which is more or less based on the type of locator we used. Test execution time will increase if we use a slow locator. So the selection of locator should be correct and faster. When we use the ID locator, Page Object Model (POM) has advantages. ID helps in identifying web element uniquely.

If a unique ID is not available, we use Name locator. Name locator may not identify a web element uniquely as a web page as multiple web elements can share the same name. We can request developers to provide a unique ID or Name for every web element to faster test script execution.

To locate a list of web elements, we use ClassName or TagName. LinkText or PartialLinkText can be used for anchor tag web elements.

When web element has not Unique ID and name, CSS Selector is the best option to use. It is faster and also it improves the performance, and very compatible across browsers. CSS engine is consistent in all browsers and very useful to test applications on multiple browsers. XPath does not work in IE always so CSS is best for IE.

XPath is the last option to use as a locator as it is slower among all locators, but it provides reliable ways to locate web elements. XPath engines are different in each browser and make them inconsistent across browsers.

Using the radio button we will be able to select only one option from the options available and radio buttons can be toggled only by Click() method.

We should ensure the following things before performing the click event on the Radio buttons -

  • A radio button is displayed on the webpage
  • Radio is enabled on the webpage
  • Check the default selection of the Radio button

By using the Click() method in Selenium you can perform the action on the Radio button.

For ex.

tripRadioBtn = driver.findElement (By.id("oneway"));
tripRadioBtn.click();

Selenium WebDriver has predefined methods -

isDisplayed() method -

WebElement tripRadioBtn = driver.findElement (By.id("id value"));

It will check tripRadioBtn.is displayed. This will returns a Boolean value, if it returns true then said radio button is present on the webpage, or it returns False.

isEnabled() method -

tripRadioBtn.isEnabled()

This will returns a Boolean value, if it returns true then said radio button is enabled on the webpage, or it returns False

isSelected() method -

tripRadioBtn.isSelected()

This will returns a Boolean value, if it returns true then said radio button is selected, or it returns False

During test case creation, you need to frequently verify that the targeted element is enabled or disabled on the web page before performing any action on it. Selenium WebDriver has built-in isEnabled() method to check whether the status of the element is enabled on the web page.

If the specified element is enabled, isEnabled() method of WebDriver will verify and return true, else will return false.

Syntax is  - 

driver.findElement(By.xpath(“//input[@name=’fname’]“)).isEnabled();

To take some action based on the element’s enabled status, we can use the isEnabled() method with if condition to take action.

//verify name text box is enabled or not
If (FirstName.isEnabled ()) 
{
System.out.print ( “Text box First name is enabled. ”);
}
Else
{
System.out.print ( “Text box First name is disabled. ”)
}

Using the radio button we are able to select only one option from the options available and radio buttons can be toggled only by Click() method.

We should ensure the following things before performing any action on the Radio buttons -

  • A radio button is displayed on the webpage
  • Radio is enabled on the webpage
  • Check the default selection of the Radio button

Using a predefined isEnabled() method in Selenium WebDriver you can confirm whether the radio button is enabled or not.

For ex. While automating radio buttons on flight reservation site you can check -

tripRadioBtn = driver.findElement (By.locator("locator value"));

tripRadioBtn.isEnabled()

This will returns a Boolean value, if the output is  true then said radio button is enabled on the webpage, or output is False.

A Framework is nothing but a simple structure that becomes a framework when we blend various guidelines, concepts, coding standards, processes, practices, project hierarchies, modularity, reporting mechanism, test data injections, etc. This blend has to be in the right way and in the right techniques which makes it stand as a great Structure. And in layman terms we can say a framework is a set of rules or guidelines which should be followed while scripting to achieve the desired benefits like reduced maintenance cost, re-usability of code, increase code coverage with minimal coding.

Below are some examples of Automation frameworks:

  1. Data-Driven Testing Framework - It drives the data through the script to the application which is under test.
  2. Keyword Driven Testing Framework - It drives the data using special Keywords available inside the third party files (Excell, CSV, XML, Database files) to fetch and transmit the data to the application under test.
    It drives the data through the use of special Keywords to the script and then to the application under test.
  3. Hybrid Testing Framework - A framework which combines the benefits of two or more framework and designing techniques to make the testing process smooth and powerful.
  4. Behaviour Driven Framework - Test cases written in the plain English language to execute the test on the basis of the expected behaviour of the application and ease to the stakeholders (non-technical) to understand and contribute.
  5. Test-Driven Framework - An Approach in which we create a test to make it fail, then we work on the application to make it pass and then we refactor to improve the quality of the application.

Advanced

Download and install AutoIt tool

Open SciTE Script editor and add the below mentioned AutoIt script. Save it as ‘UploadFile.au3’ in your system.

Below is the AutoIt Script:

WinWaitActive("File Upload") 
Send("D:\SoftwareTestingMaterial\UploadFile.txt") 
Send("{ENTER}")

Save it as ‘UploadFile.au3’

After the file is saved, we need to convert the ‘UploadFile.au3’ to ‘UploadFile.exe’. (This can be done by Right Click on the file and click on ‘Compile Script’) .

Add the below mentioned Selenium Script and run :

Runtime.getRuntime().exec(“D:\\Automation\\AutoIt\\Uploadfile.exe”);

The main difference between driver.findElement() and driver.findElements() commands is-

findElement() returns a single WebElement (which is found first) based on the locator passed as the parameter. Whereas findElements() returns a list of WebElements satisfying the locator value passed. It returns an empty list if there are no web elements matching the locator strategy.

Syntax of findElement()-

WebElement textbox = driver.findElement(By.id(“textBoxLocator”));

Syntax of findElements()-

List <WebElement> elements = element.findElements(By.id(“value”));

Another difference between the two is- if no element is found then findElement() throws NoSuchElementException whereas findElements() returns a list of 0 elements.

Page Object Model is an Object Repository design Pattern most commonly used in Selenium Test Automation. It is primarily used for enhancing test maintenance and reducing code duplication. Page object model (POM) can be used in any type of framework - modular, data-driven, hybrid framework or keyword driven, etc. A page object is an object-oriented class that serves as an interface to a page of the Application Under Test (AUT). The tests use these methods of page object class whenever they need to interact with the UI of that page. The benefit is that if there is a UI change, there is no need to change the tests only the code within the page object needs to be changed.

Page Factory in Selenium is an extension to Page Object Model and can be used to initialize web elements that are defined in web page classes or Page Objects.

Page Factory will initialize every WebElement variable with a reference to a corresponding element on the actual web page based on configured “locators”. This is done by using the annotation @FindBy. Example

@FindBy(xpath = "(//option[text()='Basic auth'])[2]")
private WebElement typeSctApp;

Desired Capabilities is used to set properties for the WebDriver. It is also used to set the properties of browser such as to set BrowserName, Platform, Version of Browser etc.

Example :

String baseUrl , nodeUrl;
baseUrl = "https:// https://www.seleniumhq.org/ ";
nodeUrl = "http://192.168.10.21:5568/wd/hub";
DesiredCapabilities dcap = DesiredCapabilities.firefox();
dcap.setBrowserName("firefox");
dcap.setPlatform(Platform.WIN10_1);
driver = new RemoteWebDriver(new URL(nodeUrl),dcap);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

Selenese is the set of selenium commands which are used for running the tests

There are three types of Selenese :

Actions: These are commands used for performing the operations and interactions with the target elements and manipulate the state of the application

Assertions: These commands are used as checkpoints or verification points. It verifies the state of the application conforms to the expected state.

Accessors: These commands examine the state of the application and store the values in a variables

WebDriver is an Interface which is implemented by FirefoxDriver Class.

FirefoxDriver is a class. It implements all the methods of WebDriver interface.

It makes the reference of the interface and makes an object of FirefoxDriver so as to call implemented methods of WebDriver interface.

We declare the left-hand side variable as WebDriver and right-hand side to create an object for a particular type of the browser and assign it.

WebDriver driver = new FirefoxDriver();

When we use ALT/SHIFT/CONTROL keys, we hold onto those keys and click other buttons simultaneously to achieve the special functionality.

To  hold onto these keys while subsequent keys are pressed, you need to define two more methods: keyDown(modifier_key) and keyUp(modifier_key)


Parameters: Modifier_key (keys.ALT or Keys.SHIFT or Keys.CONTROL)

The purpose is to Perform a modifier key press and not to release the modifier key. Subsequent interactions may assume it’s kept pressed.

Parameters: Modifier_key (keys.ALT or Keys.SHIFT or Keys.CONTROL)

The purpose is to Perform a key release.

With a combination of these two methods, you can capture the special function of a particular key.

public static void main(String[] args) 
{
String baseUrl = “https://www.facebook.com”;
WebDriver driver = new FirefoxDriver();
driver.get("baseUrl");
WebElement txtUserName = driver.findElement(By.id(“Email”);
Actions builder = new Actions(driver);
Action seriesOfActions = builder
 .moveToElement(txtUserName)
 .click()
 .keyDown(txtUserName, Keys.SHIFT)
 .sendKeys(txtUserName, “hello”)
 .keyUp(txtUserName, Keys.SHIFT)
 .doubleClick(txtUserName);
 .contextClick();
 .build();
seriesOfActions.perform();
}

Selenium IDE has a built-in linguistic system called Selenese. Selenese is a collection of Selenium commands to perform actions on a web page.

Selenese can detect broken links on a page, or check the presence of a web element, Ajax, JavaScript alerts and a lot of many things. Mainly there are three types of command in Selenese. 

  1. Actions: Action can alter the state of an application. For example,  clicking on a link, or selecting a value from the drop-down, etc. Action command directly interacts with web elements.
  2. Accessors: Accessor commands monitor the state of an application and store it into some variable. For example, storeTextPresent, or storeElementPresent, etc are accessor commands because it reads the state of the element and stores it into a variable.
  3. Assertions: Assertion commands allow adding a checkpoint or a verification point. The assertion command confirms the current state of a UI element. These commands verify if a certain condition is met.

Selenium RC has a complex architecture whereas in WebDriver those complications are removed. Let us discuss why WebDriver is better than the RC.

Selenium RC is slow because it uses an additional JavaScript layer known as the core and On the other hand, WebDriver is fast as it natively speaks with the browser by utilizing browser's built-in engine to control it.

The selenium core can not ignore the disabled elements while WebDriver handles the page elements more realistic way.

Selenium RC has a more mature set of APIs but contains redundancies and complicated and confusing commands. Whereas, WebDriver APIs his simpler and have a cleaner interface and do not have redundant and confusing commands.

Selenium RC does not provide support headless HtmlUnit browser. It requires a real visible browser to operate on. On the other hand, WebDriver supports a headless HtmlUnit browser.

Selenium RC includes a test result generator for HTML reports. Whereas WebDriver does not have any built-in reporting ability.

For Mozilla’s Gecko engine, Marionette is the web automation driver. In the test automation developers community, it is also famous as the Gecko Driver. Marionette can control both the chrome i.e. menus and functions or the content of the webpage loaded inside the browsing context, giving a high level of control and ability to replicate user actions. Along with performing actions on the browser, Marionette can also read the properties and attributes of the DOM

It has two modules, first is the server that accepts requests and executes and the second is a client to forward commands to the server.

We use Marionette to run UI tests in the Firefox browser. A test automation engineer should add its dependencies to the framework, import the required classes and call the methods to control the web page. Marionette also returns the status of commands executed on the browser which can help in verifying the authenticity of the actions performed.

Selenium is a set of different software tools each with a different purpose and approach to supporting test automation. Selenium Webdriver is one of the tools in Selenium. The Java version of WebDriver supports the implementation of the Selenium-RC API. WebDriverBackedSelenium is a class name where you can create an object for it. The WebDriver API is fundamentally different in its design compared to Selenium RC. You can use the WebDriver technology using the Selenium-RC API. This is mainly provided for backward compatibility. WebDriverBacked selenium allows you to use existing Selenium RC test suites using Selenium-RC API to use WebDriver under the cover. WebDriverBackedSelenium allows you to create tests with Selenium Remote Control syntax.

String baseUrl = "https://nikasio.com";
String remoteControl = "localhost";
Int port = 4444;
String browser = "*firefox";
Selenium selenium = new DefaultSelenium(remoteControl, port ,browser ,baseUrl);
selenium.start();
selenium.open("/");
selenium.click("link=chapter1");
// rest of the test code
WebDriverBackedSelenium is a class name where we can create an object for it as below: Selenium WebDriver= new WebDriverBackedSelenium(WebDriver objectName, “URL path of website”).

The main purpose of this is when we want to write code using both WebDriver and selenium RC, we must use the above-created object to use selenium commands.

Many times test cases fail while running automated test scripts due to various reasons. It might be due to multiple reasons like network issues, system issues or browser issues, etc. and we need to execute test scripts again using TestNG in Selenium. We can test these failed cases in two ways:

Case 1: Execute failed test cases using TestNG in Selenium – By using “testng-failed.xml”

  1. After the first run of the automated test run, right-click on Project - Click on Refresh
  2. A folder will be generated named the “test-output” folder, and inside “test-output” folder, you can find “testng-failed.xml”
  3. Run “testng-failed.xml” to execute the failed test cases again.

Case 2: Execute failed test cases using TestNG in Selenium – By Implementing TestNG IRetryAnalyzer.

Create a class to implement IRetryAnalyzer. Here we create a class (say, RetryFailedTestCases) and implementing IRetryAnalyzer.

package runfailedtestcases;
import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;
public class RetryFailedTestCases implements IRetryAnalyzer {
    private int retryCnt = 0;
    //You can ntioned maxRetryCnt (Maximiun Retry Count) as per your requirement. Here if any failed testcases then it runs two times
    private int maxRetryCnt = 2;
    //This method will be called every time a test fails and will return TRUE if a test fails and need to be retried, else it returns FALSE
    public boolean retry(ITestResult result) {
        if (retryCnt < maxRetryCnt) {
            System.out.println("Retrying " + result.getName() + " again and the count is " + (retryCnt+1));
            retryCnt++;
            return true;
        }
        return false;
    }
}

A simple implementation of this ‘IAnnotationTransformer’ interface can help you set the ‘setRetryAnalyzer’ for ‘ITestAnnotation’. Add the above class name (RetryFailedTestCases.class) in the below program.

package runfailedtestcases;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import org.testng.IAnnotationTransformer;
import org.testng.IRetryAnalyzer;
import org.testng.annotations.ITestAnnotation;
public class RetryListenerClass implements IAnnotationTransformer {
 @Override
 public void transform(ITestAnnotation testannotation, Class testClass, Constructor testConstructor, Method testMethod) {
 IRetryAnalyzer retry = testannotation.getRetryAnalyzer();
  if (retry == null) {
 testannotation.setRetryAnalyzer(RetryFailedTestCases.class);
 }
 }

We can evaluate the above example by simple tests

Testcase 1:

package runfailedtestcases;
import org.testng.Assert;
import org.testng.annotations.Test;
public class Test1 {
 @Test
 public void test1(){
 System.out.println("Test 1");
 Assert.assertTrue(true);
 }
}

Testcase 2:

package runfailedtestcases;
import org.testng.Assert;
import org.testng.annotations.Test;
public class Test2 {
 @Test
 public void test2(){
 System.out.println("Test 2");
 Assert.assertTrue(false);
 }
}

As per the lines of code in Test2, it will fail. So Test2 will be executed 2 times as we took the maxRetryCnt as 2 in Retry Class. First, let us add the below-mentioned Listener to the testng.xml file. The below-mentioned syntax is to add Listener for RetryListnereClass subtext

<listeners>
        <listener class-name="softwareTestingMaterial.RetryListenerClass"/>
</listeners>
Final testng.xml will look like below:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="My Suite">
<listeners>
        <listener class-name="softwareTestingMaterial.RetryListenerClass"/>
  </listeners>
 <test name="Test1">
 <classes>
 <class name="runfailedtestcases.Test1" />
 </classes>
 </test> <!-- Test -->
 <test name="Test2">
 <classes>
 <class name="runfailedtestcases.Test2" />
 </classes>
 </test> <!-- Test -->
</suite> <!-- Suite -->

After executing testng.xml the output shows Test 2 is executed 3 times as we have mentioned “maxRetryCnt=2”. Even though we have just 2 test cases, we could see total test runs are 4 in the result.

This way we could run failed test cases by applying TestNG in Selenium.

Tooltip is also known as the Help text. Tooltip is a text that appears when the mouse hovers over an object like a link, button image, etc. Sometimes it is a requirement of a project to address tooltip text. This tooltip text provides information related to the object on which it appears. Objects have various attributes in HTML i.e object properties and one of them is “title” which contains tooltip text. Selenium’s purpose is to read that text of the object. Tooltip can appear in two ways - Static and Dynamic ways and both can be seen by mouse hover on the object. 

1. Static Tooltip:

Selenium can capture the Static tooltip by calling getAttribute(‘title’) method of WebElement and return the text of title which will be nothing but the tooltip.

We will use the class attribute to make the image object(WebElement) and then we will use the getAttribute method.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class staticToolTip {
public static void main(String[] args) {
                //Keep the chromedriver at project path location in eclipse
System.setProperty("WebDriver.chrome.driver",
 System.getProperty("user.dir")+"/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://nikasio.com");
//Find the element by using class name
WebElement edit = driver.findElement(By.id("header_logo"));
String tooltiptext = edit.getAttribute("title");
System.out.println(tooltiptext);
driver.quit();
}
}

2. Dynamic Tooltip:

Dynamic Tooltip in applications is usually created by jQuery/javascript plugins. When we hover mouse on the object, tooltip object appears in HTML and as soon as the mouse is moved the object text disappears. The getAttribute(‘title’) will not simply work. In the case of Dynamic tooltip, you have to use the ‘Actions class’ of Selenium.

In the above image, you will see object related to this link but nothing for tooltip

Once you will hover the mouse over the link, you will see a new entry in HTML(div tag ) which is nothing but the tooltip and when you will take the mouse out of the link, below div tag will disappear.

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
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 dynamictoolTip {
public static void main(String[] args) throws InterruptedException {
System.setProperty("WebDriver.chrome.driver", System.getProperty("user.dir")+"/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://nikasio.com");
//Create object for link
WebElement exampleLink = driver.findElement(By.class("avatar user-1-avatar avatar-150 photo"));
//Create the Actions class
Actions act = new Actions(driver);
act.moveToElement(exampleLink).build().perform();
//Create object for inner div 
WebElement toolTip = driver.findElement(By.className("header_logo"));
//Get the text of Tooltip
String tooltiptext = toolTip.getText();
System.out.println(tooltiptext);
driver.quit();
}
}

Selenium Webdriver is itself an API and it’s an interface. Selenium hierarchy contains two WebDriver - Remote WebDriver and Selenium WebDriver. Remote WebDriver is a class that implements the Selenium WebDriver interface whereas browser classes ChromeDriver(), FirefoxDriver() and etc. extend the Remote WebDriver. There are many APIs and methods in Selenium and following are some as follows:

 1. Navigation Commands:

a. navigate().to(URL) - To navigate to a particular URL.
Driver.navigate().to("http://qatechhub.com");

b. get(url) - To navigate to a particular URL.
Driver.get("http://qatechhub.com");

c. navigate().back() - Takes us  back in the browsing
Driver.navigate().back();

d. navigate().forward() - Takes us  forward in the browsing
Driver.navigate().forward();

e. navigate().refresh() - reloads the page.

Driver.navigate().refresh();

2. Resizing Windows:

Set Size - Sets the size of the window; the unit used is pixels.

Dimension d = new Dimension(640, 640);

Driver.manage().window().setSize(d);

Maximizing the Window - To set the size of the window as full size as per the screen.

Driver.manage().window().maximize();

3. Deleting Cookies:

Delete cookies in Selenium as per below command

Driver.manage().deleteAllCookies();

This will not actually delete the physical cookies file from the browser but it bypasses these files that cookies don’t appear in the browsing experience.

4. Closing the Browser:

close() - Closes the active window

Driver.close();

quit() - closes all the windows which are open in Selenium sessions

Driver.quit();

5. Different Get Methods:

a. getCurrentUrl() - This method returns the URL of the currently active page as a string.

b. getPageSource() - This method returns the complete HTML source code of the page.

c. getTitle() - This method returns the title of the current page as a string.

d. getWindowHandle() - This method returns the sessionId or windowHandle as a string.

e. getWindowHandles() - This method returns a set of sessionIds or windowHandles.

6. Searching WebElements: 

To search for web elements on a web page below methods are used:

a. findElement() - This method returns a web element that matches the required criteria.

    WebElement element = Driver.findElement(By.id("text-box"));

b. findElements() - This method returns a list of web elements which match the criteria.

    List<WebElement> allElements = Driver.findElements(By.id("checkbox"));

7. Mouse Operations:

Mouse operations like mouse hover, drag, and drop, right clickor double click with Selenium actions class is used:

Mouse Hover:      

Actions action = new Actions(Driver);

WebElement mobileElement = Driver.findElement(By.linkText("Mobile & Accessories"));

action.moveToElement(mobileElement).build().perform();

Drag and Drop:

WebElement source = Driver.findElement(By.id("draggable"));

WebElement target = Driver.findElement(By.id("droppable"));

Actions action = new Actions(Driver);

action.dragAndDrop(source, target).build().perform();

We can configure our WebDriver test or WebDriver test suites for software testing project in testng.xml file. If we have two/multiple classes in test suite we can run those classes in the same test as well as two different tests too. Let's take a simple example here:

Create 3 classes under project = TestNGOne and package = TestNGOnePack as below.

BaseClassOne will be used for initializing and closing WebDriver instance,

ClassOne and ClassTwo will be used as test classes.

  1. BaseClassOne.java:
package TestNGOnePack;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
public class BaseClassOne {
    //Declared as public static to use same WebDriver instance publicly
    public static WebDriver driver = null;
    //@BeforeSuite annotation describes this method has to run before all suites
    @BeforeSuite 
    public void setup() throws Exception { 
         System.setProperty("WebDriver.gecko.driver", "D:\geckodriver.exe");
  driver = new FirefoxDriver();
         driver.manage().window().maximize();
         driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
         driver.get("https://nikasio.com"); 
    } 
    //@AfterSuite annotation - this method has to run after execution of all suites
    @AfterSuite
         public void tearDown() throws Exception { 
         driver.quit(); 
    } 
}

The above class will be base class and be used to initialize and close the WebDriver instance. 

  1. ClassOne.java:
package TestNGOnePack;
import org.testng.annotations.Test;
public class ClassOne extends TestNGOnePack.BaseClassOne{
 //@Test annotation - this method as a test method
 @Test
  public void testmethodone() {
    String title = driver.getTitle();
    System.out.print("nCurrent page title is : "+title);
    String Workdir = System.getProperty("user.dir");
    String Classpackname = this.getClass().getName();
    System.out.print("n'"+Workdir+" -> "+Classpackname+" -> testmethodone' has been executed successfully");
  }
}

Above ClassOne is inherited or created from BaseClassOne.

  1. ClassTwo.java:
package TestNGOnePack;
import org.testng.annotations.Test;
public class ClassTwo extends TestNGOnePack.BaseClassOne{
 //@Test annotation describes this method as a test method
 @Test
  public void testmethodone() {
  driver.navigate().to("http://facebook.com");
  String title = driver.getTitle();
  System.out.print("nCurrent page title is : "+title);
  String Workdir = System.getProperty("user.dir");
  String Classpackname = this.getClass().getName();
  System.out.print("n'"+Workdir+" -> "+Classpackname+" -> testmethodone' has been executed successfully");
  }
}

Above ClassTwo is also inherited or created from BaseClassOne.

Configure testng.xml to run two classes in one test:

Copy the below-given lines in the testng.xml file and run it as TestNg Suite.

<suite name="Suite One" >
 <test name="Test One" >
  <classes>
   <class name="TestNGOnePack.ClassOne" />
   <class name="TestNGOnePack.ClassTwo" /> 
  </classes>
 </test> 
</suite>

When execution is completed, View execution results report.

Configure testng.xml to run two classes in two tests:

Now if we want to run both classes as separate test then we have to configure testng.xml file as below

<suite name="Suite One" >
 <test name="Test One" >
  <classes>
   <class name="TestNGOnePack.ClassOne" />  
  </classes>
 </test> 
 <test name="Test Two" >
  <classes>
   <class name="TestNGOnePack.ClassTwo" /> 
  </classes>
 </test> 
</suite>

Here, "ClassOne" is included in 'Test One" test and "ClassTwo" is included in 'Test Two" test. Now run below given testng.xml file and verify results.

After comparing both results. In the 1st example, Both classes are executed under the same test but in the second example, both classes are executed under separate tests. This way you can configure the testng.xml file as per your requirement.

Many web browsers are available and used nowadays. Users may use FireFox, or Safari, or Chrome, or Internet Explorer browser for reading. Users may use different versions of the browsers and also may be running browsers on any OS like Windows, or Mac, or Linux.

To help users get the best user experience, you need to go extra miles and test Web application on different browsers along with different platforms. You need to test applications on different browsers and OS. You need to put extra effort and spend time testing your web application on every possible OS and available browser. Is it possible and feasible? Now how to make it possible? Here Selenium grid fits well and saves your time and energy.

Now the question is, Is it possible to set up selenium grid infrastructure using local systems? It is challenging to maintain Selenium Grid with all required browsers and operating systems and for that multiple online platforms provide an online Selenium Grid which you can access to run selenium scripts.

One of the important tools in the Selenium suite is Selenium Grid. Selenium grid has the capability of co-ordinating WebDriver tests or RC tests that can run on multiple browsers simultaneously. Selenium Grid can initiate WebDriver tests or RC tests on different operating systems. Selenium Grid even initiates or executes tests hosted on different machines. There can be only one hub and it will be a central part of the Selenium Grid setup and nodes are test machines on which tests are run.

Selenium Grid

Let us see how it works? Grid hosts a Hub-Node architecture. Here there is one Hub acts as the master and one or more nodes that act as slaves.

Consider you have 100 tests that are required to be executed simultaneously on different machines in different combinations of operating systems and browsers. Each machine has a different OS and inside OS, the test is executed on different browsers. This will save a lot of your execution time.

We know Selenium Grid is one of the important tools of Selenium suite and it functions on Node and multiple hub basis. Let us see what is Hub and Node? and how it works?

The Hub - There is only one Hub called as master and that is a central point of that grid setup. Any grid setup will have only one Hub. Tests that need to be executed should be loaded on HUB. Tests that are loaded on Hub will be executed only on Nodes. Any host machine can be configured as Hub and will co-ordinated test execution on other hosts.

The Nodes - Nodes are the machines on which tests are run. Hub will launch these tests. The Hub can launch one or more Nodes on remote machines or on the same machine where Hub is located. Each node can have different operating systems and different versions of the same browser or different browsers.

You need to configure Hub first and the register nodes with Hub to execute tests.

Before proceeding further with configuration make sure Java is installed on your system and environment variable is set. Now to configure Hub, first, you need to download the Selenium Server Jar file 'selenium-server-standalone-3.141.59.jar' from Seleniumhq's website.

Place this Jar file in an appropriate directory (says c:/Selenium Server. )

Open windows command prompt and navigate to the directory where Jar file is stored.

for example c:>Selenium Server

Now execute below command -

java -jar selenium-server-standalone-3.141.59.jar -role hub

Where selenium-server-standalone-3.141.59.jar is the downloaded jar file. -role flag is used to set a host machine as Hub.

One thing to keep in mind is Hub will be up and running as long as the command prompt is open. By default, the selenium grid uses 4444 port for web interfaces.

To check whether the Hub is up and running open browser and navigate to http://localhost:4444 and you will see below UI.

Nodes are host machines on which the tests are run. These tests are launched by the Hub. Hub can launch one or more nodes on remote machines or on the same machine where Hub is located. Node executes tests you loaded on the hub.

To configure nodes, you need to download the Jar files on the Node machines and place it on any directory of convenience. Open windows command prompt and execute below command-

java -DWebDriver.chrome.driver=C:chromedriver.exe -jar selenium-server-standalone-3.141.59.jar -role node -hub http://192.168.2.11:4444/grid/register

To set a Node on the same machine as you Hub, you need to open another command prompt other than Hub prompt.

-DWebDriver.chrome.driver=C:chromedriver.exe is used to set the browser driver path. 

-role node flag used to set a particular host machine as a node.

-hub http://192.168.2.11:4444/grid/register directs Node to connect with Hub using Hub's IP address. If you want to launch a Node on the same machine as your HUb, then you can specify ‘-hub http://localhost:4444/grid/register‘ in the command.

To check Nodes are registered, open and browser and navigate to http://localhost:4444/grid/console

Once Selenium Grid is configured, you can proceed with the test script execution on Nodes.

To execute scripts two important library packages DesiredCapabilities object and RemoteWebDriver object are required. DesiredCapabilities library is used to set browser type and the OS of Node. Use the below code to import the DesiredCapabilities object.

import org.openqa.selenium.remote.DesiredCapabilities;

The RemoteWebDriver object is used to select the Node on which test will be executed. Use the below code to import RemoteWebDriver object.

import java.net.MalformedURLException;

import java.net.URL;

import org.openqa.selenium.remote.RemoteWebDriver;

You can mouse hover on the logos of browser present in the Selenium Hub console to get the details like browserName and platform of the Node.

Use below code to setup grid

package TestGrid;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.net.MalformedURLException;
import java.net.URL;
import org.junit.Assert;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
public class TestGrid {
 static WebDriver driver;
 static String nodeUrl;
 @BeforeTest
 public void setup() throws MalformedURLException {
 nodeUrl = "http://192.168.2.11:4444/wd/hub";
 DesiredCapabilities capabilities = DesiredCapabilities.chrome();
 capabilities.setBrowserName("chrome");
 capabilities.setPlatform(Platform.WINDOWS);
 driver = new RemoteWebDriver(new URL(nodeUrl), capabilities);
 }
 @Test
 public void simpleTest() {
 driver.get("https://nikasio.com");
 }
 @AfterTest
 public void afterTest() {
 driver.quit();
 }
}

Here the test is divided into three test annotations @BeforeTest, @Test, and @AfterTest. In @BeforeTest, DesiredCapabilities and RemoteWebDriver objects are used to configure Node. In @Test directed Node to navigate to nikasio.com's home page.In @AfterTest, directed Node to quite the browser instance.

On execution, you will get below output.

May 18, 2018 4:15:07 PM org.openqa.selenium.remote.ProtocolHandshake createSession

INFO: Detected dialect: OSS

PASSED: simpleTest

Every website is comprised of n number of technologies that are used in the backend and frontend. Three major technologies used for the front end and in the rendering are HTML5, CSS3, and JavaScript.

Every browser has a different rendering engine to compute these three technologies. Chrome uses Blink, Firefox uses Gecko and IE uses edge HTML and Charka. Because of these rendering engines, the same website renders complete differently in all these different browsers and that is the reason why you need cross-browser testing. To ensure the website works fine and functions the same across different browser versions and on different operating systems, cross-browser testing is required.

Reasons for why cross-browser testing is required are -

  1. To ensure browser compatibility with different operating systems.
  2. To ensure image orientation on different browsers.
  3. Different orientation of Javascript on each browser can cause an issue.
  4. To check font size mismatch or font not rendered properly.
  5. To check compatibility with the new web framework.

Selenium does not support report generation and test case management, we use the TestNG framework with selenium to support these features. TestNG is more advanced than JUnit and implementation of annotations is easy. 

The test suite is not defined in testing source code and it is represented in an XML file because the suite is a collection of test cases and an execution is the feature of the test suite.

To execute test cases in a suite or group of test cases, you need to create a testng.xml file. The testng.xml file contains the name of all classes and methods that needs to be executed as a part of execution flow.

Advantages of using testng.xml file are -

  1. The execution of multiple test cases from multiple classes is allowed.
  2. Parallel execution is allowed.
  3. Execution of test cases in groups, where a single test can belong to multiple groups is allowed.

We always wish to test our software using different sets of data to make sure software works properly under a different set of data.

Parameterization is the technique used to define values in the testng.xml file and send these values as parameters to the test case. Parameterization is useful when you want to pass multiple data to different test environments. We need to pass multiple data to the application at runtime. Below is the code in which empName is annotated as a parameter

public class ParameterizedTest1{
 @Test
 @Parameters("empName")
 public void parameterTest(String myName) {
 System.out.println("Parameterized value is : " + myName);
 }
}

To pass the parameter value using testng.xml, use the 'parameters' tag. Look at the below code -

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
 <suite name=”CustomSuite">
  <test name=”CustomTest”> 
   <parameter name="empName" value=”Donald"/>
    <classes>
     <class name="ParameterizedTest1" />
    </classes> 
  </test>
 </suite>

Maven builds the code and manages dependencies on the fly. Also due to its ability for supporting deployments Maven is used wide-spread. Maven makes a lot of post-development tasks easy. There are a bunch of other tools you can use instead of Maven.

Ant - Ant (Another Neat Tool) is a command-line tool written in Java and uses XML to build modules, define targets and specify dependencies.

IVY - IVY is a build tool from Apache and is an extension to Ant. IVY helps in managing the external dependencies during development. IVY provides a way of adding dependencies to the classpath and packaging them with the application. IVY is a lightweight alternative to Maven.

Gradle - Gradle an open-source build tool automates the build process taking over Ant and Maven tools. Gradel uses domain-specific language instead of XML templates for defining the project configuration. The groovy syntax is followed in a domain-specific language.

Gradel suggests a polyglot build system that integrates the project with different technologies and programming languages. Gradle makes version management easier than Maven with the help of dynamic versioning.

The two most commonly used network protocols are HTTP and HTTPS. There are multiple ways to handle both protocols in Selenium.

HTTP protocol -

No Authentication - When a website is using HTTP protocol and does not require any authentication, it is easy to handle

WebDriver driver = new ChromeDriver();

driver.get("http://destination-url/");

Basic authentication - Many websites apply basic authentication scheme before allowing access to the home page. You can handle basic authentication in three ways-

i) Using WebDriverWait and Alert classes to implement basic authentication for HTTP.

WebDriverWait testwait = new WebDriverWait(driver, 10); 
Alert testalert = testwait.until(ExpectedConditions.alertIsPresent()); 
testalert.authenticateUsing(new UserAndPassword(**user**, **pass**));

ii) pass user/pass pair within the HTTP URL as a parameter to WebDriver's Get method.

String target = “http://user:pass@host”;
public void login(String username, String password){
    WebDriver driver = new ChromeDriver();
    String URL = "http:// + username + ":" + password + "@" + "website link";
    driver.get(URL);
    driver.manage().window().maximize();
}

iii) Setting up browser preferences using the Selenium's profile classes.

FirefoxProfile FF = new FirefoxProfile();
FF.setPreference("network.http.phishy-userpass-length", 255);
driver = new FirefoxDriver(FF);
driver.get("http://user:pass@www.targetsite.com/");

HTTPS protocol - HTTPS is a secured protocol of HTTP and it encrypts communication exchanged between the web server and the browser. HTTPS protocol uses an SSL certificate that downloads onto the browser on initiating the first request. SSL certificate has a public key for encrypting the data transfer from client to server.

In short, we need to handle the SSL certificates in Selenium. To manage SSL in different browsers you need to use different approaches.

Handle SSL in Firefox - In firefox SSL is enabled using Selenium Profile APIs.

First load the profile of running browser instance.

ProfilesIni pf = new ProfilesIni();
FirefoxProfile ff = pf.getProfile ("myProfile");

Set the following properties to avoid security exception while opening the HTTPS site 

ff.setAcceptUntrustedCertificates(true);
ff.setAssumeUntrustedCertificateIssuer(false);

Now create Firefox driver instance using profile object.

WebDriver driver = new FirefoxDriver (ffProfile);

Handle SSL in Chrome - Chrome need to set SSL options via desired capabilities of selenium WebDriver.

First prepare DesiredCapabilities instance

DesiredCapabilities set_ssl = DesiredCapabilities.chrome();
set_ssl.setCapability (CapabilityType.ACCEPT_SSL_CERTS, true);
WebDriver driver = new ChromeDriver (set_ssl);

Handle SSL in IE - In IE SSL is managed with two different methods.

i) Native JavaScript code -

driver.navigate ().to ("javascript:document.getElementById('overridelink').click()");
ii) DesiredCapabilities Class -

DesiredCapabilities ssl = new DesiredCapabilities();
ssl.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
System.setProperty("WebDriver.ie.driver","IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver(ssl);

Continuous Integration is a technique which regulates the development process by automating the build and testing code on every commit made to the version control. Continuous Integration makes sure that new changes do not have any adverse effect on the build and existing functionality is not affected. In the case of a failed scenario, everyone including the one responsible for the changes will know about the failure without delay. With this timely reporting, developers find fair time to fix the issues and get prompt feedback.

Continuous Integration encourages best practices amongst developers by allowing them to merge their changes with other's code before checking into a shared repository. On time code integration eliminates changes of conflicts, duplication of functionality.

CI suggests using a version control system that supports Git workflows for intelligent branching. CI proposes having the main branch that keeps the stable code that can be used to deliver build at any time. At the same time developer can create transient features branches to start his work without interrupting others. Developers need to sync his code with Main Branch at regular intervals. On feature-complete, a developer should merge his code into the main branch after verification

In some automation scripts, there is a need to use mouse or keyboard actions to interact with OS like Alerts, download pop-up, Print pop-up, etc. and Selenium Webdriver cannot handle these OS pop-ups or applications.

A Robot is a class in Selenium and is used to generate native system input events for test automation, where you need control over the mouse and keyboard. To handle the OS popups which WebDriver does not handle, the Robot class was introduced.

The main purpose of ow Robot class is to assist automation testing for Java platform implementations. In other words, the Robot class provides control over mouse and keyboard devices.

This class is easy to implement and easily integrate with the automation framework.

Let us see the importance of the Robot class -

  1. Using Robot class you can easily upload a file
  2. Robot class handle the mouse and keyboard functions
  3. pop-ups can be handled easily with Robot class

Below are the few methods that help in the easy execution of test scripts required for Robot class implementation.

  1. KeyPress()
  2. KeyRelease()
  3. MouseMove()
  4. MousePress()
  5. MouseRelease()

Let us understand these methods in details -

  1. KeyPress() - Use this method when you want to press any key

Example : robot.keyPress(keyEvent.VK_UP); will press UP key on the keyboard.

  1. KeyRelease() - Use this method to release the pressed key on the keyboard.

Example: robot.keyRelease(keyEvent.VK_CAPS_LOCK); will release the pressed caps lock key on the keyboard.

  1. MouseMove() - use this method when you want to move the mouse pointer in the X and Y coordinates.

Example : robot.mouseMove(coordinates.get.X(),coordinates.get.Y());will move the mouse over X and Y coordinates.

  1. MousePress() - use this method to press the left button of the mouse.

Example - robot.mousePress(InputEvent.BUTTON1_MASK); will press the mouse button

  1. MouseRelease() - Help this method in releasing the pressed button of the mouse.

Example - robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK); will release the right click of the mouse.

Let us see the code on how to declare robot class -

Robot robot = new Robot();

robot.keyPress(KeyEvent.VK_DOWN); //will press down key on the keyboard

robot.keyPress(KeyEvent.VK_TAB); // will press tab key on the keyboar

TestNG is an open-source testing framework inspired by JUnit and NUnit but presenting some new functionality that makes it simply easy, effortless, and powerful to use where NG of TestNG stands for Next Generation. It is same as JUnit but it is more robust than JUnit. To make execution more efficient and powerful more functionality is added. It is very much Data-Driven but is powerful than JUnit. Most of the negative traits of the older framework are removed from TestNG and it gives developers the ability to write more flexible and powerful tests with the help of annotations, grouping, sequencing and parameterizing.

Advantages of TestNG are -

  • Can generate logs
  • Due to Annotations code is efficient and easy to manage
  • Ability to generate execution Reports in HTML
  • Can be Grouped & Prioritised tests easily
  • Parallel testing is possible in TestNG
  • Data Parameterization is supported
  • Due to annotation testing time reduced considerable testing time reduce considerably
  • Scheduling of test cases can be done properly
  • Can use a data-driven approach by using a data provider
  • Proper control over test execution is possible

Selenium is limited to browser testing. To access databases using Selenium WebDriver we need to use JDBC that is Java Database Connectivity.  JDBC is a SQL level API to execute SQL commands and is responsible for the connection between Java and different databases. So use JDBC Driver to connect to the Database with Selenium when using Java as a programming language.

To connect to the database first download the JDBC jar file and add it to your project.

Also you need database URL with port, user ID and password. Syntax in order to make connection to the database is -

DriverManager.getConnection(URL, "userid", "password" )

Where, URL format is  jdbc:<dbtype>://<host>:3036/db_name" and  <dbtype>- database driver. To connect to oracle database <dbtype> will be "oracle"

So for Oracle database with name ‘demo’ URL will be jdbc:oracle://<host>:3036/demo

So code for database connection will be

Connection con = DriverManager.getConnection(dbUrl, username, password);

You need to load JDBC driver and code is

Class.forName(“com.oracle.jdbc.Driver”);

Once connection is established you need to execute queries and for that you need to declare statement object to send queries

Statement stmt = con.createStatement();

Now use the executeQuery method to execute the SQL queries

stmt.executeQuery(“Select * from demo”);

Store the result of executed query in the ResultSet Object

ResultSet rs= stmt.executeQuery(stmt);

Import java.sql.Connection;
Import java.sql.DriverManager;
Import java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;
Import org.testng.annotations.Test;
public class TestDatabase {
public void TestVerifyDB () {
Class.forName ( “Sun.jdbc.odbc.jdbcodbcDriver” );
String dblocation = “ C: \\ Users\\Desktop\\DB\\TestDB1.accdb”;
Connection con = DriverManager.getConnection ( dbUrl, username, password );
Statement smt = con.createStatement ();
ResultSet rs = smt.executeQuery ( “Select*from TestDB1” );

Links or URLs which are not reachable or functioning are broken links due to some error.

There are different status codes for HTTP which are having different purposes. 2XX is valid request status and the URL will always have status 2XX. Status 4XX and 5XX are for invalid HTTP request out of which 4XX status code is for client-side error and 5XX status code are for server response error.

It is not possible to confirm that the link is working or not till we click on the link and confirm it.

It is always good practice to make sure that there are no broken links because the end-user should not land into an error page.

You can not check all links manually, because each web page may have a large number of links & manual process has to be repeated for all pages. It will be a tedious task.

With Selenium, we can automate the process.

  • First, collect all links on the web page based on a tag.
  • Then send an HTTP request for the link and read the HTTP response code.
  • Based on the HTTP response code determine whether the link is valid or broken
  • Repeat this for all the links captured.

Data is very important and critical information to run automation successfully. Data can be in different forms to serve different purposes.  For example in automation, input data includes test cases, and it could be test data that feeds into test cases as parameters.

Selenium supports Excel, CSV, XML, JSON, SQL, YAML data types for automation.

1. Excel Data.

Excel is a popular data format used widely in automation. It can store test cases as well as test data.

Excel format is not only flexible but also support Create/Delete/Update/Delete (CRUD) operations also. All language that Selenium supports have ready-made libraries to access and manipulate excel data.

2. XML Data.

An XML document is in a format readable by both humans and machines. XML is a web standard and faster than Excel in reading or writing data. There are built-in or third-party XML parsers available to manage the data. XML has a unique feature to search specific elements called XPath. Xpath is a known term in Selenium which traverses the DOM of a web page.

Many times Excel and XML are used together in Selenium automation projects. You can write the test cases in Excel and create a macro to convert them in XML format. The automation suite reads the test cases from XML and executes them.

3. SQL Data.

SQL data is customizable and enables fast data access. It stores both the test cases and configuration or settings as required.

Separate database software like MySQL/MongoDB/SQL/Oracle is required to make it work and install any of this software to prepare a new setup which is an additional overhead for the automation engineers.

4. CSV Data.

CSV that is comma-separated-values, is the simplistic way of organizing test data in tabular format. CSV data is a text file with extension .csv and it represents a sequence of rows and columns delimited by commas. Every row of CSV file is a record while every column mapped to a field.

As most programming languages come with standard libraries to read/write text files it is trivial to access CSV. Excel supports this type of data and can handle it the same way.

5. JSON Data.

JSON (JavaScript Object Notation) supports a light-weight data interchange format. JSON data is compact and fast and we can use it to represent the test data replacing Excel and XML formats. For large data size, JSON would deliver better performance.

6. YAML Data.

YAML(YAML Ain’t Markup Language) is a human-readable language for data serialization. For holding configuration this type of data format is ideal. YAML is a superset of JSON language and stores both JSON and XML data within itself.

In testing, it is very much necessary to store test result data to prove the certain test is passed/failed or functionality is covered or not and you can take screenshots as test result or proof, which can be used for reference later. The same way we need to take screenshots as proof in automation testing also. During automated testing of continuous application flow, you might want to capture a series of screenshots. Selenium WebDriver has a nice little utility MultiScreenShot to capture multiple screenshots. This utility allows you to take a screenshot of individual elements and minimize the browser.

We will see how to setup MultiScreenShot utility with selenium WebDriver and capture multiple screenshots. We will use Google.com for the explanation.

  • First, download the MultiScreenShot jar file.
  • Add jar in your Selenium Webdriver project by adding it to Java Build Path.
  • Now import the package in your class file.
  • Let us create an object to use MultiScreenShot. Pass two arguments that are the path to save screenshot and class name to create the folder and save the screenshot inside that.
    Example: path=C:UsersxxxxxxDesktopTestMultiScrShot
                     className=ScreenShots

Full page screenshot is taken when you call the method and this method accepts WebDriver object as an argument.

Example: multiScrShot.multiScreenShot(driver);

Screenshot of elements on a page is taken when you call method and method accepts WebDriver object as argument and WebElement object

Example: mShot.elementScreenShot(driver, driver.findElement(By.id("search-submit")));

Minimize the browser window when the browser window is maximized. No argument is required to pass.

Example:  multiScrShot.minimize();

package testPackage;
import java.awt.AWTException;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import multiScreenShot.MultiScreenShot;
public class TestMultiScrShot 
{
    public static void main(String[] args) throws IOException, AWTException 
    {
      MultiScreenShot multiScrShot=new MultiScreenShot("C:New","TestMultiScrShot");
        WebDriver driver =new FirefoxDriver();
        driver.get("https://google.com");
        //maximize the window
        driver.manage().window().maximize();
        //take full screenshot using MultiScreenShot class
        multiScrShot.multiScreenShot(driver);
        //take element screenshot using MultiScreenShot class
        multiScrShot.elementScreenShot(driver, driver.findElement(By.name("btnK")));
        //navigate to https://nikasio.com
        driver.navigate().to("https://nikasio.com");
        //take element screenshot using MultiScreenShot class
       multiScrShot.elementScreenShot(driver,   driver.findElement(By.id("slide-2-layer-2")));
        //take full screenshot using MultiScreenShot class
        multiScrShot.multiScreenShot(driver);
        //minimize the window using MultiScreenShot class
        multiScrShot.minimize();
        driver.quit();
    }
}

Selenium has a built-in capability to handle various types of keyboard and mouse events. In order to perform action events, we need to use org.openqa.selenium.interactions Actions class,  user-facing API for performing complex user gestures. Instead of using Keyboard or Mouse directly, you can use the selenium actions class.

Syntax to create an object ‘action’ of selenium action class –

Actions action – new Actions(driver);

To focus on element -

action.moveToElement(element).perform();

Here element is the web element we have captured and perform() method is used here to execute the action.

To click on Element -

Action.moveToElement(element).click().perform();

Here click() method is used to click the element.

Let us see methods available with Selenium Actions class -

Keyboard Events Using Selenium Actions Class API:

  • sendKeys(keysTo Send  sends a series of keystrokes onto the element
  • keyDown(theKey): Sends a keypress without release it. Subsequent actions may assume it as pressed. (example: Keys.ALT, Keys.SHIFT, or Keys.CONTROL)
  • keyUp(theKey): Performs a key release

Mouse Events Using Selenium Actions Class API:

  • click (): Simply click on an element
  • doubleClick (): Double clicks on Element
  • contextClick() : Performs a context-click (right-click) on an element
  • clickAndHold(): Clicks at the present mouse location (without releasing)
  • dragAndDrop(source, target): Clicks-and-hold at the source location and moves to the target location of the element before releasing the mouse. source – element to grab, target – element to release
  • dragAndDropBy(source, xOffset, yOffset): Invokes click-and-hold at the source location, shifts by a given offset, then frees the mouse. xOffset  is to shift horizontally and yOffset is to shift vertically
  • moveByOffset(x-offset, y-offset): Shifts the mouse from its current position or 0,0 by the given offset. x-offset – Sets the horizontal offset (negative value – shifting the mouse to the left), y-offset – Sets the vertical offset (negative value – shifting the mouse to the up)
  • moveToElement(toElement): It shifts the mouse to the center of the element
  • release(): Releases the depressed left mouse button at the existing mouse location

Additional information about an HTML is stored in Attributes and comes in pair name =”value”. In <div class=”my-class”></div> example div is tag and class is attribute with value ‘my-class’. The property presents an attribute in the HTML DOM tree. In the above example, the attribute would have a property named className and value my-class. HTML defines attributes and DOM defines properties. Attributes may have 1:1 mapping with property or may not have. The ID is an example of 1:1 mapping. Attributes are stored in HTML text document and properties are in the HTML DOM tree. Attributes carry initial default value and the attribute does not change whereas HTML properties can change.

To verify test cases, during automation we need to retrieve values of attributes or properties of web elements.

In case of a bus ticket booking application,  Colour of the available seat colour is green and booked seat colour is red. To book the ticket first we need to check whether a seat is available or not. And to test this, we need to fetch colour attribute value using the script and based on output value we need to perform further operations.

To fetch attribute value, selenium WebDriver has predefined getAttribute(value) method which returns the value of the attribute of the web element as a string. If the attribute exists, getAttribute method returns value of the property with the given name, and if it does not exist then returns value of the attribute with the given name.

Please note, when the given name is “class”, then “className” property is returned and when given name is “readOnly”, the “readOnly” property is returned.

To use getAttribute() method on a specific element, first, locate the web element and then call getAttribute() method on it by specifying the attribute.

Below is the code explaining getAttribute() method –

WebElement we = driver.findElement(By.class("class Value"));
String value = we.getAttribute("id");

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.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class GetAttributes {
public WebDriver driver;
private By bySrchButton = By.name("btnK");
@BeforeClass
public void setUp() {
driver = new FirefoxDriver();
driver.get("http://www.google.com");
}
@Test
public void getAttribute_ButtonName() {
WebElement googleSrchBtn = driver.findElement(bySrchButton);
System.out.println("Name of the button is:- " +googleSrchBtn.getAttribute("name"));
}
@Test
public void getAttribute_Id() {
WebElement googleSrchBtn = driver.findElement(bySrchButton);
System.out.println("Id of the button is:- "+ googleSrchBtn.getAttribute("id"));
}
@Test
public void getAttribute_class() {
WebElement googleSrchBtn = driver.findElement(bySrchButton);
System.out.println("Class of the button is:- "+ googleSrchBtn.getAttribute("class"));
}
@Test
public void getAttribute_InvalidAttribute() {
WebElement googleSrchBtn = driver.findElement(bySrchButton);
//Negative test as 'Status' attribute does not exist,will return null value
System.out.println("Invalid Attribute status of the button is:- "+ googleSrchBtn.getAttribute("status"));
}
@Test
public void getAttribute_ButtonLabel() {
WebElement googleSrchBtn = driver.findElement(bySrchButton);
System.out.println("Label of the button is:- "+ googleSrchBtn.getAttribute("aria-label"));
}
@AfterClass
public void tearDown() {
driver.quit();
}
}

Selenium WebDriver has predefined method clear() which is used to clear the text entered or displayed in the text fields. Some times default text is displayed in the text box or sometimes due to cookies stored pre-populated text is displayed in a text box and we need to clear that text before entering new text otherwise we might get the wrong result. To avoid test failure it is always a best practice to clear text before entering new text.

driver.findElement(By.id(“id Value”)).clear();

public void clearTheTextBoxField ()
{
_driver.findElement ( By.cssSelector ( “#textbox1” )).clear ();
}

Let us see the code for clear text. We will use google.com for our reference. In this, we are sending some text first and then clear it.

package testPackage;
import java.awt.AWTException;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import multiScreenShot.MultiScreenShot;
public class textClear 
{
    public static void main(String[] args) throws IOException, AWTException 
    {
        WebDriver driver =new FirefoxDriver();
        driver.get("https://google.com");
        //maximize the window
        driver.manage().window().maximize();
        WebElement searchBox = driver.findElement(By.name("q");
//Send text to the web element
searchBox.sendKeys("Selenium clear text testing");
//clear text
searchBox.Clear();
   }
}

Before throwing and exception Implicit wait tells WebDriver to wait for a certain amount of time. WebDriver will wait for the element based on the time we set before it throws an exception. We need to set some wait time to make WebDriver to wait and default setting is 0 (zero). Implicit Wait is in place till the browser is open. Based on time fixed for the implicit wait, the same wait time will be applied to all elements for search.

Syntax:

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

Two parameters required for Implicit wait are time as an integer value and second is time measurement in terms of seconds, minutes, milliseconds, microseconds, days, hours, etc.

WebDriver has various wait commands that help to overcome issues due to variation in a time lag. WebDriver checks whether an element is present or visible or enabled or clickable etc. 

The explicit wait tells the WebDriver to wait for certain conditions (Expected Conditions) or wait till the maximum time exceeded before throwing an exception.

The explicit wait is an intelligent wait but can be applied only on specified elements. As explicit wait waits for dynamically loaded Ajax elements, it is a better option than implicit wait. We need to provide “ExpectedConditions” along with Explicit wait.  

Syntax:

WebDriverWait wait = new WebDriverWait(WebDriverReference,TimeOut);

Below is the list of Expected Conditions used in Explicit Wait

  1. alertIsPresent()
  2. elementSelectionStateToBe()
  3. elementToBeClickable()
  4. elementToBeSelected()
  5. frameToBeAvaliableAndSwitchToIt()
  6. invisibilityOfTheElementLocated()
  7. invisibilityOfElementWithText()
  8. presenceOfAllElementsLocatedBy()
  9. presenceOfElementLocated()
  10. textToBePresentInElement()
  11. textToBePresentInElementLocated()
  12. textToBePresentInElementValue()
  13. titleIs()
  14. titleContains()
  15. visibilityOf()
  16. visibilityOfAllElements()
  17. visibilityOfAllElementsLocatedBy()
  18. visibilityOfElementLocated()

WebDriver has various wait commands that help to overcome issues due to variation in a time lag. Web driver checks whether an element is present or visible or enabled or clickable etc. 

The Fluent wait is one of the Explicit waits. In Fluent wait, you can define the maximum amount of time for a condition and frequency with which to check the condition before throwing an exception.

In short, fluent wait tries to find the web element repeatedly at regular intervals of time until the timeout or till the object gets found. Mainly FluentWait command is used when web elements are visible in few seconds and sometimes take more time than usual. Two main components of fluent wait are - Timeout value and polling frequency

FluentWait syntax is -

Wait<WebDriver> wait = new FluentWait(WebDriver reference)
.withTimeout(timeout, SECONDS)
.pollingEvery(timeout, SECONDS)
.ignoring(Exception.class);
Wait<WebDriver> wait = new FluentWait(driver).withTimeout(30, SECONDS).pollingEvery(5, SECONDS).ignoring(NoSuchElementException.class);

You often get ElementNotVisibleException when you execute selenium scripts when WebDriver locate web element on the current webpage but the element is not visible on the screen.

There are many reasons that accumulate for this exception. Below are the reasons for ElementNotVisibleException –

Not locating correctly

  • When your locator is incorrect, selenium WebDriver throws “NoSuchElementException”. Exception means, WebDriver is able to locate elements, but the desired element is not visible.
  • Your locator may be wrong in case of a nested web element. A web element inside web element is called as the nested web element.

Solution:

Locate correct and specific element.

The desired element is overlapped by another web element

  • Many applications nowadays use toaster messages. A toaster message stays on screen for 3 seconds.
  • When you click on any button or link, you might get the above exception as button or link is overlapped by toast message. Webdriver will not be able to click on button or link and throws a perfect exception stating other elements would receive the click.

Solution:

  • Wait till toast message disappears. You must use the wait property provided by selenium.

Ajax calls

  • AJAX  allows web pages to be updated asynchronously by exchanging data with a web server behind the scenes. It is possible to update parts of a web page without reloading the whole page. Selenium wait when the whole page is loading but can not recognize loading through AJAX calls. If you perform any action on a web element that is still loading because of AJAX calls when AJAX calls in progress, you may get NoSuchElementException or Element is not clickable(If action is click)  or Element is not visible exceptions.

Solution:

  • Wait till the AJAX call is complete by using selenium wait property.

Webdriver is unable to auto-scroll to the desired element

  • If your web page has a vertical scroll bar and you need to perform an action on web element which shows up in the visible area when a user scrolls down. Webdriver mostly scrolls down to element and perform desired operations but sometimes, web driver is not able to do so. In this case, you may get an exception.

Solution:

  • Scroll to the element using javascript or Actions class so that the element is visible on the screen.
  • By using selenium wait property.

The desired capability stores the browser properties like browser name, browser version, the path of the browser driver in the system, etc in a series of key/value pairs, to determine the behaviour of the browser at run time.

You can also use the Desired capability to configure the driver instance of Selenium WebDriver like FirefoxDriver, ChromeDriver, InternetExplorerDriver.

Every Testing scenario is executed on a specific testing environment and this testing environment can be a web browser, Mobile device, mobile emulator, mobile simulator, etc.

The Desired Capabilities Class tells WebDriver, which environment you going to use in the test script.

The setCapability method of the DesiredCapabilities Class can be used in Selenium Grid to perform a parallel execution on different machine configurations.

Desired capability is used to set the browser properties, Platform Name that is used when executing the test cases.

In the case of mobile automation, we perform the tests on different mobile devices, the Mobile Platform ex. iOS, Android Platform Version Ex. 3.x,4.x in Android can be set.

Desired Capabilities are more useful in mobile application automation, where the browser properties and the device properties can be set. And in Selenium grid to run the test cases on a different browser along with different operating systems and versions.

Example-

// Create a new object of DesiredCapabilities class.
DesiredCapabilities capabilities = new DesiredCapabilities();
// set the android deviceName desiredcapability.
capabilities.setCapability("deviceName", "your Device Name");
// Set the BROWSER_NAME desiredcapability.
capabilities.setCapability(CapabilityType.BROWSER_NAME, "Chrome");
// Set the android VERSION desiredcapability.
capabilities.setCapability(CapabilityType.VERSION, "5.1");
// Set the android platformName desired capability.
capabilities.setCapability("platformName", "Android");

In selenium, we can click on the Login button in different ways -

1. Navigate to login button. Once control is on the login button perform click() action.

package clicklogin;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class LoginClick {
         public static void main(String[] args) throws InterruptedException{
                     WebDriver driver = new FirefoxDriver();
                     //Launching site
                     driver.get("http://facebook.com");
                     // click on the LogIn button
                      driver.findelement(By.id("u_0_a")).click();
                     //Thread.sleep just to notice event
                     Thread.sleep(3000);
                     //Closing the driver instance
                     driver.quit();
         }
}

2. Another way to perform click on Login button using action class

package clicklogin;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class LoginClick {
         public static void main(String[] args) throws InterruptedException{
                     WebDriver driver = new FirefoxDriver();
                     //Launching site
                     driver.get("http://facebook.com");
                     // click on the LogIn button
            WebElement loginBtn = driver.findElement(By.id("u_0_a"));
                     Actions act = new Actions(driver);
                     act.moveToElement(loginBtn).click().perform().build();   
                     //Thread.sleep just to notice event
                     Thread.sleep(3000);
                     //Closing the driver instance
                     driver.quit();
         }
}

3. Third way is press Enter key

package clicklogin;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class LoginClick {
         public static void main(String[] args) throws InterruptedException{
                     WebDriver driver = new FirefoxDriver();
                     //Launching site
                     driver.get("http://facebook.com");
                     // click on the Log In button
            WebElement loginBtn = driver.findElement(By.id("u_0_a"));
                     Actions act = new Actions(driver);
                     act.SendKeys(Keys.Enter);
             //Thread.sleep just to notice event
                     Thread.sleep(3000);
                     //Closing the driver instance
                     driver.quit();
         }
}

In some scenarios, you might need to do the right click action/context on an element to perform some actions. Use Selenium WebDriver class Actions to work on Mouse and Keyboard Actions. Selenium has a built-in capability to handle various types of keyboard and mouse events. In order to perform action events, we need to use org.openqa.selenium.interactions Actions class,  user-facing API for performing complex user gestures. Instead of using Keyboard or Mouse directly, you can use the selenium actions class.

Let us see the scenario to automate

  1. First launch the web browser and open the facebook application
  2. Then find the required element and do right click on the element say LogIn button
package rightclickaction;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class RightClickAction {
         public static void main(String[] args) throws InterruptedException{
                     WebDriver driver = new FirefoxDriver();
                     //Launching site
                     driver.get("http://facebook.com");
                     //Right click on the LogIn button
                     Actions action = new Actions(driver);
                     WebElement loginBox = driver.findElement(By.cssSelector(".#u_0_a"));
                     action.contextClick(loginBox).perform();
                     //Thread.sleep just to notice event
                     Thread.sleep(3000);
                     //Closing the driver instance
                     driver.quit();
         }
}

Now a day, many web applications have the functionality to drag an element and drop it on a defined area.

There are two methods in Actions class which supports drag and drop -

  1. Actions.dragAndDrop(Sourcelocator, Destinationlocator)

Here we pass two parameters, first "sourcelocator" is the element we need to drag. The second parameter is "Destinationlocator" which is the element on which we need to drop the first element.

  1. Actions.dragAndDropBy(sourcelocator, x-axis pixel of destinationlocator,y-axis pixel of destinationlocator)

Here we pass three parameters, first "source locator", the second is x-axis pixel value of the 2nd element on which we need to drop the first element and third is y-axis pixel value of the 2nd element.

Ex. 

Actions act=new Actions(driver);    
act.dragAndDrop(From, To).build().perform();

There are two ways you can upload file in selenium WebDriver, one is using SendKey method when there is text box available to enter the file’s name and second is using AutoIT Script tool when there is no text box to enter the file’s name.

  1. Upload file using SenKey() method –
  • First, locate the text box and then using SendKey() set the file path and click on the Submit button.

Syntax is –

//locate browse button
WebElement browse =driver.findElement(By.id("id value"));
//pass the path of the file to be uploaded using Sendkeys method
browse.sendKeys("D:\\SoftwareTestingMaterial\\UploadFile.txt");
//click on submit button
driver.findelement(By.id(“id value”)).click;
  1. Upload using open-source AutoIT Script tool
  • First, download and install the AutoIT Script tool (http://www.autoitscript.com/site/autoit)
  • Open Autoit tool -> SciTE Script Editor
  • Paste below code in editor and save it with .exe extension say test.exe

WinWaitActive("File Upload"); Name of the file upload window (Windows Popup ame: File Upload)

Send("logo.jpg"); File name

Send("{ENTER}");

  • Compile code and then build tools menu of AutoIT editor
  • Paste below code in Java editor and execute test
// open the Upload window using selenium
driver.findElement(By.id("Id value")).click();
//wait for page to load
Thread.sleep("20000");
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + "<filepath>\\test.exe"); // Give  path where the exe is saved.

In the computing world, the same-origin policy is at the most important concept in the security model available for web applications. This policy permits scripts running on the pages that are originating from the same source or site. Origin is a combination of schemes, hostname and port number, to access each other’s DOM with no specific restrictions but at the same time, it prevents access to DOM on different sites.

The Same-origin policy also applies to an XML, HTTP request and to web socket. This mechanism bears a particular significance for modern web applications that are using user logins are extensively dependent on HTTP cookies to maintain user sessions that are authenticated, as servers act based on HTTP cookie information to reveal sensitive information or take state-changing actions. A strict separation between content provided by the unrelated sites must be maintained on the client-side to prevent the loss of data confidentiality or integrity.

Policies could have different specifications; all browsers are having an implementation of same-origin policy as it is an unavoidable security concern. An algorithm is used to evaluate the origin of the specified URL. For absolute URL, origin contains protocol, host and port number. If the URL is not absolute, then a unique identifier is used. Any two resources are considered as from the same origin only if all of the values are exactly the same.

Same Origin policy prevents JavaScript code from accessing elements from a domain that is different from where it was launched. To overcome the Same-origin policy, Selenium RC was introduced. Prior to Selenium RC, testers require to install local copies of both Selenium Core and the web server containing the web application being tested so they would belong to the same domain.

Navigation commands enable selenium to open a new web page URL, navigate to other page links available on the web page by clicking on that link or can navigate to Back or Forward or can refresh the webpage content again.

Following are some types of navigation commands:

1) navigate().to(): This command is same like get() command, it opens a new browser and will find the URL specified by the user.

e.g. driver.navigate().to(“https://google.co.in/”);

2) navigate().forward(): This command is used to navigates to next page travelled by the user and is available in the browser history.

e.g. driver.navigate().forward();

3) navigate().back(): This command is used to navigates to back page user travelled from and is available in the browser history.

e.g. driver.navigate().back();

4) navigate().refresh():This command is used to refresh the current web page. All the web page content get reloaded again

e.g. driver.navigate().refresh();

TestNG is a testing framework that is developed as inspiration from the most popular JUnit framework used for java programming language. TestNG framework is introduced to overcome the limitations of the JUnit framework. Most of the organizations are using the TestNG framework because of its advantages and additional supporting features.

Features of TestNG framework:

TestNG supports more productive and advanced features and it is easy to use. Let’s check which new features are available in the TestNG framework.

It supports for parameters

It supports testing of dependent methods

Configuring the test is flexible

It supports more powerful execution model

It embeds BeanShell which further makes it more flexible.

It is having more elegant ways of handling parameterized tests with data provider concept.

It supports multiple instances for the same test class

It becomes more extendible as different tools and plug-ins like Eclipse, Maven, IDEA is supported. 

TestNG framework allows generating reports in XML and HTML format.

TestNG is designed to cover unit testing, integration testing, functional testing and end to end testing.

Advantage of TestNG v/s JUnit

  1. TestNG have annotations that are easy to understand over JUnit
  2. In TestNG, it doesn’t require  constraints like to declare @BeforeClass and @AfterClass, which needs to be done in JUnit
  3. TestNG allows executing test cases in a group, which is not possible in JUnit.
  4. TestNG support three additional setup: @Before/AfterSuite, @Before/AfterTest and Before/AfterGroup
  5. TestNG do not require to extend any extend
  6. TestNG allows us to define dependent test cases, each test case is independent of others.
  7. Parallel test case execution is possible in TestNG.

Selenium is an automation tool allow us to record and playback application under test without having knowledge of scripting by using selenium IDE. Selenium IDE uses Selenese language to write test cases in different programming languages like Java, C#, PHP, Perl, Python, Ruby, etc. Resulting tests can be run on against different browsers.

Debugging is the process of identifying and fixing the errors that may be present in the class. It is important to debug the code in order to remove the errors so that the application will run as expected.

The following are the ways to debug the script.

  • Insert a breakpoint from where you want to execute step by step
  • Run the test case
  • At the given breakpoint, execution will be paused
  • To proceed with the next step, click on the Blue button
  • To run commands at a time, click on the run button

To make the debugging process more robust, we can install and use Power Debugger add-on.

Step 1: Open Firefox browser, search for Power Debugger in Google and click on https://addons.mozilla.org/en-US/firefox/addon/power-debugger-selenium-ide/ link.
Step 2: Add-ons page appears on the web. Click on Add to Firefox button.
Step 3: Open ‘Selenium IDE’ and you can see a new icon, “Pause on Fail” on the recording toolbar. Click it to turn it ON. Clicking it again would be turned to “OFF”.
Step 4:  We can use breakpoints to understand the error during the execution. To insert a breakpoint on a particular step, “Right Click” and select “Toggle Breakpoint” from the menu options.
Step 5: After inserting the breakpoint, a particular step will be displayed with a pause icon.
Step 6: When we execute the test script, the script execution will be paused where the breakpoint is inserted. This pause will help the user to evaluate the value/presence of an element when the execution is in progress.

Object Repository is a centralized location where you can store the object information. Object repository acts as an interface between Test script and application in identifying the objects during the execution.

The collection of web elements belonging to AUT that is Application Under Test along with their locator values is termed as Object Repository. During execution whenever an element is required in the script, locator value is populated from the object repository. Instead of hard coding locators in the script object repository can be used and locators can be stored in a central location. Objects are stored in an excel sheet or XML file which populates inside the script whenever required.

It is always recommended using an external file for object repository rather than hard coding the objects and its properties directly into our code and Reason is as it reduces the maintenance effort and provides a positive Return on investment. If any of the object properties change within the application under test, we can easily change it in external object repository file, instead of searching and doing updates for that object individually in the code.

Before creating an object repository pre-requisite is project is created and Selenium jars are added.

  1. Create a new file with extension .properties

For example, object_repo.properties

To create new file right click on project> New > File

  1. Select the parent folder and specify file name & extension.

3- Now Double click on file> File will open in Edit mode

As object repository works on KEY and Value pair, you will specify Keys based on project and values we will give locator value.

Let us take XPath for username, password and login button for facebook login page.

1 facebook.login.username.xpath=.//*[@id='email']

2 facebook.login.password.xpath=.//*[@id='pass']

3 facebook.login.Signup.xpath=.//*[@id='loginbutton']</span>

4- Write Selenium script and use the same in the script

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
 import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
 public class TestFacebook {
 public void TestObjRepo() throws IOException{
 // Specify the file location used.
//we have object repository inside project directory only
File File1=new File("object_repo.properties");
 // Create  FileInputStream object
FileInputStream fis=new FileInputStream(File1);
 // Create Properties class object to read properties file
Properties prop=new Properties();
 // Load file so we can use into our script
prop.load(File1);
 System.out.println("Property class loaded");
 // Open FirefoxBrowser
WebDriver driver=new FirefoxDriver();
 // Maximize window
driver.manage().window().maximize();
// Launch application
driver.get("http://www.facebook.com");
// Enter username use keys which is specified in Object repository.
// Here getProperty is method which will accept key and will return value
driver.findElement(By.xpath(pro.getProperty("facebook.login.username.xpath"))).
sendKeys("testor@gmail.com");
// Enter password here used keys specified in Object repository.
driver.findElement(By.xpath(pro.getProperty("facebook.login.password.xpath"))).
sendKeys("password");
// Click on login button here use keys specified in Object repository.
driver.findElement(By.xpath(pro.getProperty("facebook.login.Signup.xpath"))).click();
 }
}
</span>

While executing the test cases, the test case may fail. The way while executing the test case manually we take a screenshot and place it in result repository, same way things can be done by using Selenium WebDriver.

We may need to capture screenshots using Selenium WebDriver in scenarios -

  1. Application issues
  2. Assertion Failure
  3. Difficulty to find WebElements on the web page
  4. Timeout to find WebElements on the web page

Selenium is having an interface named TakesScreenshot to take the screenshot. This Instance has one method getScreenShotAs() will be used to take a screenshot during test execution and saves that screenshot at the desired location that we provide in the code.

Syntax to capture and save the screenshot is -

File screenshotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

For ex. File screenshotFile = ((TakesScreenshot)driver).getScreenshotAs(screenshot1.png);

Syntax to store it in our local drive is -

FileUtils.copyFile(screenshotFile, new File("filename_with_path"));

For ex. FileUtils.copyFile(screenshotFile, new File("c:\\screenshot1.png"));

package Managescreenshot;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class CaptureScreenshot {
 @Test
 public static void captureScreenMethod() throws Exception{
 System.setProperty("webdriver.gecko.driver","c://Selenium Environment//Drivers//geckodriver.exe");
 WebDriver driver = new FirefoxDriver();
 driver.manage().window().maximize();
 driver.get("https://www.facebook.com/capture-screenshot-using-selenium-webdriver");
 File screenshotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
 FileUtils.copyFile(screenshotFile, new File("C:\\screenshot1.png"));
 driver.close();
 driver.quit();
 }
}

When the script fails, to know where was the error in script capture a screenshot of the web page when the test case fails. By seeing screenshot, We can easily identify where exactly the script is failed.

To take a screenshot of the failed test, we will place the entire code in try-catch block by placing the test steps in a try block and then screen capture statement in the catch block. In try block, if a test step fails then it goes to the catch block and captures a screenshot of the web page.

package software testing material;

import java.io.File;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class CaptureFailScreenshot {
 public static void captureFailScreenMethod() throws Exception{
            System.setProperty("webdriver.gecko.driver","C://Selenium Environment//Drivers//geckodriver.exe");
 WebDriver driver = new FirefoxDriver();
 driver.manage().window().maximize();
      try{
          driver.get("https://www.facebook.com");
  driver.navigate().refresh();
driver.findElement(By.id("emaile")).sendKeys("testmail@gmail.com"); //Statement with incorrect id
             }catch(Exception e){
          File screenshotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
      FileUtils.copyFile(screenshotFile, new File("C:\\incorrectuser.png"));
 }
     driver.close();
 driver.quit();
 }
}