Selenium WebDriver With Java & TestNG Cheat Sheet
Selenium WebDriver With Java & TestNG Cheat Sheet
Selenium WebDriver With Java & TestNG Cheat Sheet
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();
//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();
// 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();
// Handle Alert
Alert alert = driver.switchTo().alert();
alert.accept();
alert.dismiss();
alert.getText();
alert.sendKeys(“Input Data");
//Handle 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);
// Handle 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();
Screenshots Capture
TakesScreenshot screenshot =((TakesScreenshot)driver);
File srcFile= screenshot.getScreenshotAs(OutputType.FILE);
FileHandler.copy(srcFile, destFile);
Manage Timeouts
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 (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");
TestNG XML File
<suite name="TestNGSuiteGroup" parallel="methods"
thread-count="4">
<listeners>
<listener class-
</listeners>
<parameter name="Data1" value="10" />
<parameter name="Data2" value="3" />
<test name="TestNGTest">
<groups>
<run>
<exclude name="Test.*" />
<include name="SmokeTest" />
</run>
</groups>
<classes>
<class name="testselenium20.Session16"/>
</classes>
</test> <!-- TestNGTest -->
</suite> <!-- TestNGSuite -->
Different approach to create XPath and CSS Selector
Description XPath CSS Selector
Whole WebPage /html html
Whole WebPage body /html/body body
image element //img img
Link //a[@href = 'url'] a[href = 'url']
Direct Child //div/a div > a
Id //tagName[@id=’idValue’] tagName#idValue
Class //tagName[@class=’classValu tagName.Value of Class
e’] attribute
Attribute //tagname[@attribute- tagName[attribute=Valu
name=’value1′] e of attribute]
Multiple Attributes //input[@type='submit' and tagname[attribute1='val
@name='btnLogin'] ue1'][attribute2='value2'
]
Contains //*[contains(@type,'sub')] <HTML
tag><[attribute*=sub
String]>
Starts with //tagname[starts- <HTML
with(@attribute, ‘Start tag><[attribute^=prefix
value’)] of the String]>
Ends with //tagname[ends- <HTML
with(@attribute, ‘End value’)] tag><[attribute$=suffix
of the String]>
Matches //*[matches(@type,'value')] N/A