The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.
Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by Saurabh Kamble, 2023-01-02 02:22:12

Selenium Notes

Selenium Notes

21. Alert
https://demoqa.com/alerts
16. Auth Pop-Up
https://the-internet.herokuapp.com/basic_auth

Keyboard action
https://the-internet.herokuapp.com/key_presses
https://text-compare.com/







package automationPractice;

import java.time.Duration;

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;

public class AlertPopUpProgram
{

public static void main(String[] args)
{

System.setProperty("webdriver.chrome.driver",
"D:\\Back-up Data\\Desktop-Backup\\Pavan Velo\\Automation

Batch Material\\Downloads\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://demoqa.com/alerts");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));

//alert button inspect + click

WebElement alertButton =
driver.findElement(By.xpath("//button[@id='alertButton']"));

alertButton.click();

//u wil get the alert
//without handling alert
//can we click on this second ele??
//NO

//solution-
//we need to switch the selenium focus from application to alert

Alert alt = driver.switchTo().alert();

//perform action on action

String aletMessage = alt.getText();
System.out.println(aletMessage);

alt.accept(); //alert accept

//now u can proceed for further execution

WebElement alertButton2 = driver.findElement(By.xpath("
//button[@id='timerAlertButton']"));

alertButton2.click();

System.out.println("end of program");

}

}





package automationPractice;

import java.time.Duration;

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

public class GetWindowHandle
{

public static void main(String[] args)
{

System.setProperty("webdriver.chrome.driver",
"D:\\Back-up Data\\Desktop-Backup\\Pavan

Velo\\Automation Batch
Material\\Downloads\\chromedriver_win32\\chromedriver.exe");

WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://vctcpune.com/");

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

//url-->
driver.getCurrentUrl(); //URL
//title
driver.getTitle(); //title

//address of webpage
String address = driver.getWindowHandle();

System.out.println(address);

}

}
Main Program

package automationPractice;

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;

public class ChildBrowserPopUP
{

public static void main(String[] args)
{

System.setProperty("webdriver.chrome.driver",
"D:\\Back-up Data\\Desktop-Backup\\Pavan

Velo\\Automation Batch
Material\\Downloads\\chromedriver_win32\\chromedriver.exe");

WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://vctcpune.com/");

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

//vctc website address
String mainPageAddress = driver.getWindowHandle();
// System.out.println("main page address is-
"+mainPageAddress);

//-------------------------------

//child browser pop-up
//ele-start sele pracctice--> click

WebElement startSelePracticeLink=
driver.findElement(By.xpath("(//a[text()='Start Selenium
Practice'])[1]"));

startSelePracticeLink.click();

//two webpages are opened
//parent + child

//Set concept does not follow the index concept
//so we cant judge which address will of which webpage

// Set<String> allPageAddresses= driver.getWindowHandles();
// System.out.println(allPageAddresses);

//List concept
//index followed-->Yes
// int x = 100;
// double y = (double)x;

List<String> allPageAddresses = new
ArrayList<String>(driver.getWindowHandles());

System.out.println(allPageAddresses);

//next page child page switch

driver.switchTo().window(allPageAddresses.get(1));

//selenium focus went on child page now

WebElement textBox =
driver.findElement(By.xpath("//input[@id='autocomplete']"));

textBox.sendKeys("INDIA");

}
}

1.Auth Pop-Up
https://the-internet.herokuapp.com/basic_auth

2.Alert
https://demoqa.com/alerts

3.ChildBrowser PopUp
https://vctcpune.com
https://vctcpune.com/selenium/practice.html



Program1-Normal program to fetch data from excel sheet

package automationPractice;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
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 ExcelSheetProgram1
{

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

//Apache POI-tool
//tool configure-jar files donwload
//jar files attached

//file locate-address
String address = "D:\\Back-up Data\\Desktop-Backup\\Pavan
Velo\\Automation Batch Material\\April-16thBatch\\Automation
notes\\AprilBatch.xlsx";

//above location-one file-open and read
FileInputStream file = new FileInputStream(address);
//exception -accept it

//workbook read
XSSFWorkbook workbook = new XSSFWorkbook(file);
//exception -accept it

//sheet read
XSSFSheet sheet = workbook.getSheet("JAVA");

//row read
XSSFRow row = sheet.getRow(0);

//cell read
XSSFCell cell= row.getCell(0);

//get the cell value now

String userName =cell.getStringCellValue();
System.out.println(userName);

//---------------
// webELement username=driver.findele
// userName.sendKeys(userName);

}

}
Program-2 fetching multiple data from multiple cells

package automationPractice;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
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 ExcelProgram2
{

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

//how to fetch mutliple data

String address = "D:\\Back-up Data\\Desktop-Backup\\Pavan
Velo\\Automation Batch Material\\April-16thBatch\\Automation
notes\\AprilBatch.xlsx";

FileInputStream file = new FileInputStream(address);
//exception -accept it

XSSFWorkbook workbook = new XSSFWorkbook(file); //exception
-accept it

XSSFSheet sheet = workbook.getSheet("JAVA");

//usrname
XSSFRow row = sheet.getRow(0);
XSSFCell cell= row.getCell(0);
String userName =cell.getStringCellValue();
System.out.println("username- "+userName);

//password
XSSFRow row1 = sheet.getRow(1);
XSSFCell cell1= row1.getCell(0);
String password =cell1.getStringCellValue();
System.out.println("password- "+password);

//assignment
//simply use the above username n password data
//try to login on saucedemo website

}

}
Program3-Method chaining in excel sheet

package automationPractice;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
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 ExcelProgram3
{

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

String address = "D:\\Back-up Data\\Desktop-Backup\\Pavan
Velo\\Automation Batch Material\\April-16thBatch\\Automation
notes\\AprilBatch.xlsx";

FileInputStream file = new FileInputStream(address);
//exception -accept it

XSSFWorkbook workbook = new XSSFWorkbook(file);
//exception -accept it

//-----------
//method chaining
//string data-->getStringCellValue()
String result =
workbook.getSheet("JAVA").getRow(0).getCell(0).getStringCellValue();
System.out.println(result);
//numberData-->getNumericCellValue-->double

double numberData =
workbook.getSheet("JAVA").getRow(1).getCell(0).getNumericCellValue();

System.out.println(numberData);
//double to int -->explicit casting

}
}
Program4-fetching multiple data from excel with nested for loop

package automationPractice;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
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 ExcelProgram4
{

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

String address = "D:\\Back-up Data\\Desktop-Backup\\Pavan
Velo\\Automation Batch Material\\April-16thBatch\\Automation
notes\\AprilBatch.xlsx";

FileInputStream file = new FileInputStream(address);
//exception -accept it

XSSFWorkbook workbook = new XSSFWorkbook(file);
//exception -accept it

XSSFSheet sheet= workbook.getSheet("JAVA");

//row-cell(column format)
//nestedd for loop
//outer-row
//inner-cell

for(int i=0; i<=2; i++) //row
{

for(int j=0; j<=2; j++) //cell
{

XSSFRow row= sheet.getRow(i);
XSSFCell cell =row.getCell(j);
double data =cell.getNumericCellValue();
System.out.println(data);
}
}

//assignment
//manually entered
// row final cond-->i<=2
// cell final cond-->j<=2

//autoamtically entered

// getRow-->sheet
int lastRowNum = sheet.getLastRowNum();
System.out.println("lastRowNum- "+lastRowNum);

//cell

// int lastCellNum = row.getLastCellNum();

}
}

package automationPractice;

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.Select;

public class IframeConcept
{

public static void main(String[] args)
{

System.setProperty("webdriver.chrome.driver",
"D:\\Back-up Data\\Desktop-Backup\\Pavan

Velo\\Automation Batch
Material\\Downloads\\chromedriver_win32\\chromedriver.exe");

WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://chercher.tech/practice/frames-example-
selenium-webdriver");

//frame/iframe-webElement behave
WebElement frame2
=driver.findElement(By.xpath("//iframe[@id='frame2']"));

//main page--> frame switch
driver.switchTo().frame(frame2);

WebElement dropDown=
driver.findElement(By.xpath("//select[@id='animals']"));

dropDown.click();

Select s = new Select(dropDown);
s.selectByVisibleText("Baby Cat");

}

}

Case-I
Case-II

Assignment-
Try to click on below checkbox

package automationPractice;

import java.util.List;

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

public class WebTableHandling
{

public static void main(String[] args)
{

System.setProperty("webdriver.chrome.driver",
"D:\\Back-up Data\\Desktop-Backup\\Pavan

Velo\\Automation Batch
Material\\Downloads\\chromedriver_win32\\chromedriver.exe");

WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://vctcpune.com/selenium/practice.html");

//single element
// WebElement header1 =
driver.findElement(By.xpath("//th[text()='Instructor']"));
// String result = header1.getText();
// System.out.println(result);

//mutiple elements
List<WebElement> columnsHeaders =
driver.findElements(By.xpath("//table//tbody//tr//th"));

//headers=columns = 3
System.out.println("column size- "+columnsHeaders.size());

for(int i=0; i<columnsHeaders.size(); i++)
{

String result=columnsHeaders.get(i).getText();
System.out.println(result);
}

//-------------
// cell data print-row data print

List<WebElement> allCellData=
driver.findElements(By.xpath("//table//tbody//tr//td"));

System.out.println("totalCells- "+allCellData.size());

for(int i=1; i<allCellData.size();i++)
{

System.out.println(allCellData.get(i).getText());
}

}

}



DOM Model Concept











//1 loginPage
//2 homePage

// 3 CartPage
//4 customerInfo

//5 billig page
//6 order Page

Design the other POM Classses as mentioned above

Program1-LoginPagePOMClass

package pomPack;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class LoginPOMPage
{

//WebElements maintain
//WebElement action maintain
//we will not run this
//supply purpose use

//elements-maintain
//username
//password
// loginButton

//WebDriver declared
WebDriver driver;

//we r not going to use this method
// WebElement userName=
driver.findElement(By.xpath("//form//div[1]//input"));
// userName.sendKeys("standard_user");

//element inspect
// @FindBy -->@ -->annonation --> methodFuntion

@FindBy(xpath="//form//div[1]//input")
WebElement userName;

//lets perform action on webelement

public void sendUseraname()
{

userName.sendKeys("standard_user");
}

//password
@FindBy(xpath="//form//div[2]//input")
WebElement passWord;

//action
public void sendPassword()
{

passWord.sendKeys("secret_sauce");
}

//loginButton
@FindBy(xpath="/html/body/div/div/div[2]/div[1]/div[1]/div/form/i
nput")
WebElement loginButton;

public void clickLoginButton()
{

loginButton.click();
}

//constructor declare

public LoginPOMPage(WebDriver driver)

{

//global local

this.driver = driver;

//selenium class
PageFactory.initElements(driver, this);

}

//POMClass
//1.WebDriver declare-globally
//2.@FindBy -element find
//3.method ->element action
//4.Constructor-global n local driver, pagefactory

}

Program2-HomePagePOMCLass

package pomPack;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class HomePagePOMClass
{

//POMClass
//1.WebDriver declare-globally
//2.@FindBy -element find
//3.method ->element action
//4.Constructor-global n local driver, pagefactory

WebDriver driver;

@FindBy(xpath="(//button[text()='Add to cart'])[1]")
WebElement bagProduct;

public void clickOnBagButton()
{

bagProduct.click();
}

//find other elements as well

public HomePagePOMClass(WebDriver driver)

{

//global local

this.driver = driver;

//selenium class
PageFactory.initElements(driver, this);

}

}

TEstClass-VerifyLoginFunctionality

package pomPack;

import java.time.Duration;

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

public class VerifyLoginFunctionality
{

public static void main(String[] args)
{

System.setProperty("webdriver.chrome.driver",
"D:\\Back-up Data\\Desktop-Backup\\Pavan

Velo\\Automation Batch
Material\\Downloads\\chromedriver_win32\\chromedriver.exe");

WebDriver driver = new ChromeDriver();
System.out.println("Browser is opened");

driver.manage().window().maximize();
System.out.println("Browser is maximized");

driver.get("https://www.saucedemo.com/");
System.out.println("URL is opened");

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

//login activity
//usrname
//passowrd
//login
//we have maintain this code in POM Class
//lets use that code here

LoginPOMPage lp = new LoginPOMPage(driver);

lp.sendUseraname();
lp.sendPassword();
lp.clickLoginButton();

//homePage

//validation

String expectedTitle = "Swag Labs";
String actaulTitle = driver.getTitle();

if(expectedTitle.equals(actaulTitle))
{

System.out.println("TC is passed");
}
else
{

System.out.println("TC is failed");
}

}
}














Click to View FlipBook Version