Table of Contents
- What is Selenium?
- Selenium Webdriver Java Cheat Sheet
- Selenium Locators Cheat Sheet
- Selenium Commands Cheat Sheet
- Selenium Grid
- TestNG with Selenium
- JUnit with Selenium
Navigating the world of software testing can sometimes feel like maneuvering through a complex maze, especially when it comes to tools as powerful and versatile as Selenium WebDriver. For those who harness the capability of this tool, executing test scripts on various web browsers becomes a breeze.
Check our Selenium Cheat Sheet, your handy companion that will help you quickly reference key commands and methods, ensuring your test automation journey is smooth and efficient.
Whether you're trying to pinpoint a specific web element using xpath or css selectors, manage cookies in a browser session, or handle unexpected pop-up alerts and frames, this cheat sheet has got you covered.
For those versed in Java, integrating with Selenium becomes second nature. However, even seasoned developers sometimes need a quick reminder or clarification.
Check out also our other Cheat Sheets:
What is Selenium?
Selenium is an open-source automation testing framework designed for automating web browsers, allowing testers and developers to write scripts in various programming languages, including Java, C#, and Python.
It provides tools and libraries to interact with web applications, facilitating tasks like clicking buttons, filling out forms, and verifying content. Selenium supports multiple browsers, including Chrome, Firefox, and Edge, making execution of cross-browser testing more efficient.
Check also our Ultimate XPath Cheat Sheet for more resources.
Selenium Webdriver Java Cheat Sheet
Run Webdriver in Browser:
WebDriver driver = new EdgeDriver();
WebDriver driver = new SafariDriver();
WebDriver driver = new ChromeDriver();
WebDriver driver = new FirefoxDriver();
Selenium Locators Cheat Sheet
Here is a comprehensive list of selenium locators you might need:
By ID: Finds an element by its unique ID.
By Name: Finds an element by its name attribute.
By Class Name: Finds an element by its class attribute.
By Tag Name: Finds elements by their tag name.
By Link Text: Finds a link by its exact text.
driver.findElement(By.linkText("Link Text Here"));
By Partial Link Text: Finds a link by a part of its text.
driver.findElement(By.partialLinkText("Partial Link Text"));
By XPath: Finds an element using XPath expressions.
By CSS Selector: Finds an element by its CSS selector.
When using these locators, ensure you're selecting the most stable and direct way to interact with web element. The goal is to maintain robustness in your automation scripts.
Selenium Commands Cheat Sheet
1. Basic Browser Operations
To initialize the Chrome WebDriver:
WebDriver driver = new ChromeDriver();
To load a specific web page:
To maximize the browser window:
To close the current browser window:
To close all browser windows associated with that driver session:
2. Navigational Commands
For refreshing the current page:
To navigate backward in the browser's history:
To navigate forward in the browser's history:
3. Web Element Interaction
To locate an element by its ID:
WebElement element = driver.findElement(By.id("elementId"));
To click on a web element:
To input text into an input field:
To clear content from an input field:
To retrieve text from a web element:
String text = element.getText();
4. Selenium WebDriver Waits
For implicit waiting (waits for elements to become available up to a set time):
For explicit waiting (waits for a specific condition up to a set time):
WebDriverWait wait = new WebDriverWait(driver, 10);
5. Advanced Interactions
To switch to an alert window:
Alert alert = driver.switchTo().alert();
To switch to a specific frame by name or ID:
java -jar selenium-server-standalone-x.x.x.jar -role hub
Start a Node:
java -jar selenium-server-standalone-x.x.x.jar -role node -hub http://<hub_ip>:4444/grid/register
TestNG with Selenium
@AfterSuite: Run methods before and after the suite respectively.
@AfterTest: Execute methods before and after any test tag in the XML.
@AfterClass: Execute methods before and after the current class.
@AfterMethod: Run methods before and after every
@Test: Marks a method as part of the test.
Priority & Dependencies
DataProvider to pass parameters to test methods.
@Test(groups="groupName"): Helps to categorize tests.
Assert.assertEquals(actual, expected): Compare two values.
JUnit with Selenium
@AfterAll: Run methods once, before and after all tests in the class.
@AfterEach: Execute methods before and after each test method.
@Test: Marks a method as a test method.
Assert.assertEquals(expected, actual): Compare two values.
Assert.assertTrue(condition): Assert a condition as true.
Assumptions.assumeTrue(condition): Continue with the test only if the condition is true.
Timeouts & Disable:
@Test(timeout=1000): Fail the test if it takes longer than 1000ms.
Disable a test:
@MethodSource, etc. for various input sources.
So whether you're a novice just diving into the realm of test automation or a seasoned automation engineer looking for a quick refresher, let this cheat sheet guide your WebDriver driver in the fast-paced world of web automation.
Also, check out our No-code Xpath builder.
Happy (automated) testing!
Q: What is a web element?
A: A web element refers to any component or element on a web page, such as buttons, text boxes, drop-down lists, etc. Selenium provides methods to locate and interact with web elements.
Q: How can I locate web elements using Selenium?
A: Selenium provides multiple methods to locate web elements, such as xpath, CSS selectors, class name, id, name, link text, and partial link text.
Q: What is a frame in Selenium?
A: A frame, also known as an iframe, is an HTML element that allows content from another HTML document to be embedded within the current document. Selenium provides methods to switch between frames for interacting with elements inside them.
Q: What is an alert in Selenium?
A: An alert is a pop-up window that appears on a web page to notify the user or request input. Selenium provides methods to handle alerts programmatically.
Q: What are some best practices for using Selenium?
A: Some best practices for using Selenium include using explicit waits instead of hard coding wait times, using Page Object Model design pattern, organizing tests into separate test classes, and maintaining clear and descriptive test names.
Q: Can I write tests using Selenium in other languages?
A: Yes, Selenium supports multiple programming languages including Java, Python, C#, and Ruby. However, this cheat sheet specifically focuses on Selenium Webdriver with Java.
Q: Is there any online training available for learning Selenium Webdriver?
A: Yes, there are many online platforms that offer comprehensive Selenium Webdriver training, such as Intellipaat, Udemy, and Coursera. These courses cover topics from basic to advanced Selenium concepts.
Q: What does the "navigator" term refer to in Selenium?
A: In Selenium, the "navigator" term refers to the WebDriver interface's method that allows navigation actions on a web page, such as navigating to a URL, navigating back, refreshing the page, etc.