Selenium Cheatsheet (Java)

Selenium is a popular open-source automation framework that has quickly become an indispensable resource for SQA analysts. By allowing for the automation of web browsers, Selenium improves the efficiency with which testers can carry out a wide range of tasks, including the automation of repetitive actions, the execution of functional tests, and the verification of cross-browser compatibility. This selenium cheatsheet will provide you with a quick reference to the Selenium codes for use in your software quality assurance efforts.

Selenium WebDriver

ActionCodeDescription
Firefox driverWebDriver Driver = new FirefoxDriver ();Driver is an object;
IE driverSystem.setProperty(“webdriver.ie. driver”, PATH);
WebDriver driver = new InternetExplorerDriver ();
PATH = path of IEDriver exe file;
Chrome driverSystem.setProperty(“webdriver.ie. driver”,PATH);
WebDriver driver = new ChromeDriver ();
PATH = path of chrome exe file;

Identify Elements

ActionCodeDescription
Find single elementdriver.findElement(locator)Locator is a location of
element
Find multiple elementsdriver.findElements(locator)Locator is a location of
element

Locating UI Elements

ActionCodeDescription
By IDdriver. findElement (By.id(str));Str is id of element
By namedriver.findElement(By.name(str));Str is name of element
By Class Namedriver. findElement (By. className(str));Str is class value of element
By CSS selectordriver.findElement(By.cssSelector(str));Str is cssSelector of element
By link textdriver.findElement(By.linkText(str));Str is link text of element
By partial link textdriver.findElement(By.partialLinkText(str));Str is partial text of element
By tag namedriver.findElement(By.tagName(str));Str is tag name of element
By XPathdriver.findElement(By.xpath(xpath));Str is xpath of element

Handling Java Script Alerts

To handle alerts, first, we need to switch to alert.


Alert al=driver.switchTo().alert();
The Actions list.
ActionCode
Click on ok in alertal.accept();
Click on cancelal.dismiss();
Type in alert boxal.sendKeys(“text”);
Get text from alert boxal.getText();

Capture Screen Shot of Browser

ActionCodeDescription
Capture screenFile scrFile1 =
((TakesScreenshot)driver).getScreenshotAs(OutputType.FIL
E);
Save screenshot as
k2.png
Save to diskFileUtils.copyFile(scrFile1, new File(“c:\tmp\k2.png”));Save the screenshot as
k2.png

User Actions

ActionCodeDescription
Write in text fieldsdriver.findElement(locator).sendKeys(text);Text: what u want to write
locator is a location element
Click button or click radio
button or check box
driver. findElement(locator). click();Locator: location element
Clear text in text fielddriver. findElement(locator). clear();Locator: location element
Navigate back and forward
in browser
driver.navigate(). back();
driver.navigate().forward();
Navigate to framedriver. switchTo().frame(frame);frame can be integer value
represents position of frame or
string represents id of frame or
WebElement represents frame of
frame.
Navigate to next window
or pop up window
driver.switchTo().window(hashCode);hashCode is hash code of window
Get inner text of element
or inner text of table
driver.findElement(locator).getText();Locator: location element
Working on auto
complete/suggestions
Or Calendar pop up
driver. findElement(locator). click();Get the locator of hidden division
or element and perform required
operation.

Select Drop-Down List

Using the Select class we can work on the Select drop-down. Create a select object for a specific select
drop-down.

//creating webelement for select dropdown
WebElement usrs=driver.findElement(By.name(“users”));
Select usr=new Select(usrs);

We can select options drop-down in 3 different ways as explained below:

ActionCodeDescription
Select by using id of option tagusr.selectById(ID);ID is string, value of id attribute of
option.
Select by using index of option tagusr.selectByIndex(i);i is the position of option
Select by using visible text in option
tag
usr.selectByVisibleText(str)str is the text inside option tag.

Working on Excel sheet

Before working on Excel first we need to read Excel in the input stream using the file io stream.

FileInputStream fis=new FileInputStream(“Path of .xlsx file”);
ActionCodeDescription
Convert file io into
workbook
Workbook wb = WorkbookFactory.create(fis);Create function creates work
book.
Get into specified
sheet
Sheet s = wb.getSheet(sheetName);
Or
Sheet s = wb.getSheetAt(sheetNum);
sheetName is name of the sheet
sheetNum is index of sheet
Get into specified
row
Row r = s.getRow(rowNum);
Get into specified
column
Cell c = r.getCell(colNum);
Get cell valueString cellVal = c.getStringCellValue();
Or
boolean b = c.getBooleanCellValue();
or
Date d = c.getDateCellValue();
Or
int I = c.getNumericCellValue();
Get cell value based on value in
excel cell
Get row countint I = s.getLastRowNum();
Get Column countint j = r. getLastCellNum ();
Write back to excelc.setCellValue(“PASS1”);
FileOutputStream fos = new
FileOutputStream(“Path of .xlsx file “);
wb.write(fos);
fos.close();

Drag, Drop and Mouse Over, Mouse Events

We use Actions Class for drag and drop
Create an object-to-action class

Actions a=new Actions(driver);
ActionCodeDescription
Drag and Drop
using source and
destination
a.dragAndDrop(src, des).build().perform();Src and dest is the
WebElement object of
source and destination
of drag and drop
element.
Drag and drop to
specific position
a.dragAndDrop(src, x,y).build().perform();x and y are integer
values for specific
position.
Mouse over on
specific element
a.moveToElement(element).build().perform();Element is an object of
WebElement which
points to required
element.
Mouse right clicka.contextClick(element).build().perform();Element is an object of
WebElement which
points to required
element
Mouse
movement after
right click
a.sendKeys(Keys.).build().perform();Keys is a class contains
all key strokes such as
right left, enter, back
button

By giving testers access to a robust automation framework, Selenium has dramatically improved the field of Software Quality Assurance. Because of Selenium’s ability to automate web browsers, it is much easier to carry out routine tasks, functional tests, and browser compatibility checks. The key features of Selenium are summarized in this selenium cheatsheet, making it a useful resource for SQA experts who want to make the most of this flexible tool in their testing.

Leave a Reply

Your email address will not be published. Required fields are marked *

Article

Next article

Understanding API Concepts