Selenium WebDriver Cheat Sheet
Selenium WebDriver Cheat Sheet
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<
WebDriverManager:
Chrome WebDriverManager.chromedriver().setup();
Firefox WebDriverManager.firefoxdriver().setup();
IE WebDriverManager.iedriver().setup();
Browser Navigation
Navigate to driver.get("https://selenium.dev");
driver.navigate().to("https://selenium.dev");
Get current URL driver.getCurrentUrl();
Back driver.navigate().back();
Forward driver.navigate().forward();
Refresh driver.navigate().refresh();
TheTestingFreak
Handle Windows and tabs
Element Validation
isEnabled driver.findElement(By.name("btnK")).isEnabled();
isSelected driver.findElement(By.name("yes")). isSelected();
isDisplayed driver.findElement(By.name("btnK")).isDisplayed();
Handling Frames
TheTestingFreak
Window Management
Handling Alerts
Get Commands
TheTestingFreak
Locators
Keyboard events
SendKeys() driver.findElement(By.name("q")).sendKeys("q");
clear() driver.findElement(By.name("q")).clear();
keyUp() Actions action = new Actions(driver);
action.keyDown(Keys.CONTROL);
keyDown() Actions action = new Actions(driver);
action.keyUp(Keys.CONTROL);
TheTestingFreak
Mouse Events
Dropdowns
TheTestingFreak
WebElements
FindElement It returns first matching single WebElement reference
Syntax:
WebElement findElement(By by)
FindElements It returns a list of all matching WebElements
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
=driver.findElement(By.tagName("form")); WebElement
searchBox = searchForm.findElement(By.name("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 = driver.findElement(By.tagName("div"));
List<WebElement> elements =
element.findElements(By.tagName("p"));
Cookies
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
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
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
Screenshots
TakeScreenshot:
TheTestingFreak