Coder Social home page Coder Social logo

nilportugues / seleniumquery Goto Github PK

View Code? Open in Web Editor NEW

This project forked from seleniumquery/seleniumquery

0.0 2.0 0.0 7.63 MB

jQuery-like cross-driver interface in Java for Selenium WebDriver

Home Page: http://seleniumquery.github.io/

License: Other

Java 92.10% HTML 7.90%

seleniumquery's Introduction

seleniumQuery

Maven Central

Codacy Badge codecov.io GitHub license Join the chat at https://gitter.im/seleniumQuery/seleniumQuery

Build Status Windows Build Status Build status wercker status Circle CI Run Status

Sauce Test Status

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.



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.19.0</version>
</dependency>



Looking for more examples?

Download and execute the seleniumQuery showcase project. It contains many demonstrations of what seleniumQuery is capable of.


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.


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.

$(). function Property/Evaluation Function Evaluation Function
.waitUntil()

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().contains("<div>expected</div>");
.assertThat()

Asserts, fluently, that the function has a specified value or matches a specified condition.

$("#stuff").assertThat().val().isEqualTo("expectedValue");
$(".m-e").assertThat().attr("attrName").isEqualTo("expectedValue");
$("span").assertThat().size().isGreaterThan(7);
$("#age").assertThat().val().matches(".*\d{10}\*");
$("#ipt").assertThat().val().matches(value -> value.length() > 50)
.val()
$(".myInput").assertThat().val().isEqualTo("expectedValue");
.text()
$(".div").assertThat().text().isEqualTo("expectedValue");
.attr("attrName")
$(".myInput").assertThat().attr("attrName").isEqualTo("expectedValue");
.prop("propName")
$(".myInput").assertThat().prop("propName").isEqualTo("expectedValue");
.html()
$(".div").assertThat().html().isEqualTo("expectedValue");
.size()
$(".div").assertThat().size().isEqualTo(0);
.isEqualTo("string" | <number> | other)
$(".myInput").assertThat().val().isEqualTo("expectedValue");
.isBlank()

Tests if the result of the preceding function is empty (""), null or whitespace only:

(null).isBlank()      = true
("").isBlank()        = true
(" ").isBlank()       = true
("bob").isBlank()     = false
("  bob  ").isBlank() = false

Example:

$(".myInput").assertThat().text().isBlank();
.isGreaterThan(<number>)
$(".myInput").assertThat().size().isGreaterThan(7);
.isLessThan(<number>)
$(".myInput").assertThat().size().isGreaterThan(7);
.contains("string")
$(".aDivDiv").waitUntil().html().contains("<div>expected</div>");
$(".aDivDiv").assertThat().text().contains("expectedText");
.containsIgnoreCase("string")
$(".aDivDiv").assertThat().text().containsIgnoreCase("eXpeCTedText");
.matches("string regex")
$(".myInput").assertThat().val().matches(".*\d{10}\*");
$("...").waitUntil().html().matches("my[0-9]regex.*?");
.matches(java.util.Pattern)
$("...").assertThat().val().matches(java.util.Pattern);
.matches(<Hamcrest Matcher>)
$("#myDiv").waitUntil().text().matches(Matchers.containsString("John"));
.matches(<lambda predicate>)
$("#ipt").waitUntil().val().matches(value -> value.length() > 50)
.is("selector")
$(".myInput").assertThat().is(":disabled");
$(".myInput").assertThat().is(":visible:enabled");
.isEmpty()

Evaluates if the size of this seleniumQuery is equal to zero.

 $("div").waitUntil().isEmpty();
 $("div").assertThat().isEmpty();
.isNotEmpty()

Evaluates if the size of this seleniumQuery is greated than zero.

 $("div").waitUntil().isNotEmpty();
 $("div").assertThat().isNotEmpty();
.isPresent()

Evaluates if this seleniumQuery object has elements (is not empty).

Note: this is an alias to .isNotEmpty().

 $("div").waitUntil().isPresent();
 $("div").assertThat().isPresent();
.isVisible()

Evaluates if this seleniumQuery object has only visible elements.

Note: this is different from .is(":visible") because .is() requires only one element to match the selector (to be visible), whereas this .isVisible() method requires all matched elements to be visible.

$("span.all-visible").waitUntil().isVisible();
$("span.all-visible").assertThat().isVisible();
.isDisplayed()

Evaluates if this seleniumQuery object has only visible elements.

Note: this is different from .is(":visible") because .is() requires only one element to match the selector (to be visible), whereas this .isVisible() method requires all matched elements to be visible.

This is an alias to .isVisible().

$("span.all-visible").waitUntil().isDisplayed();
$("span.all-visible").assertThat().isDisplayed();
.isHidden()

Evaluates if this seleniumQuery object is not empty and has only hidden elements.

Note: while .isNotVisible() considers an empty set a success, this method doesn't.

$("span.non-empty-and-all-hidden").waitUntil().isHidden();
$("span.non-empty-and-all-hidden").assertThat().isHidden();
.isNotVisible()

Evaluates if this seleniumQuery object is empty or has only hidden elements.

Note: while .isHidden() considers an empty set a failure, this method doesn't.

$("span.empty-or-all-hidden").waitUntil().isNotVisible();
$("span.empty-or-all-hidden").assertThat().isNotVisible();

You can also chain calls using .and():

$("span").assertThat().size().isGreaterThan(5).and().text().isEqualTo("a b c d e");

Or use functions after waiting/asserting using .then():

$("#div").waitUntil().isVisible().then().click();



Flexible WebDriver builder system

How to setup the WebDriver? Simply use our builder. You can download their executables before or you can let seleniumQuery automatically download and configure them. Setup in seleniumQuery is all too easy:

// Using Chrome, general example:
$.driver()
    .useChrome() // configures Chrome as the driver
    .headless() // configures Chrome to run in headless mode
    .autoDriverDownload() // automatically downloads and configures chromedriver.exe
    .autoQuitDriver(); // automatically quits the driver when the JVM shuts down

// Using Firefox
$.driver()
    .useFirefox() // configures Firefox as the driver
    .headless() // configures Firefox to run in headless mode
    .autoDriverDownload() // automatically downloads and configures geckodriver.exe
    .autoQuitDriver(); // automatically quits the driver when the JVM shuts down

For more examples, options and all supported drivers, see table below.

Existing WebDriver Instance

Using seleniumQuery in an existing WebDriver instance

The driver builder functions are a bonus, you don't have to use them. For seleniumQuery, it makes no difference.

If you want to create the WebDriver yourself or add seleniumQuery to an existing WebDriver instance just:

WebDriver myExistingDriverInstance = ...; // created elsewhere by whoever

$.driver().use(myExistingDriverInstance); // from now on, $ will work on myExistingDriverInstance

// now you can use all of seleniumQuery's power!
$("#phone").assertThat().val().isEqualTo("99887766");

chrome

Chrome

Here's how seleniumQuery can simplify a ChromeDriver instantiation.

Headless mode available. Automatic driver download available.

.useChrome()

Configures Chrome as the driver to be used. Provides additional configuration options that can be chained.

Calling $.driver().useChrome() does not instantiate the driver right away, it merely configures it. The driver will be instantiated when first used only (e.g. when $.url("http://somepage") is called).

.headless()

Runs Chrome in headless mode.

.autoQuitDriver()

Automatically quits the driver upon JVM shutdown.

If you don't use .autoQuitDriver(), you can quit the driver calling $.quit().

Having .autoQuitDriver() on and still calling $.quit() makes no harm, so quit at will.

.autoDriverDownload()

seleniumQuery automatically downloads and configures the driver. The download is managed by webdrivermanager. Additional options are available, pass a lambda to .autoDriverDownload(<labda>) to be able to configure:

$.driver().useChrome().autoDriverDownload((BrowserManager configurer) -> {
    // se more options at https://github.com/bonigarcia/webdrivermanager#webdrivermanager-api
    configurer.proxy("http://corpproxy:8182");
    configurer.proxyUser("myUser");
    configurer.proxyPass("myPass")
});
.withOptions(ChromeOptions)

Enables additional configuration through a ChromeOptions instance.

.withPathToChromeDriver(String)

If you don't want seleniumQuery to automatically download the executable for you (using .autoDriverDownload()) you can specify the location of chromedriver.exe/chromedriver yourself:

$.driver().useChrome().withPathToChromeDriver("path/to/chromedriver.exe");

If you use neither .autoDriverDownload() nor .withPathToChromeDriver(), seleniumQuery will attempt to find the executable on your PATH or classpath. If it doesn't find it anywhere, an exception will be thrown.

.withCapabilities(DesiredCapabilities)

Configures the given DesiredCapabilities in the driver to be instantiated. Prefer ChromeOptions when possible.


// Using Chrome
$.driver().useChrome(); // will look for chromedriver/exe to you, including in the classpath!
// if you don't have chromedriver.exe and want seleniumQuery to auto download and configure it
$.driver().useChrome().headless().autoDriverDownload();
// If you want to set the path to chromedriver.exe yourself
$.driver().useChrome().withPathToChromeDriver("path/to/chromedriver.exe")
// General example:
$.driver()
    .useChrome() // configures Chrome as the driver
    .headless() // configures Chrome to run in headless mode
    .autoDriverDownload() // automatically downloads and configures chromedriver.exe
    .autoQuitDriver(); // automatically quits the driver when the JVM shuts down
// using options
$.driver()
    .useChrome()
    .withOptions(<some ChromeOptions instance>)

firefox

Firefox

Easy FirefoxDriver instantiation and configuration with seleniumQuery.

Headless mode available. Automatic driver download available.

.useFirefox()

Configures Firefox as the driver to be used. Provides additional configuration options that can be chained.

Calling $.driver().useFirefox() does not instantiate the driver right away, it merely configures it. The driver will be instantiated when first used only (e.g. when $.url("http://somepage") is called).

.headless()

Runs Firefox in headless mode.

.autoQuitDriver()

Automatically quits the driver upon JVM shutdown.

If you don't use .autoQuitDriver(), you can quit the driver calling $.quit().

Having .autoQuitDriver() on and still calling $.quit() makes no harm, so quit at will.

.autoDriverDownload()

seleniumQuery automatically downloads and configures the driver. The download is managed by webdrivermanager. Additional options are available, pass a lambda to .autoDriverDownload(<labda>) to be able to configure:

$.driver().useFirefox().autoDriverDownload((BrowserManager configurer) -> {
    // se more options at https://github.com/bonigarcia/webdrivermanager#webdrivermanager-api
    configurer.proxy("http://corpproxy:8182");
    configurer.proxyUser("myUser");
    configurer.proxyPass("myPass")
});
.withOptions(FirefoxOptions)

Enables additional configuration through a FirefoxOptions instance.

.withBinary(FirefoxBinary)

Enables additional configuration through a FirefoxBinary instance.

.withProfile(FirefoxProfile)

Enables additional configuration through a FirefoxProfile instance.

.withCapabilities(DesiredCapabilities)

Configures the given DesiredCapabilities in the driver to be instantiated. Prefer FirefoxOptions when possible.


// Using Firefox
$.driver()
    .useFirefox() // configures Firefox as the driver
    .headless() // configures Firefox to run in headless mode
    .autoDriverDownload() // automatically downloads and configures geckodriver.exe
    .autoQuitDriver(); // automatically quits the driver when the JVM shuts down
// simplified setting of profile, options and binary
$.driver()
    .useFirefox()
    .withProfile(<an instance of FirefoxProfile>)
    .withOptions(<an instance of FirefoxOptions>)
    .withBinary(<an instance of FirefoxBinary>);

opera

Opera

Automatic driver download available.

.useOpera()

Configures Opera as the driver to be used. Provides additional configuration options that can be chained.

Calling $.driver().useOpera() does not instantiate the driver right away, it merely configures it. The driver will be instantiated when first used only (e.g. when $.url("http://somepage") is called).

.autoQuitDriver()

Automatically quits the driver upon JVM shutdown.

If you don't use .autoQuitDriver(), you can quit the driver calling $.quit().

Having .autoQuitDriver() on and still calling $.quit() makes no harm, so quit at will.

.autoDriverDownload()

seleniumQuery automatically downloads and configures the driver. The download is managed by webdrivermanager. Additional options are available, pass a lambda to .autoDriverDownload(<labda>) to be able to configure:

$.driver().useOpera().autoDriverDownload((BrowserManager configurer) -> {
    // se more options at https://github.com/bonigarcia/webdrivermanager#webdrivermanager-api
    configurer.proxy("http://corpproxy:8182");
    configurer.proxyUser("myUser");
    configurer.proxyPass("myPass")
});
.withOptions(OperaOptions)

Enables additional configuration through a OperaOptions instance.

.withBinary(string | File)

Configures the Opera browser binary location. Example:

$.driver().useOpera().autoDriverDownload()
    .withBinary("C:/Program Files/Opera/49.0.2725.47/opera.exe");
.withCapabilities(DesiredCapabilities)

Configures the given DesiredCapabilities in the driver to be instantiated. Prefer OperaOptions when possible.


// Opera
// we'll download the driver for you
$.driver().useOpera().autoDriverDownload();
// simplified setting of options and binary
$.driver()
    .useOpera()
    .withOptions(<an instance of OperaOptions>)
    .withBinary("C:/Program Files/Opera/49.0.2725.47/opera.exe") // example path
    .autoDriverDownload();

PhantomJS

PhantomJS

Always headless, webkit-based. Automatic executable download available.

.usePhantomJS()

Configures PhantomJS as the driver to be used. Provides additional configuration options that can be chained.

Calling $.driver().usePhantomJS() does not instantiate the driver right away, it merely configures it. The driver will be instantiated when first used only (e.g. when $.url("http://somepage") is called).

.autoQuitDriver()

Automatically quits the driver upon JVM shutdown.

If you don't use .autoQuitDriver(), you can quit the driver calling $.quit().

Having .autoQuitDriver() on and still calling $.quit() makes no harm, so quit at will.

.autoDriverDownload()

seleniumQuery automatically downloads and configures the driver. The download is managed by webdrivermanager. Additional options are available, pass a lambda to .autoDriverDownload(<labda>) to be able to configure:

$.driver().usePhantomJS().autoDriverDownload((BrowserManager configurer) -> {
    // se more options at https://github.com/bonigarcia/webdrivermanager#webdrivermanager-api
    configurer.proxy("http://corpproxy:8182");
    configurer.proxyUser("myUser");
    configurer.proxyPass("myPass")
});
.withPathToPhantomJS(String)

If you don't want seleniumQuery to automatically download the executable for you (using .autoDriverDownload()) you can specify the location of phantomjs.exe/phantomjs yourself:

$.driver().usePhantomJS().withPathToPhantomJS("path/to/phantomjs.exe");

If you use neither .autoDriverDownload() nor .withPathToPhantomJS(), seleniumQuery will attempt to find the executable on your PATH or classpath. If it doesn't find it anywhere, an exception will be thrown.

.withCapabilities(DesiredCapabilities)

Configures the given DesiredCapabilities in the driver to be instantiated.


// PhantomJS (GhostDriver)
// we'll download phantomjs.exe for you
$.driver().usePhantomJS().autoDriverDownload();
// or, we may find phantomjs[.exe] for you, throwing an error if not present
$.driver().usePhantomJS();  
// Or you may set the path yourself
$.driver().usePhantomJS().withPathToPhantomJS("path/to/phantomjs.exe");

HtmlUnit

HmtlUnit

Always headless, java-based. No need to download anything.

.useHtmlUnit()

Configures HmtlUnit as the driver to be used. Provides additional configuration options that can be chained.

Calling $.driver().useHtmlUnit() does not instantiate the driver right away, it merely configures it. The driver will be instantiated when first used only (e.g. when $.url("http://somepage") is called).

.autoQuitDriver()

Automatically quits the driver upon JVM shutdown.

If you don't use .autoQuitDriver(), you can quit the driver calling $.quit().

Having .autoQuitDriver() on and still calling $.quit() makes no harm, so quit at will.

Note: Since HtmlUnit is a java-based driver, it will quit upon JVM shutdown anyway.

.withoutJavaScript()

Runs HtmlUnit with disabled JavaScript.

.emulatingChrome()

Configures HtmlUnit to emulate Chrome.

.emulatingFirefox()

Configures HtmlUnit to emulate Firefox.

.emulatingInternetExplorer()

Configures HtmlUnit to emulate Internet Explorer.

.withCapabilities(DesiredCapabilities)

Configures the given DesiredCapabilities in the driver to be instantiated.


// There are many possibilities to set up HtmlUnitDriver
// HtmlUnit default (Chrome/JavaScript ON)
$.driver().useHtmlUnit();
// Want disabled JavaScript, just call .withoutJavaScript()
$.driver().useHtmlUnit().withoutJavaScript();

// HtmlUnit emulating Chrome
$.driver().useHtmlUnit().emulatingChrome();
$.driver().useHtmlUnit().emulatingChrome().withoutJavaScript();
// HtmlUnit emulating Firefox
$.driver().useHtmlUnit().emulatingFirefox(); // could disable JS here as well
// And IE
$.driver().useHtmlUnit().emulatingInternetExplorer11(); // JS is disableable as well
$.driver().useHtmlUnit().emulatingInternetExplorer(); // will pick latest IE

safari

Safari

seleniumQuery tests Safari as a remote driver.

$.driver().useDriver(new SafariDriver());

edge

Edge

Automatic driver download available.

.useEdge()

Configures Edge as the driver to be used. Provides additional configuration options that can be chained.

Calling $.driver().useEdge() does not instantiate the driver right away, it merely configures it. The driver will be instantiated when first used only (e.g. when $.url("http://somepage") is called).

.autoQuitDriver()

Automatically quits the driver upon JVM shutdown.

If you don't use .autoQuitDriver(), you can quit the driver calling $.quit().

Having .autoQuitDriver() on and still calling $.quit() makes no harm, so quit at will.

.autoDriverDownload()

seleniumQuery automatically downloads and configures the driver. The download is managed by webdrivermanager. Additional options are available, pass a lambda to .autoDriverDownload(<labda>) to be able to configure:

$.driver().useEdge().autoDriverDownload((BrowserManager configurer) -> {
    // se more options at https://github.com/bonigarcia/webdrivermanager#webdrivermanager-api
    configurer.proxy("http://corpproxy:8182");
    configurer.proxyUser("myUser");
    configurer.proxyPass("myPass")
});
.withOptions(EdgeOptions)

Enables additional configuration through a EdgeOptions instance.

.withCapabilities(DesiredCapabilities)

Configures the given DesiredCapabilities in the driver to be instantiated. Prefer EdgeOptions when possible.


// Edge
// we'll download the driver for you
$.driver().useEdge().autoDriverDownload();
// simplified setting of options
$.driver()
    .useEdge()
    .withOptions(<an instance of EdgeOptions>);

internet explorer

Internet Explorer

Automatic driver download available. Additional info about configuration can be found in our IE Driver wiki.

.useInternetExplorer()

Configures Internet Explorer as the driver to be used. Provides additional configuration options that can be chained.

Calling $.driver().useInternetExplorer() does not instantiate the driver right away, it merely configures it. The driver will be instantiated when first used only (e.g. when $.url("http://somepage") is called).

.autoQuitDriver()

Automatically quits the driver upon JVM shutdown.

If you don't use .autoQuitDriver(), you can quit the driver calling $.quit().

Having .autoQuitDriver() on and still calling $.quit() makes no harm, so quit at will.

.autoDriverDownload()

seleniumQuery automatically downloads and configures the driver. The download is managed by webdrivermanager. Additional options are available, pass a lambda to .autoDriverDownload(<labda>) to be able to configure:

$.driver().useInternetExplorer().autoDriverDownload((BrowserManager configurer) -> {
    // se more options at https://github.com/bonigarcia/webdrivermanager#webdrivermanager-api
    configurer.proxy("http://corpproxy:8182");
    configurer.proxyUser("myUser");
    configurer.proxyPass("myPass")
});
.withPathToIEDriverServerExe(String)

If you don't want seleniumQuery to automatically download the executable for you (using .autoDriverDownload()) you can specify the location of IEDriverServer.exe yourself:

$.driver().useInternetExplorer().withPathToIEDriverServerExe("C:\\IEDriverServer.exe");

If you use neither .autoDriverDownload() nor .withPathToIEDriverServerExe(), seleniumQuery will attempt to find IEDriverServer.exe on your PATH or classpath. If it doesn't find it anywhere, an exception will be thrown.

.withCapabilities(DesiredCapabilities)

Configures the given DesiredCapabilities in the driver to be instantiated.


// InternetExplorerDriver
// we'll download the driver for you
$.driver().useInternetExplorer().autoDriverDownload();
// or we search IEDriverServer.exe on your computer (path and classpash) for you
$.driver().useInternetExplorer();
// Or you set the path yourself
$.driver().useInternetExplorer().withPathToIEDriverServerExe("C:\\IEDriverServer.exe");

Available $("selector").functions()

Check the javadocs for our $().functions.

More info also in our API wiki page.


Available $.functions()

Check the javadocs for our $.functions.

Read about our global functions in the API wiki page.


Powerful selector system

Let the tool do the hard work and find elements easily:

  • CSS3 Selectors - $(".myClass"), $("#table tr:nth-child(3n+1)")
  • jQuery/Sizzle enhancements - $(".claz:eq(3)"), $(".claz:contains('My Text!')")
  • XPath - $("//div/*/label/preceding::*")
  • and even some own seleniumQuery selectors: $("#myOldDiv").is(":not(:present)").

You pick your style. Whatever is more interesting at the moment. Mixing is OK:

$("#tab tr:nth-child(3n+1)").find("/img[@alt='calendar']/preceding::input").val("2014-11-12")

Find more about them in seleniumQuery Selectors wiki page.



seleniumQuery still is Selenium - with "just" a jQuery interface

So there is a important aspect of it: Although our functions yield the same result as if you were using jQuery, remember we always execute them from the user perspective. In other words, when you call:

$(":input[name='email']").val("[email protected]");

We don't change the value attribute directly like jQuery does. We actually do as a user would: We clear the input and type, key by key, the string provided as argument!

And we go the extra mile whenever possible:

  • Our $().val() even works on contenteditable elements AND documentMode=on <iframe>s: They don't have value, but we type the text in them, again, key by key, as an user would;
  • If it is an <input type="file"> we select the file;
  • When the element is a <select>, we choose the <option> by the value given (same as $("selector").as().select().selectByValue("123")).

Always from the user perspective

On the same tone, when selecting/checking <option>s or checkboxes or radios, try not to use $().prop("selected", true) directly to them (which to work, of course, would need JS to be enabled on the driver). Do as an user would: call .click()! Or, better yet, use seleniumQuery's .as().select() functions: $().as().select().selectByVisibleText("My Option") or $().as().select().selectByValue("123").



Using multiple browsers/drivers simultaneously

Typically, the $ is a static variable, thus every command you issue only affects the one same instance of WebDriver.

But... what if you want/need to use two WebDrivers at the same time?

We've got your back, see the example:

public static void main(String[] args) {
  String demoPage = "https://cdn.rawgit.com/seleniumQuery/seleniumQuery-showcase/master/Agent.html";

  // using two drivers (chrome and firefox) at the same time
  SeleniumQueryBrowser chrome = new SeleniumQueryBrowser();
  chrome.$.driver().useHtmlUnit().emulatingChrome().autoQuitDriver();
  chrome.$.url(demoPage);

  SeleniumQueryBrowser firefox = new SeleniumQueryBrowser();
  firefox.$.driver().useHtmlUnit().emulatingFirefox().autoQuitDriver();
  firefox.$.url(demoPage);

  chrome.$("#agent").assertThat().text().contains("Chrome");
  firefox.$("#agent").assertThat().text().contains("Firefox");
}

Plugin System

seleniumQuery supports plugins through the .as(PLUGIN) function, such as:

$("div").as(YOURPLUGIN).someMethodFromYourPlugin();

There are some default plugins. To check them out, call .as() without arguments. Example:

// the .select() plugin
$("#citiesSelect").as().select().selectByVisibleText("New York");
// picks an <option> in the <select> based in the <option>'s visible text

For an example of how to create your own plugin, check the seleniumQuery Plugin wiki page.


Alternate symbols

If the dollar symbol, $, gives you the yikes -- we know, it is used for internal class names --, it is important to notice that the $ symbol in seleniumQuery is not a class name, but a static method (and field). Still, if you don't feel like using it, you can resort to sQ() or good ol' jQuery() and benefit from all the same goodies:

import static io.github.seleniumquery.SeleniumQuery.sQ;
import static io.github.seleniumquery.SeleniumQuery.jQuery;
...
String oldStreet = sQ("input.street").val();
sQ("input.street").val("4th St!");

String oldStreetz = jQuery("input.street").val();
jQuery("input.street").val("5th St!");

More

Find more on our wiki.


Changelog/Roadmap

See releases.

Contributing or Requesting Features

The tool quite simple, so there's a lot of room for improvement. If you think something would be useful for you, it would probably be useful to us all, tell us what you're thinking!

seleniumquery's People

Contributors

acdcjunior avatar desmorto avatar gitter-badger avatar gmkumar2005 avatar gordolio avatar noxtrez avatar pimeenta avatar renanleandrof avatar ricardocarvalhods avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.