-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathWeb.php
More file actions
354 lines (262 loc) · 10.6 KB
/
Web.php
File metadata and controls
354 lines (262 loc) · 10.6 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<?php
namespace Tawk\Tests\TestFiles\Modules;
use Tawk\Tests\TestFiles\Types\TawkConfig;
use Tawk\Tests\TestFiles\Helpers\Common;
use Tawk\Tests\TestFiles\Types\Web\WebConfiguration;
use Tawk\Tests\TestFiles\Types\Web\WebDependencies;
use Tawk\Tests\TestFiles\Types\WebUserConfig;
class Web {
private Webdriver $driver;
private string $base_url;
private string $admin_url;
private string $plugin_page_url;
private string $plugin_settings_url;
private WebUserConfig $admin;
private TawkConfig $tawk;
private bool $logged_in;
private bool $plugin_installed;
private bool $plugin_activated;
private bool $widget_set;
public function __construct( WebDependencies $dependencies, WebConfiguration $config ) {
$this->driver = $dependencies->driver;
$this->base_url = Common::build_url( $config->web->url );
$this->admin_url = $this->base_url . 'wp-admin/';
$this->plugin_page_url = $this->admin_url . 'plugins.php';
$this->plugin_settings_url = $this->admin_url . 'options-general.php?page=tawkto_plugin';
$this->admin = $config->web->admin;
$this->tawk = $config->tawk;
$this->logged_in = false;
$this->plugin_installed = false;
$this->plugin_activated = false;
$this->widget_set = false;
}
public function get_base_url() {
return $this->base_url;
}
public function get_admin_url() {
return $this->admin_url;
}
public function get_plugin_page_url() {
return $this->plugin_page_url;
}
public function get_plugin_settings_url() {
return $this->plugin_settings_url;
}
public function login() {
if ( true === $this->logged_in ) {
$this->driver->goto_page( $this->admin_url );
return;
}
$this->driver->get_driver()->manage()->deleteAllCookies();
$this->driver->goto_page( $this->base_url . 'wp-login.php' );
$this->driver->find_element_and_input( '#user_login', $this->admin->username );
$this->driver->find_element_and_input( '#user_pass', $this->admin->password );
$this->driver->find_element_and_click( '#wp-submit' );
$this->driver->wait_until_page_fully_loads();
// prevent reauth=1 loop.
$this->driver->goto_page( $this->plugin_page_url );
$current_url = $this->driver->get_current_url();
if ( false !== strpos( $current_url, 'reauth=1' ) ) {
return $this->login();
}
$this->logged_in = true;
}
public function logout() {
if ( false === $this->logged_in ) {
return;
}
$this->driver->get_driver()->navigate()->refresh();
$logout_selector = '#wp-admin-bar-logout > a';
$logout_url = $this->driver->find_element_and_get_attribute_value( $logout_selector, 'href' );
$this->driver->get_driver()->get( $logout_url );
$this->logged_in = false;
}
public function install_plugin() {
if ( true === $this->plugin_installed ) {
return;
}
$file_upload_id = '#pluginzip';
$file_path = getcwd() . '/tmp/tawkto-live-chat.zip';
$this->driver->goto_page( $this->admin_url . 'plugin-install.php?tab=upload' );
$this->driver->wait_until_element_is_located( $file_upload_id );
// find the upload input and send the zip file there.
$this->driver->upload_file( $file_upload_id, $file_path );
// find the install button and click.
$this->driver->find_element_and_click( '#install-plugin-submit' );
$this->driver->wait_until_url_contains( $this->admin_url . 'update.php?action=upload-plugin' );
// this ensures that the plugin's installed.
$this->driver->wait_until_element_text_contains( 'a.button.button-primary', 'Activate' );
$this->plugin_installed = true;
}
public function activate_plugin() {
if ( true === $this->plugin_activated ) {
return;
}
$this->driver->goto_page( $this->plugin_page_url );
$activate_id = '#activate-tawkto-live-chat';
$this->driver->find_element_and_click( $activate_id );
$this->plugin_activated = true;
}
public function deactivate_plugin() {
if ( false === $this->plugin_activated ) {
return;
}
$this->driver->goto_page( $this->plugin_page_url );
$activate_id = '#deactivate-tawkto-live-chat';
$this->driver->find_element_and_click( $activate_id );
$this->plugin_activated = false;
}
public function uninstall_plugin() {
if ( false === $this->plugin_installed ) {
return;
}
$this->driver->goto_page( $this->plugin_page_url );
$activate_id = '#delete-tawkto-live-chat';
$this->driver->find_element_and_click( $activate_id );
$this->driver->wait_for_alert_and_accept();
$this->driver->wait_until_element_is_located( '#tawkto-live-chat-deleted' );
$this->plugin_installed = false;
}
public function goto_widget_selection() {
// incase current frame is not on the default one.
$this->driver->switch_to_default_frame();
$this->driver->goto_page( $this->plugin_settings_url );
$tab_id = '#account-settings-tab';
$this->driver->find_element_and_click( $tab_id );
// incase the "property and widget is already set" notice appears.
$reselect_link_id = '#reselect';
$reselect_link = $this->driver->find_and_check_element( $reselect_link_id );
if ( false === is_null( $reselect_link ) ) {
$reselect_link->click();
}
$this->driver->wait_for_frame_and_switch( '#tawk-iframe', 10 );
// driver currently on tawk-iframe frame
// incase the current session hasn't logged in to the plugin yet.
$login_button_id = '#login-button';
$login_button = $this->driver->find_and_check_element( $login_button_id );
if ( true === is_null( $login_button ) ) {
return;
}
$this->driver->find_element_and_click( '#login-button' );
$window_handles = $this->driver->get_driver()->getWindowHandles();
$new_window_handle = end( $window_handles );
$this->driver->get_driver()->switchTo()->window( $new_window_handle );
// driver currently on tawk.to OAuth login popout.
// currently logged in page not shown because driver creates new browser instance.
// handle login page.
$this->driver->find_element_and_input( '#email', $this->tawk->username );
$this->driver->find_element_and_input( '#password', $this->tawk->password );
$this->driver->find_element_and_click( 'button[type="submit"]' );
$start = microtime( true );
while ( ( microtime( true ) - $start ) < 5 ) {
// consent already allowed, window will automatically close.
$current_handles = $this->driver->get_driver()->getWindowHandles();
if ( ! in_array( $new_window_handle, $current_handles, true ) ) {
break;
}
// handle consent page if shown.
try {
$this->driver->find_element_and_click( '#allow' );
break;
} catch ( \Exception $e ) { // phpcs:ignore
// consent button not found, do nothing.
}
usleep( 10000 );
}
// go back to tawk-iframe frame.
$this->driver->get_driver()->switchTo()->window( reset( $window_handles ) );
$this->driver->wait_for_frame_and_switch( '#tawk-iframe', 10 );
}
public function goto_visibility_options() {
// incase current frame is not on the default one.
$this->driver->switch_to_default_frame();
$this->driver->goto_page( $this->plugin_settings_url );
$tab_id = '#visibility-options-tab';
$this->driver->find_element_and_click( $tab_id );
}
public function goto_privacy_options() {
// incase current frame is not on the default one.
$this->driver->switch_to_default_frame();
$this->driver->goto_page( $this->plugin_settings_url );
$tab_id = '#privacy-options-tab';
$this->driver->find_element_and_click( $tab_id );
}
public function goto_woocommerce_options() {
// incase current frame is not on the default one.
$this->driver->switch_to_default_frame();
$this->driver->goto_page( $this->plugin_settings_url );
$tab_id = '#woocommerce-options-tab';
$this->driver->find_element_and_click( $tab_id );
}
public function set_widget( $property_id, $widget_id ) {
if ( true === $this->widget_set ) {
return;
}
$this->goto_widget_selection();
$property_form_id = '#propertyForm';
$this->driver->wait_until_element_is_located( $property_form_id );
$this->driver->find_element_and_click( '#property' );
$this->driver->find_element_and_click( 'li[data-id="' . $property_id . '"]' );
$this->driver->find_element_and_click( '#widget-' . $property_id );
$this->driver->find_element_and_click( 'li[data-id="' . $widget_id . '"]' );
$this->driver->find_element_and_click( '#addWidgetToPage' );
// ensures widget is added.
$this->driver->wait_until_element_is_located( '#successMessage' );
$this->widget_set = true;
// go back to original frame.
$this->driver->switch_to_default_frame();
}
public function remove_widget() {
if ( false === $this->widget_set ) {
return;
}
$this->goto_widget_selection();
$this->driver->wait_until_element_is_located( '#propertyForm' );
$this->driver->find_element_and_click( '#removeCurrentWidget' );
// ensures widget is added.
$this->driver->wait_until_element_is_located( '#successMessage' );
$this->widget_set = false;
// go back to original frame.
$this->driver->switch_to_default_frame();
}
public function reset_visibility_options( $save_flag = true ) {
$this->goto_visibility_options();
$this->toggle_switch( '#always-display', false );
// clear out the text areas first.
$this->toggle_switch( '#exclude-url', true );
$this->driver->clear_input( '#excluded-url-list' );
$this->toggle_switch( '#include-url', true );
$this->driver->clear_input( '#included-url-list' );
// disable all other toggles except for always display.
$this->toggle_switch( '#show-onfrontpage', false );
$this->toggle_switch( '#show-oncategory', false );
$this->toggle_switch( '#show-ontagpage', false );
$this->toggle_switch( '#show-onarticlepages', false );
$this->toggle_switch( '#exclude-url', false );
$this->toggle_switch( '#include-url', false );
$this->toggle_switch( '#always-display', true );
if ( $save_flag ) {
$this->driver->move_mouse_to( '#submit-header' )->click();
$this->driver->wait_until_element_is_located( '#setting-error-settings_updated' );
}
}
public function reset_woocommerce_options( $save_flag = true ) {
$this->goto_woocommerce_options();
$this->toggle_switch( '#display-on-shop', false );
$this->toggle_switch( '#display-on-productcategory', false );
$this->toggle_switch( '#display-on-productpage', false );
$this->toggle_switch( '#display-on-producttag', false );
if ( $save_flag ) {
$this->driver->move_mouse_to( '#submit-header' )->click();
$this->driver->wait_until_element_is_located( '#setting-error-settings_updated' );
}
}
public function toggle_switch( $field_id, $enabled_flag ) {
$checkbox = $this->driver->find_element( $field_id );
if ( $checkbox->isSelected() === $enabled_flag ) {
return;
}
$slider_selector = $field_id . ' + .slider.round';
$this->driver->move_mouse_to( $slider_selector )->click();
}
}