Selenium - Selenese Commands
Selenium - WebDriver Commands and Concepts
Section titled “Selenium - WebDriver Commands and Concepts”In Selenium WebDriver, interactions with the web page are performed through commands sent to the browser driver. These commands simulate user actions. Understanding these core commands and concepts is fundamental to effective test automation.
Core Interaction Commands
Section titled “Core Interaction Commands”These are the most common methods used to interact with web elements:
findElement(By locator): Locates the first web element matching the given locator strategy. ThrowsNoSuchElementExceptionif not found.findElements(By locator): Locates all web elements matching the locator strategy. Returns an empty list if none are found.element.click(): Simulates a click on a web element (button, link, radio button, etc.).element.sendKeys(CharSequence... keysToSend): Simulates typing text into an input field or textarea.element.clear(): Clears the text from an input field or textarea.element.getText(): Retrieves the visible inner text of an element.element.getAttribute(String attributeName): Retrieves the value of a specified element attribute (e.g., ‘href’, ‘value’, ‘class’).element.isSelected(): Checks if a checkbox or radio button is selected. Returnstrueorfalse.element.isEnabled(): Checks if an element (like a button or input field) is enabled. Returnstrueorfalse.element.isDisplayed(): Checks if an element is visible on the page. Returnstrueorfalse.
Browser Navigation Commands
Section titled “Browser Navigation Commands”driver.get(String url): Navigates the browser to a specific URL.driver.navigate().to(String url): An alternative way to navigate to a URL.driver.navigate().back(): Simulates clicking the browser’s back button.driver.navigate().forward(): Simulates clicking the browser’s forward button.driver.navigate().refresh(): Reloads the current page.driver.getTitle(): Gets the title of the current page.driver.getCurrentUrl(): Gets the URL of the current page.
Assertions (Verification)
Section titled “Assertions (Verification)”While Selenium itself performs actions, verifying outcomes (Assertions) is typically handled by a testing framework like TestNG or JUnit. These frameworks provide methods to check if actual results match expected results.
Common assertion examples (using TestNG syntax):
import org.testng.Assert;
// Verify page titleString actualTitle = driver.getTitle();String expectedTitle = "Expected Page Title";Assert.assertEquals(actualTitle, expectedTitle, "Page title mismatch");
// Verify element textWebElement messageElement = driver.findElement(By.id("successMessage"));String actualMessage = messageElement.getText();Assert.assertTrue(actualMessage.contains("Successfully updated"), "Success message not found");
// Verify element is displayedWebElement submitButton = driver.findElement(By.id("submitBtn"));Assert.assertTrue(submitButton.isDisplayed(), "Submit button should be visible");Locating Elements (Locators)
Section titled “Locating Elements (Locators)”Locators are strategies used by Selenium to find HTML elements on a page. Choosing effective and stable locators is crucial for reliable tests. Modern browsers have built-in Developer Tools (usually opened by pressing F12) that allow you to inspect elements and determine suitable locators. Tools like Firebug and FirePath mentioned in older tutorials are deprecated and replaced by these built-in browser tools.
By.id("elementId"): Locates an element by its unique ‘id’ attribute. Often the fastest and most reliable.By.name("elementName"): Locates an element by its ‘name’ attribute.By.className("elementClassName"): Locates elements by their ‘class’ attribute. Be cautious if multiple elements share the same class.By.tagName("htmlTagName"): Locates elements by their HTML tag name (e.g., ‘a’, ‘input’, ‘div’).By.linkText("exactLinkText"): Locates an anchor element (<a>) by its exact visible text.By.partialLinkText("partialLinkText"): Locates an anchor element (<a>) by a partial match of its visible text.By.cssSelector("cssSelectorSyntax"): Locates elements using CSS selector patterns. Powerful and often preferred.By.xpath("xpathExpression"): Locates elements using XPath expressions. Very powerful but can be slower and more brittle if not written carefully.
Refer to the ‘Selenium - Locators’ chapter for detailed examples and best practices for each strategy.