0% found this document useful (0 votes)
80 views

Selenium Cheatsheet

The document summarizes key Selenium WebDriver concepts including: 1. Creating browser drivers for Firefox, Internet Explorer, and Chrome. 2. Opening web applications and identifying/locating UI elements using different locator strategies. 3. Performing user actions like entering text, clicking, clearing fields and navigating. 4. Using the Actions class to perform actions like drag and drop, mouse hover and right click events. 5. Downloading files by setting browser preferences and profiles. 6. Handling JavaScript alerts and popups. 7. Managing untrusted security certificates.

Uploaded by

Saurabh Jadhav
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views

Selenium Cheatsheet

The document summarizes key Selenium WebDriver concepts including: 1. Creating browser drivers for Firefox, Internet Explorer, and Chrome. 2. Opening web applications and identifying/locating UI elements using different locator strategies. 3. Performing user actions like entering text, clicking, clearing fields and navigating. 4. Using the Actions class to perform actions like drag and drop, mouse hover and right click events. 5. Downloading files by setting browser preferences and profiles. 6. Handling JavaScript alerts and popups. 7. Managing untrusted security certificates.

Uploaded by

Saurabh Jadhav
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

SeleniumWebDriver

JOIN- https://t.me/Tcs_wings_2022 Selenium: Creating Driver\\


Slno Action Selenium code Description
1 Firefox driver WebDriver Driver = new FirefoxDriver(); Driver is an object
2 IE driver System.setProperty("webdriver.ie.driver",PATH); PATH = path of IEDriver exe file;
WebDriver driver = new InternetExplorerDriver();
3 chrome System.setProperty("webdriver.ie.driver",PATH); PATH = path of chrome exe file;
Driver WebDriver driver = new ChromeDriver();

Selenium: opening an application


slno Action code

1 Opening an application Driver.get(“url”);

Selenium: Indentify Elements


Slno Action Code Description
1 Find single Element driver.findElement(locator) Locator is a location of element
2 Find multiple Elements driver.findElements(locator) Locator is a location of element

Selenium: Locating UI Elements


Slno Action Code Description
1 By ID driver.findElement(By.id(str)); Str is id of element
2 By Name driver.findElement(By.name(str)); Str is name of element
3 By class name driver.findElement(By.className(str)); Str is class value of element
4 By css selector driver.findElement(By.cssSelector(str)); Str is cssSelector of element
5 By link text driver.findElement(By.linkText(str)); Str is link text of element
6 By partial link text driver.findElement(By.partialLinkText(str)); Str is partial text of element
7 By tag name driver.findElement(By.tagName(str)); Str is tag name of element
8 By XPath driver.findElement(By.xpath(xpath)); Str is xpath of element

Selenium: User Actions


Slno Action Code Description
1 Write in text fields driver.findElement(locator).sendKeys(text); Text: what u want to write
locator is a location element
2 Click button or click driver.findElement(locator).click(); locator is a location element
radio button
or check box
3 Clear text in driver.findElement(locator).clear(); locator is a location element
text field
4 Navigate back and driver.navigate().back();
forward in browser driver.navigate().forward();

MaheshMQspiders,MysorePage1
SeleniumWebDriver
5 Navigate to frame driver.switchTo().frame(frame); frame can be integer value
represents position of frame or
string represents id of frame or
WebElement represents frame
of
frame.
6 Navigate to next driver.switchTo().window(hashCode); hashCode is hash code of window
window or pop up

MaheshMQspiders,MysorePage2
https://t.me/Tcs_wings_2022
SeleniumWebDriver

window
7 Get inner text of driver.findElement(locator).getText(); locator is a location element
element or inner text
of table

8 Working on auto driver.findElement(locator).click(); Get the locator of hidden


complete/suggestions division or element and perform
Or required operation
Calendar pop up

Selenium: Drag, Drop and Mouse Over, Mouse Events


We use Actions Class for drag and drop
1st Create an object to action class
Actionsact=newActions(driver);
There are 2 ways to do drag and drop
slno Action code description
1 Drag and Drop act.dragAndDrop(src, des).build().perform(); Src and dest is the
using source and webElement object of
destination source and destination
of drag and drop
element.
2 Drag and Drop to act.dragAndDropBy(src, x,y).build().perform(); x and y are integer
specific position values for specific
position.
3 Mouse over on act.moveToElement(element).build().perform(); Element is an object
specific element. of WebElement which
points to required
element.
4 Mouse right act.contextClick(element).build().perform(); Element is an object
click of WebElement which
points to required
element.
5 Mouse act.sendKeys(Keys.<keyboardstrokes>).build().perform(); Keys is a class
movement after contains all key strokes
right click such as
right left, enter,
back button etc

Selenium: File Download


To automate file download in fire fox we have to do following steps:
1. Set the path where to download.
2. Set browser preferences not to ask confirmation.
1. Set the path where to download
For this we have set browser.download.folderList preference to integer value. The values are

MaheshMQspiders,MysorePage3
https://t.me/Tcs_wings_2022
SeleniumWebDriver
0 to save in desktop
1 Save in downloads folder in your computer
2 User defined path

If the value is 2 then we have to set browser.download.dir preferences to path of specific folder.

MaheshMQspiders,MysorePage4
SeleniumWebDriver

2. Set browser preferences not to ask confirmation


The browser.helperApps.neverAsk.saveToDisk preference to be set to the MIME Type of file, we need
to download.
1st we need to create fire fox profile for browser by creating object to FirefoxProfile:
FirefoxProfile prof = new FirefoxProfile();
Slno Action Code Description
1 Set prof.setPreference("browser.download.folderList",in Use object created for fire
browser.download. tegerVal); fox profile and integerVal
folderList is 0 or 1 or 2 based
location where you want to
save.
2 Set prof.setPreference("browser.download.dir", "d:\\"); If integerVal in above code
browser.download. is 2 then set the path where
dir you need to download the
file.
3 Set prof.setPreference("browser.helperApps.neverAsk.s MimeType is MIME type
browser.helperApp aveToDisk", MimeType); of file
s.neverAsk.saveTo
Disk
4 Use profile WebDriver driver = new FirefoxDriver(prof);
created to create
driver

Selenium: Handling Java Script Alerts


To handle alert first we need to switch to alert.
Alertal=driver.switchTo().alert();
The Actions list.

slno Action code


1 Click on ok in alert al.accept();
2 Click on cancel. al.dismiss()
3 Type in alert box. al.sendKeys(“hi”);
4 Get text from alert box. al.getText();

Selenium: Handling Un Trusted Certificate Exception


The below code used to handle un trusted certificate exception.
FirefoxProfile prof =newFirefoxProfile(); //create firefox
profileprof.setAcceptUntrustedCertificates(true);prof.setAssumeUntru
stedCertificateIssuer(true);
WebDriverdriver=newFirefoxDriver(prof);

Selenium: Capture Screen Shot of Browser


Action Code Description
Slno

MaheshMQspiders,MysorePage3
SeleniumWebDriver
1 Capture File scrFile1 = It captures screen shot of
screen ((TakesScreenshot)driver).getScreenshotAs(OutputType.FI particular page and
LE); stores it in variable
2 Save to disk FileUtils.copyFile(scrFile1, new File("c:\\tmp\\k2.png")); Save screen shot
as k2.png

MaheshMQspiders,MysorePage4
SeleniumWebDriver

Selenium: Work on drop down list (select drop down list)


Using Select class we can work on select drop down. Create select object for specific select drop down.
WebElementusrs =driver.findElement(By.name("users"));//createWebElementforselectdropdownSelectusr
=newSelect(usrs);
We can select options of drop down in 3 different ways as explained below
slno Action code description
1 Select by using id of option tag usr.selectById(id); Id is string, value of id attribute
of option.
2 Select by using index of option tag usr.selectByIndex(i); I is the position of option
3 Select by using visible text in option tag usr.selectByVisibleText(str) str is the text inside option tag.

Selenium: Working on excel sheet


Before working on excel first we need to read excel in input stream using file io stream.
FileInputStream fis = new FileInputStream("D:\\jul_weekend\\hybridframework\\tests\\testscenarios.xlsx");
slno Action Code Description
1 Convert file io Workbook wb = WorkbookFactory.create(fis); Create function
into workbook creates work book.
2 Get into Sheet s = wb.getSheet(sheetName); sheetName is name
specified sheet Or of the sheet
Sheet s = wb.getSheetAt(sheetNum);
sheetNum is index
of sheet
3 Get into specified Row r = s.getRow(rowNum);
row
4 Get into Cell c = r.getCell(colNum);
specified column
5 Get cell value String retVal = c.getStringCellValue(); Get cell value based
Or
boolean b = c.getBooleanCellValue(); on value in excel cell
or
Date d = c.getDateCellValue();
Or
int I = c.getNumericCellValue();
6 Get row count int I = s.getLastRowNum();
7 Get Column int j = r. getLastCellNum ();
count
8 Write back c.setCellValue("PASS1");
to excel FileOutputStream fos = new FileOutputStream("C:\\Documents and
Settings\\mahesh\\Desktop\\Book1.xlsx");
wb.write(fos);
fos.close();

MaheshMQspiders,MysorePage5
SeleniumWebDriver

Selenium: Hybrid Frame Work Explanation

MaheshMQspiders,MysorePage6
SeleniumWebDriver

T1 Excel sheet

T2 TestE
Driver Excel
xecutor
T3 Library

T4

Frame Explain
1. Our frame work is keyword hybrid frame work which runs from excel.
2. Driver is the starting point of execution. Driver launches the application, adds the required tests to
the test suite, and runs the test suite. The test required to run will be present in excel file called
“TestScenarios”. The sheet called test suit in excel file contains list of all the test cases to be run with
execution status against each script. If execution status is yes the test is executed else it is skipped.
3. Driver has the code to read this excel sheet and based on the execution status, script will be added to
the test suite.
4. We use TestN(JUnit) to execute the tests.
5. When driver runs the tests, it creates an object of test executor class and calls the execute test method.
6. Test executor connects to excel through the particular sheet of the scenario which is running,
loops through all the test steps, and executes the steps.
7. To connect with excel, apache POI API are used. The inbuilt methods are used get data from
excel sheet.
8. When each step is executed, script checks the element is present and performs the action. For
every action performed log message is generated which is written back to the excel sheet.

MaheshMQspiders,MysorePage7

You might also like