1: <?php
2: namespace Azalea\Selenium\Toolkit;
3:
4: use Azalea\Selenium\Toolkit\SeleniumTestCase;
5: use Azalea\Selenium\Toolkit\SauceLabsTestCase;
6: use Azalea\Selenium\Toolkit\Dom;
7:
8: 9: 10: 11:
12: class TestCase extends SauceLabsTestCase
13: {
14:
15: protected $dom = null;
16:
17:
18: protected $nestedSelectors = array();
19:
20: 21: 22:
23: public function setUpPage()
24: {
25: parent::setUpPage();
26: $this->dom = new Dom($this);
27: }
28:
29: 30: 31: 32: 33: 34: 35:
36: public function within($selector, \Closure $callback)
37: {
38: $this->nestedSelectors[] = $selector;
39: if ($this->dom->getRoot() == "body") {
40: $this->dom->setRoot($selector);
41: } else {
42: $this->dom->setRoot($this->dom->getRoot()." ".$selector);
43: }
44: $callback($this);
45: array_pop($this->nestedSelectors);
46: $lastSelector = end($this->nestedSelectors);
47: $this->dom->setRoot(($lastSelector ? $lastSelector : "body"));
48: }
49:
50: 51: 52:
53:
54: 55: 56: 57: 58: 59: 60:
61: public function getView($path, $args = array())
62: {
63: return ResourceLoader::load($path, $this, $args);
64: }
65:
66: 67: 68:
69:
70: 71: 72: 73: 74: 75:
76: public function wait($timeout = 5)
77: {
78: sleep($timeout);
79: return $this;
80: }
81:
82: 83: 84: 85: 86:
87: public function clickAndWait($element, $timeout = 2)
88: {
89: $element->click();
90: $this->wait($timeout);
91: return $this;
92: }
93:
94: 95: 96: 97: 98: 99: 100: 101:
102: public function waitForHash($hash, $timeout = 30)
103: {
104: $driver = $this;
105: $this->spinWait(function () use ($driver, $hash) {
106: $matches = null;
107: return (preg_match($hash, $driver->getCurrentHash(), $matches) == 1);
108: }, $timeout);
109:
110: return $this;
111: }
112:
113: 114: 115: 116: 117: 118:
119: public function spinWait($test, $timeout = 30)
120: {
121: $num_tries = 0;
122: $result = false;
123: $exception = null;
124:
125: while ($num_tries < $timeout && !$result) {
126: try {
127: $result = call_user_func_array($test, array($this));
128: } catch (\Exception $e) {
129: $result = false;
130: $exception = $e;
131: }
132:
133: if (!$result) {
134: sleep(1);
135: }
136:
137: $num_tries++;
138: }
139:
140: if ($result) {
141: return true;
142: }
143:
144: if ($exception !== null) {
145: throw $exception;
146: }
147:
148: throw new Exception\TimeoutException(get_class($this).": Timeout in ".get_called_class()." after ".$num_tries." seconds.");
149: }
150:
151: 152: 153:
154:
155: 156: 157: 158: 159: 160: 161:
162: public function __call($method, $args)
163: {
164: if ($this->dom) {
165: if (is_callable(array($this->dom, $method))) {
166: return call_user_func_array(array($this->dom, $method), $args);
167: }
168: }
169:
170: return parent::__call($method, $args);
171: }
172: }
173:
174: ?>