jQuery API for Selenium WebDriver/JSDom
npm install selenium-query
Selenium WebDriver, JSDom, Cheerio, Plain-HTTP.
typescript
import SQuery from 'selenium-query';
let $ = await SQuery.load(url, config?: IConfig)
`
Query and Manipulate
$3
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
`typescript
import $ from '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);
`
$3
As with jQuery you can define an extension method and call it in your tests
`typescript
import $ from 'selenium-query';
$.fn.doBaz = function(){
return this.each(el => {
// do some usefull things with WebElement/JsDomElement/CherioElement
});
};
$(driver)
.find('input')
.doBaz();
`
$3
> Allows to get and to listen for events emitted by the browser
`typescript
import { BrowserNetworkMonitor } from 'selenium-query';
let monitor = await BrowserNetworkMonitor.start(driver);
monitor
.on('requestWillBeSent', req => console.log(req))
.on('responseReceived', req => console.log(req))
.on('loadingFinished', req => console.log(req));
// ... e.g. after the a page is loaded
let { request, response } = monitor.getRequest(/index.html/);
console.log(request.headers, response.headers);
// get the response body
let { base64Encoded, body } = monitor.getResponseBody(mainPageRequest);
`
$3
> Allows to send custom responses for requests back to the browser
`typescript
import { BrowserNetworkInterceptor } from 'selenium-query';
let interceptor = await BrowserNetworkInterceptor.start(driver);
interceptor.register({
match: /index.html/,
response: {
status: 200,
headers: {
'Content-Type': 'text/html'
},
body: ' Changed
'
}
});
// ... load index.html, and the modified content should be loaded
`
$3
##### :text and improved :has selectors
`typescript
let html =
;
SQuery.pseudo.isBar = async ($el, innerQuery) => {
let $children = await $el.find('#bar');
return $children.length > 0;
};
let value1 = await $.find('li:text(Bar)').attr('name');
let value2 = await $.find('li:has(span#id)').attr('name');
let value3 = await $.find('li:isBar()').attr('name');
// value1 === value2 === value3 === 'y'
`
API
##### ☰
- constructor
- Collection
- length
- eq
- slice
- each
- map
- toArray
- Traverse
- find
- filter
- children
- parent
- closest
- Attributes
- attr
- removeAttr
- prop
- removeProp
- val
- css
- Class
- hasClass
- addClass
- removeClass
- toggleClass
- Manipulate
- remove
- Dimension and Position
- height
- width
- innerHeight
- innerWidth
- offset
- position
- scrollTop
- scrollLeft
- Content
- html
- text
- append
- prepend
- before
- after
- Events
- trigger
- click
- change
- focus
- blur
- :sparkles: type
- :sparkles: press
- :sparkles: sendKeys
- :sparkles: select
- Misc
- eval
- Document
- load
- getDriver
- setDriver
- :zap: JsDom
- build
- load
- :zap: Cheerio
- build
- load
- :zap: Network
- load
##### constructor(WebDriver|WebElement|Array
- WebDriver
- WebElement
`typescript
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
`typescript
await $(driver)
.find('button')
.eq(0)
.css('background-color', 'red')
// instead of an equivalent
let buttons = await $(driver).find('button')
let firstButton = await buttons.eq(0);
await firstButton.css('background-color', 'red');
console.log('The color has been changed.'));
`
##### slice([start:number = 0, end:number = .length]):SQuery
Get elements range.
##### each(function
Enumerate the collection. The callback function can return a promise, if an async job is performed.
##### map(function
Map the collection into the new one. Return the value from the function or a promise which resolves then with the value.
##### toArray():Promise
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
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
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
Get or set style properties
Class
##### hasClass(name:string):Promise
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
##### width():Promise
##### innerHeight():Promise
##### innerWidth():Promise
##### offset():Promise