Skip to content

Commit

Permalink
add util, up v, small refactor for proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
potapovDim committed Oct 3, 2018
1 parent cc40c96 commit af91869
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 173 deletions.
9 changes: 8 additions & 1 deletion __specs__/integration/chrome/client.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,19 @@ describe('client chrome', () => {
await client.goTo(pathResolver(file))
const table = $('table').waitForElement(500)
const tableHeaderObj = await table.util.getTableHeaderObject()

expect(Array.isArray(tableHeaderObj)).to.eql(false)
expect(typeof tableHeaderObj).to.eq('object')
expect(Object.keys(tableHeaderObj).length).to.eql(3)
})

it('element util (getSelectListCollection)', async () => {
const file = 'select'
await client.goTo(pathResolver(file))
const selectFirst = $('select').waitForElement(200)
const listCollection = await selectFirst.util.getSelectListCollection()
expect(Array.isArray(listCollection)).to.eql(true)
})

it('keyPress', async () => {
const file = 'keyboard'
await client.goTo(pathResolver(file))
Expand Down
1 change: 1 addition & 0 deletions __specs__/spec_utils/select.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<option value="4">4</option>
<option value="5">5</option>
</select>

<select name="test new" id="test-optgroup">
<optgroup title="test opt 2">
<option value="a">a</option>
Expand Down
75 changes: 0 additions & 75 deletions build-model/element.js

This file was deleted.

111 changes: 64 additions & 47 deletions docs/elementUtil.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,6 @@
## element util is API for some elements, as table, list ul, list select ect

```js
const awb = require('awb')
const {$} = awb()
const tableElement = $('table').waitForElement(150)

const tableCollection = await tableElement.util.getTableCollection()
// table collection will be next
```
```js
[
{ Company: { text: 'Alfreds Futterkiste' },
Contact: { text: 'Maria Anders' },
Country: { text: 'Germany' } },
{ Company: { text: 'Centro comercial Moctezuma' },
Contact: { text: 'Francisco Chang' },
Country: { text: 'Mexico' } },
{ Company: { text: 'Ernst Handel' },
Contact: { text: 'Roland Mendel' },
Country: { text: 'Austria' } },
{ Company: { text: 'Island Trading' },
Contact: { text: 'Helen Bennett' },
Country: { text: 'UK' } },
{ Company: { text: 'Laughing Bacchus Winecellars' },
Contact: { text: 'Yoshi Tannamuri' },
Country: { text: 'Canada' } },
{ Company: { text: 'Magazzini Alimentari Riuniti' },
Contact: { text: 'Giovanni Rovelli' },
Country: { text: 'Italy' } }
]
```


### Table util
```html
<table>
<tbody>
Expand All @@ -55,21 +24,69 @@ const tableCollection = await tableElement.util.getTableCollection()
<td>Roland Mendel</td>
<td>Austria</td>
</tr>
<tr>
<td>Island Trading</td>
<td>Helen Bennett</td>
<td>UK</td>
</tr>
<tr>
<td>Laughing Bacchus Winecellars</td>
<td>Yoshi Tannamuri</td>
<td>Canada</td>
</tr>
<tr>
<td>Magazzini Alimentari Riuniti</td>
<td>Giovanni Rovelli</td>
<td>Italy</td>
</tr>
</tbody>
</table>
```

```js
// util.getTableCollection()
const awb = require('awb')
const {$} = awb()
const tableElement = $('table').waitForElement(150)

const tableCollection = await tableElement.util.getTableCollection()
// table collection will be next
```
```js
[
{ Company: { text: 'Alfreds Futterkiste' },
Contact: { text: 'Maria Anders' },
Country: { text: 'Germany' } },
{ Company: { text: 'Centro comercial Moctezuma' },
Contact: { text: 'Francisco Chang' },
Country: { text: 'Mexico' } },
{ Company: { text: 'Ernst Handel' },
Contact: { text: 'Roland Mendel' },
Country: { text: 'Austria' } }
]
```

```js
// util.getTableHeaderObject()
const awb = require('awb')
const {$} = awb()
const tableElement = $('table').waitForElement(150)

const tableCollection = await tableElement.util.getTableHeaderObject()
// table header object will be next
```

```js
{ Company: 0, Contact: 1, Country: 2 }
```
### Select list util
```html
<select name="test" id="test-select">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
```
```js
// util.getTableHeaderObject()
const awb = require('awb')
const {$} = awb()
const selectElement = $('select').waitForElement(150)

const collection = await selectElement.util.getSelectListCollection()
// select collection will be next
```
```js
[ { index: 0, isSelected: true, text: '1', value: '1' },
{ index: 1, isSelected: false, text: '2', value: '2' },
{ index: 2, isSelected: false, text: '3', value: '3' },
{ index: 3, isSelected: false, text: '4', value: '4' },
{ index: 4, isSelected: false, text: '5', value: '5' } ]
```
4 changes: 4 additions & 0 deletions lib/element.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ interface IGetTableCollection {
interface IGetTableHeaderObject {
(indexRowHeader = 0, indexRowFooter = null): Promise<object>
}
interface IGetSelectListCollection {
(): Promise<any[] | object>
}

interface IElementUtil {
getTableCollection: IGetTableCollection
getTableHeaderObject: IGetTableHeaderObject
getSelectListCollection: IGetSelectListCollection
}

declare function elementsFn(): ElementAWB
Expand Down
34 changes: 2 additions & 32 deletions lib/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const proxyActions = [
'elements',
'wait',
'$',
'util',
'$$'
]

Expand Down Expand Up @@ -294,38 +295,6 @@ function elementsInitializer(requests, client) {
return callWithWrap.call(this, clickElement, this.elementId)
}

async getOptionList() {
if(this.subCall) {await this.subCall(); this.subCall = null}
if(!this.elementId) {await this.getThisElement()}
if(await this.getTag() !== 'select') {console.error('element is not select list')}

return callWithWrap.call(this, executeScript, function() {
const element = arguments[0]
const list = arguments[0]

function buildOptionCollection(options) {
return Array.prototype.reduce.call(options, function(acc, cur, ind) {
acc.push({
index: ind,
text: cur.innerText,
isSelected: cur.selected
})
return acc
}, [])
}

if(!list.querySelector('optgroup')) {
return buildOptionCollection(list.querySelectorAll('option'))
} else {
return Array.prototype.reduce.call(list.querySelectorAll('optgroup'), function(acc, cur, ind) {
acc[cur.label] = buildOptionCollection(cur.querySelectorAll('option'))
return acc
}, {})
}
element.scrollIntoView()
}, {ELEMENT: this.elementId, [WEB_EMENET_ID]: this.elementId})
}

async toElement(...args) {
if(this.subCall) {await this.subCall(); this.subCall = null}
if(this.elementId) {await this.getThisElement(args)}
Expand Down Expand Up @@ -457,6 +426,7 @@ function elementsInitializer(requests, client) {
if(!this.elements || !this.elements.length) {await self.getElements()}
return self.elements[index]
}

return getThisWrapperProxy(self, thisCall, proxyActions, true)
}

Expand Down
29 changes: 29 additions & 0 deletions lib/elementUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,28 @@ const getTableData = `function getTableData(rootTbl, indexRowHeader, indexRowFoo
return bodyData
}`

const getOptionList = `function getOptionList(list) {
function buildOptionCollection(options) {
return Array.prototype.reduce.call(options, function(acc, cur, ind) {
acc.push({
index: ind,
text: cur.innerText,
value: cur.value,
isSelected: cur.selected
})
return acc
}, [])
}
if(!list.querySelector('optgroup')) {
return buildOptionCollection(list.querySelectorAll('option'))
} else {
return Array.prototype.reduce.call(list.querySelectorAll('optgroup'), function(acc, cur, ind) {
acc[cur.label.length ? cur.label : cur.title] = buildOptionCollection(cur.querySelectorAll('option'))
return acc
}, {})
}
}`


function GetElementUtils(client, element) {
Expand All @@ -158,6 +180,13 @@ function GetElementUtils(client, element) {
return getTableData(arguments[0], arguments[1])
`, {ELEMENT: this.element.elementId, [WEB_EMENET_ID]: this.element.elementId}, indexRowHeader)
}
this.getSelectListCollection = async function() {
if(!this.element.elementId) {await this.element.getThisElement()}
return this.client.executeScript(`
const getOptionList = ${getOptionList};
return getOptionList(arguments[0])
`, {ELEMENT: this.element.elementId, [WEB_EMENET_ID]: this.element.elementId})
}
}


Expand Down
11 changes: 6 additions & 5 deletions lib/proxySubCalls.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ function getThisWrapperProxy(that, thisCall, proxyActions, elementsProxy = false
return new Proxy(that, {
get(target, action) {
if(!proxyActions.includes(action)) {

if(elementsProxy) {
return async (...args) => {
const element = await thisCall()
Expand All @@ -85,10 +84,12 @@ function getThisWrapperProxy(that, thisCall, proxyActions, elementsProxy = false
return target[action](...args)
}
} else {
return (...args) => {
args.push(thisCall, that)
return target[action](...args)
}
return typeof target[action] === 'object'
? target[action]
: (...args) => {
args.push(thisCall, that)
return target[action](...args)
}
}
}
})
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "awb",
"version": "0.6.4",
"version": "0.6.5",
"description": "simple light weight interface for selenium webdriver (node js)",
"main": "./awb.js",
"bin": {
Expand Down
Loading

0 comments on commit af91869

Please sign in to comment.