Selenium Java Basics - Cheatsheet

Cheatsheet Testing

Complete reference of Selenium WebDriver basic commands used in real-world automation projects

What is this? Official Docs

Selenium is a browser automation framework used to automate and test web applications across browsers.

What is Selenium?

Selenium is an open-source browser automation framework used to automate web browsers. It enables testers and developers to simulate real user interactions and validate web application behavior across browsers and platforms.

โšก Key Features

  • Real browser automation
  • Cross-browser support
  • Multi-language bindings
  • Parallel execution support

๐ŸŽฏ Use Cases

  • UI automation testing
  • Regression and smoke testing
  • Cross-browser validation
  • CI/CD pipeline automation

๐Ÿš€ Selenium WebDriver - Create Driver

Create a new WebDriver instance.

WebDriver driver = new ChromeDriver();

๐Ÿ›‘ Selenium WebDriver - Quit Driver

Terminate browser and WebDriver session.

driver.quit();

๐Ÿ”ฒ Selenium Browser - Maximize Window

Maximize browser window.

driver.manage().window().maximize();

๐Ÿ”ป Selenium Browser - Minimize Window

Minimize browser window.

driver.manage().window().minimize();

๐Ÿ–ฅ๏ธ Selenium Browser - Fullscreen Mode

Set browser to fullscreen.

driver.manage().window().fullscreen();

๐ŸŒ Selenium Navigation - Open URL

Navigate to a URL.

driver.get("https://example.com");

๐Ÿงญ Selenium Navigation - Navigate To

Navigate using Navigation API.

driver.navigate().to("https://example.com");

โฌ…๏ธ Selenium Navigation - Back

Go back to previous page.

driver.navigate().back();

โžก๏ธ Selenium Navigation - Forward

Go forward in browser history.

driver.navigate().forward();

๐Ÿ”„ Selenium Navigation - Refresh

Refresh current page.

driver.navigate().refresh();

๐Ÿ†” Selenium Locator - By ID

Locate element using ID.

driver.findElement(By.id("username"));

๐Ÿ“› Selenium Locator - By Name

Locate element using name attribute.

driver.findElement(By.name("password"));

๐ŸŽจ Selenium Locator - By Class Name

Locate element using class name.

driver.findElement(By.className("btn-primary"));

๐ŸŽฏ Selenium Locator - By CSS Selector

Locate element using CSS selector.

driver.findElement(By.cssSelector("#login button"));

๐Ÿ” Selenium Locator - By XPath

Locate element using XPath.

driver.findElement(By.xpath("//input[@type='text']"));

Locate link using full text.

driver.findElement(By.linkText("Click here"));

Locate link using partial text.

driver.findElement(By.partialLinkText("Click"));

๐Ÿ‘† Selenium Element - Click

Click on an element.

element.click();

โŒจ๏ธ Selenium Element - Send Keys

Type text into input field.

element.sendKeys("admin");

๐Ÿงน Selenium Element - Clear Input

Clear text from input field.

element.clear();

๐Ÿ“ Selenium Element - Get Text

Fetch visible text.

element.getText();

๐Ÿ”– Selenium Element - Get Attribute

Fetch attribute value.

element.getAttribute("value");

๐Ÿ‘๏ธ Selenium Element - Is Displayed

Check visibility of element.

element.isDisplayed();

โœ… Selenium Element - Is Enabled

Check if element is enabled.

element.isEnabled();

โ˜‘๏ธ Selenium Element - Is Selected

Check selection state.

element.isSelected();

โฑ๏ธ Selenium Wait - Implicit Wait

Apply global implicit wait.

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

โœ”๏ธ Selenium Alert - Accept Alert

Accept browser alert.

driver.switchTo().alert().accept();

โŒ Selenium Alert - Dismiss Alert

Dismiss browser alert.

driver.switchTo().alert().dismiss();

๐Ÿ–ผ๏ธ Selenium Frame - Switch by Index

Switch to iframe using index.

driver.switchTo().frame(0);

๐Ÿ  Selenium Frame - Switch to Default Content

Return to main page.

driver.switchTo().defaultContent();

๐ŸชŸ Selenium Window - Get Window Handle

Get current window ID.

driver.getWindowHandle();

๐Ÿ”„ Selenium Window - Switch Window

Switch to another browser window.

driver.switchTo().window(windowId);

๐Ÿ“ธ Selenium Screenshot - Capture Screenshot

Capture screenshot of browser.

File src = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

๐Ÿ“„ Selenium Browser - Get Title

Get page title.

driver.getTitle();

๐Ÿ”— Selenium Browser - Get Current URL

Get current page URL.

driver.getCurrentUrl();