All Selenium Commands with Examples
Browser Commands
get(String url)
: Navigates to the given URL.
Example:driver.get("https://www.automationCodes.com");
getCurrentUrl()
: Returns the URL of the current page.
Example:String currentUrl = driver.getCurrentUrl();
getTitle()
: Returns the title of the current page.
Example:String title = driver.getTitle();
getPageSource()
: Returns the source of the current page.
Example:String pageSource = driver.getPageSource();
close()
: Closes the current browser window.
Example:driver.close();
quit()
: Closes the browser and ends the session.
Example:driver.quit();
Navigation Commands
navigate().to(String url)
: Navigates to the given URL.
Example:driver.navigate().to("https://www.automationCodes.com");
navigate().back()
: Navigates back to the previous page.
Example:driver.navigate().back();
navigate().forward()
: Navigates forward to the next page.
Example:driver.navigate().forward();
navigate().refresh()
: Refreshes the current page.
Example:driver.navigate().refresh();
Window Management Commands
manage().window().maximize()
: Maximizes the current window.
Example:driver.manage().window().maximize();
manage().window().minimize()
: Minimizes the current window.
Example:driver.manage().window().minimize();
manage().window().fullscreen()
: Sets the window to full screen mode.
Example:driver.manage().window().fullscreen();
manage().window().getSize()
: Returns the size of the current window.
Example:Dimension size = driver.manage().window().getSize();
manage().window().setSize(Dimension targetSize)
: Sets the size of the current window.
Example:driver.manage().window().setSize(new Dimension(1024, 768));
manage().window().getPosition()
: Returns the position of the current window.
Example:Point position = driver.manage().window().getPosition();
manage().window().setPosition(Point targetPosition)
: Sets the position of the current window.
Example:driver.manage().window().setPosition(new Point(0, 0));
Frame and IFrame Commands
switchTo().frame(int index)
: Switches focus to the frame at the given index.
Example:driver.switchTo().frame(0);
switchTo().frame(String id)
: Switches focus to the frame with the given id.
Example:driver.switchTo().frame("frameName");
switchTo().frame(WebElement frameElement)
: Switches focus to the given frame element.
Example:driver.switchTo().frame(driver.findElement(By.id("frameID")));
switchTo().defaultContent()
: Switches focus back to the default content.
Example:driver.switchTo().defaultContent();
switchTo().parentFrame()
: Switches focus to the parent frame.
Example:driver.switchTo().parentFrame();
Alert Commands
switchTo().alert()
: Switches focus to an open alert.
Example:Alert alert = driver.switchTo().alert();
alert().accept()
: Accepts the currently open alert.
Example:alert.accept();
alert().dismiss()
: Dismisses the currently open alert.
Example:alert.dismiss();
alert().sendKeys(String keysToSend)
: Sends keys to the currently open alert.
Example:alert.sendKeys("example text");
alert().getText()
: Gets the text of the currently open alert.
Example:String alertText = alert.getText();
Element Interaction Commands
findElement(By locator)
: Finds the first element matching the locator.
Example:WebElement element = driver.findElement(By.id("elementId"));
findElements(By locator)
: Finds all elements matching the locator.
Example:List<WebElement> elements = driver.findElements(By.className("elementsClass"));
isDisplayed()
: Returns true if the element is visible.
Example:boolean isVisible = element.isDisplayed();
isEnabled()
: Returns true if the element is enabled.
Example:boolean isEnabled = element.isEnabled();
isSelected()
: Returns true if the element is selected.
Example:boolean isSelected = element.isSelected();
getAttribute(String name)
: Returns the value of the given attribute.
Example:String attributeValue = element.getAttribute("name");
getCssValue(String propertyName)
: Returns the value of the given CSS property.
Example:String cssValue = element.getCssValue("color");
getLocation()
: Returns the location of the element.
Example:Point location = element.getLocation();
getSize()
: Returns the size of the element.
Example:Dimension elementSize = element.getSize();
getTagName()
: Returns the tag name of the element.
Example:String tagName = element.getTagName();
getText()
: Returns the text content of the element.
Example:String text = element.getText();
Element Action Commands
click()
: Clicks the element.
Example:element.click();
submit()
: Submits the element.
Example:element.submit();
sendKeys(CharSequence... keysToSend)
: Sends keys to the element.
Example:element.sendKeys("input text");
clear()
: Clears the content of the element.
Example:element.clear();
Wait Commands
implicitlyWait(long timeout, TimeUnit unit)
: Sets the implicit wait timeout.
Example:driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
setScriptTimeout(long timeout, TimeUnit unit)
: Sets the script timeout.
Example:driver.manage().timeouts().setScriptTimeout(5, TimeUnit.SECONDS);
pageLoadTimeout(long timeout, TimeUnit unit)
: Sets the page load timeout.
Example:driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
ExpectedConditions
: Provides methods for common explicit waits.
Example:WebDriverWait wait = new WebDriverWait(driver, 10); wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("elementId")));
Keyboard and Mouse Action Commands
Actions
: Provides methods for advanced user interactions.
Example:Actions actions = new Actions(driver);
keyDown(Keys theKey)
: Performs a key press.
Example:actions.keyDown(Keys.CONTROL).sendKeys("a").perform();
keyUp(Keys theKey)
: Performs a key release.
Example:actions.keyUp(Keys.CONTROL).perform();
click()
: Clicks the mouse.
Example:actions.click().perform();
doubleClick()
: Double clicks the mouse.
Example:actions.doubleClick(element).perform();
contextClick()
: Performs a context click (right-click).
Example:actions.contextClick(element).perform();
dragAndDrop(WebElement source, WebElement target)
: Drags and drops an element.
Example:actions.dragAndDrop(sourceElement, targetElement).perform();
moveToElement(WebElement target)
: Moves the mouse to the given element.
Example:actions.moveToElement(element).perform();
Cookies Management Commands
manage().getCookies()
: Returns all cookies.
Example:Set<Cookie> cookies = driver.manage().getCookies();
manage().getCookieNamed(String name)
: Returns the cookie with the given name.
Example:Cookie cookie = driver.manage().getCookieNamed("cookieName");
manage().addCookie(Cookie cookie)
: Adds a cookie.
Example:driver.manage().addCookie(new Cookie("cookieName", "cookieValue"));
manage().deleteCookieNamed(String name)
: Deletes the cookie with the given name.
Example:driver.manage().deleteCookieNamed("cookieName");
manage().deleteCookie(Cookie cookie)
: Deletes the given cookie.
Example:driver.manage().deleteCookie(cookie);
manage().deleteAllCookies()
: Deletes all cookies.
Example:driver.manage().deleteAllCookies();
Screen Capture Commands
getScreenshotAs(OutputType<T> target)
: Captures a screenshot of the current page.
Example:File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
Session Management Commands
getSessionId()
: Returns the current session ID.
Example:SessionId sessionId = ((RemoteWebDriver)driver).getSessionId();
Browser Options and Capabilities Commands
ChromeOptions
: Configures Chrome options.
Example:ChromeOptions options = new ChromeOptions();
FirefoxOptions
: Configures Firefox options.
Example:FirefoxOptions options = new FirefoxOptions();
EdgeOptions
: Configures Edge options.
Example:EdgeOptions options = new EdgeOptions();
DesiredCapabilities
: Configures browser capabilities.
Example:DesiredCapabilities capabilities = new DesiredCapabilities();
Logs Management Commands
manage().logs()
: Provides access to log types.
Example:LogEntries logs = driver.manage().logs().get(LogType.BROWSER);
manage().logs().get(LogType logType)
: Gets the log entries for the given log type.
Example:LogEntries logEntries = driver.manage().logs().get(LogType.DRIVER);
JavaScript Execution Commands
executeScript(String script, Object... args)
: Executes the given JavaScript.
Example:((JavascriptExecutor)driver).executeScript("window.scrollTo(0, document.body.scrollHeight);");
executeAsyncScript(String script, Object... args)
: Executes the given asynchronous JavaScript.
Example:((JavascriptExecutor)driver).executeAsyncScript("window.setTimeout(arguments[arguments.length - 1], 5000);");
File Upload and Download Commands
sendKeys(CharSequence... keysToSend)
: Sends keys to an input element.
Example:driver.findElement(By.id("uploadElement")).sendKeys("/path/to/file");
Proxy Management Commands
Proxy
: Configures proxy settings.
Example:Proxy proxy = new Proxy();
Mobile Web Testing Commands (Specific to Appium)
TouchAction
: Provides methods for touch interactions.
Example:TouchAction touchAction = new TouchAction(driver);
tap(int x, int y)
: Performs a tap at the given coordinates.
Example:touchAction.tap(PointOption.point(100, 200)).perform();
press(int x, int y)
: Performs a press at the given coordinates.
Example:touchAction.press(PointOption.point(100, 200)).perform();
release(int x, int y)
: Performs a release at the given coordinates.
Example:touchAction.release().perform();
moveTo(int x, int y)
: Moves the touch to the given coordinates.
Example:touchAction.moveTo(PointOption.point(200, 300)).perform();
Remote WebDriver Commands
RemoteWebDriver
: Allows running tests on remote machines.
Example:RemoteWebDriver remoteDriver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);
WebDriver Timeout Commands
manage().timeouts().implicitlyWait(long timeout, TimeUnit unit)
: Sets the implicit wait timeout.
Example:driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
manage().timeouts().setScriptTimeout(long timeout, TimeUnit unit)
: Sets the script timeout.
Example:driver.manage().timeouts().setScriptTimeout(5, TimeUnit.SECONDS);
manage().timeouts().pageLoadTimeout(long timeout, TimeUnit unit)
: Sets the page load timeout.
Example:driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
0 Comments