Thursday, June 27, 2024

TestNG Introduction

 TestNG  :

What is TestNG? 

TestNG is a testing framework inspired by JUnit and NUnit but designed to cover a wider range of test categories, from unit tests to integration tests. 

Why should we go for TestNG?

TestNG offers several advantages that make it a preferred choice for testing: 

You can run a single class or multiple classes.

Example: Running as a Java application

A.Java

B.Java

C.Java 

You can group test cases for different types of testing such as smoke tests, regression tests, and integration tests.

  ex:   smoke test cases

        regression test cases

      Integration test cases

 

You can run test cases based on priority

       test(priority =0)  --> 1st

       test(priority =1)  --> 2nd

 

You can run test cases in parallel--> parallel testing

             tc1

            tc2

            tc3

 

            Can run all  test cases at the same time

            tc1  6pm

            tc2  6pm

            tc3  6pm

 

TestNG allows you to generate reports and log user-defined messages.

            Java -   Syso.println("");

      Selenim -  will not generate any automation results - so we go for TestNG

 

We can perform DDT

            Data Driven Testing :  testing the application with multiple/different sets of data

 

Example 1: Login functionality with different users and passwords.        

            user     pwd

            -----------------

            ram,     mercury         

            Sita,     mercury

            Raju,   mercury

 

Example 2: Creating orders with different product names, quantities, and addresses.

                                    Productname Qty   Address

                                    ------------------------------

                                    Samsung,        2,         BNG

                                    Shampoo,        3,         CHN

                                    Santoor,          5,         HYD

 

 

 

Download testNG:

Refer: https://www.guru99.com/install-testng-in-eclipse.html

To install TestNG in Eclipse, follow these steps:

  1. Go to the Eclipse Help menu.
  2. Select Eclipse Marketplace.
  3. In the search bar, type "TestNG" and press Enter.
  4. You will see "TestNG for Eclipse" plugin. Click the Install button.
  5. Click Next.
  6. Accept the terms by selecting the Accept terms... radio button.
  7. Click Next.
  8. If any warning messages appear, click OK and continue.
  9. Restart Eclipse.

Verify TestNG Installation

To verify if TestNG is installed, follow these steps:

  1. Go to Window > Show View > Other.
  2. Search for "TestNG".
  3. If TestNG is installed, it will display the 'TestNG' option.
  4. If TestNG is not installed, it will not display the 'TestNG' option.

 

TestNG can be referred to as both a plugin and a library.

Add TestNG Library

To add the TestNG library to your project, follow these steps:

  1. Select your project.
  2. Right-click and select Properties.
  3. Go to Java Build Path.
  4. Click the Libraries tab.
  5. Click/ Select Class Path
  6. Click Add Library.
  7. Select the TestNG option and click Next.
  8. Click Apply and Close.
  9. Check the 'TestNG' library file added to your project.

To verify, check in the Project Explorer window to see if TestNG exists.

Download Files in Chrome, Firefox, etc.

 

Download Files in Chrome, Firefox, etc.

Setting Download Location in Chrome:

To set a default download location in Chrome, follow these steps:

  1. Manual Settings:
    • Open Chrome.
    • Click the three dots (menu) in the top-right corner.
    • Select Settings.
    • In the search bar, type downloads.
    • Scroll to the bottom to see Downloads settings.
    • Under Location, you will see the default download path (e.g., C:\Users\Lenovo\Downloads).
    • You can change the download location by clicking Change.
    • To prompt where to save each file before downloading, enable Ask where to save each file before downloading.

 

Note:

We can change downloads location folder name .

 

 

2.     Automated Settings with Selenium:

  • To change download settings in automation, you need to configure ChromeOptions and FirefoxOptions.

 

 

DownLoad File in default Location:

 

package UploadfileBasics;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

 

public class DownloadsFileInDefaultLocation {

 

            public static void main(String[] args) {

                        System.setProperty("webdriver.chrome.driver", ".\\Drivers\\chromedriver.exe");

 

                        //                     //open chrome browser

                        WebDriver  driver =  new ChromeDriver();               

                        driver.get("https://www.w3schools.com/howto/tryit.asp?filename=tryhow_html_download_link2");

 

                        // click Logo

//                     driver.findElement(By.xpath("//img[@alt='W3Schools']")).click();

                                               

 

                        //selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//img[@alt='W3Schools']"}

 

                        // first Check that ele is in frame, if it is in frame, we have to  swicth control to frame..

                        //   by name or id  or indexno or Webele

                        // Switch to Frame

                        driver.switchTo().frame("iframeResult");

                       

                                    // click Logo

                                    driver.findElement(By.xpath("//img[@alt='W3Schools']")).click();

 

                        //   it downloads file in default location of chrome browser ????

 

                        // Switch control to main page

                        driver.switchTo().defaultContent();

                        System.out.println("ends");

 

            }

 

}

 

 

DownLoad File in Specific folder /Project Location:

 

// set  download location for Automation

                        HashMap<String, Object> hm = new HashMap<String, Object>();

 

                        // dont want popup window Where we need to save the file -- disable popup window - asking where to save the

                        hm.put("profile.default_content_settings.popups", 0);

 

                        // To download any file in the given location

                        hm.put("download.default_directory", "C:\\brahma\\MyDownloads");

//                     C:\brahma\MyDownloads

                       

 

---------------------------

                       

package UploadfileBasics;

 

import java.util.HashMap;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.chrome.ChromeOptions;

 

public class DownloadsFileInProjectLocation {

 

            public static void main(String[] args) {

            //  download  file in this folder--C:\brahma\Practise\SelniumPractiseNew\Jul2023WorkSpace\SeleniumProjectSep72023

                       

                                    // to get the current project  directory/folder location

                                    String ProjectDirectoryName = System.getProperty("user.dir");

                                   

                                    // To download any file in the 'current Project' location

                                    // get project folder path

 

                                    //   C:\brahma\Practise\SelniumPractiseNew\March52024MyWorkspace\SeleniumMay52024\src\Downloads

                                    System.out.println("ProjectDirectoryName="+ProjectDirectoryName);

                                    // C:\brahma\Practise\SelniumPractiseNew\March52024MyWorkspace\SeleniumMay52024

 

                                    // C:\brahma\Practise\SelniumPractiseNew\March52024MyWorkspace\SeleniumMay52024\Downloads

//                                 ProjectDirectoryName =  ProjectDirectoryName + "\\Downloads";

                                   

                                    // check

                                    // set  download location for Automation

                                    HashMap<String, Object> hm = new HashMap<String, Object>();

 

                                    // dont want popup window Where we need to save the file -- disable popup window - asking where to save the

                                    hm.put("profile.default_content_settings.popups", 0);

 

                                    // To download any file in the given location

//                                 hm.put("download.default_directory", "C:\\brahma\\MyDownloads");

//                                 C:\brahma\MyDownloads

//                                 hm.put("download.default_directory", "C:\\brahma\\MyDownloads");

                                    //                     hm.put("download.default_directory", ".\\Downloads"); // dont use  --  not working

//                                 hm.put("download.default_directory", ProjectDirectoryName);

                                    hm.put("download.default_directory", ProjectDirectoryName +"\\src\\UploadfileBasics");

//                                 hm.put("download.default_directory",".\\src\\UploadfileBasics");// not working

                                   

                                   

                                    ChromeOptions chOptions  = new ChromeOptions();

                                    // accept SSL error

//                                 chOptions.setAcceptInsecureCerts(true);

                                   

                                    chOptions.setExperimentalOption("prefs", hm);     

 

 

                                    System.setProperty("webdriver.chrome.driver", ".\\Drivers\\chromedriver.exe");

                                    WebDriver driver = new ChromeDriver(chOptions);

                                    //                     //open chrome browser by passing chromeoptions  ref var

 

                                    //  while opening chrome browser, it applies these setings

                                    //                                     modifies these settings in chrome browser

                                   

 

                                    driver.get("https://www.w3schools.com/howto/tryit.asp?filename=tryhow_html_download_link2");

 

                                    // click Logo

                                    //                     driver.findElement(By.xpath("//img[@alt='W3Schools']")).click();

                                    //selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//img[@alt='W3Schools']"}

                                    // first Check that ele is in frame, if it is in frame, we have to  swicth control to frame..

                                                            //   by name or id  or indexno or Webele

                                                            // Switch to Frame

                                                            driver.switchTo().frame("iframeResult");                 

                                                                        // click Logo

                                                            driver.findElement(By.xpath("//img[@alt='W3Schools']")).click();

 

                                                            //   it downloads file in default location of chrome browser

 

                                                            // Swicthc control to main page

                                                            driver.switchTo().defaultContent();

 

 

                                    System.out.println("ends");

 

 

            }

 

}

 

HW :  Download files in project folder in Edge browser?

 

HW   Download files in project folder in Firefox browser?

SSL Error (or) Security Certificates Handling

 

FAQ: SSL Error (or) Security Certificates Handling

There are times when we open a webpage and it displays a warning saying that "This web page is not trusted/Secured." It may ask, "Do you want to continue?" This happens when the website is not secured or trusted, leading to a security certificate error.

When :

 

if website is not secured  / trusted,  it displays  Security ceritificate error

 

 

Click continue to open the web site

 

with out handling Certificate error:

 

           

package SecurityDownloadsBasics;

 

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

 

public class HandlingCertificateErrors {

 

            public static void main(String[] args) {

                        // TODO Auto-generated method stub

                        System.setProperty("webdriver.chrome.driver", ".\\Drivers\\chromedriver.exe");

                        //

                        //                     //                     //open chrome browser

                                                WebDriver  driver =  new ChromeDriver();    // Def constr           

                                                driver.get("https://expired.badssl.com/");

 

                        // Your connection is not private

                        //                     Attackers might be trying to steal your information from expired.badssl.com (for example, passwords, messages, or credit cards). Learn more

                        //                     NET::ERR_CERT_DATE_INVALID

 

                        // HW click advanced >  ClicK Proceed button

 

            }

 

}

 

                       

Handling Certificate error using "ChromeOptions":

 

// ChromeOptions-is predefined class in Selenium 

                                    //can be used to apply Different  settings/ to apply Different  options to chrome browser

                                    // // to accept Security certificate

 

 

------------------------------           

 

package UploadfileBasics;

 

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.chrome.ChromeOptions;

 

import com.sun.tools.classfile.StackMapTable_attribute.chop_frame;

 

public class HandlingCertificateErrorsUsingChromeOptions {

 

            public static void main(String[] args) {

                        System.setProperty("webdriver.chrome.driver", ".\\Drivers\\chromedriver.exe");

                        // Handle security certificate error  --> use ChromeOptions -class

                        // create obj for ChromeOptions-class

                        ChromeOptions chOptions = new ChromeOptions();

                        // accepts security certificate error -->  some method

                        chOptions.setAcceptInsecureCerts(true);

                        //   pass   true value

                       

                        //  inform browser with above settings/pass chromeoptions ref var in chromedriver

                        //                                             //                     //open chrome browser

//                     WebDriver driver=  new ChromeDriver();//  no  parameter (or) DC

                        WebDriver driver = new ChromeDriver(chOptions);

                       

                        // 1 PC

                        // while opening chrome browser - it opens with given options/ settings

 

                        driver.get("https://expired.badssl.com/");//  //  no certificate error

                       

                        // check certificate error -->  wont display SSL error now

                                   

 

                        System.out.println("ends here");

 

 

            }

 

}

 

 

-----------------

// HW handle SSL error in Edge browser, Firefox browser 

                        //                                             chrome --> ChromeOptions -class

                        //                                             edge browser --> EdgeOptions -class

                        //                                  firefox browser --> FirefoxOptions

                        //                                             Ie browser-- > InternetExplorerOptions

                       

//                     EdgeOptions

//                     FirefoxOptions

//                     InternetExplorerOptions

                       

SSL - Secure Socket Layer

 

An SSL certificate is a digital certificate that authenticates a website's identity and enables an encrypted connection.

SSL stands for Secure Sockets Layer, a security protocol that creates an encrypted link between a web server and a web browser.

 

 

Refer:

https://www.kaspersky.com/resource-center/definitions/what-is-a-ssl-certificate

 

FAQ:  How can u handle certificate error  (or) SSL error?

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...