All Selenium concepts with
examples
In Java
LinkedIn: Japneet Sachdeva
Opening and Closing Browsers
WebDriver driver = new ChromeDriver(); // Open Chrome
[Link]("[Link] // Navigate to URL
[Link](); // Close all browser windows
[Link](); // Close the current browser window
LinkedIn: Japneet Sachdeva
Element Interaction
[Link]([Link]("username")).sendKeys("testuser"); // Enter text
[Link]([Link]("submit")).click(); // Click a button
[Link]([Link]("Home")).click(); // Click a link
[Link]([Link]("//div[@class='message']")).getText(); // Get text
LinkedIn: Japneet Sachdeva
Navigation
[Link]().to("[Link] // Navigate to a new URL
[Link]().back(); // Go back to the previous page
[Link]().forward(); // Go forward to the next page
[Link]().refresh(); // Refresh the current page
LinkedIn: Japneet Sachdeva
Window and Frame Handling
[Link]().window("windowName"); // Switch to a specific window
[Link]().frame("frameId"); // Switch to a frame
[Link]().defaultContent(); // Switch back to the main content
LinkedIn: Japneet Sachdeva
Browser Management
[Link]().window().maximize(); // Maximize the browser window
[Link]().window().fullscreen(); // Set full-screen mode
[Link]().timeouts().implicitlyWait(10, [Link]); // Set
implicit wait
LinkedIn: Japneet Sachdeva
Mouse and Keyboard Actions
Actions actions = new Actions(driver);
[Link](element).click().build().perform(); // Move to and click
[Link](element).perform(); // Double-click
[Link](source, target).perform(); // Drag and drop
[Link](element, "text").perform(); // Send keystrokes
LinkedIn: Japneet Sachdeva
Assertions
[Link]([Link]()); // Assert element visibility
[Link]([Link](), "Expected Title"); // Assert title
By default these are hard assertions, means if these fail then tests will be
exited or stopped
LinkedIn: Japneet Sachdeva
Soft Assertions
SoftAssert softassert = new SoftAssert();
//Soft assert applied to verify title
[Link](ActualTitle, ExpectedTitle);
[Link]();
By default these assertions, do not stop if these assertions fail & are reported
in your test reports
LinkedIn: Japneet Sachdeva
Explicit Wait
WebDriverWait wait = new WebDriverWait(driver, 10);
[Link]([Link](element)); // Explicit wait
These are conditional waits & can be applied to satisfy a particular condition, then
continue test execution if condition met or failed if not met in mentioned amount of time
LinkedIn: Japneet Sachdeva
Implicit Wait
[Link]().timeouts().implicitlyWait(10, [Link]);
Once the command is run, Implicit Wait remains for the entire duration for which
the browser is open. It’s default setting is 0, and the specific wait time needs to be
set by the following protocol.
Implicit wait increases test script execution time
LinkedIn: Japneet Sachdeva
Fluent Wait - looks for a web element repeatedly at regular intervals until timeout happens or until the object is found
FluentWait wait = new FluentWait(driver);
//Specify the timeout of the wait
[Link](5000, [Link]);
//Specify polling time
[Link](250, [Link]);
//Specify what exceptions to ignore
[Link]([Link])
//This is how we specify the condition to wait on.
[Link]([Link]());
LinkedIn: Japneet Sachdeva
Alerts
Alert alert = [Link]().alert();
[Link](); // Accept an alert
[Link](); // Dismiss an alert
[Link]("text"); // Send text to an alert
LinkedIn: Japneet Sachdeva
Getting Page Information
[Link](); // Get the page title
[Link](); // Get the current URL
[Link](); // Get the entire page source code
LinkedIn: Japneet Sachdeva
Element Information
[Link](); // Check if an element is visible
[Link](); // Check if an element is enabled
[Link](); // Check if an element is selected
[Link]("attribute_name"); // Get an element's attribute value
[Link](); // Get an element's tag name
LinkedIn: Japneet Sachdeva
Menu Handling
Select select = new Select(element); // Create a Select object for dropdowns
[Link](1); // Select an option by index
[Link]("option2"); // Select an option by value
[Link]("Option 3"); // Select an option by visible text
LinkedIn: Japneet Sachdeva
Keyboard Actions
[Link]([Link]).sendKeys("a").keyUp([Link]).perfor
m(); // Ctrl+A
[Link]([Link]).sendKeys("hello").keyUp([Link]).perform(); //
Type "HELLO"
[Link]([Link]).perform(); // Press Enter
LinkedIn: Japneet Sachdeva
Mouse Actions
[Link](element).perform(); // Right-click
[Link](element).perform(); // Click and hold
[Link](element).perform(); // Release the mouse button
LinkedIn: Japneet Sachdeva
Taking Screenshots
File screenshot = ((TakesScreenshot)driver).getScreenshotAs([Link]);
LinkedIn: Japneet Sachdeva
Switching to Iframes
[Link]().frame("iframe_name");
LinkedIn: Japneet Sachdeva
Scrolling
JavascriptExecutor js = (JavascriptExecutor)driver;
[Link]("[Link](0, 1000)");
Actions actions = new Actions(driver);
// Scroll down a specific distance:
[Link](element).clickAndHold().moveByOffset(0,
500).release().build().perform();
LinkedIn: Japneet Sachdeva
Cookie Management
Get all cookies: Set<Cookie> cookies = [Link]().getCookies();
Get a specific cookie: Cookie cookie =
[Link]().getCookieNamed("cookie_name");
Add a cookie: [Link]().addCookie(new Cookie("cookie_name",
"cookie_value"));
Delete a cookie: [Link]().deleteCookieNamed("cookie_name");
Delete all cookies: [Link]().deleteAllCookies();
LinkedIn: Japneet Sachdeva
Alert Handling (Extended)
Dismiss an alert: [Link]();
Get alert text: String alertText = [Link]();
LinkedIn: Japneet Sachdeva
Page Source Interactions
Find elements in page source: List<WebElement> elements =
[Link]([Link]("//*"));
Execute JavaScript code: JavascriptExecutor js = (JavascriptExecutor)driver;
[Link]("[Link]('myElement').click();");
LinkedIn: Japneet Sachdeva
Advanced Navigation
Refresh the page with hard reload: [Link]().refresh(true);
Navigate to a page relative to the current URL:
[Link]().to("new_page.html");
LinkedIn: Japneet Sachdeva
Logging and Reporting
Enable logging: [Link]("[Link]",
"[Link]");
Create custom test reports: Utilize third-party libraries like ExtentReports or Allure
or TestNG reports
LinkedIn: Japneet Sachdeva
Headless Mode
Run tests without a visible browser: ChromeOptions options = new
ChromeOptions();
[Link]("--headless");
WebDriver driver = new ChromeDriver(options);
LinkedIn: Japneet Sachdeva
Setting browser capabilities
ChromeOptions options = new ChromeOptions();
[Link]("--start-maximized");
WebDriver driver = new ChromeDriver(options);
LinkedIn: Japneet Sachdeva
Disabling images
[Link]("--disable-images");
LinkedIn: Japneet Sachdeva
Enabling experimental features
[Link]("useAutomationExtension", false);
LinkedIn: Japneet Sachdeva
Uploading Files
Using sendKeys() for file uploads:
WebElement uploadElement = [Link]([Link]("upload"));
[Link]("C:\\path\\to\\[Link]");
LinkedIn: Japneet Sachdeva
Handling Shadow DOM Elements
Using executeScript() to interact with shadow DOM:
JavascriptExecutor js = (JavascriptExecutor)driver;
WebElement shadowRoot = (WebElement)[Link]("return
[Link]('shadow-host').shadowRoot");
WebElement element =
[Link]([Link]("element-inside-shadow-dom"));
LinkedIn: Japneet Sachdeva
Cross-Browser Testing:
Using different WebDriver implementations for various browsers:
WebDriver driver = new FirefoxDriver();
WebDriver driver = new EdgeDriver();
LinkedIn: Japneet Sachdeva
Different Browser Options
ChromeOptions options = new ChromeOptions();
[Link]("browserName", "chrome");
[Link]("version", "104.0");
[Link]("platformName", "Windows 10");
[Link]("--start-maximized");
[Link]("acceptInsecureCerts", true);
WebDriver driver = new ChromeDriver(options);
LinkedIn: Japneet Sachdeva
Refer to Selenium Documentation for insights
Selenium Documentation Link
LinkedIn: Japneet Sachdeva