Selenium Cheat Sheet 1700837961
Selenium Cheat Sheet 1700837961
Cheat sheet
Suriya prakash. R
TEST CONSULTATNT – ATOS,
www.linkedin.com/in/suriya-prakash-359039228
Driver Initialization
Chrome WebDriver driver = new ChromeDriver();
Firefox WebDriver driver = new FirefoxDriver();
Edge WebDriver driver = new EdgeDriver();
Safari WebDriver driver = new SafariDriver();
Locating Elements
By ID: driver.findElement(By.id (<element ID>))
By Name: driver.findElement(By.name(<element name>))
By Class Name: driver.findElement(By.className (<element class>))
By Tag Name: driver.findElement(By.tagName (<html tag name>))
By CSS Selector: driver.findElement(By.cssSelector("Tag#Value of id attribute"))
By XPath: driver.findElement(By.xpath (“//input[@type='submit']”))
By Link Text: driver.findElement(By.linkText (<link text>))
By Partial Link Text: driver.findElement(By.partialLinkText (<link text>))
TestNG
@BeforeSuite Will run before the execution of all the test methods in the suite
@BeforeTest Will execute before the execution of all the test methods of available classes
belonging to that folder
@BeforeClass Will execute before the first method of the current class is invoked
@BeforeMethod Will execute before each test method runs
@Test This is the main part of our automation script where we write the business
logic we want to automate
@AfterMethod Will execute after the execution of each test method
@AfterClass Will execute after the execution of all the test methods of the current class
@AfterTest Will execute after the execution of all the test methods of available classes
belonging to that folder
@AfterSuite Will execute after the execution of all the test methods in the suite
Junit
@Test Represents the method or class as a test block, also accepts parameters.
@Before The method with this annotation gets executed before all the other tests.
@BeforeClass The method with this annotation gets executed once before class.
@After The method with this annotation gets executed after all the other tests are
executed.
@AfterClass The method with this annotation gets executed once after class.
@Ignore It is used to ignore certain test statements during execution.
@Disabled Used to disable the tests from execution, but the corresponding reports of the
tests are still generated.
Working with Files
Upload a file:
driver.findElement(By.id("file-upload")).sendKeys(“path/to/your/file.txt”);
- Using System.getproperty(“user.dir”)+” path/to/your/file.txt”
driver.findElement(By.id("file-submit")).submit();
Using InputStream:
FileInputStream inputStream = new FileInputStream(“SampleFile.txt”);
InputStreamReader reader = new InputStreamReader(inputStream, “UTF-16”);
int character;
while ((character = reader.read()) != -1) {
System.out.print((char) character);
}
reader.close();
Using FileReader:
FileReader reader = new FileReader(“SampleFile.txt”);
int character;
while ((character = reader.read()) != -1) {
System.out.print((char) character);
}
reader.close();
Selenium Navigators
Navigate to a URL driver.get("<URL>") or driver.navigate().to("<URL>")
Refresh the page driver.navigate().refresh()
Navigate forward in browser driver.navigate().forward()
history
Navigate back in browser driver.navigate().back()
history
Minimize window:
driver.manage().window().minimize();
Fullscreen window:
driver.manage().window().fullscreen();
Take a Screenshot
import org.apache.commons.io.FileUtils;
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("./image.png"));
Selenium Operations
Launch a Webpage:
driver.get("<URL>") or driver.navigate().to("<URL>")
Click a button:
WebElement searchButton = driver.findElement(By.name("btnK"));
searchButton.click();
Accept an alert pop-up:
driver.switchTo( ).alert( ).accept();
Implicit wait:
import java.util.concurrent.TimeUnit;
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
Explicit wait:
import java.util.concurrent.TimeUnit;
Sleep:
Thread.sleep(<Time in MilliSeconds>);