All Selenium Command with Examples

All Selenium Commands with Examples


Browser Commands

  1. get(String url): Navigates to the given URL.
    Example: driver.get("https://www.automationCodes.com");

  2. getCurrentUrl(): Returns the URL of the current page.
    Example: String currentUrl = driver.getCurrentUrl();

  3. getTitle(): Returns the title of the current page.
    Example: String title = driver.getTitle();

  4. getPageSource(): Returns the source of the current page.
    Example: String pageSource = driver.getPageSource();

  5. close(): Closes the current browser window.
    Example: driver.close();

  6. quit(): Closes the browser and ends the session.
    Example: driver.quit();

Navigation Commands

  1. navigate().to(String url): Navigates to the given URL.
    Example: driver.navigate().to("https://www.automationCodes.com");

  2. navigate().back(): Navigates back to the previous page.
    Example: driver.navigate().back();

  3. navigate().forward(): Navigates forward to the next page.
    Example: driver.navigate().forward();

  4. navigate().refresh(): Refreshes the current page.
    Example: driver.navigate().refresh();

Window Management Commands

  1. manage().window().maximize(): Maximizes the current window.
    Example: driver.manage().window().maximize();

  2. manage().window().minimize(): Minimizes the current window.
    Example: driver.manage().window().minimize();

  3. manage().window().fullscreen(): Sets the window to full screen mode.
    Example: driver.manage().window().fullscreen();

  4. manage().window().getSize(): Returns the size of the current window.
    Example: Dimension size = driver.manage().window().getSize();

  5. manage().window().setSize(Dimension targetSize): Sets the size of the current window.
    Example: driver.manage().window().setSize(new Dimension(1024, 768));

  6. manage().window().getPosition(): Returns the position of the current window.
    Example: Point position = driver.manage().window().getPosition();

  7. 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

  1. switchTo().frame(int index): Switches focus to the frame at the given index.
    Example: driver.switchTo().frame(0);

  2. switchTo().frame(String id): Switches focus to the frame with the given id.
    Example: driver.switchTo().frame("frameName");

  3. switchTo().frame(WebElement frameElement): Switches focus to the given frame element.
    Example: driver.switchTo().frame(driver.findElement(By.id("frameID")));

  4. switchTo().defaultContent(): Switches focus back to the default content.
    Example: driver.switchTo().defaultContent();

  5. switchTo().parentFrame(): Switches focus to the parent frame.
    Example: driver.switchTo().parentFrame();

Alert Commands

  1. switchTo().alert(): Switches focus to an open alert.
    Example: Alert alert = driver.switchTo().alert();

  2. alert().accept(): Accepts the currently open alert.
    Example: alert.accept();

  3. alert().dismiss(): Dismisses the currently open alert.
    Example: alert.dismiss();

  4. alert().sendKeys(String keysToSend): Sends keys to the currently open alert.
    Example: alert.sendKeys("example text");

  5. alert().getText(): Gets the text of the currently open alert.
    Example: String alertText = alert.getText();

Element Interaction Commands

  1. findElement(By locator): Finds the first element matching the locator.
    Example: WebElement element = driver.findElement(By.id("elementId"));

  2. findElements(By locator): Finds all elements matching the locator.
    Example: List<WebElement> elements = driver.findElements(By.className("elementsClass"));

  3. isDisplayed(): Returns true if the element is visible.
    Example: boolean isVisible = element.isDisplayed();

  4. isEnabled(): Returns true if the element is enabled.
    Example: boolean isEnabled = element.isEnabled();

  5. isSelected(): Returns true if the element is selected.
    Example: boolean isSelected = element.isSelected();

  6. getAttribute(String name): Returns the value of the given attribute.
    Example: String attributeValue = element.getAttribute("name");

  7. getCssValue(String propertyName): Returns the value of the given CSS property.
    Example: String cssValue = element.getCssValue("color");

  8. getLocation(): Returns the location of the element.
    Example: Point location = element.getLocation();

  9. getSize(): Returns the size of the element.
    Example: Dimension elementSize = element.getSize();

  10. getTagName(): Returns the tag name of the element.
    Example: String tagName = element.getTagName();

  11. getText(): Returns the text content of the element.
    Example: String text = element.getText();

Element Action Commands

  1. click(): Clicks the element.
    Example: element.click();

  2. submit(): Submits the element.
    Example: element.submit();

  3. sendKeys(CharSequence... keysToSend): Sends keys to the element.
    Example: element.sendKeys("input text");

  4. clear(): Clears the content of the element.
    Example: element.clear();

Wait Commands

  1. implicitlyWait(long timeout, TimeUnit unit): Sets the implicit wait timeout.
    Example: driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

  2. setScriptTimeout(long timeout, TimeUnit unit): Sets the script timeout.
    Example: driver.manage().timeouts().setScriptTimeout(5, TimeUnit.SECONDS);

  3. pageLoadTimeout(long timeout, TimeUnit unit): Sets the page load timeout.
    Example: driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);

  4. 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

  1. Actions: Provides methods for advanced user interactions.
    Example: Actions actions = new Actions(driver);

  2. keyDown(Keys theKey): Performs a key press.
    Example: actions.keyDown(Keys.CONTROL).sendKeys("a").perform();

  3. keyUp(Keys theKey): Performs a key release.
    Example: actions.keyUp(Keys.CONTROL).perform();

  4. click(): Clicks the mouse.
    Example: actions.click().perform();

  5. doubleClick(): Double clicks the mouse.
    Example: actions.doubleClick(element).perform();

  6. contextClick(): Performs a context click (right-click).
    Example: actions.contextClick(element).perform();

  7. dragAndDrop(WebElement source, WebElement target): Drags and drops an element.
    Example: actions.dragAndDrop(sourceElement, targetElement).perform();

  8. moveToElement(WebElement target): Moves the mouse to the given element.
    Example: actions.moveToElement(element).perform();

Cookies Management Commands

  1. manage().getCookies(): Returns all cookies.
    Example: Set<Cookie> cookies = driver.manage().getCookies();

  2. manage().getCookieNamed(String name): Returns the cookie with the given name.
    Example: Cookie cookie = driver.manage().getCookieNamed("cookieName");

  3. manage().addCookie(Cookie cookie): Adds a cookie.
    Example: driver.manage().addCookie(new Cookie("cookieName", "cookieValue"));

  4. manage().deleteCookieNamed(String name): Deletes the cookie with the given name.
    Example: driver.manage().deleteCookieNamed("cookieName");

  5. manage().deleteCookie(Cookie cookie): Deletes the given cookie.
    Example: driver.manage().deleteCookie(cookie);

  6. manage().deleteAllCookies(): Deletes all cookies.
    Example: driver.manage().deleteAllCookies();

Screen Capture Commands

  1. getScreenshotAs(OutputType<T> target): Captures a screenshot of the current page.
    Example: File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

Session Management Commands

  1. getSessionId(): Returns the current session ID.
    Example: SessionId sessionId = ((RemoteWebDriver)driver).getSessionId();

Browser Options and Capabilities Commands

  1. ChromeOptions: Configures Chrome options.
    Example: ChromeOptions options = new ChromeOptions();

  2. FirefoxOptions: Configures Firefox options.
    Example: FirefoxOptions options = new FirefoxOptions();

  3. EdgeOptions: Configures Edge options.
    Example: EdgeOptions options = new EdgeOptions();

  4. DesiredCapabilities: Configures browser capabilities.
    Example: DesiredCapabilities capabilities = new DesiredCapabilities();

Logs Management Commands

  1. manage().logs(): Provides access to log types.
    Example: LogEntries logs = driver.manage().logs().get(LogType.BROWSER);

  2. 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

  1. executeScript(String script, Object... args): Executes the given JavaScript.
    Example: ((JavascriptExecutor)driver).executeScript("window.scrollTo(0, document.body.scrollHeight);");

  2. 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

  1. sendKeys(CharSequence... keysToSend): Sends keys to an input element.
    Example: driver.findElement(By.id("uploadElement")).sendKeys("/path/to/file");

Proxy Management Commands

  1. Proxy: Configures proxy settings.
    Example: Proxy proxy = new Proxy();

Mobile Web Testing Commands (Specific to Appium)

  1. TouchAction: Provides methods for touch interactions.
    Example: TouchAction touchAction = new TouchAction(driver);

  2. tap(int x, int y): Performs a tap at the given coordinates.
    Example: touchAction.tap(PointOption.point(100, 200)).perform();

  3. press(int x, int y): Performs a press at the given coordinates.
    Example: touchAction.press(PointOption.point(100, 200)).perform();

  4. release(int x, int y): Performs a release at the given coordinates.
    Example: touchAction.release().perform();

  5. moveTo(int x, int y): Moves the touch to the given coordinates.
    Example: touchAction.moveTo(PointOption.point(200, 300)).perform();

Remote WebDriver Commands

  1. RemoteWebDriver: Allows running tests on remote machines.
    Example: RemoteWebDriver remoteDriver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);

WebDriver Timeout Commands

  1. manage().timeouts().implicitlyWait(long timeout, TimeUnit unit): Sets the implicit wait timeout.
    Example: driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

  2. manage().timeouts().setScriptTimeout(long timeout, TimeUnit unit): Sets the script timeout.
    Example: driver.manage().timeouts().setScriptTimeout(5, TimeUnit.SECONDS);

  3. manage().timeouts().pageLoadTimeout(long timeout, TimeUnit unit): Sets the page load timeout.
    Example: driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);

Post a Comment

0 Comments