Selenium Cheat Sheet
Selenium Cheat Sheet
System.setProperty(“webdriver.chrome.driver”, “/path/to/chromedriver”);
Firefox:
System.setProperty(“webdriver.gecko.driver”, “/path/to/geckodriver”);
Edge:
System.setProperty(“webdriver.edge.driver”, “/path/to/MicrosoftWebDriver”);
2. Browser Initialization
Firefox
Chrome
Internet Explorer
Safari Driver
3. Desired capabilities
(Doc link )
Chrome:
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(“browserName”, “chrome”);
caps.setCapability(“browserVersion”, “80.0””);
caps.setCapability(“platformName”, “win10”);
WebDriver driver = new ChromeDriver(caps); // Pass the capabilities as an argument to the driver object
Firefox:
WebDriver driver = new FirefoxDriver(caps); // Pass the capabilities as an argument to the driver object
4. Browser options
Chrome: (Doc link )
WebDriver driver = new ChromeDriver(chromeOptions); // Pass the capabilities as an argument to the driver obje
WebDriver driver = new FirefoxDriver(caps); // Pass the capabilities as an argument to the driver object
Options VS Desired capabilities: There are two ways to specify capabilities. 1. ChromeOptions/FirefoxOptions class —
Recommended 2. Or you can specify the capabilities directly as part of the DesiredCapabilities — its usage in Java is
deprecated
5. Navigation
Navigate to URL — (doc link1 link2)
driver.get(“http://google.com”)
driver.navigate().to(“http://google.com”)
Myth — get() method waits till the page is loaded while navigate() does not.
Referring to the selenium official doc, get() method is a synonym for to() method. Both do the same thing.
All the URL loaded in browser will be stored in history and navigate method allows us to access it. Try executing the below code
driver.get(“http://madhank93.github.io/");
driver.get(“https://www.google.com/");
driver.navigate().back();
Refresh page
driver.navigate().refresh()
driver.navigate().forward()
driver.navigate().back()
driver.findElement()
When no match has found(0) throws NoSuchElementException
when 1 match found returns a WebElement instance
when 2 matches found returns only the first matching web element
driver.findElements()
7. Locator Strategy
(doc link)
By id
element = driver.findElement(By.id(“login”))
By Class Name
element = driver.findElement(By.className(“Content”));
By Name
element = driver.findElement(By.name(“pswd”));
By Tag Name
element = driver.findElement(By.tagName(“div”));
By Link Text
<a href=”#”>News</a>
element = driver.findElement(By.linkText(“News”));
By XPath
element = driver.findElement(By.xpath(“//input[[@placeholder]
(http://twitter.com/placeholder)=’Username’]”));
By CSS Selector
element = driver.findElement(By.cssSelector(“input.username”));
8. Click on an element
click() — method is used to click on en element
driver.findElement(By.className("Content")).click();
driver.findElement(By.className("email")).sendKeys(“[email protected]”);
driver.findElement(By.xpath(“//input[[@placeholder]
(http://twitter.com/placeholder)=’Username’]”)).clear();
11. Select a drop-down
(doc link)
<select id="country">
<option value="US">United States</option>
<option value="CA">Canada</option>
<option value="MX">Mexico</option>
</select>`
import org.openqa.selenium.support.ui.Select;
// Single selection
of the webpage
driver.switchTo.frame(string frameNameOrID) — mentioning the frame element or ID, the Driver will switch to that
specific frame
driver.switchTo.frame(WebElement frameElement) — mentioning the frame web element, the Driver will switch to that
specific frame
getWindowHandles() — used to retrieve a set of handles of the all the pages available
Implicit Wait—used to wait for a certain amount of time before throwing an exception
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Explicit Wait — used to wait until a certain condition occurs before executing the code.
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.presenceOfElementLocated(By.name("login")));
alertIsPresent()
elementSelectionStateToBe()
elementToBeClickable()
elementToBeSelected()
frameToBeAvaliableAndSwitchToIt()
invisibilityOfTheElementLocated()
invisibilityOfElementWithText()
presenceOfAllElementsLocatedBy()
presenceOfElementLocated()
textToBePresentInElement()
textToBePresentInElementLocated()
textToBePresentInElementValue()
titleIs()
titleContains()
visibilityOf()
visibilityOfAllElements()
visibilityOfAllElementsLocatedBy()
visibilityOfElementLocated()
Fluent Wait — defines the maximum amount of time to wait for a certain condition to appear
Firefox:
int x1 = position.getX();
int y1 = position.getY();`
Maximise window:
driver.manage().window().minimize();
Fullscreen window:
driver.manage().window().fullscreen();
By default, when Selenium WebDriver loads a page, it follows the normal pageLoadStrategy.
normal:
eager: When set to eager, Selenium WebDriver waits until DOMContentLoaded event fire is returned.
none: When set to none Selenium WebDriver only waits until the initial page is downloaded.
keyboard events:
keyDown()
keyUp()
sendKeys()
Mouse events:
clickAndHold()
contextClick() — peforms the mouse right click action
doubleClick()
dragAndDrop(source,target)
dragAndDropBy(source,xOffset,yOffset)
moveByOffset(xOffset,yOffset)
moveByElement()
release()
actions.perform();
22. Cookies
addCookie(arg)
getCookies()
getCookieNamed()
driver.manage().getCookieNamed("foo");
deleteCookieNamed()
driver.manage().deleteCookieNamed("foo");
deleteCookie()
Cookie cookie1 = new Cookie("test2", "cookie2");
driver.manage().addCookie(cookie1);
deleteAllCookies()
getScreenshotAs — used to Capture the screenshot and store it in the specified location. This method throws
WebDriverException. copy() method from File Handler class is used to store the screeshot in a destination folder
((JavascriptExecutor)driver).executeScript("alert('hello world');");
References:
[1]https://www.selenium.dev/selenium/docs/api/java/overview-summary.html
[2]https://www.selenium.dev/documentation/en/