Selenium Practice Programs

 ACTION

package Selenium;


import java.time.Duration;


import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.interactions.Actions;


import io.github.bonigarcia.wdm.WebDriverManager;


public class Action {


public static void main(String[] args) throws InterruptedException {

Action ac= new Action();

ac.actionTest();

}


public void actionTest() throws InterruptedException {

WebDriverManager.chromedriver().setup();

WebDriver driver = new ChromeDriver();

//Implicit wait

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

driver.get("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login");

driver.manage().window().maximize();

Actions ac = new Actions(driver);

//ac.moveToElement(null).perform();

//ac.contextClick().perform();

//ac.dragAndDrop(null, null).perform();

//ac.doubleClick().perform();

//ac.clickAndHold(null).perform();

String attr=driver.findElement(By.xpath("//p[normalize-space()='Username : Admin']")).getAttribute("class");

System.out.println(attr);

driver.close();

}


}

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

ACTION SLIDER

package Selenium;


import java.time.Duration;


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;


import io.github.bonigarcia.wdm.WebDriverManager;


public class ActionSlider {


public static void main(String[] args) throws InterruptedException {

ActionSlider ac= new ActionSlider();

ac.actionSliderTest();

}


public void actionSliderTest() throws InterruptedException {

WebDriverManager.chromedriver().setup();

WebDriver driver = new ChromeDriver();

//Implicit wait

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

driver.get("https://testautomationpractice.blogspot.com/");

driver.manage().window().maximize();

Thread.sleep(4000);

//Minimum Slider

Actions ac = new Actions(driver);

WebElement minSlider =driver.findElement(By.xpath("//div[@id='HTML7']//span[1]"));

System.out.println(minSlider.getLocation()); //812 2025

Thread.sleep(2000);

ac.dragAndDropBy(minSlider, 30, 0).perform();

Thread.sleep(2000);

System.out.println(minSlider.getLocation());//912 2025

//Maximum slider

WebElement maxSlider =driver.findElement(By.xpath("//div[@id='HTML7']//span[2]"));

System.out.println(maxSlider.getLocation()); //812 2025

Thread.sleep(2000);

ac.dragAndDropBy(maxSlider, -20, 0).perform();

Thread.sleep(2000);

System.out.println(maxSlider.getLocation());//912 2025

driver.close();

}


}

------------------------------
ALERT

package Selenium;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import io.github.bonigarcia.wdm.WebDriverManager;


public class Alerts {
public static void main(String[] args) throws InterruptedException {
Alerts wh= new Alerts();
wh.AlertTest();
}

public void AlertTest() throws InterruptedException {
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
//Implicit wait
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("https://testautomationpractice.blogspot.com/");
driver.manage().window().maximize();
//Simple Alert
/*WebElement simpleAlert =driver.findElement(By.xpath("//button[@id='alertBtn']"));
simpleAlert.click();
Thread.sleep(2000);
Alert myAlert =driver.switchTo().alert();
String alertText =myAlert.getText();
if (alertText.equals("I am an alert box!"))
{
System.out.println("Text matches");
}
 
driver.switchTo().alert().accept();
Thread.sleep(2000);*/
//Prompting Alert
driver.findElement(By.xpath("//button[@id='promptBtn']")).click();
Alert myAlert2 =driver.switchTo().alert();
myAlert2.sendKeys("Amit Singh");
Thread.sleep(2000);
myAlert2.accept();
Thread.sleep(2000);
//Switching to Alert using Explicit wait
/*driver.findElement(By.xpath("//button[@id='promptBtn']")).click();
WebDriverWait myWait = new WebDriverWait(driver, Duration.ofSeconds(10));
Alert myAlert =myWait.until(ExpectedConditions.alertIsPresent());
myAlert.sendKeys("Amit Singh");
Thread.sleep(2000);
myAlert.accept();
Thread.sleep(2000);*/
// Authentication Aler popup
//driver.get("https://admin:admin@the-internet.herokuapp.com/basic_auth");
//driver.manage().window().maximize();
//Thread.sleep(4000);
driver.close();
 
 
}
}
------------------------------
ASSERTIONS

package Selenium;

import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;

public class Assertions {

@Test
public void hardAssertion() {
// Assertion for all types of data types
Assert.assertEquals("String","String");
}
@Test
public void softAssertion()
{
SoftAssert sa= new SoftAssert(); // Soft Assertions methods need to be accessed via object
sa.assertEquals("String","String1");
sa.assertAll(); //Mandatory for soft Assertions
}

}

---------------------------------------
BROKEN LINKS

package Selenium;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

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.chrome.ChromeOptions;

import io.github.bonigarcia.wdm.WebDriverManager;


public class BrokenLinks {
public static void main(String[] args) throws InterruptedException, IOException {
ChromeOptions options = new ChromeOptions();
options.addArguments("Headless=New");
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver(options);
//Implicit wait
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("https://testautomationpractice.blogspot.com/");
driver.manage().window().maximize();
//1) Find all links
List<WebElement> links =driver.findElements(By.xpath("//a"));
System.out.println("Total number of links are: " +links.size());
//2) Read all the links and consider only which has href tags
int counter =0;
for(WebElement link:links)
{
String attr=link.getAttribute("href");
if (attr == null || attr.isEmpty())
{
System.out.println("----------------------------Not possible to check as required field i empty");
continue;
}
//3) Hit URL to the server by crating a connection
URL linkURL = new URL(attr); //convert String to URL
HttpURLConnection conn=(HttpURLConnection) linkURL.openConnection();//Open connection to server
conn.connect(); // Connect to server and send request to server
//Compare response code
if (conn.getResponseCode()>=400)
{
System.out.println(linkURL +"--------------------------Broken Link" );
counter = counter +1;
}
else
{
System.out.println(linkURL +"-------------------------It is not a broken link");
}
}
System.out.println("Total number of broken links are: " +counter);
}
}


---------------------------------------------
CALENDAR
package Selenium;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import io.github.bonigarcia.wdm.WebDriverManager;


public class Calendar {
public static void main(String[] args) throws InterruptedException {
Calendar cal= new Calendar();
cal.calendarText();
}

public void calendarText() throws InterruptedException {
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
//Implicit wait
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("https://www.globalsqa.com/demo-site/datepicker/");
driver.manage().window().maximize();
//Switch to frame
WebElement frm=driver.findElement(By.xpath("(//iframe[@class='demo-frame'])[1]"));
driver.switchTo().frame(frm);
// Click on date picker
driver.findElement(By.xpath("//input[@id='datepicker']")).click();
//Input for selecting date
String month = "July";
    String year ="2026";
    String date ="10";
while(true)
{
String currentMonth =driver.findElement(By.xpath("//span[@class='ui-datepicker-month']")).getText();
String currentYear=driver.findElement(By.xpath("//span[@class='ui-datepicker-year']")).getText();
if(currentMonth.equals(month) && currentYear.equals(year))
{
break;
}
driver.findElement(By.xpath("//span[@class='ui-icon ui-icon-circle-triangle-e']")).click();
}
List <WebElement> dates =driver.findElements(By.xpath("//tbody//tr//td//a"));
for(int i=0; i<dates.size(); i++)
{
String dt =dates.get(i).getText();
if(dt.equals(date))
{
dates.get(i).click();
Thread.sleep(3000);
break;
}
}
driver.quit();
 
 
 
}
}

---------------------------------------
CHECKBOXES

package Selenium;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import io.github.bonigarcia.wdm.WebDriverManager;


public class Checkboxes {
public static void main(String[] args) throws InterruptedException {
Checkboxes wh= new Checkboxes();
wh.CheckboxTest();
}

public void CheckboxTest() throws InterruptedException {
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
//Implicit wait
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("https://testautomationpractice.blogspot.com/");
driver.manage().window().maximize();
//Select all checkboxes
List <WebElement> checkBoxes = driver.findElements(By.xpath("//input[@class='form-check-input' and @type='checkbox']"));
 
for(int i=0; i<checkBoxes.size(); i++)
{
checkBoxes.get(i).click();
}
Thread.sleep(2000);
driver.quit();
driver.quit();
 
 
}
}

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

DATA DRIVEN TESTING USING EXCEL

package Selenium;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import io.github.bonigarcia.wdm.WebDriverManager;


public class DataDrivenTestingExcel {
public static void main(String[] args) throws InterruptedException, IOException {
DataDrivenTestingExcel de= new DataDrivenTestingExcel();
//de.readExcel();
//de.writeExcel();
de.writeExcelDynamic();
}
public void readExcel() throws IOException
{
//Reading data from excel
//1) Open file
FileInputStream fis = new FileInputStream(System.getProperty("user.dir")+"\\TestData\\TestData.xlsx");
//2) Open Workbook
XSSFWorkbook book = new XSSFWorkbook(fis);
//3) Open Sheet
XSSFSheet sheet=book.getSheet("Sheet1");
//4) Find number of Rows
int rows =sheet.getLastRowNum();
//5) Find number of cells from any row
int cells=sheet.getRow(1).getLastCellNum();
//6) Iterate the row and cell by finding current Row and Current cell
//Row count starts with 0 and cell count starts with 1
for(int r=0;r<=rows;r++)
{
XSSFRow row =sheet.getRow(r);
for(int c=0;c<cells;c++)
{
System.out.print(row.getCell(c).toString() +"\t"); 
}
System.out.println();
}
//7) close the workbook
book.close();
//8) Close the file
fis.close();
}
public void writeExcel() throws IOException
{
//Writing data into Excel
//1) Create File
FileOutputStream fos = new FileOutputStream(System.getProperty("user.dir")+"\\TestData\\TestOutput.xlsx");
//2) Create workbook
XSSFWorkbook book = new XSSFWorkbook();
//3) Create worksheet
XSSFSheet sheet =book.createSheet("Sheet01");
//4) Create Row
XSSFRow row1=sheet.createRow(0);
//5) Create Cell
row1.createCell(0).setCellValue("Name");
row1.createCell(1).setCellValue("Mobile");
//6) Add workbook to file
book.write(fos);
//7) Close workbook
book.close();
//8) Close file
fos.close();
}
public void writeExcelDynamic() throws IOException
{
//Writing data into Excel
//1) Create File
FileOutputStream fos = new FileOutputStream(System.getProperty("user.dir")+"\\TestData\\TestOutput.xlsx");
//2) Create workbook
XSSFWorkbook book = new XSSFWorkbook();
//3) Create worksheet
XSSFSheet sheet =book.createSheet("Sheet01");
//4) Take Row and cell size from user
Scanner sc = new Scanner(System.in);
System.out.println("Enter Number of rows");
int noOfRows = sc.nextInt();
System.out.println("Enter Number of cells");
int noOfCells = sc.nextInt();
//5) Take data from user and add it in excel using for loop
for(int r=0; r<=noOfRows; r++)
{
XSSFRow row =sheet.createRow(r);
for(int c=0; c<noOfCells; c++)
{
XSSFCell cell =row.createCell(c);
cell.setCellValue(sc.next());
}
}
//6) Add workbook to file
book.write(fos);
//7) Close workbook
book.close();
//8) Close file
fos.close();
}
}

--------------------------------------------
DROP DOWN

package Selenium;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import io.github.bonigarcia.wdm.WebDriverManager;


public class DropDown {
public static void main(String[] args) throws InterruptedException {
DropDown dd= new DropDown();
dd.dropDownTest();
}

public void dropDownTest() throws InterruptedException {
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
//Implicit wait
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
/*driver.get("https://getbootstrap.com/docs/4.1/components/dropdowns/");
driver.manage().window().maximize();
//bootstrap dropdown implementation
driver.findElement(By.xpath("//button[@id='dropdownMenuButton']")).click();
Thread.sleep(3000);
//driver.findElement(By.xpath("//div[@class='dropdown-menu show']//a[@class='dropdown-item'][normalize-space()='Action']")).click();
List<WebElement> dropDowns =driver.findElements(By.xpath("//div[@class='dropdown show']//div//a"));
System.out.println(dropDowns.size());
 
for (int i=0;i<dropDowns.size();i++)
{
System.out.println(dropDowns.get(i).getText()); 
}
 
for(WebElement ddList:dropDowns)
{
System.out.println(ddList.getText());
}*/
//Dropdown for Auto Search
driver.get("https://www.google.com");
driver.manage().window().maximize();
driver.findElement(By.xpath("//textarea[@id='APjFqb']")).sendKeys("Selenium");
List <WebElement> options = driver.findElements(By.xpath("//div[@class='aajZCb']//div[@id='Alh6id']//ul/li"));
System.out.println(options.size());
Thread.sleep(2000);
/*for (int i=0; i<options.size();i++)
{
if(options.get(i).getText().equals("selenium"))
{
options.get(i).click();
break;
}
}*/
for( WebElement option : options)
{
//System.out.println(option.getText());
String text = option.getText();
if (text.equals("selenium"))
{
option.click();
break;
}
}
Thread.sleep(4000);
driver.close();
 
 
}
}
--------------------------------------------------------

DROP DOWN CALENDAR

package Selenium;

import java.time.Duration;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import io.github.bonigarcia.wdm.WebDriverManager;

public class DropDownCalendar 
{
public static void main(String[]args) throws InterruptedException
{
DropDownCalendar ac =new DropDownCalendar();
ac.advancedCalendarTest();
}
public void advancedCalendarTest() throws InterruptedException {
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
//implicit wait
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("https://www.globalsqa.com/demo-site/datepicker/");
driver.manage().window().maximize();
//Input
String year = "2026";
String month = "June";
String Date ="30";
driver.findElement(By.xpath("//li[@id='DropDown DatePicker']")).click();
//Switch to frame
WebElement frm=driver.findElement(By.xpath("//div[@class='single_tab_div resp-tab-content resp-tab-content-active']//iframe[@class='demo-frame']"));
driver.switchTo().frame(frm);
driver.findElement(By.xpath("//input[@id='datepicker']")).click();
}


}

------------------------------------------
ENABLE EXTENSIONS

package Selenium;

import java.io.File;
import java.time.Duration;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class EnableExtensions {

public static void main(String[] args) throws InterruptedException {
ChromeOptions options = new ChromeOptions();
File file = new File ( "C:\\Users\\AmitkumarSingh\\Desktop\\TR\\SelectorsHub-Chrome-Web-Store.crx");
options.addExtensions(file);
WebDriver driver = new ChromeDriver(options);
//Implicit wait
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("https://testautomationpractice.blogspot.com/");
driver.manage().window().maximize();

}
}
-----------------------------------------------------

FRAMES


package Selenium;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import io.github.bonigarcia.wdm.WebDriverManager;


public class Frames {
public static void main(String[] args) throws InterruptedException {
Frames fr= new Frames();
fr.FrameTest();
}

public void FrameTest() throws InterruptedException {
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
//Implicit wait
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("https://ui.vision/demo/webtest/frames/");
driver.manage().window().maximize();
WebElement frame4 = driver.findElement(By.xpath("//frame[@src='frame_4.html']"));
driver.switchTo().frame(frame4);
driver.findElement(By.xpath("//input[@name='mytext4']")).sendKeys("Welcome to frame 4");
Thread.sleep(2000);
driver.switchTo().defaultContent();
WebElement frame1 = driver.findElement(By.xpath("//frame[@src='frame_1.html']"));
driver.switchTo().frame(frame1);
driver.findElement(By.xpath("//input[@name='mytext1']")).sendKeys("Welcome to frame 1");
Thread.sleep(2000);
driver.close();
 
 
}
}
-------------------------------------------------------

HEADLESS

package Selenium;

import java.io.File;
import java.time.Duration;

import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;


public class Headless {
public static void main(String[]args)
{
headlessTest();
}

public static  void headlessTest() {
// Running the script in headless mode
ChromeOptions options =new ChromeOptions();
options.addArguments("headless=New");
WebDriver driver = new ChromeDriver(options);
driver.get("https://gst.gov.in");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
// Take screenshot using TakesScreenshot interface
TakesScreenshot sc = (TakesScreenshot)driver;
File source =sc.getScreenshotAs(OutputType.FILE);
File target = new File("C:\\Users\\AmitkumarSingh\\eclipse-workspace\\Selenium\\Screenshots\\gst.png");
source.renameTo(target);
driver.close();
}

}

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

JAVA SCRIP EXECUTOR


package Selenium;


import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import io.github.bonigarcia.wdm.WebDriverManager;

public class JavascriptExecutorDemo {
public static void main (String[] args) throws InterruptedException
{
JavascriptExecutorDemo kb = new JavascriptExecutorDemo();
kb.JavascriptExecutorTest();
}

public void JavascriptExecutorTest() throws InterruptedException 

{
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.get("https://testautomationpractice.blogspot.com/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
WebElement name =driver.findElement(By.xpath("//input[@id='name']"));
// JavascriptExecutor is an interface
// Action methods doesn't work sometimes, to fix code need to be added in JS
// Perform typecasting
// call executeScriptmethod
// Script to add Name
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].setAttribute('value','Amit')",name);
// Script to click
WebElement checkBox =driver.findElement(By.xpath("//input[@id='sunday']"));
js.executeScript("arguments[0].click()",checkBox);
//script to scroll page
WebElement scroll=driver.findElement(By.xpath("//input[@id='input1']"));
js.executeScript("arguments[0].scrollIntoView()",scroll);
//change the resolution of the page
js.executeScript("document.body.style.zoom='200%'");
Thread.sleep(3000);
driver.quit();
}

}
;
--------------------------------------------------------------

KEYBOARD ACTIONS

package Selenium;


import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import io.github.bonigarcia.wdm.WebDriverManager;

public class Keyboard {
public static void main (String[] args) throws InterruptedException
{
Keyboard kb = new Keyboard();
kb.keyboardTest();
}

public void keyboardTest() throws InterruptedException 

{
//WebDriverManager automatically detects the OS and downloads webdrives and cache it
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.get("https://text-compare.com/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
// Take locator of source
// Take locator of target
// Add text in source
// Select All using CTRL+A
// Copy using CTRL+C
// Press TAB
// Paste using CTRL+V
driver.findElement(By.xpath("//textarea[@class='inputText' and @id='inputText1']")).sendKeys("Welcome to the Selenium");
Actions ac = new Actions(driver);
ac.keyDown(Keys.CONTROL).sendKeys("A").keyUp(Keys.CONTROL).perform();
ac.keyDown(Keys.CONTROL).sendKeys("C").keyUp(Keys.CONTROL).perform();
ac.keyDown(Keys.TAB).keyDown(Keys.UP).perform();
ac.keyDown(Keys.CONTROL).sendKeys("V").keyUp(Keys.CONTROL).perform();
WebElement feedback =driver.findElement(By.xpath("//a[normalize-space()='Feedback']"));
//Open URL in new browser
ac.keyDown(Keys.CONTROL).click(feedback).keyUp(Keys.CONTROL).perform();
Thread.sleep(3000);
Set <String>winhandles =driver.getWindowHandles();
List <String> handles = new ArrayList(winhandles);
String activeHandle =handles.get(1);
driver.switchTo().window(activeHandle);
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.numberOfWindowsToBe(2));
WebElement feedbackMess =driver.findElement(By.xpath("//textarea[@id='id_message']"));
feedbackMess.sendKeys("I am happy");
Thread.sleep(2000);
driver.quit();
}

}
;
------------------------------------------
PAGINATION TABLE

package Selenium;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import io.github.bonigarcia.wdm.WebDriverManager;


public class PaginationTable {
public static void main(String[] args) throws InterruptedException {
PaginationTable tb= new PaginationTable();
tb.paginationTableTest();
}

public void paginationTableTest() throws InterruptedException {
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
//Implicit wait
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("https://ecommerce-playground.lambdatest.io/index.php?route=product/category&path=25_28");
driver.manage().window().maximize();
//Find page count
String  pageCount =driver.findElement(By.xpath("//div[contains(text(),'Showing')]")).getText();
System.out.println(pageCount);
int page=Integer.parseInt((pageCount.substring(pageCount.indexOf("(")+1,pageCount.indexOf("Pages")-1)));
//Click on all pages
for(int i=1; i<page; i++)
{
WebElement activePage =driver.findElement(By.xpath("//ul[@class='pagination']//*[text()="+i+"]"));
activePage.click();
//Find product names
List<WebElement> productName =driver.findElements(By.xpath("//a[@class=\"text-ellipsis-2\"]"));
//Find product price
List<WebElement> productPrice =driver.findElements(By.xpath("//span[@class=\"price-new\"]"));
//Display Product name and product price
for (int j=0;j<productName.size();j++)
{
System.out.print(productName.get(j).getText()+" - "+productPrice.get(j).getText()+"\tab"); 
}
System.out.println();
}
driver.quit();
 
 
 
}
}


-----------------------------------------------------------
READ PROPERTIES


package Selenium;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.Properties;

public class ReadProperties {

public static void main(String[]args) throws IOException
{
FileInputStream fis = new FileInputStream(System.getProperty("user.dir")+"\\TestData\\Property.properties");
Properties prop = new Properties();
prop.load(fis);
prop.getProperty("url");
System.out.println(prop.getProperty("URL"));
Collection<Object> values =prop.values();
System.out.println(values);
fis.close();
}

}

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

SCREENSHOTS

package Selenium;

import java.io.File;
import java.time.Duration;

import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Screenshot {
public static void main(String[]args)
{
screenshotTest();
}

public static  void screenshotTest() {
WebDriver driver = new ChromeDriver();
driver.get("https://gst.gov.in");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
TakesScreenshot sc = (TakesScreenshot)driver;
File source =sc.getScreenshotAs(OutputType.FILE);
File target = new File("C:\\Users\\AmitkumarSingh\\eclipse-workspace\\Selenium\\Screenshots\\ss.png");
source.renameTo(target);
driver.close();
}

}
-------------------------------------------------------------------

SHADOW DOM

package Selenium;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import io.github.bonigarcia.wdm.WebDriverManager;


public class ShadowDom {
public static void main(String[] args) throws InterruptedException, IOException {
//ChromeOptions options = new ChromeOptions();
//options.addArguments("Headless=New");
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
//Implicit wait
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("https://practice.expandtesting.com/shadowdom");
driver.manage().window().maximize();
//This Element is inside single shadow DOM.
SearchContext shadow = driver.findElement(By.cssSelector("#shadow-host")).getShadowRoot();
Thread.sleep(1000);
shadow.findElement(By.cssSelector("#my-btn")).click();
Thread.sleep(4000);
driver.close();
}
}


;
-------------------------------------------------------------------

SVG ELEMENTS

package Selenium;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import io.github.bonigarcia.wdm.WebDriverManager;


public class SVGElements {
public static void main(String[] args) throws InterruptedException, IOException {
//ChromeOptions options = new ChromeOptions();
//options.addArguments("Headless=New");
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
//Implicit wait
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("https://testautomationpractice.blogspot.com/");
driver.manage().window().maximize();
// name attribute is used for SVG element, normal xpath doesn't works here
boolean svg =driver.findElement(By.xpath("//*[name()='circle' and contains(@cx,'15')]")).isDisplayed();
System.out.println(svg);
driver.close();
}
}


;
----------------------------------------------------------------------

TABLE

package Selenium;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import io.github.bonigarcia.wdm.WebDriverManager;


public class Table {
public static void main(String[] args) throws InterruptedException {
Table tb= new Table();
tb.TableTest();
}

public void TableTest() throws InterruptedException {
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
//Implicit wait
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("https://www.gst.gov.in/");
driver.manage().window().maximize();
//Find Rows
List<WebElement> rows =driver.findElements(By.xpath("//table[@class='col-xs-12 panl newse']//tr"));
int rowCount =rows.size();
System.out.println(rowCount);
//Find Columns
List<WebElement> columns =driver.findElements(By.xpath("//table[@class='col-xs-12 panl newse']//tr[1]//td"));
int columnCount =columns.size();
System.out.println(columnCount);
//Print all values from the table
/*for(int r=1;r<=rowCount; r++ )
{
for(int j=1;j<=columnCount; j++)
{
String value =driver.findElement(By.xpath("//table[@class='col-xs-12 panl newse']//tr["+r+"]//td["+j+"]")).getText();
System.out.println(value);
}
System.out.println();
}*/
//print data where date is Apr 16th, 2026 (Second Row)
for (int r=1;r<=rowCount; r++)
{
String date =driver.findElement(By.xpath("//table[@class='col-xs-12 panl newse']//tr["+r+"]//td[1]")).getText();
if (date.contains("Apr 16th, 2026"))
{
String data = driver.findElement(By.xpath("//table[@class='col-xs-12 panl newse']//tr["+r+"]//td[1]")).getText();
System.out.println(data);
}
}
driver.quit();
 
 
 
}
}

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

UPLOAD FILES


package Selenium;

import java.time.Duration;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class UploadFiles {


public static void main(String[] args) throws InterruptedException {
WebDriver driver = new ChromeDriver();
//Implicit wait
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("https://testautomationpractice.blogspot.com/");
driver.manage().window().maximize();
String file1 = "C:\\Users\\AmitkumarSingh\\Desktop\\PNG_Connectin.txt";
String file2 = "C:\\Users\\AmitkumarSingh\\Desktop\\Addresses.txt";
driver.findElement(By.xpath("//input[@id='multipleFilesInput']")).sendKeys(file1 +"\n"+ file2);
Thread.sleep(6000);
driver.close();
}

}

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

VERIFY TITLE

package Selenium;

import java.time.Duration;
import java.util.List;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;

import io.github.bonigarcia.wdm.WebDriverManager;

public class VerifyTitle {
public static void main(String[] args) throws InterruptedException {
VerifyTitle vt= new VerifyTitle();
vt.allTest();
}

public void allTest() throws InterruptedException {
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
//Implicit wait
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login");
driver.manage().window().maximize();
System.out.println (driver.getTitle());
System.out.println (driver.getWindowHandle());
//Explicit wait
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement orangeHRM =wait.until(ExpectedConditions.visibilityOfElementLocated((By.xpath("//a[normalize-space()='OrangeHRM, Inc']"))));
orangeHRM.click();
//Fluent wait
Wait<WebDriver> fwait =new FluentWait<WebDriver>(driver)
.withTimeout(Duration.ofSeconds(10))
.pollingEvery(Duration.ofSeconds(10));
 
//WebElement enterName =driver.findElement(By.cssSelector("[placeholder='Enter Name']"));
//enterName.sendKeys("David");
//WebElement search =driver.findElement(By.xpath("//input[@class='wikipedia-search-input'][@id=\"Wikipedia1_wikipedia-search-input\"]"));
//search.sendKeys("Search");
//Thread.sleep(2000);
//WebElement orangeHRM =driver.findElement(By.xpath("//a[normalize-space()='OrangeHRM, Inc']"));
//orangeHRM.click();
Set <String>winhan = driver.getWindowHandles();
System.out.println(winhan);
driver.quit();
    }
}



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

WINDOW HANDLE

package Selenium;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;

import io.github.bonigarcia.wdm.WebDriverManager;

public class WindowHandle {
public static void main(String[] args) throws InterruptedException {
WindowHandle wh= new WindowHandle();
wh.windowHandleTest();
}

public void windowHandleTest() throws InterruptedException {
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
//Implicit wait
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login");
driver.manage().window().maximize();
WebElement orangeHRM =driver.findElement(By.xpath("//a[normalize-space()='OrangeHRM, Inc']"));
orangeHRM.click();
 
//Approach1
Set <String>windowIds =driver.getWindowHandles();
List <String> windowLists = new ArrayList(windowIds);
String parentWindow = windowLists.get(0);
String childWindow = windowLists.get(1);
 
driver.switchTo().window(childWindow);
System.out.println(driver.getTitle());
 
//Approach2
 
for (String windowID:windowIds)
{
String windowTitle =driver.switchTo().window(windowID).getTitle();
 
if (windowTitle.equals("OrangeHRM: All in One HR Software for Businesses"))
{
System.out.println("User is on child page");
}
else
{
System.out.println(driver.getTitle());
}
 
}
 
driver.quit();
 

    }
}




----------------------------------------
UTILITY - DATA DRIVEN USECASE

package Utility;
import java.io.IOException;
import java.time.Duration;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.Select;

import io.github.bonigarcia.wdm.WebDriverManager;

public class DataDrivenUsecase {

public static void main(String[]args) throws IOException, InterruptedException
{
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
//Implicit wait
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("https://www.sbisecurities.in/calculators/fd-calculator");
driver.manage().window().maximize();
String fileLocation = System.getProperty("user.dir")+"\\TestData\\CalculatorTestData.xlsx";
//1) Find row count using existing util
int rowCount=ExcelUtils.getRowCount(fileLocation, "Sheet1");
//2) Read data from excel
for(int r=1;r<=rowCount;r++)
{
String fdInvestment =ExcelUtils.getCellData(fileLocation, "Sheet1", r,0);
String numOfYears =ExcelUtils.getCellData(fileLocation, "Sheet1", r,1);
String intRate =ExcelUtils.getCellData(fileLocation, "Sheet1", r,2);
String compFreq =ExcelUtils.getCellData(fileLocation, "Sheet1", r,3);
String expRet =ExcelUtils.getCellData(fileLocation, "Sheet1", r,4);
//3) Pass data into application
driver.findElement(By.xpath("//div[@class='input-group']//input[@id='input_fd_investment']")).clear();
driver.findElement(By.xpath("//div[@class='input-group']//input[@id='input_fd_investment']")).sendKeys(fdInvestment);
driver.findElement(By.xpath("//body/app-root/app-fd-calculator[@class='ng-star-inserted']/div[@class='container']/div[@class='col-md-12 pt-5 mt-5']/div[@class='row pdt-30']/div[@class='col-xl-9 col-lg-7 col-md-7']/div[@class='border']/form[@id='validForm']/div[@class='row mt-2 d-flex']/div[@class='form-group col-md-6 mb-3']/input[1]")).clear();
driver.findElement(By.xpath("//body/app-root/app-fd-calculator[@class='ng-star-inserted']/div[@class='container']/div[@class='col-md-12 pt-5 mt-5']/div[@class='row pdt-30']/div[@class='col-xl-9 col-lg-7 col-md-7']/div[@class='border']/form[@id='validForm']/div[@class='row mt-2 d-flex']/div[@class='form-group col-md-6 mb-3']/input[1]")).sendKeys(numOfYears);
driver.findElement(By.xpath("//input[@id='input_interest']")).clear();
driver.findElement(By.xpath("//input[@id='input_interest']")).sendKeys(intRate);
Select dd = new Select(driver.findElement(By.xpath("//select[@id='frequency']")));
dd.selectByVisibleText(compFreq);
driver.findElement(By.xpath("//button[normalize-space()='Calculate']")).click();
//4) Compare results
String actualRet=driver.findElement(By.xpath("//div[@id='lumpsum_return']//p[3]//span")).getText();
Double actualRetConv = Double.parseDouble(actualRet.replace("₹", "").replace(",", "").trim());
System.out.println(actualRet);
System.out.println(expRet);
System.out.println(Double.parseDouble(expRet));
System.out.println(actualRetConv);
if (Double.parseDouble(expRet)==actualRetConv)
{
System.out.println("Pass");
ExcelUtils.putCellData(fileLocation, "Sheet1", r,6,"Pass");
}
else
{
System.out.println("Fail");
ExcelUtils.putCellData(fileLocation, "Sheet1", r,6,"Fail");
}
}
driver.close();
}

}

---------------------------------------------------
EXCEL UTILITY

package Utility;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelUtils {

public static int getRowCount(String fileLocation, String sheetName) throws IOException
{
FileInputStream fis =new FileInputStream(fileLocation);
XSSFWorkbook book = new XSSFWorkbook(fis);
XSSFSheet sheet =book.getSheet(sheetName);
int rowCount =sheet.getLastRowNum();
book.close();
fis.close();
return rowCount;
}
public static String getCellData(String fileLocation, String sheetName, int rowNum, int colNum) throws IOException
{
FileInputStream fis =new FileInputStream(fileLocation);
XSSFWorkbook book = new XSSFWorkbook(fis);
XSSFSheet sheet =book.getSheet(sheetName);
XSSFRow row =sheet.getRow(rowNum);
XSSFCell cell= row.getCell(colNum);
String data =cell.toString();
book.close();
fis.close();
return data;
}
public static void putCellData(String fileLocation, String sheetName, int rowNum, int cellNum, String data) throws IOException
{
FileInputStream fis =new FileInputStream(fileLocation);
XSSFWorkbook book = new XSSFWorkbook(fis);
XSSFSheet sheet =book.getSheet(sheetName);
XSSFRow row =sheet.getRow(rowNum);
XSSFCell cellNumber=row.createCell(cellNum);
cellNumber.setCellValue(data);
FileOutputStream fos = new FileOutputStream(fileLocation);
book.write(fos);
book.close();
fis.close();
fos.close();
}

}

-------------------------------------------------
TEST NG PRACTICE

package TestNG;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.Assert;
import org.testng.annotations.Test;

public class CheckTitle {
WebDriver driver;


@Test(priority=1,groups= {"smoke"})
public void login()
{
//ChromeOptions options =new ChromeOptions();
//options.addArguments("headless=true");
//driver = new ChromeDriver(options);
//driver.get("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login");
Assert.assertTrue(true);
}
@Test(priority=2,dependsOnMethods={"login"}, groups= {"smoke"})
public void checkTitle()
{
System.out.println("Title");
Assert.assertTrue(true);
}
@Test(priority=3)
public void logout()
{
System.out.println("Logout");
Assert.assertTrue(true);
}

}
--------------------------
package TestNG;

import static org.testng.Assert.assertTrue;

import java.time.Duration;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.testng.annotations.DataProvider;


import io.github.bonigarcia.wdm.WebDriverManager;

public class DataProviderDemo {
WebDriver driver;
@BeforeClass
public void openBrowser()
{
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
//Implicit wait
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("https://www.sbisecurities.in/calculators/fd-calculator");
driver.manage().window().maximize();
Assert.assertTrue(true);
}
@Test(priority=1,dataProvider="dp")
public void bankCalculator(String fdInvestment, String numOfYears, String intRate, String compFreq) throws InterruptedException
{
driver.findElement(By.xpath("//div[@class='input-group']//input[@id='input_fd_investment']")).clear();
driver.findElement(By.xpath("//div[@class='input-group']//input[@id='input_fd_investment']")).sendKeys(fdInvestment);
driver.findElement(By.xpath("//body/app-root/app-fd-calculator[@class='ng-star-inserted']/div[@class='container']/div[@class='col-md-12 pt-5 mt-5']/div[@class='row pdt-30']/div[@class='col-xl-9 col-lg-7 col-md-7']/div[@class='border']/form[@id='validForm']/div[@class='row mt-2 d-flex']/div[@class='form-group col-md-6 mb-3']/input[1]")).clear();
driver.findElement(By.xpath("//body/app-root/app-fd-calculator[@class='ng-star-inserted']/div[@class='container']/div[@class='col-md-12 pt-5 mt-5']/div[@class='row pdt-30']/div[@class='col-xl-9 col-lg-7 col-md-7']/div[@class='border']/form[@id='validForm']/div[@class='row mt-2 d-flex']/div[@class='form-group col-md-6 mb-3']/input[1]")).sendKeys(numOfYears);
driver.findElement(By.xpath("//input[@id='input_interest']")).clear();
driver.findElement(By.xpath("//input[@id='input_interest']")).sendKeys(intRate);
Select dd = new Select(driver.findElement(By.xpath("//select[@id='frequency']")));
dd.selectByVisibleText(compFreq);
driver.findElement(By.xpath("//button[normalize-space()='Calculate']")).click();
Assert.assertTrue(true);
Thread.sleep(2000);
}

@DataProvider(name ="dp", indices= {0,2})
public Object[][] dataProviderTest() {
Object[][] data = { 
{"2000","10","10","Monthly"},
{"3000","10","10","Monthly"},
{"4000","10","10","Monthly"},
{"5000","10","10","Monthly"}
    };
return data;
}
@AfterClass
public void closeBrowser()
{
driver.close();
Assert.assertTrue(true);
}

}

-----------------------------------------------------------
LISTENER

package TestNG;

import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
import org.testng.annotations.Test;

public class Listener implements ITestListener {

public void onStart(ITestContext context)
{
System.out.println("Listener before test");
}
public void onTestSuccess(ITestResult result)
{
System.out.println("Test is passed");

}

}

----------

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">


<suite name="Suite">

<listeners>

<listener class-name="TestNG.Listener"/>

</listeners>

<test thread-count="5" name="Test">

<classes>

<class name ="TestNG.CheckTitle"/>

</classes>

</test> <!-- Test -->

</suite> <!-- Listener -->


-----------------------------------------------
PARAMETERS USING XML

package TestNG;

import static org.testng.Assert.assertTrue;

import java.time.Duration;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Parameters;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ParametersUsingXML {

WebDriver driver;
@BeforeClass
@Parameters("browser")
public void openBrowser(String br)
{
switch(br)
{
case "chrome": driver = new ChromeDriver(); break;
case "firefox": driver =new FirefoxDriver(); break;
//Failing for Edge
case "edge": driver = new EdgeDriver();break;
default: System.out.println("Invalid Browser");return;
}
/*if(br=="chrome")
{
driver = new ChromeDriver();
}
else if(br=="edge")
{
driver = new EdgeDriver();
}
else
{
System.out.println("Incorrect Browser");
return;
}*/
//driver = new ChromeDriver();
//Implicit wait
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("https://www.sbisecurities.in/calculators/fd-calculator");
driver.manage().window().maximize();
Assert.assertTrue(true);
}
@Test(priority=1,dataProvider="dp")
public void bankCalculator(String fdInvestment, String numOfYears, String intRate, String compFreq) throws InterruptedException
{
driver.findElement(By.xpath("//div[@class='input-group']//input[@id='input_fd_investment']")).clear();
driver.findElement(By.xpath("//div[@class='input-group']//input[@id='input_fd_investment']")).sendKeys(fdInvestment);
driver.findElement(By.xpath("//body/app-root/app-fd-calculator[@class='ng-star-inserted']/div[@class='container']/div[@class='col-md-12 pt-5 mt-5']/div[@class='row pdt-30']/div[@class='col-xl-9 col-lg-7 col-md-7']/div[@class='border']/form[@id='validForm']/div[@class='row mt-2 d-flex']/div[@class='form-group col-md-6 mb-3']/input[1]")).clear();
driver.findElement(By.xpath("//body/app-root/app-fd-calculator[@class='ng-star-inserted']/div[@class='container']/div[@class='col-md-12 pt-5 mt-5']/div[@class='row pdt-30']/div[@class='col-xl-9 col-lg-7 col-md-7']/div[@class='border']/form[@id='validForm']/div[@class='row mt-2 d-flex']/div[@class='form-group col-md-6 mb-3']/input[1]")).sendKeys(numOfYears);
driver.findElement(By.xpath("//input[@id='input_interest']")).clear();
driver.findElement(By.xpath("//input[@id='input_interest']")).sendKeys(intRate);
Select dd = new Select(driver.findElement(By.xpath("//select[@id='frequency']")));
dd.selectByVisibleText(compFreq);
driver.findElement(By.xpath("//button[normalize-space()='Calculate']")).click();
Assert.assertTrue(true);
Thread.sleep(2000);
}

@DataProvider(name ="dp", indices= {0})
public Object[][] dataProviderTest() {
Object[][] data = { 
{"2000","10","10","Monthly"},
{"3000","10","10","Monthly"},
{"4000","10","10","Monthly"},
{"5000","10","10","Monthly"}
    };
return data;
}
@AfterClass
public void closeBrowser()
{
driver.close();
Assert.assertTrue(true);
}

}


---

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">


<suite name="Suite" parallel="tests">

<test thread-count="5" name="ChromeTest">

<parameter name="browser" value="chrome"/>

<classes>

<class name="TestNG.ParametersUsingXML"/>

</classes>

</test> <!-- Test -->

<test thread-count="5" name="FirefoxTest">

<parameter name="browser" value="firefox"/>

<classes>

<class name="TestNG.ParametersUsingXML"/>

</classes>

</test> <!-- Test -->

</suite> <!-- Suite -->


GROUPING XML

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">




<suite name="Suite">

<test thread-count="5" name="Test">

<groups>

<run>

<include name="smoke"/>

</run>

</groups>

<classes>

<class name="TestNG.CheckTitle"/>

</classes>

</test> <!-- Test -->

</suite> <!-- Suite -->




No comments:

Post a Comment