Category Menu


Interview Questions Category Menu

Interview Questions

Cheat Sheet Category Menu

Cheat Sheets

Selenium Cheat Sheet

Posted on

//Webdriver Methods
driver.close(); //Close the current window
//Navigation Methods
driver.navigate().to(""); // Load a new web page in the current browser window
driver.navigate().back(); // Move back a single "item" in the browser's history.
driver.navigate().forward(); // Move a single "item" forward in the browser's history.
driver.navigate().refresh(); // Refresh the current page
//Managing the current window
driver.manage().window().fullscreen(); //Fullscreen the current window if it is not already fullscreen
driver.manage().window().maximize(); // Maximizes the current window if it is not already maximized
driver.manage().window().minimize(); // Minimizes the current window if it is not already minimized
//managing driver timeouts
//Specifies the amount of time the driver should wait when searching for an element if it is not immediately present.
//Sets the amount of time to wait for a page load to complete before throwing an error.
//Sets the amount of time to wait for an asynchronous script to finish execution before throwing an error. 
// Switch Methods
driver.switchTo().alert(); // Switches to the active modal dialog
driver.switchTo().window("window2"); // Switch focus to window2
driver.switchTo().defaultContent(); // Switch to the main document when a page contains iframes
driver.switchTo().parentFrame(); // Switch focus to the parent context.
driver.switchTo().activeElement(); // Switches focus to the active element
//Alert Methods
Alert alert = driver.switchTo().alert();
alert.accept(); // To accept the alert dialog
alert.dismiss(); // To dismiss the alert dialog
alert.getText(); // To get alert dialog text
alert.sendKeys("sample"); // To enter text in prompt dialog
//Element Methods
WebElement element = driver.findElement("some_id"));
element.sendKeys("some text"); // To type into an element, which may set its value
element.getText(); // Get the visible text of this element
element.isDisplayed(); // Check if this element displayed
element.isEnabled(); // Check if this element enabled
element.getTagName(); // Get the tag name of this element; // Click this element
element.submit(); // It will submit the form
element.getAttribute("value"); // Get the value of the given attribute
element.getCssValue("float"); // Get the value of a given CSS property
element.findElement("someId")); // Find the first WebElement within element
element.findElements(By.className("items")); // Find WebElements within element
element.clear(); // It will reset element value
//Select Methods
Select select = new Select(element);
// Select the option at the given index.
// Select all options that have a value matching the argument.
select.selectByValue("some value");
// Select all options that display text matching the argument.
select.selectByVisibleText("Visible text");
// Clear all selected entries.
// Deselect the option at the given index.
// Deselect all options that have a value matching the argument.
select.deselectByValue("some value");
// Deselect all options that display text matching the argument.
select.deselectByVisibleText("visible text");
// Returns all selected options belonging to this select tag
// Returns the first selected option in this select tag
// Returns all options belonging to this select tag
// Check whether this select element support selecting multiple options
//Javascript Executor Methods
JavascriptExecutor je = (JavascriptExecutor) driver;
// Executes JavaScript in the context of the currently selected frame or window.
//Execute an asynchronous piece of JavaScript in the context of the currently selected frame or window.
je.executeAsyncScript("window.setTimeout(arguments[arguments.length - 1], 2000);");
// For emulating complex user gestures, use this class rather than using the Keyboard or Mouse directly
Actions actions = new Actions(driver);
actions.dragAndDrop(sourceElement, destinationElement); // Drang and drop; // Clicks at the current mouse location.
actions.clickAndHold(); // Clicks (without releasing) at the current mouse location
actions.contextClick(); // Performs a context-click at the current mouse location.
actions.doubleClick(); // Performs a double-click at the current mouse location.
actions.keyDown(Keys.ENTER); // Performs a modifier key press.
actions.moveToElement(destinationElement); // Moves the mouse to the middle of the element.
actions.release(); // Releases the depressed left mouse button at the current mouse location.
actions.sendKeys(); // Sends keys to the active element.
// Find Elements By
By.className("class_name"); // Find element by class name"element_id"); // Find element by id
By.cssSelector("#css_selector"); // Find element by css selector
By.linkText("link_text"); // Find element link text"name"); // Find element name
By.tagName("tag_name"); // Find element tag name
By.xpath("//xpath"); // Find element by xpath
By.partialLinkText("partial_link_text"); // Find element by partial link text