Cheat sheet for Selenium
Setting up System Properties for browsers
Chrome [Link](“[Link]”,
“/path/to/chromedriver”);
Firefox [Link](“[Link]”,”/path/to/geckodriver”);
IE [Link](“[Link]", "Path/to/IEdriver ");
Safari:
• Run the following command from the terminal for the first time and type your
password at the prompt to authorise WebDriver
safaridriver --enable
• Enable the Developer menu from Safari preferences
• Check the Allow Remote Automation option from with the Develop menu
• Run the following command from the terminal for the first time and type
your password at the prompt to authorise WebDriver
/usr/bin/safaridriver -p 1337<
Initializing the driver
Chrome WebDriver driver = new ChromeDriver();
Firefox WebDriver driver = new FirefoxDriver();
IE WebDriver driver = new InternetExplorerDriver();
Safari WebDriver driver = new SafariDriver();
WebDriverManager:
Chrome [Link]().setup();
Firefox [Link]().setup();
IE [Link]().setup();
Browser Navigation
Navigate to [Link]("[Link]
[Link]().to("[Link]
Get current URL [Link]();
Back [Link]().back();
Forward [Link]().forward();
Refresh [Link]().refresh();
TheTestingFreak
Handle Windows and tabs
Get window handle of the [Link]();
current window
Get handles of all the [Link]();
windows opened
Open a new tab and switches [Link]().newWindow([Link]);
to the new tab
Open a new window and [Link]().newWindow(WindowType.
switches to new window WINDOW);
Switch back to old tab or [Link]().window(originalWindow);
window
Closed the current browser [Link]();
window
Close all the windows and [Link]();
tabs associated with the
Webdriver session
Element Validation
isEnabled [Link]([Link]("btnK")).isEnabled();
isSelected [Link]([Link]("yes")). isSelected();
isDisplayed [Link]([Link]("btnK")).isDisplayed();
Handling Frames
Switch to specific frame with [Link]().frame(webElement iframe);
the help of Webelement.
Switch to specific frame with [Link]().frame(string frameName);
the help of frame name
Switch to specific frame with [Link]().frame(string ID);
the help of frame ID
Switch to specific frame with [Link]().frame(int frameNumber);
the help of index
Switch back to the main [Link]().defaultContent();
window
TheTestingFreak
Window Management
To get the size of the browser [Link]().window().[Link]();
window [Link]().window().[Link]();
To restore the window and [Link]().window().setSize(new
sets the window size Dimension(1024, 768));
To get the coordinates of the [Link]().window().getPosition().getX();
browser window [Link]().window().getPosition().getY();
To set the window position [Link]().window().setPosition(new
x=100 and y=200 Point(100, 300));
To maximize the browser [Link]().window().maximize();
window
To minimize the browser [Link]().window().minimize();
window
To enlarge the browser and [Link]().window().fullscreen();
fills the entire screen, similar
to pressing F11 in most
browsers.
Handling Alerts
To capture the alert message. [Link]().[Link]();
Click 'OK' button on the alert. [Link]().[Link]();
Click 'Cancel' button on the alert. [Link]().[Link]();
To send some data to alert box. [Link]("Selenium");
Get Commands
To get the title of currently [Link]();
opened web page
To get the URL of the [Link]();
currently opened web page
To get the Page source of [Link]();
currently opened web page
To get the text of a [Link]([Link]("q")).getText();
webElement
To get the value of the [Link]([Link]("q")).getAttribute("type");
web element’s attribute
TheTestingFreak
Locators
By ID Locates the element whose ID attribute matches the search value
[Link]([Link](“email”);
By name Locates elements whose NAME attribute matches the search value
[Link]([Link](“email”);
By Cssselector Locates elements matching a CSS selector
[Link]([Link]("#email"));
By LinkText Locates anchor elements whose visible text matches the search value
[Link]([Link]("Forgotten password? "));
By Locates anchor elements whose visible text contains the search value
PartialLinkText [Link]([Link]("password?"));
By Tagname Locates elements whose tag name matches the search value
[Link]([Link]("a"));
By class name Locates elements whose class name contains the search value
[Link]([Link]("email"));
By Xpath Locates elements matching an XPath expression
[Link]([Link](“//input[@id='email'] ");
above() Locates the element, which appears above to the specified element
[Link](with([Link]("input")) .above(passwordField));
below() Locates the element which appears below to the specified element
[Link](with([Link]("input")) .below(emailAddressField));
toLeftOf() Locates the element which appears to left of the specified element
[Link](with([Link]("button")) .toLeftOf(submitButton));
toRightOf() Locates the element which appears to left of the specified element
[Link](with([Link]("button")).toRightOf(submitButton));
near() Locates the element which is at most 50px away from the specified
element.
[Link](with([Link]("input")).near(emailAddressLabel));
Keyboard events
SendKeys() [Link]([Link]("q")).sendKeys("q");
clear() [Link]([Link]("q")).clear();
keyUp() Actions action = new Actions(driver);
[Link]([Link]);
keyDown() Actions action = new Actions(driver);
[Link]([Link]);
TheTestingFreak
Mouse Events
clickAndHold Actions action = new Actions(driver);
[Link](webElement).build().perform();
contextClick Actions action = new Actions(driver);
[Link](webElement).build().perform();
doubleClick Actions action = new Actions(driver);
[Link](webElement).build().perform();
moveToElement Actions action = new Actions(driver);
[Link](webElement).build().perform();
moveByOffset Actions action = new Actions(driver);
[Link](xOffset,yOffset).build().perform();
dragAndDrop Actions action = new Actions(driver);
[Link](sourceEle,targetEle).build().perform();
dragAndDropBy Actions action = new Actions(driver);
[Link](sourceEle, targetEleXOffset,
targetEleYOffset).build().perform();
release Actions action = new Actions(driver);
[Link]().build().perform();
Dropdowns
Select an option from the dropdown based selectByIndex(1);
on index
Select an option from the dropdown based selectByValue("value1");
on its value attribute
Select an option from the dropdown based selectByVisibleText("White");
on the visible text
To clear the selected entries of deselectAll();
the dropdown
Deselect an option from deselectByIndex(5);
the dropdown based on index
Deselect an option from
the dropdown based on its value attribute deselectByValue(“value1”);
Deselect an option from
the dropdown based on the visible text deselectByVisibleText(“White”);
To get all the options in a dropdown or getOptions();
multi-select box
To get the first selected option of
the dropdown. getFirstSelectedOption();
To get all the selected options of
the dropdown getAllSelectedOptions();
TheTestingFreak
WebElements
FindElement It returns first matching single WebElement reference
When no match has found(0 elements) throws
NoSuchElementException
Syntax:
WebElement findElement(By by)
FindElements It returns a list of all matching WebElements
When no match has found(0 elements) throws emptyLis-
tofWebElementObject
Syntax:
List<WebElement> findElements(By by)
Find Element From It is used to find a child element within the context of
Element parent element
Syntax:
WebElement searchForm
=[Link]([Link]("form")); WebElement
searchBox = [Link]([Link]("q"));
Find Elements From It is used to find the list of matching child WebElements
Element within the context of parent element
Syntax:
WebElement element = [Link]([Link]("div"));
List<WebElement> elements =
[Link]([Link]("p"));
Cookies
Add Cookie [Link]().addCookie(new Cookie("key", "value"));
Get Named Cookie [Link]().getCookieNamed("foo");
Get All Cookies [Link]().getCookies();
Delete Cookie [Link]().deleteCookie(cookie1);
Delete All Cookies [Link]().deleteAllCookies();
TheTestingFreak
Waits
Implicit wait – An implicit wait is to tell the WebDriver to poll the DOM for a certain
amount of time when trying to find an element or elements if they are not
immediately available
[Link]().timeouts().implicitlyWait(10, [Link]);
Fluent Wait – It defines the maximum amount of time to wait for a certain
condition as well as the frequency to check for the condition to appear
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout([Link](30))
.pollingEvery([Link](5))
.ignoring([Link]);
WebElement foo = [Link](new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return [Link]([Link]("foo"));
}
});
Explicit Wait – It is used to wait until a certain condition occurs before proceeding
further in the code.
WebDriverWait wait = new WebDriverWait(driver,30);
[Link]([Link]([Link]("login")));
Screenshots
TakeScreenshot:
It is used to capture screenshot for current browsing context
File scrFile = ((TakesScreenshot)driver).getScreenshotAs([Link]);
[Link](scrFile, new File("./[Link]"));
TakeElementScreenshot:
It is Used to capture screenshot of an element for current browsing context.
File scrFile = [Link]([Link]);
[Link](scrFile, new File("./[Link]"));
TheTestingFreak