Skip to content

Commit

Permalink
Merge pull request #198 from joshtombs/jt/shorthand
Browse files Browse the repository at this point in the history
Introduce Widget static methods
  • Loading branch information
samccone committed Aug 26, 2014
2 parents 4e8d726 + 6ffd127 commit b32e45f
Show file tree
Hide file tree
Showing 4 changed files with 337 additions and 0 deletions.
188 changes: 188 additions & 0 deletions docs/widget.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ All `Widgets` extend from seleniums [WebElement](http://selenium.googlecode.com/
* [Overriding](#overriding)
* [Shorthand](#shorthand)
* [Root](#root)
* [Static Methods](#static-methods)
* [click](#static-click)
* [fill](#static-fill)
* [hover](#static-hover)
* [doubleClick](#static-doubleclick)
* [read](#static-read)
* [isPresent](#static-ispresent)
* [isVisible](#static-isvisible)
* [getAttribute](#static-getattribute)
* [getValue](#static-getvalue)
* [getText](#static-gettext)
* [getInnerHTML](#static-getinnerhtml)
* [getOuterHTML](#static-getouterhtml)
* [hasClass](#static-hasclass)
* [sendKeys](#static-sendkeys)
* [clear](#clear)
* [Interacting with the DOM](#interacting-with-the-dom)
* [Click](#click)
* [Fill](#fill)
Expand Down Expand Up @@ -128,6 +144,178 @@ var PuppySearch = Widget.extend({
});
```

## Static Methods

Static methods can be used for situations where you do not want to declare a new Widget to do something. Static methods ALWAYS require selectors, otherwise Pioneer won't know what you want to operate on!

These methods allow you to do simple operations with less code.
For example:

```js
new this.Widget({
root: "#that-button"
}).click()
```

can be simplified to:

```js
this.Widget.click({selector: "#that-button"})
```

### static click

Static implementation of [click](#click)

### static fill

Static implementation of [fill](#fill)

```js
this.W.fill({
selector: ".field1",
value: ["such good text", Driver.Key.ENTER]
})
```

### static hover

Static implementation of [hover](#hover)

```js
this.W.hover({
selector: "#your-target"
})
```

### static doubleClick

Static implementation of [doubleClick](#doubleclick)

```js
this.W.doubleClick({
selector: "#some-target"
})
```

### static read

Static implementation of [read](#read)

```js
this.W.read({
selector: "p.third",
transformer: function(text){
text.toUpperCase()
}
})
```

### static isPresent

Static implementation of [isPresent](#ispresent) not the element is present.

```js
this.W.isPresent({
selector: "body"
})
```

### static isVisible

Static implementation of [isVisible](#isvisible) not the element is visible.

```js
this.W.isVisible({
selector: ".hidden"
})
```

### static getAttribute

Static implementation of [getAttribute](#getattribute)

```js
this.W.getAttribute({
selector: "img.thumb",
attribute: "width"
})
```

### static getValue

Static implementation of [getValue](#getvalue)

```js
this.W.getValue({
selector: ".field2"
})
```

### static getText

Static implementation of [getText](#gettext)

```js
this.W.getText({
selector: "p.fifth"
})
```

### static getInnerHTML

Static implementation of [getInnerHTML](#getinnerhtml)

```js
this.W.getInnerHTML({
selector: ".some-div"
})
```

### static getOuterHTML

Static implementation of [getOuterHTML](#getouterhtml)

```js
this.W.getOuterHTML({
selector: "#container"
})
```

### static hasClass

Static implementation of [hasClass](#hasclass)

```js
this.W.hasClass({
selector: "li.active",
className: "inactive"
})
```

### static sendKeys

Static implementation of [sendKeys](#sendkeys)

```js
this.W.sendKeys({
selector: ".username",
keys: "pioneer_expert"
})
```

### static clear

Static implementation of [clear](#clear)

```js
this.W.clear({
selector: ".password"
}).then(function(widget){
...
})
```

## Interacting with the DOM

### Click
Expand Down
8 changes: 8 additions & 0 deletions src/widgets/Widget.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ $ = Driver.promise
_this.el = el
_this

staticMethods = ["click", "fill", "hover", "doubleClick", "read", "isPresent", "isVisible", "getAttribute", "getValue", "getText", "getInnerHTML", "getOuterHTML", "hasClass", "sendKeys", "clear"]
for staticMethod in staticMethods
m = (args...) =>
@find({root: "html"}).then (w) ->
w[args[0]].apply(w, args.slice(1))

@[staticMethod] = _.partial(m, staticMethod)

constructor: (attributes = {}) ->
_.extend @, attributes

Expand Down
44 changes: 44 additions & 0 deletions test/integration/features/shorthand.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
Feature: Shorthand Widget Methods

Background:
When I view "sample.html"

Scenario: Click
When I shorthand click an element

Scenario: Fill
When I shorthand fill an element
Then I shorthand getValue the element

Scenario: Hover
When I shorthand hover an element

Scenario: Double Click
When I shorthand double click an element

Scenario: isPresent
When I shorthand isPresent an element

Scenario: isVisible
When I shorthand isVisible an element

Scenario: getAttribute
When I shorthand getAttribute an element

Scenario: getText
When I shorthand getText an element

Scenario: getInnerHTML
When I shorthand getInnerHTML an element

Scenario: getOuterHTML
When I shorthand getOuterHTML an element

Scenario: hasClass
When I shorthand call hasClass an element

Scenario: sendKeys
When I shorthand call hasClass an element

Scenario: clear
When I shorthand call clear an element
97 changes: 97 additions & 0 deletions test/integration/steps/shorthand.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
_ = require('lodash')

module.exports = ->

@When /^I shorthand double click an element$/, ->
@W.doubleClick({
selector: '.double'
}).then (w) -> w.read({selector: ".double"}).should.eventually.eql("wow such click")

@When /^I shorthand hover an element$/, ->
@W.hover({
selector: "h4"
}).then (w) ->
w.read({selector: "h4"}).should.eventually.eql("ok ok only on hover")

@When /^I shorthand click an element$/, ->
@W.click()

@When /^I shorthand fill an element$/, ->
@W.fill({
selector: ".inputbox",
value: "wow"
})

@When /^I shorthand isPresent an element$/, ->
@W.isPresent(".wow doge").should.eventually.eql(true)

@When /^I shorthand isVisible an element$/, ->
@W.isVisible({
selector: ".wow"
}).should.eventually.eql(true)

@When /^I shorthand getAttribute an element$/, ->
@W.getAttribute({
attribute: "width",
selector: ".wow"
}).should.eventually.eql("400px")

@When /^I shorthand getValue the element$/, ->
@W.getValue({
selector: ".inputbox"
}).should.eventually.eql("wow")

@When /^I shorthand getText an element$/, ->
@W.getText({
selector: "h1"
}).should.eventually.eql("hello world")

@When /^I shorthand getInnerHTML an element$/, ->
@W.getInnerHTML({
selector: ".wow"
}).then (html) ->
html.trim()
.should.eql("<doge>many money</doge>")

@When /^I shorthand getOuterHTML an element$/, ->
@W.getOuterHTML({
selector: ".wow doge"
}).then (html) ->
html.split('\n').map((string) ->
string.trim()
)
.join("")
.should.eql("<doge>many money</doge>")

@When /^I shorthand call hasClass an element$/, ->
@W.hasClass({
selector: ".hidden",
className: "doge"
}).should.eventually.be.true

@When /^I shorthand call sendKeys an element$/, ->
@W.sendKeys({
selector: ".inputbox",
keys: [
"dogecoins",
Driver.Key.SPACE,
"are",
Driver.Key.SPACE,
"worth",
Driver.Key.SPACE,
"alot"
]
}).then =>
@getValue({
selector: ".inputbox"
}).should.eventually.eql("dogecoins are worth alot")

@When /^I shorthand call clear an element$/, ->
new @W({
root: ".inputbox"
}).sendKeys("pioneer is awesome man...").then =>
@W.clear({
selector: ".inputbox"
}).then (widget) ->
widget.getValue({selector: ".inputbox"})
.should.eventually.eql("")

0 comments on commit b32e45f

Please sign in to comment.