selenium-query
jQuery API for Selenium WebDriver/JSDom
Last updated 2 months ago by tenbits .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install selenium-query 
SYNC missed versions from official npm registry.

Query and Manipulation Library

Build Status NPM version TypeScript

jQuery-alike API for Selenium WebDriver, JSDom and Cheerio

Single API to query web-pages or html blocks with supported providers: Selenium WebDriver, JSDom, Cheerio (puppeteer on roadmap).

Use for tests or crawlers.


Asynchronous nature

As the WebDriver methods are async, Selenium Query instance implements Promise and you can chain the function calls or use async/await. A very basic example

let $ = require('selenium-query');
$(driver)
    .find('.foo')
    .filter('input')
    .attr('placeholder', 'Baz')
    .val()
    .then(value => console.log(value));

// or via await
let value = await $(driver).find('input.foo').val();
console.log(value);

Extension methods

As with jQuery you can define an extension method and call it in your tests

let $ = require('selenium-query');
$.fn.doBaz = function(){
    return this.each(el => {
        // do some usefull things with WebElement/JsDomElement/CherioElement
    });
};
$(driver)
	.find('input')
	.doBaz();

API

constructor(WebDriver|WebElement|Array<WebElement>|SQuery|Array<SQuery>)
let SQuery = require('selenium-query');
let $document = SQuery(driver);
let $inputs = $document.find('inputs');

Collection

length:number

Count of WebElements in a current set.

:exclamation: Due to asynchronous nature, sometimes you have to wait until the promise is resolved to get the correct length value

eq(index:number):SQuery

Get the SQuery instance with only one element at the index.

:exclamation: Once again, wait until the promise is resolved, or chain the manipulations

$(driver)
    .find('button')
    .eq(0)
    .css('background-color', 'red')
    .done(() => console.log('The color has been changed.'))
// instead of an equivalent
$(driver)
    .find('button')
    .done(buttons => {
        buttons
            .eq(0)
            .done(firstButton => {
                firstButton
                    .css('background-color', 'red')
                    .done(() => console.log('The color has been changed.'))
            })
    });
slice([start:number = 0, end:number = .length]):SQuery

Get elements range.

each(function<node:WebElement, index:number, Promise|void 0>):SQuery

Enumerate the collection. The callback function can return a promise, if an async job is performed.

map(function<node:WebElement, index:number, Promise|any>):SQuery

Map the collection into the new one. Return the value from the function or a promise which resolves then with the value.

toArray():Promise<Array<any>>

Returns a promise which resolves with an Array instance of current elements in collection

Traverse

find(selector:string):SQuery

Find element(s).

filter(selector:string):SQuery

Filter element(s) out of the current collection.

children([selector:string]):SQuery

Get, and optionally filter, children of every element in the collection.

parent():SQuery

Get parent elements of every element in the collection

closest(selector):SQuery

Find ancestor of every element in the collection

Attributes

attr(key:string | key:string, val:any | attributes:Object ):SQuery|Promise<any>

Get attribute value of the first element in the collection, or set attribute(s) to each element.

removeAttr(key:string):SQuery

Remove the attribute

prop(key:string | key:string, val:any | properties:Object):SQuery|Promise<any>

Get property value of the first element in the collection, or set property(ies) to each element.

removeProp(key:string):SQuery

Delete property

val([value:string]):SQuery

Get or set value property, like input.value

css(key:string | key:string, val:string | css:Object ):SQuery|Promise<any>

Get or set style properties

Class

hasClass(name:string):Promise<boolean>

Check if the first element has the class name.

addClass(name:string):SQuery

Add the class name(s) to every element in the collection

removeClass(name:string):SQuery

Remove the class name(s) of every element in the collection

toggleClass(name:string):SQuery

Toggle the class name(s) of every element in the collection

Manipulate

remove():SQuery

Remove the elements from the parent nodes

Dimensions

height():Promise<number>
width():Promise<number>
innerHeight():Promise<number>
innerWidth():Promise<number>
offset():Promise<object{top,left}>
position():Promise<object{top,left}>
scrollTop():Promise<number>
scrollLeft():Promise<number>

Content

html([html:string]):SQuery|Promise<string>
text([text:string]):SQuery|Promise<string>
append(html:string):SQuery
prepend(html:string):SQuery
before(html:string):SQuery
after(html:string):SQuery

Events

trigger(type:string [, data:Object]):SQuery

Trigger native or custom event.

click():SQuery
change():SQuery

Trigger change event

focus():SQuery
blur():SQuery
type(text:string):SQuery

Enter the text.

:exclamation: Meta keys are supported in {}

press(combination:string):SQuery

Press key combination. E.g.: ctrl+c, a+b+c, ctrl+alt+d, ctrl++ (control and plus keys)

sendKeys(text:string):SQuery

Call native Selenums sendKeys fn on each element

select(text:string | start:number[, end:number]):SQuery

Select an option from the select element, or if the input the selects a text or range

Misc

eval(fn:Function, ...args):Promise<any>

Evaluate function in Browser.

:exclamation: The first argument is the first element in the set

$(driver)
	.find('button')
	.eval(function(el){
		// browser context
		// do smth. with the Element and return a value
	});

Document

static load(url:string[, config:WebDriverOptions]):SQuery

Create or reuse a WebDriver, and load the page.

WebDriverOptions defaults

{
	name: 'Chrome',
	args: ['no-sandbox'],
	binaryPath: null,

	// For better control and to change the behaviour of how the options are created and applied,
	// you can define next functions
	applyOptions: function(builder, options) {},
	setOptions (builder, options) {},
	setArguments (options) {},
	setBinaryPath (options) {},
	setLogging (options) {}
}

JsDom

static SQuery.jsdom.build(config: IJsdomParams):SQuery

interface IJsdomParams {
    html: string
}

Create SQuery collection with JsDom driver

static SQuery.jsdom.load(url: string, config: IJsdomLoadParams):SQuery

interface IJsdomLoadParams {
    headers?: {[name: string] : string }
	method?
	query?: {[name: string] : string }
    payload?
	cookies?: string | string[]
	cache?: {
		folder?: string
		maxAge?: number
    }
    cacheQueryIgnore?: string[]	
	/** Webdriver will load this url, or requested url, to set the cookies first */
	cookieOrigin?: string
}

Cheerio

static SQuery.cheerio.build(config: ICheerioParams):SQuery

interface ICheerioParams {
    html: string
}

Create SQuery collection with Cheerio driver (Only query and manipulation methods are implemented)

static SQuery.cheerio.load(url: string, config: ICheerioLoadParams):SQuery

interface ICheerioLoadParams {
    headers?: {[name: string] : string }
	method?
	query?: {[name: string] : string }
    payload?
	cookies?: string | string[]
	cache?: {
		folder?: string
		maxAge?: number
    }
    cacheQueryIgnore?: string[]	
	/** Webdriver will load this url, or requested url, to set the cookies first */
	cookieOrigin?: string
}

Example

$
	.load('http://google.com')
	.find('input')
	.css('background-color', 'red');

:checkered_flag:


:copyright: MIT, Alex Kit

Current Tags

  • 0.51.98                                ...           latest (2 months ago)

57 Versions

  • 0.51.98                                ...           2 months ago
  • 0.51.96                                ...           2 months ago
  • 0.51.95                                ...           2 months ago
  • 0.51.94                                ...           2 months ago
  • 0.51.93                                ...           2 months ago
  • 0.51.91                                ...           3 months ago
  • 0.51.90                                ...           3 months ago
  • 0.51.89                                ...           4 months ago
  • 0.51.88                                ...           4 months ago
  • 0.51.87                                ...           4 months ago
  • 0.51.86                                ...           4 months ago
  • 0.51.85                                ...           4 months ago
  • 0.51.84                                ...           4 months ago
  • 0.51.83                                ...           4 months ago
  • 0.51.82                                ...           4 months ago
  • 0.51.80                                ...           4 months ago
  • 0.51.78                                ...           4 months ago
  • 0.51.77                                ...           4 months ago
  • 0.51.76                                ...           4 months ago
  • 0.51.75                                ...           4 months ago
  • 0.51.74                                ...           4 months ago
  • 0.51.73                                ...           4 months ago
  • 0.51.71                                ...           5 months ago
  • 0.51.70                                ...           5 months ago
  • 0.51.69                                ...           5 months ago
  • 0.51.68                                ...           6 months ago
  • 0.51.65                                ...           6 months ago
  • 0.51.64                                ...           6 months ago
  • 0.51.63                                ...           6 months ago
  • 0.51.61                                ...           7 months ago
  • 0.51.60                                ...           9 months ago
  • 0.51.58                                ...           9 months ago
  • 0.51.56                                ...           9 months ago
  • 0.51.52                                ...           10 months ago
  • 0.51.51                                ...           10 months ago
  • 0.51.50                                ...           10 months ago
  • 0.51.49                                ...           10 months ago
  • 0.51.47                                ...           10 months ago
  • 0.51.45                                ...           10 months ago
  • 0.51.44                                ...           10 months ago
  • 0.51.43                                ...           10 months ago
  • 0.51.42                                ...           10 months ago
  • 0.51.41                                ...           10 months ago
  • 0.50.40                                ...           10 months ago
  • 0.50.39                                ...           10 months ago
  • 0.50.36                                ...           10 months ago
  • 0.50.35                                ...           4 years ago
  • 0.50.34                                ...           4 years ago
  • 0.50.33                                ...           4 years ago
  • 0.50.32                                ...           4 years ago
  • 0.50.31                                ...           4 years ago
  • 0.50.30                                ...           4 years ago
  • 0.50.29                                ...           4 years ago
  • 0.50.28                                ...           4 years ago
  • 0.50.27                                ...           4 years ago
  • 0.50.26                                ...           4 years ago
  • 0.50.25                                ...           4 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 5
Last Day 0
Last Week 0
Last Month 115
Dependencies (7)
Dev Dependencies (8)

Copyright 2014 - 2016 © taobao.org |