Selenium WebDriver Code Examples in Java Programming (Helpful to refer this sheet before attending Interviews) https://engineers-hub.teachable.com/
|
Firefox (Below v.47)Refer Part2-Page58
|
System.setProperty("webdriver.gecko.driver", "PathTo\\geckodriver.exe");
|
|
WebDriver driver = new FirefoxDriver();
|
|
|
ChromeRefer Part2-Page59
|
System.setProperty("WebDriver.chrome.driver", System.getProperty("user.dir")+"//BrowserDrivers//chromedriver.exe");
|
|
WebDriver driver = new ChromeDriver();
|
|
|
Internet ExplorerRefer Part2-Page59
|
System.setProperty("WebDriver.ie.driver", "PathTo//IEDriverServer.exe");
|
|
Download from The Internet Explorer Driver Server at http://docs.seleniumhq.org/download/
|
WebDriver driver = new InternetExplorerDriver();
|
|
Safari Driver
|
WebDriver driver = new SafariDriver();
|
|
Locating by ID Refer Part2-Page37
|
driver.findElement(By.id("q")).sendKeys("Selenium 3");
|
|
Locating by NameRefer Part2-Page37
|
driver.findElement(By.name("q")).sendKeys("Selenium 3");
|
|
Locating by XpathRefer Part2-Page37/38
|
driver.findElement(By.xpath("//input[@id='q']")).sendKeys("Selenium 3");
|
|
Locating Hyperlinks by Link Text Refer Part2-Page38/39
|
driver.FindElement(By.LinkText("edit this page")).Click();
|
|
Locating by DOMRefer Part2-Page39
|
dom =document.getElementById('signinForm')
|
|
Locating by CSSRefer Part2-Page40-43
|
driver.FindElement(By.CssSelector("#rightbar > .menu > li:nth-of-type(2) > h4"));
|
|
Locating by ClassNameRefer Part2-Page37
|
driver.findElement(By.className("profile-header"));
|
|
Locating by TagNameRefer Part2-Page37
|
driver.findElement(By.tagName("select")).Click();
|
|
Locating by LinkTextRefer Part2-Page37-40
|
driver.findElement(By.linkText("NextPage")).click();
|
|
Locating by PartialLinkTextRefer Part2-Page37-40
|
driver.findElement(By.partialLinkText("NextP")).click();
|
|
'Server Exception : sessionId should not be nullRefer Part2-Page160
|
Replace Chrome Driver with Latest Version
|
|
ERROR: No sessionId providedRefer Part2-Page160
|
Use Jenkins
|
|
UnreachableBrowserExceptionRefer Part2-Page160
|
Registry Key Changes Recommended
|
|
Illegal state exception using Selenium 3Refer Part2-Page161
|
Get latest Firefox Gecko Driver
|
|
TestNG
|
@BeforeSuite @AfterSuite @BeforeTest @AfterTest @BeforeGroups @AfterGroups @BeforeClass @AfterClass @BeforeMethod @AfterMethod
|
|
JUnit
|
@After @AfterClass @Before @BeforeClass @Ignore @Test
|
|
Executes an asynchronous piece of JavaScript
|
executeAsyncScript()
|
|
Executes JavaScript
|
executeScript()
|
|
Capture the screenshot and store
|
getScreenshotAs
|
|
addCookie(arg)
|
driver.manage().addCookie(new Cookie("foo", "bar"));
|
|
getCookies()
|
driver.manage().getCookies(); // to get all cookies
|
|
getCookieNamed()
|
driver.manage().getCookieNamed("foo");
|
|
deleteCookieNamed()
|
driver.manage().deleteCookieNamed("foo");
|
|
deleteCookie()
|
Cookie cookie1 = new Cookie("test2", "cookie2"); driver.manage().addCookie(cookie1); driver.manage().deleteCookie(cookie1); // deleting cookie object
|
|
deleteAllCookies()
|
driver.manage().deleteAllCookies(); // deletes all cookies
|
|
normal
|
ChromeOptions chromeOptions = new ChromeOptions(); chromeOptions.setPageLoadStrategy(PageLoadStrategy.NORMAL); WebDriver driver = new ChromeDriver(chromeOptions);
|
|
eager: When setting to eager, Selenium WebDriver waits until DOMContentLoaded event fire is returned.
|
ChromeOptions chromeOptions = new ChromeOptions(); chromeOptions.setPageLoadStrategy(PageLoadStrategy.EAGER); WebDriver driver = new ChromeDriver(chromeOptions);
|
|
none: When set to none Selenium WebDriver only waits until the initial page is downloaded.
|
ChromeOptions chromeOptions = new ChromeOptions(); chromeOptions.setPageLoadStrategy(PageLoadStrategy.NONE); WebDriver driver = new ChromeDriver(chromeOptions);
|
|
driver.switchTO().alert.getText()
|
to retrieve the alert message
|
|
driver.switchTO().alert.accept()
|
to accept the alert box
|
|
driver.switchTO().alert.dismiss()
|
to cancel the alert box
|
|
driver.switchTO().alert.sendKeys(“Text”)
|
to send data to the alert box
|
|
driver.findElement()
|
When no match has found(0) throws NoSuchElementException when 1 match found returns a WebElement instance when 2+ matches found returns only the first matching web element
|
|
driver.findElements()
|
when no macth has found (0) returns an empty list when 1 match found returns a list with one WebElement when 2+ matches found returns a list with all matching WebElements
|
|
Launch WebpageRefer Part2-Page60
|
driver.get("www.webdriverinselenium.com");
|
|
Click ButtonRefer Part2-Page60
|
driver.findElement(By.id("submit")).click();
|
|
Store TextRefer Part2-Page60
|
String txtDropdown = driver.findElement(By.ID("select")).getText();
|
|
Enter TextRefer Part2-Page61
|
driver.findElement(By.xpath("//input[@name='FirstName034']")).sendKeys("NarayananPalani");
|
|
Handle AlertRefer Part2-Page62
|
Alert Alertpopup = driver.switchTo().alert();
|
|
(Mouse)ClickRefer Part2-Page62
|
driver.findElement(By.xpath("//input[@value='OBJECT NAME']")).click();
|
|
Compare TextRefer Part2-Page64
|
Assert.assertTrue(chkbox12.isSelected());
|
|
Disable a FieldRefer Part2-Page64
|
driver.getElementsByName('<ObjectID>')[0].setAttribute('disabled', '');
|
|
Enable a FieldRefer Part2-Page64
|
driver.getElementsByName('<ObjectID>')[0].removeAttribute('disabled';
|
|
ScreenshotRefer Part2-Page64
|
File snapshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
|
|
FileUtils.copyFile(snapshot, new File("C:\\screenshot.jpg"));
|
|
|
Print the Title of the PageRefer Part2-Page68
|
String pagetitle = driver.getTitle();
|
|
System.out.print(pagetitle);
|
|
|
Implicit WaitRefer Part2-Page69
|
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
|
|
Explicit WaitRefer Part2-Page70
|
WebDriverWait wait = new WebDriverWait(driver, 20);
|
|
wait.until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//div[@id='<Object ID>']"), "Enter Your Personal Details"));
|
|
|
Fluent WaitRefer Part2-Page70
|
Wait wait = new FluentWait(driver)
|
|
.withTimeout(20, SECONDS)
|
|
|
SleepRefer Part2-Page70
|
Thread.Sleep(10);
|
|
Selenium Certification
|
|
|
Selenium Certification Q&A
|
|
|
Part1-Software Automation Testing Secrets Revealed
|
|
|
Part2-Software Automation Testing Secrets Revealed
|
|
|
Part3-Software Automation Testing Secrets Revealed
|
|
|
Udemy Course-Selenium
|
|
|
Udemy Course-ISTQB Test Automation Engineering
|
|
|
Useful Commands (Credits: Madhan K)
|
|
|
Useful Commands (Credits: Bijan Patel)
|
|
keyboard events
|
keyDown(); keyUp(); sendKeys()
|
|
Mouse events
|
clickAndHold(); contextClick()// — performs the mouse right-click action; doubleClick(); dragAndDrop(source,target); dragAndDropBy(source,xOffset,yOffset); moveByOffset(xOffset,yOffset); moveByElement(); release()
|
|
isEnabled()
|
determines if an element is enabled or not, returns a boolean.
|
|
isSelected()
|
determines if an element is selected or not, returns a boolean.
|
|
isDisplayed()
|
determines if an element is displayed or not, returns a boolean.
|
|
getWindowHandle()
|
used to retrieve the handle of the current page (a unique identifier)
|
|
getWindowHandles()
|
used to retrieve a set of handles of the all the pages available
|
|
driver.switchTo().window(“windowName/handle”)
|
switch to a window
|
|
driver.close()
|
closes the current browser window
|
|
driver.quit();
|
This method Closes all windows opened by the WebDriver.
|
|
Mentioning the frame index number, the Driver will switch to that specific frame
|
driver.switchTo.frame(int frameNumber)
|
|
Mentioning the frame element or ID, the Driver will switch to that specific frame
|
driver.switchTo.frame(string frameNameOrID)
|
|
Mentioning the frame web element, the Driver will switch to that specific frame
|
driver.switchTo.frame(WebElement frameElement)
|
|
Switching back to the main window
|
driver.switchTo().defaultContent()
|
|
getTitle()
|
used to retrieve the current title of the webpage
|
|
getCurrentUrl()
|
used to retrieve the current URL of the webpage
|
|
getPageSource()
|
used to retrieve the current page source of the webpage
|
|
getText()
|
used to retrieve the text of the specified web element
|
|
getAttribute()
|
used to retrieve the value specified in the attribute
|
|
Refresh Page
|
driver.navigate().refresh()
|
|
Navigate forwards in the browser history
|
driver.navigate().forward()
|
|
Navigate backward in the browser history
|
driver.navigate().back()
|
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.