SeleniumQuery
jQuery-like cross-driver interface in Java for Selenium WebDriver
Install / Use
/learn @seleniumQuery/SeleniumQueryREADME
seleniumQuery
<!-- [](https://www.codacy.com/app/acdcjunior/seleniumQuery) --> <!-- [](https://travis-ci.org/seleniumQuery/seleniumQuery) [](https://ci.appveyor.com/project/acdcjunior/seleniumQuery/branch/master) [](https://codeship.com/projects/142644) [](https://app.wercker.com/project/bykey/b772beb5c952865d659e548bf7d64f48) [](https://circleci.com/gh/seleniumQuery/seleniumQuery) --> <!-- [](https://app.shippable.com/github/seleniumQuery/seleniumQuery) [](https://saucelabs.com/u/acdcjunior) -->Feature-rich jQuery-like Java interface for Selenium WebDriver
seleniumQuery is a feature-rich cross-driver Java library that brings a jQuery-like interface for Selenium WebDriver.
It is designed to be a thin layer over Selenium. You can use seleniumQuery to manage the WebDriver for you, or you can use seleniumQuery on top of your favorite selenium framework just to make some cases simpler when needed.
Example snippet:
// Regular Selenium
WebElement el = driver.findElement(By.cssSelector(".street"));
String oldStreet = element.getAttribute("value"); // what if ".street" is a <select>? this won't work
element.setAttribute("value", "4th St!")
// seleniumQuery
// getting the value
String oldStreet = $(".street").val(); // works even if it is a <select>, <textarea>, etc.
// setting the value
$("input.street").val("4th St!"); // also would work for a <select>
And much more. The example above is of something that has an equivalent in Selenium. Not everything does (many things would require tons of boilerplate in vanilla Selenium).
No special configuration needed - use seleniumQuery's goodies in your project right now:
On a regular WebElement...
// an existing WebElement...
WebElement existingWebElement = driver.findElement(By.id("myId"));
// call jQuery functions
String elementVal = $(existingWebElement).val();
boolean isButton = $(existingWebElement).is(":button"); // enhanced selector!
for (WebElement child: $(existingWebElement).children()) {
System.out.println("That element's child: "+child);
}
Or an existing WebDriver...
// an existing WebDriver...
WebDriver driver = new FirefoxDriver();
// set it up
$.driver().use(driver);
// and use all the goods
for (WebElement e: $(".myClass:contains('My Text!'):not(:button)")) {
System.out.println("That element: " + e);
}
What can you do with it?
Allows querying elements by:
- CSS3 Selectors -
$(".myClass"),$("#table tr:nth-child(3n+1)"); - jQuery enhanced selectors -
$(":text:eq(3)"),$(".myClass:contains('My Text!')"); - XPath -
$("//div/*/label/preceding::*"); - and even some own seleniumQuery selectors:
$("#myOldDiv").is(":not(:present)").
Built using Selenium WebDriver's capabilities, no jQuery.js is embedded at the page, no side-effects are generated.
<br><br>
Quickstart: A running example
Try it out now with the running example below:
import static io.github.seleniumquery.SeleniumQuery.$; // this will allow the short syntax
public class SeleniumQueryExample {
public static void main(String[] args) {
// The WebDriver will be instantiated only when first used
$.driver()
.useChrome() // sets Chrome as the driver (this is optional, if omitted, will default to HtmlUnit)
.headless() // configures chrome to be headless
.autoDriverDownload() // automatically downloads and configures chromedriver.exe
.autoQuitDriver(); // automatically quits the driver when the JVM shuts down
// or, instead, use any previously existing driver
// $.driver().use(myExistingInstanceOfWebDriver);
// starts the driver (if not started already) and opens the URL
$.url("http://www.google.com/?hl=en");
// interact with the page
$(":text[name='q']").val("seleniumQuery"); // the keys are actually typed!
// Besides the short syntax and the jQuery behavior you already know,
// other very useful function in seleniumQuery is .waitUntil(),
// handy for dealing with user-waiting actions (specially in Ajax enabled pages):
// the command below waits until the button is visible and then performs a real user click (not just the JS event)
$(":button[value='Google Search']").waitUntil().isVisible().then().click();
// this waits for the #resultStats to be visible using a selector and, when it is visible, returns its text content
String resultsText = $("#resultStats").waitUntil().is(":visible").then().text();
// .assertThat() functions: fluently asserts that the text contains the string "seconds", ignoring case
$("#resultStats").assertThat().text().containsIgnoreCase("seconds");
System.out.println(resultsText);
// should print something like: About 4,100 results (0.42 seconds)
// $.quit(); // would quit the driver, but it is not needed as .autoQuitDriver() was used
}
}
To get the latest version of seleniumQuery, add to your pom.xml:
<dependency>
<groupId>io.github.seleniumquery</groupId>
<artifactId>seleniumquery</artifactId>
<version>0.20.0</version>
</dependency>
<br><br>
Looking for more examples?
Download and execute the seleniumQuery showcase project. It contains many demonstrations of what seleniumQuery is capable of.
<br>Features
seleniumQuery implements all jQuery functions that are useful to browser manipulation.
On top of it, we add many other useful functions (see $("selector").waitUntil() and $("selector").assertThat() below).
Our main goal is to make emulating user actions and reading the state of pages easier than ever, with a consistent behavior across drivers.
Readable jQuery syntax you already know
Make your code/tests more readable and easier to maintain. Leverage your knowledge of jQuery.
// Instead of regular Selenium code:
WebElement element = driver.findElement(By.id("mySelect"));
new Select(element).selectByValue("ford");
// You can have the same effect writing just:
$("#mySelect").val("ford");
Get to know what jQuery functions seleniumQuery supports and what else it brings to the table on our seleniumQuery API wiki page.
<br>Waiting (ajax testing) and asserting
WebDriver's FluentWait is great, but it requires too much boilerplate code. Enters the .waitUntil() function:
// Below is an example of a <div> that should be hidden as effect of an Ajax call.
// The code will hold until the modal is gone. If it is never gone, seleniumQuery will throw a timeout exception
$("#modalDiv :button:contains('OK')").click();
$("#modalDiv :button:contains('OK')").waitUntil().is(":not(:visible)");
// Or the two commands above, fluently:
$("#modalDivOkButton").click().waitUntil().is(":not(:visible)");
You can also assert directly into the seleniumQuery object using .assertThat():
$("#modalDiv :button:contains('OK')").assertThat().is(":not(:visible)");
$("#myInput").assertThat().val().isBlank();
Any function that can be used with $().waitUntil() can also be used with $().assertThat() and vice-versa.
See below, expand (click on the arrow) each item for more details.
In order to handle interactions with Ajax-enabled pages, you can use the .waitUntil() function:
- The
.waitUntil()functions will requery the DOM for the elements until the given condition is met, returning a new seleniumQuery object when that happens.
// .waitUntil() will requery the DOM every time until the matched set fulfills the requirements
// .is() functions
$(".aDivDiv").waitUntil().is(":present");
$(".myInput").waitUntil().is(":enabled");
$(".aDivDiv").waitUntil().is(":visible");
$(".myInput").waitUntil().is(":visible:enabled");
// functions such as .val(), .text() and others are also available
$(".myInput").waitUntil().val().isEqualTo("expectedValue");
$(".aDivDiv").waitUntil().text().contains("expectedText");
// and more...
$(".myInput").waitUntil().val().matches(".*\d{10}\*");
$(".myInput").waitUntil().size().isGreaterThan(7);
$(".aDivDiv").waitUntil().html().contai
