Table of Contents
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. Check also our list of the Best Selenium Practice Websites for more knowledge.
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.
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.
Selenium Cheat Sheet - WebDriver
Run Webdriver in Browser:
- Edge:Â Â
WebDriver driver = new EdgeDriver();
- Safari:Â Â
WebDriver driver = new SafariDriver();
- Chrome:Â Â Â
WebDriver driver = new ChromeDriver();
- Firefox:Â Â Â
WebDriver driver = new FirefoxDriver();
Selenium Cheat Sheet - Locators
Here is a comprehensive list of selenium locators you might need:
-
By ID: Finds an element by its unique ID.
driver.findElement(By.id("elementId"));
-
By Name: Finds an element by its name attribute.
driver.findElement(By.name("elementName"));
-
By Class Name: Finds an element by its class attribute.
driver.findElement(By.className("className"));
-
By Tag Name: Finds elements by their tag name.
driver.findElements(By.tagName("a"));
-
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.
driver.findElement(By.xpath("//tagname[@attribute='value']"));
-
By CSS Selector: Finds an element by its CSS selector.
driver.findElement(By.cssSelector("tagname.class#id"));
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 Cheat Sheet - Commands
1. Basic Browser Operations
To initialize the Chrome WebDriver: WebDriver driver = new ChromeDriver();
To load a specific web page: driver.get("https://www.example.com");
To maximize the browser window: driver.manage().window().maximize();
To close the current browser window: driver.close();
To close all browser windows associated with that driver session: driver.quit();
2. Navigational Commands
For refreshing the current page: driver.navigate().refresh();
To navigate backward in the browser's history: driver.navigate().back();
To navigate forward in the browser's history: driver.navigate().forward();
3. Web Element Interaction
To locate an element by its ID: WebElement element = driver.findElement(By.id("elementId"));
To click on a web element: element.click();
To input text into an input field: element.sendKeys("Text Here");
To clear content from an input field: element.clear();
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):Â
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
For explicit waiting (waits for a specific condition up to a set time):
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOf(element));
5. Advanced Interactions
To switch to an alert window: Alert alert = driver.switchTo().alert();
To switch to a specific frame by name or ID: driver.switchTo().frame("frameNameOrId");
To execute JavaScript on the loaded web page:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("javascriptCodeHere");
Selenium Grid
Start Hub:
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
Annotations
@BeforeSuite
,@AfterSuite
: Run methods before and after the suite respectively.@BeforeTest
,@AfterTest
: Execute methods before and after any test tag in the XML.@BeforeClass
,@AfterClass
: Execute methods before and after the current class.@BeforeMethod
,@AfterMethod
: Run methods before and after every@Test
method.@Test
: Marks a method as part of the test.
Priority & Dependencies
Setting priority: @Test(priority=1)
Setting dependencies: @Test(dependsOnMethods={"method_name"})
Parameterized Testing
Using @Parameters
or DataProvider
to pass parameters to test methods.
Groups:
@Test(groups="groupName")
: Helps to categorize tests.
Assertion:
Assert.assertEquals(actual, expected)
: Compare two values.
JUnit with Selenium
Annotations:
@BeforeAll
,@AfterAll
: Run methods once, before and after all tests in the class.@BeforeEach
,@AfterEach
: Execute methods before and after each test method.@Test
: Marks a method as a test method.
Test Lifecycle:
Test initialization: @BeforeEach
Test cleanup: @AfterEach
Assertions:
Assert.assertEquals(expected, actual)
: Compare two values.
Assert.assertTrue(condition)
: Assert a condition as true.
Assumptions:
Assumptions.assumeTrue(condition): Continue with the test only if the condition is true.
Timeouts & Disable:
Timeout: @Test(timeout=1000)
: Fail the test if it takes longer than 1000ms.
Disable a test: @Disabled
Parameterized Testing:
@ParameterizedTest
with @ValueSource
, @EnumSource
, @MethodSource
, etc. for various input sources.
Conclusion
In this article, we broke down essential commands and methods, helping you to specify and automate tasks with ease. From executing JavaScript on a page to handling the intricacies of different web elements, this cheat sheet will be your go-to guide.
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.
Happy (automated) testing!
Q&A
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.