Selenium cheat sheet

SDET | Writing about test automation, Java, Selenium, and everything in between. Helping devs build better software through clean, reliable testing.
Automate Browser navigations driver.get("link"); driver.navigate().to("link"); driver.navigate.back(); driver.navigate.forward(); driver.navigate.refresh();Window Properties getWindowHandle(); getWindowHandles(); switchTo().window(windowHandle); manage().window().maximize(); manage().window().minimize(); manage().window().fullScreen(); manage().window().getSize(); manage().window().setSize(Dimension); manage().window().getPosition(); manage().window().setPosition(Point);Static and Dynamic dropdown Static dropdown: WebElement dropdown = driver.findElement(); Select select = new Select(dropdown); select.selectByVisibleText("text"); select.selectByValue("Value 2"); select.selectByIndex(3); Dynamic dropdown: Locate the dropdown and wait for the dropdown options to load WebDriverWait wait = new WebDriverWait(driver.Duration.ofSeconds(10));Handling Checkboxes WebElement checkbox = driver.findElement(); checkbox.isSelected(); checkbox.isEnabled(); checkbox.isDisplayed(); Handling multiple checkboxes: // Locate all checkboxes using a common locator List<WebElement> checkboxes = driver.findElements(By.cssSelector("input[type='checkbox']")); // Loop and click each checkbox if not already selected for (WebElement checkbox : checkboxes) { if (!checkbox.isSelected()) { checkbox.click(); } }Alerts Alert alert = driver.switchTo().alert(); alert.accept(); alert.dismiss(); alert.getText(); alert.sendKeys("text");Waits Implicit wait: driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10)); Explicit wait: WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username"))); ExpectedConditions.visibilityOfElementLocated(By) ExpectedConditions.elementToBeClickable(By) ExpectedConditions.presenceOfElementLocated(By) ExpectedConditions.alertIsPresent() ExpectedConditions.frameToBeAvailableAndSwitchToIt() Fluent wait: Wait<WebDriver> wait = new FluentWait<>(driver) .withTimeout(Duration.ofSeconds(30)) .pollingEvery(Duration.ofSeconds(5)) .ignoring(NoSuchElementException.class); WebElement element = wait.until(driver -> driver.findElement(By.id("dynamicElement")));Iframes and child windows driver.switchTo().frame(0); driver.switchTo().frame("iframeName"); WebElement iframe = driver.findElement(By.xpath("//iframe[@id='frame1']")); driver.switchTo().frame(iframe); driver.switchTo().defaultContent(); // back to main page driver.switchTo().parentFrame(); // back to immediate parent iframe (if nested) Child windows: Store the main window handle String mainWindow = driver.getWindowHandle(); Click to open new window/tab driver.findElement(By.id("openWindow")).click(); Get all window handles Set<String> allWindows = driver.getWindowHandles(); for (String handle : allWindows) { if (!handle.equals(mainWindow)) { driver.switchTo().window(handle); // switch to child window System.out.println("Child Title: " + driver.getTitle()); // Perform actions driver.close(); // close child if needed } } Switch back to main window driver.switchTo().window(mainWindow);Scrolling JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("window.scrollBy(0, 500)"); js.executeScript("window.scrollTo(0, document.body.scrollHeight)"); js.executeScript("window.scrollTo(0, 0)"); WebElement element = driver.findElement(By.id("elementId")); js.executeScript("arguments[0].scrollIntoView(true);", element);Handling https certification ChromeOptions options = new ChromeOptions(); options.setAcceptInsecureCerts(true); WebDriver driver = new ChromeDriver(options); driver.get("https://your-ssl-warning-site.com");Screenshots TakesScreenshot ts = (TakesScreenshot) driver; File src = ts.getScreenshotAs(OutputType.FILE); File dest = new File("screenshot.png"); FileUtils.copyFile(src, dest);Automate broken linksAssertions in selenium Assert.assertEquals(actualTitle, expectedTitle); Assert.assertTrue(element.isDisplayed()); Assert.assertFalse(driver.getPageSource().contains("Error")); Assert.assertNotNull(driver.findElement(By.id("logo")));Handling cookies Cookie cookie = new Cookie("test_cookie", "123456"); driver.manage().addCookie(cookie); Set<Cookie> allCookies = driver.manage().getCookies(); Cookie sessionCookie = driver.manage().getCookieNamed("JSESSIONID"); System.out.println(sessionCookie.getValue()); driver.manage().deleteCookieNamed("test_cookie");Mouse Actions WebElement menu = driver.findElement(By.id("menu")); Actions actions = new Actions(driver); actions.moveToElement(menu).perform(); actions.moveToElement(button).click().perform(); actions.contextClick(element).perform(); actions.doubleClick(element).perform(); actions.clickAndHold(element).perform(); actions.dragAndDrop(source, target).perform();



