Selenium User Handbook
Are you searching for a handy resource that you can refer to whenever you need to review the fundamental operations of Selenium? Well if you are then this is just what you need.
We at Intellipaat understand that it could be a bit of a hassle to remember every aspect of technology and that is why we have come up with a Selenium cheat sheet to assist our learners whenever they need a handy reference containing all the basics of selenium.
We assume that you have a basic understanding of selenium if you are referring to this cheat sheet.
Download the printable PDF of Selenium cheat sheet
What is selenium?
Selenium is an open-source framework to automate and perform software testing such as smoke tests, integration tests, etc. on web applications. It provides a playback/recording tool and domain-specific language.
Get started with Selenium:
-
Driver Initialization Basics:
- Firefox WebDriver driver = new FirefoxDriver();
- Chrome WebDriver driver = new ChromeDriver();
- Internet Explorer WebDriver driver = new SafariDriver();
- Safari Driver WebDriver driver = new InternetExplorerDriver();
-
Driver Initialization Advanced:
A. Load Firefox from a different location:
System.setProperty("webdriver.firefox.bin", "path/to/firefox/binary");
FirefoxProfile fp = new FirefoxProfile();
B. Load Firefox addons
File file = new File("path/to/extension.xpi");
fp.addExtension(file);
Selenium Locators:
Selenium locators are used to find and match the webpage elements that selenium interacts with. Following are some locators in selenium:
driver.findElement(By.id("q")).sendKeys("Se lenium 3");
driver.findElement(By.name("q")).sendKeys ("Selenium 3");
driver.findElement(By.xpath("//input[@id='q'])).sendKeys("Selenium 3");
- Locating Hyperlinksby Link Text
driver.FindElement(By.LinkText("edit this page")).Click();
dom =document.getElementById('signinForm')
driver.FindElement(By.CssSelector("#rightbar> .menu >li:nth-of-type(2) > h4"));
driver.findElement(By.className("profileheader"));
driver.findElement(By.tagName("select")).C lick();
driver.findElement(By.linkText("NextP age")).click();
- Locating by PartialLinkText
driver.findElement(By.partialLinkText(" NextP")).click();
Get 100% Hike!
Master Most in Demand Skills Now!
Selenium Navigators:
The navigator interface in selenium helps in moving backward and forwards in the browser’s history. Following are some navigator commands you can use:
driver.get(“http://newexample.com”)
driver.navigate().to(“http://newexample.com”)
driver.navigate().refresh()
- Navigate forwards in browser history
driver.navigate().forward()
- Navigate backward in browser history
driver.navigate().back()
TestNG:
TestNG is an open-source framework for automated testing. The NG in TestNG stands for Next Generation. It is similar to Junit but with more functionality to offer. Following are the TestNG annotations:
- @test: This annotation marks a class or method as a part of a test
- @BeforeSuite: This annotation makes sure that the method only run once before all the tests have run
- @AfterSuite: This annotation makes sure that the method runs once after the execution of all the tests
- @BeforeTest: This annotation will make sure that the method marked with this annotation runs before the first method annotated with @test
- @AfterTest: This annotation will make sure that the method marked with this annotation runs after all the methods annotated with @test execute all the classes inside <test> tag in the testng.xml file.
- @BeforeGroups : A method annotated with this annotation will run before all the first test methods run in that specific group
- @AfterGroups: A method annotated with this annotation will run after all the test methods run in that specific group
- @BeforeClass: A method annotated with this annotation will run only once per class and before all the first test methods run
- @AfterClass: A method annotated with this annotation will run only once per class and after all the test methods run
- @BeforeMethod: A method annotated with this annotation will run before every @test annotated method
- @AfterMethod: A method annotated with this annotation will run after every @test annotated method
JUNIT
JUNIT (Java Unit Testing Tool) is a framework used to perform unit-level testing. Following are the JUNIT annotations:
- @Test: test method to run with public void return type
- @After: method to run after the test method
- @AfterClass: method to run before the test method
- @Before: method to run before the test method
- @BeforeClass: method to run once before any of the test methods in the class have been executed
- @Ignore: This annotation is used to ignore a method
Windows:
Sometimes the web applications may have multiple frames or windows. Selenium assigns each window a unique alphanumeric id which is called window handle. Selenium then uses the id to switch control among windows.
String handle=driver.getWindowHandle();
Set<String> handles = getWindowHandles();
driver.switchTo().window(handle);
-
How to switch to a newly created window
String curWindow=driver.getWindowHandle();
Set<String> handles = getWindowHandles();
For(string handle: handles){
If (!handle.equals(curWindow))
{driver.switchTo().window(handle);
}
}
Frames
driver.switchTO().frame(1);
driver.switchTo().frame(“name”)
- Using web Element Object:
driver.switchTO().frame(element);
- Get back to the main document:
driver.switchTO().defaultContent();
Operations:
In selenium there are certain operations that can be performed on the web elements. Following is the list of those operations along with their respective syntax:
get("www.webdriverinselenium.com");
findElement(By.id("submit")).click();
AlertAlertpopup = driver.switchTo().alert();
getElementsByName('') [0].setAttribute('disabled', '')
getElementsByName('') [0].removeAttribute('disabled');
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);
manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebDriverWait wait = new WebDriverWait(driver, 20);
Thread.Sleep(10);
Watch this Selenium Projects For Beginners Tutorial
Alerts:
Sometimes a message box pops up on the screen to display some kind of notification to the user or maybe an ask for permission or to display a warning etc. These messages are called alerts. Alert interface provides some ways to handle the alerts in selenium:
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”);
Selenium Grid:
Selenium Grid helps selenium run multiple tests across different operating systems, browsers, and machines in parallel.
java-jar selenium-server- standalone-x.xx.x.jar-role hub
java-jar selenium-server- standalone-x.xx.x.jar-role node-hub
http://localhost:4444/grid/register
http://localhost:4444/grid/console
Download a Printable PDF of this Cheat Sheet
This would be all for the Selenium cheat sheet. In case you are looking to learn Selenium in-depth then you should definitely check out the Selenium training provided by Intellipaat. In this online training, you will get to learn the automation testing framework for web applications, TDD, selenium architecture, JaCoCo, TestNG, and Sikuli. You will work on real-life projects and assignments and prepare yourself for Certified Selenium Professional certification in this Selenium training. On top of that, you will have 24*7 technical support from our experts here at Intellipaat. Now you will learn more about the Features of Selenium in the next blog.