Tuesday, 29 October 2019

Docker Selenium Image Setup


Go to Docker Hub to download Selenium Hub and Node containers. Steps to do so:

On docker Hub, search with 'Selenium' and hit enter, you will be redirected on search listing page. We will download below containers to setup Selenium Grid.

  • Selenium Hub
  • Chrome/Firefox Node Debug

Download Selenium Hub and Node

To Setup Selenium Grid, download hub and node images. Open terminal/power shell and use the below commands to pull below images:

Selenium Hub

Image for running a Grid Hub
docker pull selenium/hub


Chrome Node Debug

Grid Node with Chrome installed and runs a VNC server, needs to be connected to a Grid Hub
docker pull selenium/node-chrome-debug


Firefox Node Debug

Grid Node with Firefox installed and runs a VNC server, needs to be connected to a Grid Hub
docker pull selenium/node-firefox-debug


Verify downloaded images using below command
docker images




Install Docker on Windows

Prerequisites

  • Windows 10 64-bit: Pro, Enterprise, or Education (Build 15063 or later).
  • BIOS - Virtualization Enabled
  • Hyper-V and Containers Windows features must be enabled

Installation

  • Download Docker Desktop
  • Double-click Docker Desktop Installer.exe to run the installer.
  • Follow the Steps on installer wizard
  • Click Finish to launch Docker.

Whale icon in the status bar should be steady and up and running to access docker from Terminal.



Open Terminal/Power Shell to use Docker.








Thursday, 7 February 2019

Retry failed test case in testng Selenium

How can we execute failed test cases in Selenium using TestNG


There are multiple ways to execute Failed test cases in Selenium using TestNG:
  • 'testng-failed.xml'
  • IRetryAnalyzer

testng-failed.xml


If any test case is failed, testng-failed.xml is generated in the reports output directory(Target directory). We can simply run above xml to rerun the failed test cases.

Location of  testng-failed.xml




Running testng-failed.xml
In eclipse - Simply right click on xml file and run as testng


IRetryAnalyzer


TestNG has an Interface to implement to be able to have a chance to retry a failed test that is IRetryAnalyzer.

If you want TestNG to automatically retry a test whenever it fails. You can use a retry analyzer.
To retry a failed test you have bind retry analyzer to each test which you want to invoke on test failure.

How can we bind retry analyzer at Test Level

You can use retryAnalyzer attribute and assign it to implemented class of IRetryAnalyzer interface at test level. As shown below:

@Test(retryAnalyzer = RetryAnalyzerImpl.class)

After binding it to the test level, TestNG automatically invokes the retry analyzer to determine if TestNG can retry a test case again in case of failure for a specified number of times.

Complete implementation of IRetryAnalyzer class is shown below:



Sample Test class implementation is shown below:


Setup RetryAnalyzer at Class/Suite Level


We can setRetryAnalyzer in setup method - @BeforeClass, @BeforeSuite instead of @Test as shown in RetryTest.java

Sample Test class implementation is shown below:


Wednesday, 6 February 2019

Run Test Sequentially in Selenium Testng

There are multiple ways to run test sequentially in Selenium using TestNG
  • Defining each test in order in XML
  • Using 'Priority' attribute 

Lets say we have 4 test in a class as shown below. By default test will run in an unpredictable order.
package com.example;

import org.testng.annotations.Test;

public class SequentialTest {

 @Test
 public void firstTest() {
  System.out.println("1");
 }

 @Test
 public void secondTest() {
  System.out.println("2");
 }

 @Test
 public void thirdTest() {
  System.out.println("3");
 }
 
 @Test
 public void fouthTest() {
  System.out.println("4");
 }
}

How can we run test sequentially using TestNG in Selenium


Testng.XML


Using Testng.xml we can run test sequentially. By default, TestNG will run your tests in the order they are found in the XML file. If you want the classes and methods listed in this file to be run in an unpredictable order, set the preserve-order attribute to false.

By default preserve-order is true.


@Test (priority=" ")


Using 'priority' attribute we can run test sequentially using TestNG in Selenium. We can define priority for each test methods. Lower priorities will be scheduled first.

Thursday, 3 January 2019

Locators in Selenium | Locating Element in Selenium by Identifier

In Selenium a web element on the page should be uniquely identified by the script to perform some actions on it. Actions like Click, Select, Check, Drag Drop etc,

To Locate Element in Selenium, we can use following locators:
  • Id
  • Name
  • Class Name
  • CSS Selector
  • linkText
  • Xpath
  • tagName
  • partialLinkText

Locate Element by ID

Finding element by ID is the most common and fasted way of locating an element. ID's are mostly unique for each elements. If no element has a matching ID attribute, a NoSuchElementException will be raised.

How to find element by ID

Open Developers tool (F12) > Select an element to inspect on the page, we are inspecting 'email' field by its ID attribute as shown below:


Command in Selenium to find Element by ID

driver.findElement(By.id("login-username"));

Locate Element by Name

Finding element by Name is almost same as finding element by ID. Name of elements are not unique all the time. If more than one elements are present on the page with same name then the first element on the web page is selected. If no element has a matching name attribute, a NoSuchElementException will be thrown.

How to Element by Name

Open Developers tool (F12) > Select an element to inspect on the page, we are inspecting 'email' field by its Name attribute as shown below:


Command in Selenium to find Element by Name

driver.findElement(By.name("username"));

Locate Element by Class Name

Use this whenever you want to find element by class attribute. More than one element can have same Class Name on a Page. Probability of Class Name change is high as UI changes.  If no element has a matching Class Name attribute, a NoSuchElementException will be thrown.

How to find Element by Class Name

Open Developers tool (F12) > Select an element to inspect on the page, we are inspecting 'email' field by its Class Name of element as shown below:


Command in Selenium to find Element by Class Name

driver.findElement(By.className("phone-no"));

Locate Element by LinkText

This selector will locate link with specified text on the page no matter where it’s located.

How to find Element by LinkText

Open Developers tool (F12) > Select an element to inspect on the page, we are inspecting 'Privacy' link by its Text as shown below:


Command in Selenium to find Element by LinkText

driver.findElement(By.linkText("Privacy"));

Locate Element by partial Link Text

This selector will locate link with specified partial text on the page no matter where it’s located.

How to find Element by Partial Link Text

Open Developers tool (F12) > Select an element to inspect on the page, we are inspecting 'Privacy' link by its  Partial Text as shown below:


Command in Selenium to find Element by Partial Link Text

driver.findElement(By.partialLinkText("Priva"));

Locate Element by Xpath

When we don't have specific ID, Name attribute of an element, we can use Xpath to find that element. We can either use Relative Xpath or Absolute Xpath(from the root of HTML) of an element.
Script can fail on using Absolute Xpath as any minor changes in position of elements can break it.

How to find Element by Xpath

Open Developers tool (F12) > Select an element to inspect on the page, we are inspecting 'email' field as shown below: Right Click and Copy Xpath

//*[@id="login-username"]

Command in Selenium to find Element by Xpath

driver.findElement(By.xpath("//*[@id="login-username"]"));

Locate Element by Tag Name

A web page can contain multiple Tag of same type. So either use it when any Tag is specifically used on a web page or to get List of web elements of same type.

How to find Element by Tag Name

Open Developers tool (F12) > Select an element to inspect on the page, we are inspecting 'email' input field by its tag Input as shown below:


Command in Selenium to find Element by Tag Name

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

Locate Element by CSS Selectors

A CSS Selector is a combination of an element selector and a value which identifies the web element within a web page. If no element has a matching CSS Seelctor attribute, a NoSuchElementException will be thrown.

Types of CSS Selectors : 

  • ID
  • Class
  • Attribute
  • Sub String
  • Inner Text

How to find Element by CSS Selector

Open Developers tools(F12) > Select an element to inspect on the page as shown below:
We will use this screenshot to explain all types of CSS selectors

How to find Element by CSS Selector by ID


An element’s id in CSS is defined using: “#” - ID's must be unique within the DOM
Example from above screenshot.

#login-username

How to find Element by CSS Selector by Class


An element’s class in CSS is defined using: “.” 
Example from above screenshot.

.phone-no

How to find Element by CSS Selector by Attribute


We can use an attribute selector in selenium to choose elements based on any attribute value.
Example from above screenshot.

input[name='username']
input[id='login-username']

How to find Element by CSS Selector by Sub String


Selenium allows sub string matches using ^=, $=, or *=

  •  ^= Match a prefix
  •  $= Match a suffix
  •  *= Match a substring
Examples from above screenshot.

input[id^='login-']
input element with an “id” that starts with the text “login-”
input[id$='login-username']
input element with an “id” that ends with the text “-username”
input[id*='login']
input element with an “id” that contains the text “login”

How to find Element by CSS Selector by Inner Text


This selector will find link 'Terms' and 'Privacy' on the web page irrespective of its location.
Example from above screenshot.

 a:contains('Terms')
 a:contains('Privacy')

Command in Selenium to find Element by CSS Selector

driver.findElement(By.cssSelector("#login-username"));

driver.findElement(By.cssSelector(".phone-no"));


Wednesday, 26 December 2018

Jenkins Retry Failed Builds

We will use Jenkins Naginator Plugin to rerun the Failed/Unstable builds. This plugin allows you to automatically reschedule a build after a build failure.

Configure

  • Go to Manage Jenkins > Manage Plugin > Available Tab > Search 'Naginator' and install and restart


  • After installing the plugin, add the Post-Build action "Retry build after failure" on your Jenkins Job configuration page.


  • Set Build Retry

Above set Build retry screenshot shows that:

  • Job is enabled to rerun for unstable as well as Failure builds
  • Delay the build execution by Fixed 60 seconds
  • Maximum number of successive failed builds count is set to 3 (Limits successive failed build retries. Set to 0 for no limit.)

Friday, 3 October 2014

How To Execute JavaScript In Selenium WebDriver

Sometimes it is important to run JavaScript directly from the code to get any property of  Browser-BOM,  HTML-DOM  etc.
  • The Browser Object Model (BOM) allows JavaScript to "talk to" the browser. It includes Window, Navigator, Screen, History, Location etc.
  • With the HTML DOM, JavaScript can access and change all the elements of an HTML document. It includes Document, HTML, CSS, Elements etc.

We can execute JavaScript in 2 simple steps:

  1. Cast the WebDriver instance to a  JavascriptExecutor
    JavascriptExecutor js = (JavascriptExecutor) driver;
  2. Execute JavaScript
    js.executeScript("script for execution")

Example: