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

}
}





package automationPractice;

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;

public class SeleniumWaits
{

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://www.saucedemo.com/");

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

//selenium waits
//implicit wait-->globally apply
//all elements --> apply
//the element which is taking time to load this wait will be
applied to that element

//
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(20))

;

WebElement userName =
driver.findElement(By.xpath("mmm//form//div[1]//input"));

userName.sendKeys("standard_user");

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

}

}

package automationPractice;

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.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class SelemiumWaits2
{

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://www.saucedemo.com/");
//explicit wait
//only single element is giving the issue
//time--for much time u need to apply the wait
//condition --> for what u r applyinh the wait
//explicit wait
//locally apply
//selected element
WebDriverWait wait = new
WebDriverWait(driver,Duration.ofSeconds(20));

wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.
xpath("mmm//form//div[1]//input"))));

}
}









1. As WebDriver interface don’t have any method to take screenshot
so TakesScreenshot interface is used for it.

2. For this we need to do the upcasting of “driver” refVar of
WebDriver into “ts” refVar of TakesScreenshot interface.

TakesScreenshot ts = (TakesScreenshot)driver;

3. With the help of getScreenshotAs method of TakesScreenshot interface we can
take screenshot.

4. That screenshot we need to store in one file format and need to maintain that file. For
this java functions are used. -SourceFile

File sourceFile = ts.getScreenshotAs(OutputType.FILE);

5. But the file which is created above, we don’t know its file

format and its location.

6. For this we will create our own file at our own location.

DestFile.

File destFile = new File("D:\\Back-up Data\\Desktop-Backup\\Pavan
Velo\\Automation Batch
Material\\FebBatch\\FebAutomationNotes\\ScreenshotFolder\\SauceDemoLog
in.jpg");

7. Now we will copy the sourcceFile into DestFile and then we can
see this screenshot file.

FileHandler.copy(sourceFile, destFile);

Program-1 normal screenshot process

package automationPractice;

import java.io.File;
import java.io.IOException;

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.io.FileHandler;

public class Screenshot2
{

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

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.get("https://www.saucedemo.com/");

TakesScreenshot ts = (TakesScreenshot)driver;

// ts.getScreenshotAs(null); //byte format

File sourceFile = ts.getScreenshotAs(OutputType.FILE);
//file format

//we dont know the location n file format of the file

File destFile = new File("D:\\Back-up Data\\Desktop-
Backup\\Pavan Velo\\Automation Batch Material\\April-
16thBatch\\ScreenshotFolder\\Pavan.jpg");

//paste the screesnhot now

FileHandler.copy(sourceFile, destFile);

//casting--
// int a = 100; //-->a-int
// double b =(double)a; //a-int --> double-->b

}

}
Program-2 multiple screenshot
package automationPractice;

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

import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.io.FileHandler;

public class ScreenshotProgram
{

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

{
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");
Thread.sleep(3000);

//take screenshot
//webdriver dont have screenshot method
TakesScreenshot ts = (TakesScreenshot)driver;
File sourceFile = ts.getScreenshotAs(OutputType.FILE);
File destFile = new File("D:\\Back-up Data\\Desktop-
Backup\\Pavan Velo\\Automation Batch Material\\April-
16thBatch\\ScreenshotFolder\\SauceDemoLoginPage.jpg");
FileHandler.copy(sourceFile, destFile);
System.out.println("loginpage screeshot taken");

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

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

//login process
WebElement userName =
driver.findElement(By.xpath("//form//div[1]//input"));
userName.clear(); //-->caches/cookies clear
userName.sendKeys("standard_user");
System.out.println("Username is entered");
Thread.sleep(3000);

WebElement password =
driver.findElement(By.xpath("//form//div[2]//input"));

password.clear();
password.sendKeys("secret_sauce");
System.out.println("Password is entered");
Thread.sleep(3000);

WebElement loginButton =
driver.findElement(By.xpath("/html/body/div/div/div[2]/div[1]/div[1]/d
iv/form/input"));

loginButton.click();
System.out.println("Clicked on login Button");
Thread.sleep(3000);

//homepage

File sourceFile1 = ts.getScreenshotAs(OutputType.FILE);
File destFile1 = new File("D:\\Back-up Data\\Desktop-
Backup\\Pavan Velo\\Automation Batch Material\\April-
16thBatch\\ScreenshotFolder\\SauceDemoHomePage.jpg");
FileHandler.copy(sourceFile1, destFile1);
System.out.println("homepage screeshot taken");

}

}
Program-3 screenshot method
package automationPractice;

import java.io.File;
import java.io.IOException;

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.io.FileHandler;

public class Screenshot3
{

//global variable
static WebDriver driver;

public static void screenshotMethod() throws IOException
{

TakesScreenshot ts = (TakesScreenshot)driver;
File sourceFile = ts.getScreenshotAs(OutputType.FILE);
File destFile = new File("D:\\Back-up Data\\Desktop-

Backup\\Pavan Velo\\Automation Batch Material\\April-
16thBatch\\ScreenshotFolder\\SauceDemoLoginPage11.jpg");

FileHandler.copy(sourceFile, destFile);
System.out.println("loginpage screeshot taken");
}

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

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

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

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");
Thread.sleep(3000);

//screenshot
screenshotMethod();

}

}

Program-5 date format code

static WebDriver driver;

public static void screenshotMethod() throws IOException
{

//date & time format code
Date d = new Date();
DateFormat d1 = new SimpleDateFormat("MM-dd-yy & HH-mm-ss");
String date = d1.format(d);

//screenshot

TakesScreenshot ts = (TakesScreenshot)driver;
File sourceFile = ts.getScreenshotAs(OutputType.FILE);
File destFile = new File("D:\\Back-up Data\\Desktop-
Backup\\Pavan Velo\\Automation Batch
Material\\FebBatch\\FebAutomationNotes\\ScreenshotFolder1\\FilePP" +dat
e+".jpg");

FileHandler.copy(sourceFile, destFile);
System.out.println("login screenshot is taken");
}











Program-1 –normal mouse click program

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.interactions.Actions;

public class MouseAction1
{

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://www.saucedemo.com/");

//login operations
WebElement userName =
driver.findElement(By.xpath("//form//div[1]//input"));
userName.sendKeys("standard_user");

WebElement password =
driver.findElement(By.xpath("//form//div[2]//input"));

password.sendKeys("secret_sauce");

WebElement loginButton =
driver.findElement(By.xpath("/html/body/div/div/div[2]/div[1]/div[1]/d
iv/form/input"));

//click action -->click method-->WebElement
// loginButton.click();

//mouse click action-->mouse automate

Actions act = new Actions(driver);

// act.click(); //it will not the perform the action

act.click(loginButton).perform();

}

}
Program2-MouseActions program

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.interactions.Actions;

public class MouseAction1
{

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://www.saucedemo.com/");

//login operations
WebElement userName =
driver.findElement(By.xpath("//form//div[1]//input"));
userName.sendKeys("standard_user");

WebElement password =
driver.findElement(By.xpath("//form//div[2]//input"));

password.sendKeys("secret_sauce");

WebElement loginButton =

driver.findElement(By.xpath("/html/body/div/div/div[2]/div[1]/div[1]/d
iv/form/input"));

//click action -->click method-->WebElement
// loginButton.click();

//mouse click action-->mouse automate

Actions act = new Actions(driver);

// act.click(); //it will not the perform the action

act.click(loginButton).perform();

}

}
Program-3 scrolling action program

package automationPractice;

import java.time.Duration;

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

public class SrollUpDownPrgram
{

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

{
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");

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

JavascriptExecutor js =(JavascriptExecutor)driver;

Thread.sleep(3000);

//java script langauge
js.executeScript("window.scrollBy(0,1000)"); //down

Thread.sleep(3000);

js.executeScript("window.scrollBy(0,-1000)"); //up

}

}

Practice websites
1. Velocity practice page

https://vctcpune.com/selenium/practice.html

2. automation scenarios practice website
http://practice.automationtesting.in/test-cases/

3. mouse action click, doble click, right click
https://demoqa.com/

4. drag on drop
http://www.dhtmlgoodies.com/scripts/drag-drop-custom/demo-drag-drop-3.html









Program 1-List –ArrayList programs

package collectionPack;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class CollectionProgram
{

public static void main(String[] args)
{
// int a = 10;
// int b = 20;
// System.out.println(a+b);

//not possible
// int a [] = {10.99,"RAHUL", 10,20,30,40,50,60};

//more requirement

//single dataTYpe and variable
//array

//collection
//1.ArrayList --> class

// ArrayList a = new ArrayList();

// //data add
// a.add(10); //0
// a.add(20); //1
// a.add(30); //2
//
// //adv of collection-any type of data we can add
//
// a.add(40); //3
// a.add(77.89); //4
// a.add('A'); //5
// a.add("RAHUL"); //6

//print data
// System.out.println(a);

//specific data
//index concept
//get method

// System.out.println(a.get(6)); //RAHUL

//---------------------------
//2.specific dataTYpe data

// ArrayList<Integer> b = new ArrayList<Integer>();
//
// b.add(10);
// b.add(20);
// b.add(30);

// b.add(10.77); //not possible

// b.add('A'); //not possible

// b.add("RAHUL"); //not possible

// System.out.println(b);

//--------------------
// String type data

// ArrayList<String> c = new ArrayList<String>();
//
c.add("ABC"); //0
// c.add("DEF"); //1
// c.add("XYZ"); //2
//
// c.add('A'); //not possible
//// c.add(10); //not possible
////
// System.out.println(c);
//
// //replace method
//
// c.set(2, "RAHUL");

// System.out.println(c);
//
// //size of arraylist
// System.out.println(c.size()); //3
//
//

//remove the element -remove method

// c.remove(2); //RAHUL
//

// System.out.println(c.size()); //2
//
// System.out.println(c);
//
// //how to get a index of any element
// ArrayList.indexOf(c, "DEF");

// //-------------------------------
// ArrayList d = new ArrayList();
//
d.add(10); //0
// d.add(20); //1
// d.add(30); //2
//
//// System.out.println(d);
//
// d.remove(1);

// System.out.println(d);

//10 -->0
//30 -->1

// d.add(40);
//10 -->0
//30 -->1
//40 -->2

// System.out.println(d);

// d.add(1, 40);
//
// System.out.println(d);

//learn any 8-10 methods collections

//---------------------
// LinkedList concept

// LinkedList x = new LinkedList();
//
// LinkedList<Integer> y = new LinkedList<Integer>();

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

// List concept

//upcasting syntax

List a = new ArrayList();

a.add(10);
a.add(20);
a.add('A');
a.add("RAHUL");
a.add(10.77);

// System.out.println(a);

List<Double> b = new ArrayList<Double>();

b.add(10.77); //0
b.add(10.88); //1
b.add(10.99); //2

//specific list->integer type
// b.add(100);
// b.add('A');
// b.add("RAHUL");
// System.out.println(b);
// System.out.println(b.get(2));

b.remove(2);
// System.out.println(b);

//---------------------------
List x = new LinkedList();
List<String> y = new LinkedList<String>();

}
}
Program-2 Set and HashSet programs
package collectionPack;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class CollectionProgram2
{

public static void main(String[] args)

{ HashSet
//
HashSet a = new HashSet();
//
// a.add(10);
// a.add(20);
a.add(30);
// a.add("RAHUL");
a.add('A');
a.add(10.77);

System.out.println(a);

//2.syntax
HashSet<Integer> b = new HashSet<Integer>();

b.add(10);
b.add(20);
b.add("RAHUL"); //not possible

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

Set interface

Set x = new HashSet();

x.add(10);
x.add(20);
x.add(30);
x.add("PANKAJ");
x.add('C');
x.add(20.20);

System.out.println(x);

//there is no such index concept

// System.out.println(x.ge);

//not present
// x.get --> index
// x.set -->index

//if need to print single single data

Iterator it = x.iterator(); //method of Set

//it will call one by one element
//random value

// System.out.println(it.next());
// System.out.println(it.next());
// System.out.println(it.next());
// System.out.println(it.next());
// System.out.println(it.next());

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

System.out.println(it.next());
}

}

}
Program-3-difference of List and Set Programs

package collectionPack;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class DifferenceProgram
{

public static void main(String[] args)
{

List x = new ArrayList();

x.add(10);
x.add(20);
x.add(30);
x.add(30); //duplicate value
x.add(null); //empty
x.add(null);
// System.out.println(x);

//---------------
Set y = new HashSet();
y.add(10);

y.add(20);
y.add(30);
y.add(30);
y.add(null);
y.add(null);
y.add("RAHUL");
System.out.println(y);

}

}









findElements Method program

package automationPractice;

import java.time.Duration;
import java.util.ArrayList;
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 FindElements
{

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://www.saucedemo.com/");

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

WebElement userName =
driver.findElement(By.xpath("//form//div[1]//input"));

userName.clear(); //-->caches/cookies clear
userName.sendKeys("standard_user");

WebElement password =
driver.findElement(By.xpath("//form//div[2]//input"));

password.clear();
password.sendKeys("secret_sauce");

WebElement loginButton =
driver.findElement(By.xpath("/html/body/div/div/div[2]/div[1]/div[1]/d
iv/form/input"));

loginButton.click();

//homePage
//bag product-add to cart

//findElement-->single element find
// WebElement addToCartBagButton=
driver.findElement(By.xpath("//button[@id='add-to-cart-sauce-labs-
backpack']"));
// addToCartBagButton.click();

//--------------------------
// multiple elements --> findElements

// List<Integer> a = new ArrayList<Integer>();

//multiple selection xpath
List<WebElement> addToCartButtons=

driver.findElements(By.xpath("//button[text()='Add to cart']"));

//1ele-->0
//2ele-->1

// //3ele-->2
// //4ele-->3
// //5ele-->4
// //6ele-->5
//
// //mutiple ele--singl ele
addToCartButtons.get(0).click();
} addToCartButtons.get(1).click();
addToCartButtons.get(2).click();
addToCartButtons.get(3).click();
addToCartButtons.get(4).click();
addToCartButtons.get(5).click();

//repeated code--> for loop

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

addToCartButtons.get(i).click();
}

//assignment
//collection concept
//+
//findElements method
//flipkart and amazon
//multiple elements --> select

}



Assignement –try this


Click to View FlipBook Version