-
Notifications
You must be signed in to change notification settings - Fork 2
/
WooCommerceBrowser.php
132 lines (101 loc) · 3.23 KB
/
WooCommerceBrowser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
namespace Codeception\Module;
use Facebook\WebDriver\Exception\UnknownErrorException;
/**
* The WooCommerce Browser module.
*
* Extends WPWebDriver to add WooCommerce-specific methods for easier shop navigation.
*/
class WooCommerceBrowser extends WPWebDriver {
/**
* Attempts multiple times to perform an action on a clickable element.
*
* The method ignores "Element is not clickable" exceptions and tries to run the actions again.
*
* @param \Closure $action a closure that executes one or more actions
* @param int $attempts number of times to try the action before throwing an exception
* @param \Closure $exception_handler a closure used to determine whether an exception should be ignored
*/
public function tryAction( \Closure $action, $attempts = 3, \Closure $exception_handler = null ) {
do {
$attempts--;
try {
$this->waitForJqueryAjax();
$action();
break;
} catch ( UnknownErrorException $e ) {
// rethrow the exception on the last attempt
if ( $attempts <= 0 ) {
throw $e;
}
// try to click the element again if the element was not clickable for this attempt
if ( false !== strpos( $e->getMessage(), 'is not clickable at point' ) ) {
continue;
}
if ( $exception_handler && $exception_handler( $e ) ) {
continue;
}
throw $e;
}
} while ( $attempts > 0 );
}
/**
* Attempts to click an element multiple times.
*
* The method ignores "Element is not clickable" exceptions and tries to click again.
*
* @param mixed $element the element to click
* @param mixed $context CSS or XPath locator to narrow search
* @param int $attempts number of times to try the action before throwing an exception
*/
public function tryToClick( $element, $context = null, int $attempts = 3 ) {
$this->tryAction(
function() use( $element, $context ) {
$this->waitForElementClickable( $element );
$this->click( $element, $context );
},
$attempts
);
}
/**
* Attempts to tick a checkbox multiple times.
*
* The method ignores "Element is not clickable" exceptions and tries to tick the checkbox again.
*
* @param mixed $element the checkbox field
* @param int $attempts number of times to try the action before throwing an exception
*/
public function tryToCheckOption( $element, $attempts = 3 ) {
$this->tryAction(
function() use( $element ) {
$this->waitForElementClickable( $element );
$this->checkOption( $element );
},
$attempts
);
}
/**
* Attempts to select an option multiple times.
*
* The method ignores "Element is not clickable" exceptions and tries to select the option again.
*
* @param mixed $element the select or radio field
* @param mixed $option the value of the option or radio field to select
* @param int $attempts number of times to try the action before throwing an exception
*/
public function tryToSelectOption( $element, $option, $attempts = 3 ) {
$this->tryAction(
function() use( $element, $option ) {
$this->waitForElementClickable( $element );
$this->selectOption( $element, $option );
},
$attempts
);
}
/**
* Directs the test user to the cart page.
*/
public function amOnCartPage() {
$this->amOnUrl( wc_get_cart_url() );
}
}