13 February, 2022

Selenium Cheat SheetšŸ’¢

Manage Driver Initialization
WebDriver driver = new ChromeDriver();
WebDriver driver = new FirefoxDriver();
WebDriver driver = new InternetExplorerDriver();
WebDriver driver = new HtmlUnitDriver();
Manage Element Locators
driver.findElement(By.id("Id Value"));
driver.findElement(By.name("Name Value"));
driver.findElement(By.className("Class Name Value"));
driver.findElement(By.linkText("Link text Value"));
driver.findElement(By.partialLinkText("Partial Text Constant
Value"));
driver.findElement(By.tagName("Tag Name Value"));
driver.findElement(By.cssSelector("CSS Value"));
driver.findElement(By.xpath("Xpath Value"));
driver.findElement(new ByAll(By.className("ElementClass
Name"), By.id("Element Id"), By.name("Element Name")))
Manage Elements Operations
WebElement element =
driver.FindElement(By.ElementLocator("Value of Element
Locator"));
element.click();
element.sendKeys("Input Text");
element.clear();
element.submit();
element.getAttribute(“type”);
String innerText = element.getText();
boolean enabledstatus = element.isEnabled();
boolean displayedstatus = element.isDisplayed();
boolean selectedstatus = element.isSelected();
Manage Operation on drop down
Select select = new Select(element);
select.selectByIndex(Integer Index);
select.selectByVisibleText("Text");
select.SelectByValue("Value");
select.deselectAll();
select.deselectByIndex(Integer Index);
select.deselectByVisibleText("Text");
select.deselectByValue("Value");
WebElement selectedOptions = select.getOptions();
Browser Operations
String pageTitle = driver.getTitle();
String currentURL = getCurrentUrl();
String currentPageSource = driver.getPageSource();
Manage Navigation history
driver.get("https://www.facebook.com/");
driver.manage().window().maximize();
driver.navigate().to("https://www.google.com/");
driver.navigate().back();
driver.navigate().forward();
driver.navigate().refresh();
driver.close();
driver.quit();
Manage Alert
Alert alert = driver.switchTo().alert();
alert.accept();
alert.dismiss();
alert.getText();
alert.sendKeys(“Input Data");
Manage Cookies
Cookie cookie = new Cookie(“cookieName”, “cookieValue”);
driver.manage().addCookie(cookie);
driver.manage().getCookies();
driver.manage().getCookieNamed(arg0);
driver.manage().deleteAllCookies();
driver.manage().deleteCookieNamed(arg0);
Manage frames
driver.switchTo().frame(int Frame Index);
driver.switchTo().frame("frameName");
WebElement element =
driver.FindElement(By.ElementLocator("Value of Element
Locator"));
driver.switchTo().frame(element);
driver.SwitchTo().defaultContent();
Manage Screenshots Capture
TakesScreenshot screenshot =((TakesScreenshot)driver);
File srcFile= screenshot.getScreenshotAs(OutputType.FILE);
FileHandler.copy(srcFile, destFile);
Timeouts Management 
driver.manage().timeouts().implicitlyWait(10,
TimeUnit.SECONDS);
welement = wait.until(Syntax: WebDriverWait wait = new
WebDriverWait(driver, timeout);
ExpectedConditions.elementToBeClickable(locator));
welement.click();
Thread.sleep(Long milli-seconds)
driver.manage().timeouts().pageLoadTimeout(30,
TimeUnit.SECONDS);
Scroll Down or Up Web Page
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("window.scrollBy(0,100)");
js.executeScript("window.scrollTo(0,
document.body.scrollHeight)");
WebElement element =
driver.FindElement(By.ElementLocator("Value of Element
Locator"));
js. executeScript("arguments[0].scrollIntoView()", element);
TestNG Annotations
@Test
@BeforeMethod
@AfterMethod
@BeforeTest
@AfterTest
@BeforeClass
@AfterClass
@Test(enabled = false)
@Test(enabled = true)
@Test(priority=2)
@Test(priority=5,dependsOnMethods={"method1","method
2"})
@Test(dependsOnMethods = {"method1"}, alwaysRun=true)
@Test(groups = { "Group1", "Group2" })
@Parameters({"testparameter1", "testparameter2"})
@Listeners(packagename.ListenerClassName.class)
@Test (dataProvider = "getUserIDandPassword")
@Test (description = "Open Facebook Login Page",
timeOut=35000)
@Test (invocationCount = 3, invocationTimeOut = 20000)
@Test (invocationCount = 3, skipFailedInvocations = true)
@Test (invocationCount = 3)
@Test (invocationCount = 7, threadPoolSize = 2)
TestNG Assertions
SoftAssert softassert= new SoftAssert();
softassert.assertEquals(1, 1);
softassert.assertAll();
Assert.assertEquals(11, 11);
Assert.assertEquals(true, true, "Not Matching");

No comments: