-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from jhoopes/dev-interactions
Added few more APIs, database relation, browser width and code cleanup.
- Loading branch information
Showing
10 changed files
with
550 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace Modelizer\Selenium\Exceptions; | ||
|
||
class CannotClickElement extends \Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace Modelizer\Selenium\Exceptions; | ||
|
||
class CannotFindElement extends \Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
<?php | ||
|
||
namespace Modelizer\Selenium\Services; | ||
|
||
trait InteractWithPage | ||
{ | ||
/** | ||
* Visit a URL within the browser. | ||
* | ||
* @param $path | ||
* | ||
* @return $this | ||
*/ | ||
protected function visit($path) | ||
{ | ||
$this->url($path); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Scroll the page in the x-axis by the amount specified. | ||
* | ||
* @param $amount Positive values go down the page, negative values go up the page | ||
* | ||
* @return $this | ||
*/ | ||
protected function scroll($amount) | ||
{ | ||
$this->execute([ | ||
'script' => 'window.scrollBy(0, '.$amount.')', | ||
'args' => [], | ||
]); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Assert that we see text within the specified tag | ||
* Defaults to the body tag. | ||
* | ||
* @param $text | ||
* @param string $tag | ||
* | ||
* @return $this | ||
*/ | ||
protected function see($text, $tag = 'body') | ||
{ | ||
$this->assertContains($text, $this->byTag($tag)->text()); | ||
|
||
return $this; | ||
} | ||
|
||
protected function notSee($text, $tag = 'body') | ||
{ | ||
$this->assertNotContains($text, $this->byTag($tag)->text()); | ||
} | ||
|
||
/** | ||
* Assert the page is at the path that you specified. | ||
* | ||
* @param $path | ||
* | ||
* @return $this | ||
*/ | ||
protected function seePageIs($path) | ||
{ | ||
$this->assertEquals($this->baseUrl.$path, $this->url()); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Type a value into a form input by that inputs name. | ||
* | ||
* @param $value | ||
* @param $name | ||
* @param bool $clear Whether or not to clear the input first on say an edit form | ||
* | ||
* @return $this | ||
*/ | ||
protected function type($value, $name, $clear = false) | ||
{ | ||
$element = $this->findElement($name); | ||
|
||
if ($clear) { | ||
$element->clear(); | ||
} | ||
|
||
$element->value($value); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Function to type information as an array | ||
* The key of the array specifies the input name. | ||
* | ||
* @param $information | ||
* @param $clear | ||
* | ||
* @return $this | ||
*/ | ||
protected function typeInformation($information, $clear = false) | ||
{ | ||
foreach ($information as $element => $item) { | ||
$this->type($item, $element, $clear); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
protected function submitForm($inputs, $selector) | ||
{ | ||
$form = $this->byCssSelector($selector); | ||
$this->type_information($inputs); | ||
$form->submit(); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Press a button on the page that contains text. | ||
* | ||
* @param $text | ||
* | ||
* @return $this | ||
*/ | ||
protected function press($text) | ||
{ | ||
$this->findElement($text, "//button[contains(text(), '{$text}')]")->click(); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Click an element based on text passed in, or pass an Id or Name to find the element by. | ||
* | ||
* @param $textOrId | ||
* | ||
* @throws CannotClickElement Throws when the element cannot be clicked | ||
* | ||
* @return $this | ||
*/ | ||
protected function click($textOrId) | ||
{ | ||
$element = $this->findElement($textOrId, "//a[contains(text(), '{$textOrId}')]"); | ||
|
||
try { | ||
$element->click(); | ||
} catch (\Exception $e) { | ||
throw new CannotClickElement('Cannot click the element with the text: '.$textOrId); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Will attempt to find an element by different patterns | ||
* If xpath is provided, will attempt to find by that first. | ||
* | ||
* @param $value | ||
* @param null $xpath | ||
* | ||
* @throws CannotFindElement | ||
* | ||
* @return \PHPUnit_Extensions_Selenium2TestCase_Element | ||
*/ | ||
protected function findElement($value, $xpath = null) | ||
{ | ||
$element = null; | ||
try { | ||
if (!is_null($xpath)) { | ||
return $this->byXPath($xpath); | ||
} | ||
} catch (\Exception $e) { | ||
} | ||
|
||
try { | ||
return $this->byId($value); | ||
} catch (\Exception $e) { | ||
} | ||
|
||
try { | ||
return $this->byName($value); | ||
} catch (\Exception $e) { | ||
} | ||
|
||
try { | ||
return $this->byCssSelector($value); | ||
} catch (\Exception $e) { | ||
} | ||
|
||
|
||
throw new CannotFindElement('Cannot find element: '.$value.' isn\'t visible on the page'); | ||
} | ||
} |
Oops, something went wrong.