Saturday, March 9, 2019

TestNG Tutorial | TestNG Framework with Selenium : Advantages, Annotations | TestNG VS JUnit

In this tutorial, I am going to cover the Introduction to TestNG and its uses in Selenium.



Topics to be covered?
  • What is TestNG?
  • Advantages of TestNG.
  • Annotations of TestNG.
  • Difference between JUnit and TestNG.







TestNG is a testing framework which is used to design and maintain test scripts efficiently and generate test execution results in the form of HTML report. It covers a wide range of testing from Unit testing to end-to-end testing.


Advantages - 

  1. It provides effective HTML report that explains test execution result.
  2. It allows parallel execution of test cases.
  3. It supports parameterization using @parameters annotation.
  4. It provides flexibility to prioritize test cases.
  5. It provides flexibility to ignore the execution of test cases.
  6. It provides assertions to examine the expected and actual outcome.
  7. It supports grouping of test cases.
  8. It supports Data Driven Testing using @DataProvider annotation.



Annotations in TestNG – 

  1. @BeforeSuite – Method under this annotation will execute before the execution of all the tests in the suite.
  2. @AfterSuite – Method under this annotation will execute after the execution of all the tests in the suite.
  3. @BeforeClass – Method under this annotation will execute before the execution of first @test annotation.
  4. @AfterClass – Method under this annotation will execute after the execution of all @test annotation.
  5. @BeforeMethod – Method under this annotation will execute before the execution of each @test annotation.
  6. @AfterMethod – Method under this annotation will execute after the execution of each @test annotation.
  7. @Test – Defines a particular functionality of the application.
  8. @BeforeTest – Method under this annotation will execute before the execution of first @test annotation.
  9. @AfterClass – Method under this annotation will execute after the execution of all @test annotation.

JUnit

It is also a testing framework like TestNG. However, TestNG has some more useful features compared to JUnit. So we can say TestNG is the enhanced version of JUnit.


Similarities between JUnit and TestNG

  • Annotations - Both have similar annotations(@Test, @BeforeClass, @AfterClass), Almost similar annotations(TestNG has @BeforeMethod and @AfterMethod, similarly JUnit has @Before and @After).
  • Annotation to set TimeOut - Both have @Test(timeOut = 1000) in miliseconds.
  • Annotation to Ignore Test - TestNG has @Test(enabled=true) and JUnit has @Ignore.
  • Annotation for Exception - TestNG has @Test(expectedExpecptions=ExceptionName.class) and JUnit has @Test(expected=ExceptionName.class)

Dis-similarities between JUnit and TestNG

  • Annotations - Unlike TestNG, JUnit does not provide @BeforeSuite, @AfterSuite, @BeforeTest and @AfterTest annotations.
  • TestNG provides Grouping of test cases whereas JUnit does not.
  • TestNG provides parallel execution of test cases whereas JUnit does not.
  • Achieving Parametrization in JUnit is more complex than that of TestNG.
  • TestNG provides Dependency of a method on other methods functionality of test cases whereas JUnit does not.


Sunday, January 20, 2019

How To Handle Hidden Elements in Selenium WebDriver | Working with hidden controls in Selenium

In this tutorial, we will learn how to handle Hidden Elements on the web-pages using Selenium WebDriver.




Handle Hidden Elements





Hidden elements are the elements which are not displayed on the Web-Pages, however, it is available in the HTML DOM structure.

Elements are hidden on the web-pages due to below-mentioned reasons - 
  • They have a CSS display value of none.
          style=”display: none;”
  • They are form elements with type="hidden"
  • Their width and height are explicitly set to 0.
  • An ancestor element is hidden, so the element is not shown on the page.

Issue - Selenium does not allow to interact with an element which is hidden on the web-page and if we try to do so, then it will throw ''Element Not Visible Exception".

Solution - Selenium provides one feature called 'Javascript Executor' to handle such scenario.


Note - I will take 'https://learn.letskodeit.com/p/practice' website to explain the above concept.

If you open the above link and scroll down a bit, then on the right side you will get one section as displayed in the below image.





 - There are two buttons - 'Hide' and 'Show'
 - Currently, the 'Hide/Show Example' text box is displayed.
 - Once you click on the 'Hide' button, the text box will be hidden.
 - Again you click on the 'Show' button, the text box will be displayed.


Scenario 1 - Write a program to enter text in the text box which is hidden on the web-page.  

Case 1: I will hide the text box and then simply try to enter text 'text123' in the text box. "Element Not Visible Exception" will be thrown. See the below program -


 package com.sessions;  
   
 import org.openqa.selenium.By;  
 import org.openqa.selenium.JavascriptExecutor;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.chrome.ChromeDriver;  
   
 public class HiddenElementTest {  
   
      public static void main(String[] args) throws Exception {  
     System.setProperty("webdriver.chrome.driver", "F:\\software\\chromedriver_win32\\chromedriver.exe");  
             
           WebDriver driver = new ChromeDriver();  
             
           driver.get("https://learn.letskodeit.com/p/practice");  
             
           driver.manage().window().maximize();  
             
           //Clicking on the Hide button  
           driver.findElement(By.xpath("//input[@id='hide-textbox']")).click();  
                       
           driver.findElement(By.xpath("//input[@id='displayed-text']")).sendKeys("text123");;  
             
   
   
      }  
   
 }  
   


Output -





Case 2: This time I will handle Scenario 1 using Javascript Executor. See the below program -


 package com.sessions;  
   
 import org.openqa.selenium.By;  
 import org.openqa.selenium.JavascriptExecutor;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.chrome.ChromeDriver;  
   
 public class HiddenElementTest {  
   
      public static void main(String[] args) throws Exception {  
     System.setProperty("webdriver.chrome.driver", "F:\\software\\chromedriver_win32\\chromedriver.exe");  
             
           WebDriver driver = new ChromeDriver();  
             
           driver.get("https://learn.letskodeit.com/p/practice");  
             
           driver.manage().window().maximize();  
             
           //Clicking on the Hide button  
           driver.findElement(By.xpath("//input[@id='hide-textbox']")).click();  
                       
           JavascriptExecutor js = (JavascriptExecutor)driver;  
             
           // Identifying the element using ID attribute and Entering the value in the text box  
     js.executeScript("document.getElementById('displayed-text').value='text123'");  
             
             
           }  
   
 }  
   


Explanation -

  • JavascriptExecutor - It is an interface provided by Selenium WebDriver to execute Javascript commands in the Selenium Script.
  • getElementById() - It is a Javascript method that is used to locate an element on the web-page using ID attribute of that element.
  • executeScript() - It is a method of JavascriptExecutor interface to execute the javascript commands in the Selenium Script.

Note - Since the text box is hidden, so you won't be able to see the text entered in the text box. Run the program and you will find that it runs successfully without any exception. Now,  If you click on the 'Show' button then you will see the text is entered in the box.


That's all guys!!! Do comment and post your doubts related to Selenium!!!





Sunday, December 30, 2018

How To Select Date From Calendar in Selenium WebDriver using Jquery | Handle Date Time Picker Using Selenium WebDriver

In this tutorial, I am going to explain how to select a date from a calendar of any websites.




Handle Date Time Picker In Selenium



how-to-handle-calender-using-selenium-webdriver



We often observe Date Time Picker fields on the websites mostly on E-Commerce and Travel Booking websites. Different websites use the Date Time Picker field/Calendar having different UI, but the functionality is the same. So in this tutorial, we will learn how to handle Date Time Picker field/Calendar using Selenium WebDriver.

There may be different ways to handle such a scenario. But here I will explain just one way that will cover any type of Date Time Picker field of any websites. I will use just one line of code that will handle the scenario. I will use the concept of JavaScript Executor(JQuery).

We will take 'Yatra' website to demonstrate the scenario.


 package com.sessions;  
   
 import org.openqa.selenium.JavascriptExecutor;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.chrome.ChromeDriver;  
   
   
 public class CalendarTest {  
   
      public static void main(String[] args) {  
             
           System.setProperty("webdriver.chrome.driver", "F:\\software\\chromedriver_win32\\chromedriver.exe");  
             
           WebDriver driver = new ChromeDriver();  
             
           driver.get("http://www.yatra.com/");  
             
           driver.manage().window().maximize();  
             
           //Typecating driver as a JavascriptExecutor and assigning it to the variable of JavascriptExecutor  
           JavascriptExecutor js = (JavascriptExecutor )driver;  
             
           //Putting date to the the selected date picker.  
           js.executeScript("document.getElementById('BE_flight_origin_date').value='03/10'");  
   
      }  
   
 }  
   


Explanation

  • Javascript Executor is an Interface provided by Selenium WebDriver which is used to run Javascript commands from Selenium script.
  • This Interface provides a method executeScript() - to run the Javascript commands.
  • We need to import one package called org.openqa.selenium.JavascriptExecutor.
  • Since the driver can execute only selenium commands so we need to typecast driver as a JavascriptExecutor so that Javascript commands can be run from Selenium script.  
  • document.getElementById('BE_flight_origin_date').value='03/10'
Here, getElementById('value of the Id attribute') is used to locate element (date picker field) on the webpage using ID attribute and then we are setting date value to the field. We can inspect and get the Id attribute as we do in Selenium.

In case you do not know how to inspect element, follow the link - Inspect Element In Selenium



Note - Before setting the date value, first verify in which format the field is accepting the date value. Based on the format provide the value in the script. In our case format is - '03/10'.


As I said just one line of code and everything is done!!!


General Steps
  1. Define JavascriptExecutor interface variable and typecast the driver.
  2. execute the command by locating the date picker field using its Id attribute and set the value based on the acceptable date format.


That's all guys!!! Do comment and post your doubts related to Selenium!!!