0% found this document useful (0 votes)
11 views14 pages

Selenium Syntax

The document provides a comprehensive guide on building Selenium automation test scripts, focusing on working with lists and various WebDriver methods. It covers prerequisites, XPath usage, actions, handling dropdowns, alerts, JavaScript execution, and capturing screenshots. Each section includes code snippets and explanations for better understanding and implementation of Selenium in automation testing.

Uploaded by

ishivamkunal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views14 pages

Selenium Syntax

The document provides a comprehensive guide on building Selenium automation test scripts, focusing on working with lists and various WebDriver methods. It covers prerequisites, XPath usage, actions, handling dropdowns, alerts, JavaScript execution, and capturing screenshots. Each section includes code snippets and explanations for better understanding and implementation of Selenium in automation testing.

Uploaded by

ishivamkunal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

www.linkedin.

com/in/yogesh-p-346440149

How to build an selenium


list syntax using in the
Automation Testing
from scratch
Swipe for tips
PAGE 2

To build a Selenium automation test script for


working with lists, you can use Selenium WebDriver,
which is a popular tool for automating web browser
interactions. In this example, I'll show you how to
automate the testing of a list on a web page. You can
adapt this example to your specific needs.
PAGE 3

Prerequisites
1. Install Java: Make sure you have Java installed
on your system.
2. Set up an Integrated Development Environment
(IDE) like Eclipse or IntelliJ IDEA.
3. Download Selenium WebDriver for Java and add
it to your project. You can get it from the
Selenium official website
PAGE 4

In this Java example, we


1. Set the path to the ChromeDriver executable
using System.setProperty.
2. Initialize the WebDriver (ChromeDriver in this
case).
3. Navigate to the web page containing the list.
4. Locate the list element using XPath (you can use
other locators like CSS selectors).
5. Get all the list items within the list element as a
list of web elements.
6. Iterate through the list items and perform your
desired actions on each item.
7. Finally, close the WebDriver when done.
PAGE 5

WebDriver Methods - all


These are common methods provided by the
WebDriver interface in Selenium.

1. driver.get("http://www.example.com"); // Open a webpage


2.driver.navigate().back(); // Navigate back
3.driver.navigate().forward(); // Navigate forward
4.driver.navigate().refresh(); // Refresh the current page
5.driver.close(); // Close the current window
6.driver.quit(); // Quit the driver
7.driver.manage().window().maximize(); // Maximize the
window
8.String url = driver.getCurrentUrl(); // Get the current URL
9.String title = driver.getTitle(); // Get the title of the
page

These methods are used for common


navigation and control operations in
WebDriver.
PAGE 6

WebElement Methods - all


These methods apply to web elements (e.g., buttons,
input fields) located on a web page.
1.WebElementelement=driver.findElement(By.id("element_id"));
2.String text = element.getText(); // Get text
3.StringattributeValue=element.getAttribute("attribute_name");
// Get attribute value
4.boolean isDisplayed = element.isDisplayed(); // Check if
element is displayed
5.boolean isEnabled = element.isEnabled(); // Check if element
is enabled
6.boolean isSelected = element.isSelected(); // Check if
element is selected
7.element.click(); // Click on the element
8.element.clear(); // Clear the input field
9.element.sendKeys("Text to send"); // Send keys to the
element

These methods are used for interacting with web elements


on a page.
PAGE 7

XPath - all types


XPath is used to locate elements in an XML or HTML
document. Different types of XPath.
1. XPath is used to locate elements in an XML or HTML document. The different types of
XPath expressions are: Absolute XPath: It starts with the root node and navigates to the
desired node. /html/body/div[2]/form/input
2. Relative XPath: It starts with a node and navigates to the desired node. It is preferred
over absolute XPath as it is not affected by changes in the HTML structure.
//input[@name='username']
3. Attribute-based XPath: It selects elements based on the value of an attribute.
//input[@id='username']
4. Text-based XPath: It selects elements based on the text value of an element.
//a[text()='Click here']
5. Partial match XPath: It selects elements based on a partial match of an attribute value
or text value. //input[contains(@name, 'user')]
6. Logical operators XPath: It uses logical operators like 'and', 'or' to select elements
based on multiple conditions. //input[@name='username' and @type='text']

XPath expressions have been provided for each


type.
PAGE 8

Actions - all methods


These actions involve using the Actions class to
perform various user interactions.
1.Actions actions = new Actions(driver);
2.WebElement element = driver.findElement(By.id("element_id"));
3.actions.moveToElement(element).build().perform(); // Move to an element
4.actions.click(element).build().perform(); // Click on an element
5.actions.doubleClick(element).build().perform(); // Double-click
6.actions.contextClick(element).build().perform(); // Right-click
7.WebElement sourceElement = driver.findElement(By.id("source_element_id"));
8.WebElement targetElement = driver.findElement(By.id("target_element_id"));
9.actions.dragAndDrop(sourceElement, targetElement).build().perform(); // Drag and drop

The Actions class is used for advanced user interactions


like mouse actions and keyboard events.
PAGE 9

Robot
The Robot class is used to simulate keyboard and mouse
actions at the system level.
Robot robot = new Robot();
// Simulate key press and release
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);

// Simulate mouse movement and clicks


robot.mouseMove(x, y);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);

// Type a string by simulating key presses


String text = "Text to type";
for (char c : text.toCharArray()) {
int keycode = KeyEvent.getExtendedKeyCodeForChar(c);
robot.keyPress(keycode);
robot.keyRelease(keycode); }

The Robot class allows you to simulate low-level keyboard


and mouse actions.
PAGE 10

DropDown
This code snippet demonstrates working with
dropdown (select) elements using the Select class.
Select dropdown = new Select(driver.findElement(By.id("dropdown_id")));
dropdown.selectByVisibleText("Option 1"); // Select by visible text
dropdown.selectByValue("value1"); // Select by value
dropdown.selectByIndex(0); // Select by index
List<WebElement> options = dropdown.getOptions(); // Get all options
WebElement selectedOption = dropdown.getFirstSelectedOption(); // Get
selected option
boolean isMultiple = dropdown.isMu // Check if multiple selection
dropdown.deselectAll(); // Deselect all options in a multiple
select
dropdown.deselectByVisibleText("Option 1"); // Deselect by visible text
dropdown.deselectByValue("value1"); // Deselect by value
dropdown.deselectByIndex(0); // Deselect by index

The Select class is used to interact with dropdown


(select) elements.
It provides methods for selecting, deselecting, and
retrieving options from the dropdown
PAGE 11

Alert
This code snippet deals with handling alerts in
Selenium.
Alert alert = driver.switchTo().alert();

// Accept the alert (clicking "OK")


alert.accept();

// Dismiss the alert (clicking "Cancel")


alert.dismiss();

// Get the text of the alertString


alertText = alert.getText();

driver.switchTo().alert() is used to switch to an alert dialog.


alert.accept() accepts the alert (clicks the "OK" button).
alert.dismiss() dismisses the alert (clicks the "Cancel"
button).
alert.getText() retrieves the text content of the alert.
PAGE 12

JavascriptExecutor
This code snippet demonstrates how to use
JavascriptExecutor to perform actions on web elements.
JavascriptExecutor js = (JavascriptExecutor) driver;

// Set the value of an attribute


WebElement element = driver.findElement(By.id("element_id"));
js.executeScript("arguments[0].setAttribute('attribute_name',
'attribute_value')", element);

// Click on an element
WebElement element = driver.findElement(By.id("element_id"));
js.executeScript("arguments[0].click();", element);

// Scroll to an element
WebElement element = driver.findElement(By.id("element_id"));
js.executeScript("arguments[0].scrollIntoView(true);", element);

JavascriptExecutor allows you to execute JavaScript code in


the context of the current web page.
executeScript method is used to execute JavaScript code.
In the examples, JavaScript is used to set an attribute, click
on an element, and scroll to an element.
PAGE 13

TakesScreenshot
This code snippet captures a screenshot of a web page
using Selenium WebDriver.

TakesScreenshot scrShot = ((TakesScreenshot) driver);

File SrcFile = scrShot.getScreenshotAs(OutputType.FILE);

File DestFile = new File("path/to/save/screenshot.png"); FileUtils.copyFile(SrcFile,

DestFile);

TakesScreenshot is an interface provided by Selenium to


capture screenshots.
scrShot.getScreenshotAs(OutputType.FILE) captures the
screenshot as a File.
FileUtils.copyFile from Apache Commons IO library is used to
copy the captured screenshot to a specified location.
@WWW.LINKEDIN.COM/IN/YOGESH-P-
346440149

Was this helpful?


Ask any questions in the comments.
Like, share and save for later

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy