Java & Selenium - Dependencies

Selenium Core
Latest release: https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -→
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.141.59</version>
</dependency>

Selectors

Type EnumerationSorted ascending Example
Class Name By.CLASS_NAME By.className("confirm")
CSS Selector By.CSS_SELECTOR By.cssSelector("input[name='quantity']")
ID By.ID By.id("confirm-update")
Link Text By.LINK_TEXT By.linkText("Return to Cart")
Name By.NAME By.name("quantity")
Partial Link Text By.PARTIAL_LINK_TEXT By.partialLinkText("Cart")
Tag Name By.TAG_NAME By.tagName("button")
XPath By.XPATH By.xpath("//input[@value='3']")

Example HTML:
<html>
  <body>
    <a href="/account">Return to Cart</a>
    <input type="text" name="quantity" value="3">
    <button id="confirm-update" class="confirm">Confirm</button>
  </body>
<html>

Example Tests

Check for a broken image using the 'naturalWidth' attribute
@Test
public void brokenImageCheck() {
    getDriver().get("http://the-internet.herokuapp.com/broken_images");

    List<WebElement> images = getDriver().findElements(By.cssSelector(".example > img"));
    images.forEach(image → Assert.assertEquals(image.getAttribute("naturalWidth"),"120"));
}

Selenium - Helpful Methods

  Explanation Example
get() Navigate to a URL get("https://www.ultranauts.com")
getCurrentUrl() String value for URL of the current page driver.getCurrentUrl()
getTitle() String value for page/tab title driver.getTitle()
switchTo().alert() Set focus to a JavaScript alert driver.switchTo().alert()
close() Close the current browser window only driver.close()
quit() Close all windows & terminates the session driver.quit()
isDisplayed() True/false if an element is displayed or not driver.findElement(By.cssSelector("input[name='username']")).isDisplayed()
isEnabled() True/false if a user can interact with an element or not driver.findElement(By.cssSelector("input[name='username']")).isEnabled()
isSelected() True/false if a checkbox/dropdown/radio is chosen or not driver.findElement(By.cssSelector("input[name='username']")).isSelected()
navigate().back() Navigate the browser to the previous page driver.navigate().back()
navigate().forward() Navigate the browser to the next page driver.navigate().forward()
navigate().refresh() Refresh the browser driver.navigate().refresh()

Selenium - Practice Sites

Site Notes
http://the-internet.herokuapp.com/ Contains a variety of UI scenarios
https://www.saucedemo.com/ eCommerce demo with login, cart & checkout
http://automationpractice.com/ eCommerce practice site
https://www.techlistic.com/p/selenium-practice-form.html A practice form with many different elements
https://phptravels.com/demo/ Travel demo site with several scenarios

Selenium - Helpful Courses

Site Notes
Selenium Support Classes Course covering the support classes of WebDriver
Selenium Automation Frameworks Course with an overview of Selenium in Ruby, Node.js & Java
Selenium Synchronization Strategies Course covering various strategies for WebDriver synchronization
Selenium Page Objects & Automation Course covering page object design & abstraction

-- MayaMcKela - 08 Jun 2021
Topic revision: r10 - 08 Jun 2021, MayaMcKela
© 2020 Ultranauts - 75 Broad Street, 2nd Floor, Suite 206, New York, NY 10004 - info@ultranauts.co