This document provides a summary of Selenium WebDriver operations in Kotlin, including initializing drivers, locating elements, basic and advanced browser and element operations, and advanced browser configurations. It covers initializing different browser drivers, finding elements using various locators, navigating pages, interacting with elements, handling alerts and frames, taking screenshots, managing cookies and proxies, and customizing browser profiles.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
27 views1 page
Most Complete Webdriver Cheat Sheet Kotlin
This document provides a summary of Selenium WebDriver operations in Kotlin, including initializing drivers, locating elements, basic and advanced browser and element operations, and advanced browser configurations. It covers initializing different browser drivers, finding elements using various locators, navigating pages, interacting with elements, handling alerts and frames, taking screenshots, managing cookies and proxies, and customizing browser profiles.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1
Most Complete Selenium
WebDriver Kotlin Cheat Sheet
Initialize Advanced Browser Operations
// Maven: selenium-chrome-driver // Handle JavaScript pop-ups import org.openqa.selenium.chrome.ChromeDriver val alert = driver.switchTo().alert() val driver: WebDriver = ChromeDriver() alert.accept() // Maven: selenium-firefox-driver alert.dismiss() import org.openqa.selenium.firefox.FirefoxDriver // Switch between browser windows or tabs val driver: WebDriver = FirefoxDriver() val windowHandles = driver.windowHandles // Maven: selenium-edge-driver val firstTab = windowHandles.toTypedArray().first() import org.openqa.selenium.firefox.EdgeDriver driver.switchTo().window(windowHandles.toTypedArray()[2]) val driver: WebDriver = EdgeDriver() // Navigation history // Maven: selenium-ie-driver driver.navigate().back() import org.openqa.selenium.ie.InternetExplorerDriver driver.navigate().refresh() val driver: WebDriver = InternetExplorerDriver() driver.navigate().forward() // Maven: selenium-safari-driver // Maximize window import org.openqa.selenium.safari.SafariDriver driver.manage().window().maximize() val driver: WebDriver = SafariDriver() // Add a new cookie val newCookie = Cookie("customName", "customValue") driver.manage().addCookie(newCookie) // Get all cookies Locators val cookies = driver.manage().cookies // Delete a cookie by name driver.findElement(By.className("className")) driver.manage().deleteCookieNamed("CookieName") driver.findElement(By.cssSelector("css")) // Delete all cookies driver.findElement(By.id("id")) driver.manage().deleteAllCookies() driver.findElement(By.linkText("text")) //Taking a full-screen screenshot driver.findElement(By.name("name")) val screenshotFile = (driver as driver.findElement(By.partialLinkText("pText")) TakesScreenshot).getScreenshotAs(OutputType.FILE) driver.findElement(By.tagName("input")) val tempDir = getProperty("java.io.tmpdir") driver.findElement(By.xpath("//*[@id='editor']")) val destFile = File(Paths.get(tempDir, "$fileName.png").toString()) // Find multiple elements FileUtils.getFileUtils().copyFile(screenshotFile, destFile) val anchors = driver.findElements(By.tagName("a")) // Wait until a page is fully loaded via JavaScript // Search for an element inside another WebDriverWait(driver, 30).until<Any> val div = { (it as JavascriptExecutor).executeScript("return d driver.findElement(By.tagName("div")).findElement(By.tagName("a")) ocument.readyState" ) as String == "complete" } // Switch to frames driver.switchTo().frame(1) driver.switchTo().frame("frameName") Basic Browser Operations val element = driver.findElement(By.id("id")) driver.switchTo().frame(element) // Switch to the default document // Navigate to a page driver.switchTo().defaultContent() driver.navigate().to("http://google.com") // Get the title of the page val title = driver.title // Get the current URL Advanced Browser Configurations val url = driver.currentUrl // Get the current page HTML source val html = driver.pageSource // Use a specific Firefox profile val profile = ProfilesIni() val firefoxProfile = profile.getProfile("ProfileName") val firefoxOptions = FirefoxOptions() Basic Elements Operations firefoxOptions.setProfile(firefoxProfile) driver = FirefoxDriver(firefoxOptions) // Set a HTTP proxy Firefox val element = driver.findElement(By.id("id")) val profile = ProfilesIni() element.click() val firefoxProfile = FirefoxProfile() element.sendKeys("someText") firefoxProfile.setPreference("network.proxy.type", 1) element.clear() firefoxProfile.setPreference("network.proxy.http", "myproxy.com") element.submit() firefoxProfile.setPreference("network.proxy.http_port", 3239) val innerText = element.text val firefoxOptions = FirefoxOptions() val isEnabled = element.isEnabled firefoxOptions.setProfile(firefoxProfile) val isDisplayed = element.isDisplayed driver = FirefoxDriver(firefoxOptions) val isSelected = element.isSelected // Set a HTTP proxy Chrome val element = driver.findElement(By.id("id")) val proxy = Proxy() val select = Select(element) proxy.setProxyType(Proxy.ProxyType.MANUAL) select.selectByIndex(1) proxy.setAutodetect(false) select.selectByVisibleText("Ford") proxy.setSslProxy("127.0.0.1:3239") select.selectByValue("ford") val chromeOptions = ChromeOptions() select.deselectAll() chromeOptions.setProxy(proxy) select.deselectByIndex(1) driver = ChromeDriver(chromeOptions) select.deselectByVisibleText("Ford") // Accept all certificates Firefox select.deselectByValue("ford") val firefoxProfile = FirefoxProfile() val allSelected: List<WebElement> = select.getAllSelectedOptions() firefoxProfile.setAcceptUntrustedCertificates(true) val isMultipleSelect: Boolean = select.isMultiple() firefoxProfile.setAssumeUntrustedCertificateIssuer(false) val firefoxOptions = FirefoxOptions() firefoxOptions.setProfile(firefoxProfile) Advanced Elements Operations driver = FirefoxDriver(firefoxOptions) // Accept all certificates Chrome val chromeOptions = ChromeOptions() // Drag and Drop chromeOptions.addArguments("--ignore-certificate-errors") val element: WebElement = driver.findElement(By.xpath( driver = ChromeDriver(chromeOptions) "//*[@id='project']/p[1]/div/div[2]")) // Set Chrome options Actions(driver).dragAndDropBy(element, 30, 0).build().perform() val chromeOptions = ChromeOptions() // Check if an element is visible chromeOptions.addArguments("user-data-dir=C:PathToUserData") Assert.assertTrue(driver.findElement(By.xpath( driver = ChromeDriver(chromeOptions) "//*[@id='tve_editor']/div")).isDisplayed) // Turn off the JavaScript Firefox // Upload a file val profile = ProfilesIni() val element = driver.findElement(By.id("RadUpload1file0")) val firefoxProfile = profile.getProfile("ProfileName") val filePath = "D://WebDriver.Series.Tests//WebDriver.xml" firefoxProfile.setPreference("javascript.enabled", false) element.sendKeys(filePath) val firefoxOptions = FirefoxOptions() // Scroll focus to control firefoxOptions.setProfile(firefoxProfile) val link = driver.findElement(By.partialLinkText("Previous post")) driver = FirefoxDriver(firefoxOptions) (driver as JavascriptExecutor).executeScript( // Set the default page load timeout "window.scroll(0, ${link.location.getY()});") driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS) // Taking an element screenshot // Start Firefox with plugins val element = driver.findElement(By.xpath( val profile = FirefoxProfile() "//*[@id='tve_editor']/div")) firefoxProfile.addExtension(File("C:extensionsLocationextension.xpi")) val screenshotFile = val firefoxOptions = FirefoxOptions() (driver as TakesScreenshot).getScreenshotAs(OutputType.FILE) firefoxOptions.setProfile(firefoxProfile) val fullImg = ImageIO.read(screenshotFile) driver = FirefoxDriver(firefoxOptions) val point = element.location // Start Chrome with an unpacked extension val elementWidth = element.size.getWidth() val chromeOptions = ChromeOptions() val elementHeight = element.size.getHeight() chromeOptions.addArguments("load-extension=/path/to/extension") val eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(), driver = ChromeDriver(chromeOptions) elementWidth, elementHeight) // Start Chrome with a packed extension ImageIO.write(eleScreenshot, "png", screenshotFile) val chromeOptions = ChromeOptions() val tempDir: String = getProperty("java.io.tmpdir") chromeOptions.addExtensions(File("local/path/to/extension.crx")) val destFile = File(Paths.get(tempDir, "$fileName.png").toString()) driver = ChromeDriver(chromeOptions) FileUtils.getFileUtils().copyFile(screenshotFile, destFile) // Change the default files’ save location // Focus on a control val firefoxProfile = FirefoxProfile() val link = driver.findElement(By.partialLinkText("Previous post")) val downloadFilepath = "c:temp" Actions(driver).moveToElement(link).build().perform() firefoxProfile.setPreference("browser.download.folderList", 2) // Wait for visibility of an element firefoxProfile.setPreference("browser.download.dir", downloadFilepath) WebDriverWait(driver, 30).until( firefoxProfile.setPreference("browser.download.manager.alertOnEXEOpen", ExpectedConditions.visibilityOfAllElementsLocatedBy( false) By.xpath("//*[@id='tve_editor']/div[2]/div[2]/div/div"))) firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/msword, application/binary, application/ris, text/csv, image/png, application/pdf, text/html, text/plain, application/zip, application/x-zip, application/x-zip-compressed, application/download, application/octet-stream") val firefoxOptions = FirefoxOptions() firefoxOptions.setProfile(firefoxProfile) driver = FirefoxDriver(firefoxOptions)