Cheat Sheet for Selenium Automation This cheat sheet has examples on common methods used in Selenium Automation. Suggestion is to learn Core Java before moving to Selenium Automation. If you already know Java then Selenium is peanut for you. Feb 4, 2019 - Explore Ravindra Sitale's board 'Selenium WebDriver Cheat Sheet Cheat Sheet from Narayanan Palani. Selenium WebDriver Version 31' on Pinterest. See more ideas about selenium, software testing, computer programming. This cheat sheet has top common exceptions in selenium webdriver. Above all, we should examine some significant information regarding the term “exception” 01 [email protected].
Java selenium commands cheat sheet
Frequently used java selenium commands – Cheat Sheet
Visit python selenium commands cheat sheet here.
Driver setup:
Firefox:
System.setProperty(“webdriver.gecko.driver”, “Path To geckodriver”);
To download: Visit GitHub
Chrome:
System.setProperty(“webdriver.chrome.driver”, “Path To chromedriver”);
To download: Visit Here
Internet Explorer:
System.setProperty(“webdriver.ie.driver”, “Path To IEDriverServer.exe”);
To download: Visit Here
Edge:
System.setProperty(“webdriver.edge.driver”, “Path To MicrosoftWebDriver.exe”);
To download: Visit Here
Opera:
System.setProperty(“webdriver.opera.driver”, “Path To operadriver”);
To download: visit GitHub
Safari:
SafariDriver now requires manual installation of the extension prior to automation
Browser Arguments:
–headless
To open browser in headless mode. Buy post box online ireland. Works in both Chrome and Firefox browser
–start-maximized
To start browser maximized to screen. Requires only for Chrome browser. Firefox by default starts maximized
–incognito
To open private chrome browser
–disable-notifications
To disable notifications, works Only in Chrome browser
Example:
or
To Auto Download in Chrome:
To Auto Download in Firefox:
We can add any MIME types in the list
Note:
The value of browser.download.folderList can be set to either 0, 1, or 2.
0 – Files will be downloaded on the user’s desktop.
1 – Files will be downloaded in the Downloads folder.
2 – Files will be stored on the location specified for the most recent download
1 – Files will be downloaded in the Downloads folder.
2 – Files will be stored on the location specified for the most recent download
Private browser in Firefox:
Note: you may not see any indication that browser is private. To check, type ‘about:config’ and search for ‘browser.privatebrowsing.autostart’
Selenium Webdriver Cheat Sheet Example
Disable notifications in Firefox
Read Browser Details:
driver.getTitle();
driver.getWindowHandle();
driver.getWindowHandles();
driver.getCurrentUrl();
driver.getPageSource();
driver.getWindowHandle();
driver.getWindowHandles();
driver.getCurrentUrl();
driver.getPageSource();
Go to a specified URL:
driver.get(“http://google.com”)
driver.navigate().to(“http://google.com”)
driver.navigate().to(new URL(“http://google.com”))
driver.navigate().back()
driver.navigate().forward()
driver.navigate().refresh()
driver.navigate().to(“http://google.com”)
driver.navigate().to(new URL(“http://google.com”))
driver.navigate().back()
driver.navigate().forward()
driver.navigate().refresh()
Locating Elements:
driver.findEelement(By) – To find the first element matching the given locator argument. Returns a WebElement
driver.findElements(By) – To find all elements matching the given locator argument. Returns a list of WebElement
By ID
<input id=”q” type=”text”>…</input>
WebElement element = driver.findElement(By.id(“q”))
By Name
<input id=”q” name=”search” type=”text” />
WebElement element = driver.findElement(By.name(“search”));
By Class Name
<div class=”username” style=”display: block;”>…</div>
<div class=”username” style=”display: block;”>…</div>
WebElement element = driver.findElement(By.className(“username”));
By Tag Name
<div class=”username” style=”display: block;”>…</div>
<div class=”username” style=”display: block;”>…</div>
WebElement element = driver.findElement(By.tagName(“div”));
By Link Text
<a href=”#”>Refresh</a>
WebElement element = driver.findElement(By.linkText(“Refresh”));
By Partial Link Text
<a href=”#”>Refresh Here</a>
WebElement element = driver.findElement(By.partialLinkText(“Refresh”));
By XPath
<form id=”testform” action=”submit” method=”get”>
Username: <input type=”text” />
Password: <input type=”password” />
Password: <input type=”password” />
</form>
WebElement element = driver.findElement(By.xpath(“//form[@id=’testform’]/input[1]”));
By CSS Selector
<form id=”testform” action=”submit” method=”get”>
<input class=”username” type=”text” />
<input class=”password” type=”password” />
<input class=”password” type=”password” />
</form>
WebElement element = driver.findElement(By.cssSelector(“form#testform>input.username”));
Java Selenium commands for operation on Elements:
button/link/image:
click()
getAttribute()
isDisplayed()
isEnabled()
getAttribute()
isDisplayed()
isEnabled()
Selenium Webdriver Api
Text field:
sendKeys()
clear()
clear()
Checkbox/Radio:
isSelected()
click()
click()
Select:
Select select = new Select(WebElement);
select.selectByIndex();
select.selectByValue();
select.selectByVisibleText();
select.selectByValue();
select.selectByVisibleText();
select.deselectAll();
select.deselectByIndex();
select.deselectByValue();
select.deselectByVisibleText();
select.deselectByIndex();
select.deselectByValue();
select.deselectByVisibleText();
getFirstSelectedOption()
getAllSelectedOptions() – Returns List
getAllSelectedOptions() – Returns List
Element properties:
isDisplayed()
isSelected()
isEnabled()
isSelected()
isEnabled()
Read Attribute:
getAttribute(“”)
Get attribute from a disabled text box
driver.findElement(By).getAttribute(“value”);
Screenshot:
Learn About Valence Electrons | Chegg.com. image storage without using any extra libraries.
Universal Wait:
Wait using jQuery:
Note: jQuery must be defined in Web page otherwise WebDriverException will be thrown. Hence first validate jQuery is defined then check for async calls
The list here contains mostly used java selenium commands but not exhaustive. Please feel free to add in comments if you feel something is missing and should be here.