Skip to content

Instantly share code, notes, and snippets.

@acdcjunior
Last active December 17, 2017 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acdcjunior/78cc1ce6be5214d91403 to your computer and use it in GitHub Desktop.
Save acdcjunior/78cc1ce6be5214d91403 to your computer and use it in GitHub Desktop.
seleniumQuery Example class/code from demo project
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
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment