0% found this document useful (0 votes)
388 views

Selenium Cheat Sheet

The document provides an overview of Selenium basics including initializing browsers, locating elements, navigating pages, handling windows, frames and alerts. It also summarizes operations like taking screenshots, implicit and explicit waits, and using Selenium Grid for distributed testing across multiple machines.
Copyright
© © All Rights Reserved
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% found this document useful (0 votes)
388 views

Selenium Cheat Sheet

The document provides an overview of Selenium basics including initializing browsers, locating elements, navigating pages, handling windows, frames and alerts. It also summarizes operations like taking screenshots, implicit and explicit waits, and using Selenium Grid for distributed testing across multiple machines.
Copyright
© © All Rights Reserved
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/ 3

Selenium Cheat Sheet

Driver Initialization Basics

Firefox - WebDriver driver = new FirefoxDriver ();


Chrome - WebDriver driver = new ChromeDriver ();
Safari - WebDriver driver = new SafariDriver ();

Selenium Locators

By ID - driver.findElement(By.Id("q")).sendKeys("Se lenium 3");


By Name - driver.findElement(By.name("q")).sendKeys ("Selenium 3")
By Xpath - driver.findElement(By.xpath("//input[@id='q']")).sendKeys("Selenium 3");
Hyperlinks By Link - driver.FindElement(By.LinkText("edit this page")).Click();
Text
By DOM - dom =document.getElementById)'signinForm')
By CSS - driver.FindElement(By.CssSelector("#rightbar> .menu >li:nth-of-type(2) >
h4"));
By ClassName - driver.findElement(By.className("profileheader"));
By TagName - driver.findElement(By.tagName("select")).C lick();
By LinkText - driver.findElement(By.linkText("NextP age")).click();
By PartialLinkText - driver.findElement(By.partialLinkText(" NextP")).click();

Selenium Navigators

Navigate to URL - driver.get(“http://newexample.com”)


driver.navigate().to(“http://newexample.com”)

Refresh Page - driver.navigate().refresh()

Navigates forward in - driver.navigate().forward()


browser history

Navigates backward - driver.navigate().back()


in browser history

01
Windows
When web applications have multiple frames or windows, Selemium assigns each window
a unique ID which is called window handle. This ID is used to switch control among
windows.

String handle=driver.getWindowHandle();
Set<String> handles = getWindowHandles();
driver.switchTo().window(handle);

To switch to a newly created window:

String curWindow=driver.getWindowHandle();

To get all window hanles:

Set<String> handles = getWindowHandles();


For(string handle: handles)
{
If (!handle.equals(curWindow))
{
driver.switchTo().window(handle);
}
}

Frames

Using Frame Index - driver.switchTO().frame(1);


Using Name of Frame - driver.switchTo().frame(“name”)
Using Web element object - driver.switchTO().frame(element);
Get back to main document - driver.switchTO().defaultContent();

Alerts

Capture the alert message - driver.switchTO().alert.getText();


Click on the ‘OK’ button of the alert - driver.switchTO().alert.accept();
Click on the ‘Cancel’ button of the alert - driver.switchTO().alert.dismiss();
Send some data to the alert box - driver.switchTO().alert.sendKeys(“Text”);

02
Operations

Launch Webpage - get("www.webdriverinselenium.com");


Click Button - findElement(By.id("submit")).click();
Handle Alert - AlertAlertpopup = driver.switchTo().alert();
Disable a field - getElementsByName('') [0].setAttribute('disabled', '')
Enable a field - getElementsByName('') [0].removeAttribute('disabled');

Screenshot - File snapshot =


((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(snapshot, new File("C:\\screenshot.jpg"));

Print the title of the page - String pagetitle = driver.getTitle();


System.out.print(pagetitle);

Implicit Wait - manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);


Explicit Wait - WebDriverWait wait = new WebDriverWait(driver, 20);
Sleep - Thread.Sleep(10);

Selenium Grid

Start hub - java-jar selenium-server- standalone-x.xx.x.jar-role hub

Start node - java-jar selenium-server- standalone-x.xx.x.jar-role node-hub


http://localhost:4444/grid/register

Server - http://localhost:4444/grid/console

03

You might also like