-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
VarbaseContext.php
2743 lines (2476 loc) · 92.8 KB
/
VarbaseContext.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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
use WebDriver\Exception;
use Drupal\DrupalExtension\Context\RawDrupalContext;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Mink\Element\Element;
/**
* Defines application features from the specific context.
*/
class VarbaseContext extends RawDrupalContext implements SnippetAcceptingContext {
/**
* Hold the user name and password from drupal_users parameters.
*
* @var array
*/
protected $users = [];
/**
* Hold the user name and password from varbase_users parameters.
*
* @var array
*/
protected $varbaseUsers = [];
/**
* Hold all passed parameters.
*
* @var array
*/
protected $parameters = [];
/**
* Initializes context.
*
* @param array $parameters
* Context parameters (set them up through behat.yml or behat.local.yml).
*/
public function __construct(array $parameters) {
// Set the list of parameters.
$this->parameters = $parameters;
if (isset($parameters['varbase_users'])) {
$this->varbaseUsers = $parameters['varbase_users'];
foreach ($parameters['varbase_users'] as $varbaseUsername => $varbaseUser) {
$this->users[$varbaseUsername] = $varbaseUser['password'];
}
}
else {
throw new \Exception('behat.yml config files should include "varbase_users" property.');
}
}
/**
* Clean users.
*/
public function cleanUsers() {
}
/**
* Authenticate a user with password from varbase configuration.
*
* Varbase Context #varbase. If you want to see the list of users or add yours you can go and
* edit the behat.varbase.yml file under the varbase_users list.
*
* Example: I am a logged in user with the username "Content admin"
*
* @Given /^I am a logged in user with (?:|the )"(?P<username>[^"]*)"(?:| user)$/
* @Then /^I login with (?:|the )"(?P<username>[^"]*)"(?:| user)$/
*/
public function iAmloggedInUserWithTheUser($username) {
if (isset($this->users[$username])) {
try {
$password = $this->users[$username];
}
catch (Exception $e) {
throw new \Exception("Password not found for '$username'.");
}
if ($this->loggedIn()) {
$this->logout();
}
$this->getSession()->visit($this->locatePath('/user/login'));
$page = $this->getSession()->getPage();
if ($this->matchingElementAfterWait('css', '[data-drupal-selector="edit-name"]', 6000)) {
$page->fillField('name', $username);
$page->fillField('pass', $password);
$this->iScrollToBottom();
$this->iWaitForSeconds(2);
$submit = $page->findButton('op');
$submit->click();
}
}
else {
throw new \Exception("The '$username' user name is wrong or it was not listed in the list of default testing users.");
}
}
/**
* Authenticate a user with a given username and password on the spot.
*
* Varbase Context #varbase.
*
* Example: I am a logged in user with the username "testing" and password "testing user password"
*
* @Given /^I am a logged in user with (?:|the )username "(?P<username>[^"]*)" and password "(?P<password>[^"]*)"$/
*/
public function iAmLoggedInUserWithTheUsernameAndPassword($username, $password) {
// Logout if I am logged in.
if ($this->loggedIn()) {
$this->logout();
}
// Login with the.
$this->getSession()->visit($this->locatePath('/user/login'));
$page = $this->getSession()->getPage();
if ($this->matchingElementAfterWait('css', '[data-drupal-selector="edit-name"]', 6000)) {
$page->fillField('name', $username);
$page->fillField('pass', $password);
$this->iScrollToBottom();
$this->iWaitForSeconds(2);
$submit = $page->findButton('op');
$submit->click();
}
}
/**
* Logout from the current session.
*
* Varbase Context #varbase.
*
* Example: When I logout.
*
* @When /^I logout$/
*/
public function iLogout() {
// Logout if I am logged in.
if ($this->loggedIn()) {
$this->logout();
}
}
/**
* Navigate directly to an external web site.
*
* Varbase Context #varbase.
*
* Example: When I go to "https://www.google.com" website.
*
* @When /^I go to "(?P<domain>[^"]*)" website$/
*/
public function iGoToWebsite($domain) {
$this->getSession()->visit($domain);
}
/**
* Wait for seconds before going to the next step.
*
* Varbase Context #varbase.
*
* Example 1: And wait for "1" second
* Example 2: When I wait for "5" seconds
* Example 3: And wait 1 second
* Example 4: When I wait for 60 seconds
* Example 5: And wait 1s
* Example 6: When I wait for 60s.
*
* @When /^(?:|I )wait (?:|for )"(?P<seconds>\d+)" second(?:|s)$/
* @When /^(?:|I )wait (?:|for )(?P<seconds>\d+) second(?:|s)$/
* @When /^(?:|I )wait (?:|for )(?P<seconds>\d+)s$/
*/
public function iWaitForSeconds($seconds = 1) {
$this->getSession()->wait($seconds * 1000);
}
/**
* Wait for minutes before going to the next step.
*
* Varbase Context #varbase.
*
* Example 1: And I wait for "1" minute
* Example 2: When I wait for "2" minutes
* Example 3: And wait 1 minute
* Example 4: When I wait for 3 minutes
* Example 5: And wait 1m
* Example 6: When I wait for 3m.
*
* @When /^(?:|I )wait (?:|for )"(?P<minutes>\d+)" minute(?:|s)$/
* @When /^(?:|I )wait (?:|for )(?P<minutes>\d+) minute(?:|s)$/
* @When /^(?:|I )wait (?:|for )(?P<minutes>\d+)m$/
*/
public function iWaitForMinutes($minutes = 1) {
$this->getSession()->wait($minutes * 60 * 1000);
}
/**
* Wait max of seconds for the page to be ready and loaded.
*
* Varbase Context #varbase.
*
* Example 1: And wait
* Example 2: And I wait
* Example 3: And wait for the page
* Example 4: And I wait for the page
* Example 5: And wait max of 5 seconds
* Example 6: And wait max of 5s
* Example 7: And I wait max of 5s
* Example 8: And I wait max of "5" seconds
* Example 9: And I wait max of "5" seconds for the page to be ready and loaded.
*
* @Given /^(?:|I )wait max of "(?P<time>\d+)" second(?:|s)(?:| for the page to be ready and loaded)$/
* @Given /^(?:|I )wait max of (?P<time>\d+) second(?:|s)(?:| for the page to be ready and loaded)$/
* @Given /^(?:|I )wait max of (?P<time>\d+)s(?:| for the page to be ready and loaded)$/
* @Given /^(?:|I )wait(?:| for the page)$/
*
* @throws \WebDriver\Exception
* If timeout is reached.
*/
public function iWaitMaxOfSecondsForThePageToBeReadyAndLoaded($time = 10000) {
if (!$this->getSession()->getDriver() instanceof Selenium2Driver) {
return;
}
$start = microtime(TRUE);
$end = $start + $time / 1000.0;
$conditions = [
// Page is ready.
"document.readyState == 'complete'",
// jQuery is loaded.
"typeof $ != 'undefined'",
// No ajax request is active.
"!$.active",
];
$condition = implode(' && ', $conditions);
// Make sure the AJAX calls are fired up before checking the condition.
$this->getSession()->wait(100, FALSE);
$this->getSession()->wait($time, $condition);
// Check if we reached the timeout unless the condition is false to explicitly wait the specified time.
if ($condition !== FALSE && microtime(TRUE) > $end) {
throw new \Exception(sprintf('Timeout of %d reached when checking on %s', $time, $condition));
}
}
/**
* Editor Media Browser functions.
*
* ==========================================================================.
*/
/**
* Click the editor media browser command button.
*
* Varbase Context #varbase.
*
* Example 1: When I click the editor media browser command button.
*
* @When /^I click the editor media browser command button$/
*/
public function iClickTheEditorMediaBrowserCommandButton() {
$editorMediaBrowserButton = $this->getSession()->getPage()->find('css', '.cke_button.cke_button__media');
if (empty($editorMediaBrowserButton)) {
throw new \Exception('The editor media browser button dose not exist.');
}
$editorMediaBrowserButton->click();
}
/**
* Check if the editor media browser is open.
*
* Varbase Context #varbase.
*
* @Then /^the editor media browser should be open$/
*/
public function theEitorMediaBrowserIsOpen() {
if (!$elem = $this->getSession()->getPage()->find('css', '.ui-dialog.media-wrapper')
|| !$this->getSession()->getPage()->find('css', '.ui-dialog.media-wrapper .media-browser-panes')) {
throw new \Exception('The editor media browser failed to open.');
}
}
/**
* Press a button in the filter form under the editor media browser.
*
* Varbase Context #varbase.
*
* Example 1: When I press the "Apply" button under the editor media browser
* Example 2: And I press the "Submit" button under the editor media browser.
*
* @When /^I press (?:|the )"([^"]*)" button under the editor media browser$/
*/
public function iPressTheButtonUnderTheEditorMediaBrowser($button) {
// Switch to the "entity_browser_iframe_editor_media_browser" iframe.
$this->getSession()->switchToIFrame('entity_browser_iframe_editor_media_browser');
try {
$this->getSession()->wait(1000, 'typeof(jQuery)=="undefined" || jQuery("#autocomplete").length === 0');
}
catch (UnsupportedDriverActionException $e) {
}
$this->getSession()->getPage()->pressButton($button);
// Switch back too the page from the "entity_browser_iframe_editor_media_browser" iframe.
$this->getSession()->switchToIFrame(NULL);
}
/**
* Click on a link or button under the editor media browser.
*
* Varbase Context #varbase.
*
* Example 1: When I click "Submit" button under the media browser
* Example 2: When I click "Submit" under media browser
* Example 3: When I click "Upload" under the media browser.
*
* @When /^I click "([^"]*)" (?:|button )under (?:|the )editor media browser$/
*/
public function iClickButtonUnderTheEditorMediaBrowser($text) {
// Switch to the "mediaBrowser" iframe.
$this->getSession()->switchToIFrame('entity_browser_iframe_editor_media_browser');
// Find the Tab by txt.
$element = $this->getSession()->getPage()->find('xpath', "//*[contains(@class, 'button') and text() = '" . $text ."']");
if (empty($element)) {
throw new \Exception('The editor media browser dose not have [ ' . $text . ' ] button.');
}
$element->click();
// Switch back too the page from the "entity_browser_iframe_editor_media_browser" iframe.
$this->getSession()->switchToIFrame(NULL);
}
/**
* Click on a tab under the editor media browser.
*
* Varbase Context #varbase.
*
* Example 1: When I click on the "Library" tab under the editor media browser
* Example 2: When I click on the "My files" tab under the editor media browser.
*
* @When /^I click on the "([^"]*)" tab under the editor media browser$/
*/
public function iClickOnTheTabUnderTheMediaBrowser($text) {
// Switch to the "entity_browser_iframe_editor_media_browser" iframe.
$this->getSession()->switchToIFrame('entity_browser_iframe_editor_media_browser');
// Find the Tab by txt.
$element = $this->getSession()->getPage()->find('xpath', "//*[contains(@class, 'ui-tabs-anchor') and text() = '" . $text . "']");
if (empty($element)) {
throw new \Exception('The editor media browser dose not have [ ' . $text . ' ] tab.');
}
$element->click();
// Switch back too the page from the "mediaBrowser" iframe.
$this->getSession()->switchToIFrame(NULL);
}
/**
* Select the file under the editor media browser.
*
* Varbase Context #varbase.
*
* Example 1: When I click on the "Flag Earth" file under the editor media browser.
*
* @When /^I select (?:|the )"([^"]*)" file under (?:|the )editor media browser$/
*/
public function iSelectTheFileUnderTheEditorMediaBrowser($text) {
// Switch to the "entity_browser_iframe_editor_media_browser" iframe.
$this->getSession()->switchToIFrame('entity_browser_iframe_editor_media_browser');
// Find the file by text.
$element = $this->getSession()->getPage()->find('xpath', "//div[contains(@class, 'media-item') and contains(@title, '" . $text ."')]");
if (empty($element)) {
throw new \Exception('The editor media browser dose not have [ ' . $text . ' ] file.');
}
$element->click();
// Switch back too the page from the "entity_browser_iframe_editor_media_browser" iframe.
$this->getSession()->switchToIFrame(NULL);
}
/**
* Click on the first delete button.
*
* Varbase Context #varbase.
*
* @When I click the delete button
*/
public function iClickTheDeleteButton() {
// Find the delete button by text.
$element = $this->getSession()->getPage()->find('xpath', "//button[text()='Delete']");
if (empty($element)) {
throw new \Exception('The delete action button is not found.');
}
$element->click();
}
/**
* Click on the first Next button in the tour.
*
* Varbase Context #varbase.
*
* @When I click next button in tour
*/
public function iClickNextInTour() {
// Find the next button in tour by text.
$element = $this->getSession()->getPage()->find('xpath', "//footer[contains(@class, 'shepherd-footer')]//button[text()='Next']");
if (empty($element)) {
throw new \Exception('The Next action button in the tour is not found.');
}
$this->getSession()->executeScript('document.querySelector("body > div.drupal-tour.shepherd-enabled > div.shepherd-content > footer > button").click();');
}
/**
* @Then :text should be in the breadcrumb
*/
public function shouldBeInTheBreadcrumb($text) {
// Find a text in the breadcrumb.
$element = $this->getSession()->getPage()->find('xpath', "//ol[contains(@class, 'breadcrumb')]//*[text()='" . $text ."']");
if (empty($element)) {
throw new \Exception('The ' . $text . ' not found in the breadcrumb');
}
}
/**
* Fill in a form field with id|name|title|alt|value.
*
* Under the editor media browser.
* Varbase Context #varbase.
*
* Example: I fill in "flag earth" for "File name" under the editor media browser.
*
* @When /^(?:|I )fill in "(?P<field>(?:[^"]|\\")*)" with "(?P<value>(?:[^"]|\\")*)" under (?:|the )editor media browser$/
* @When /^(?:|I )fill in "(?P<field>(?:[^"]|\\")*)" with: under (?:|the )editor media browser$/
* @When /^(?:|I )fill in "(?P<value>(?:[^"]|\\")*)" for "(?P<field>(?:[^"]|\\")*)" under (?:|the )editor media browser$/
*/
public function iFillInFieldUnderTheEditorMediaBrowser($field, $value) {
// Switch to the "entity_browser_iframe_editor_media_browser" iframe.
$this->getSession()->switchToIFrame('entity_browser_iframe_editor_media_browser');
$field = str_replace('\\"', '"', $field);
$value = str_replace('\\"', '"', $value);
$this->getSession()->getPage()->fillField($field, $value);
// Switch back too the page from the "entity_browser_iframe_editor_media_browser" iframe.
$this->getSession()->switchToIFrame(NULL);
}
/**
* Check if we can see a text under the editor media browser.
*
* Varbase Context #varbase.
*
* Example 1: Then I should see "this text" under editor media browser
* Example 2: Then I should see "this text" under the editor media browser modal window.
*
* @Then /^I should see "([^"]*)" under (?:|the )editor media browser(?:| modal window)$/
*/
public function iShouldSeeTextUnderTheEditorMediaBrowser($text) {
// Switch to the "entity_browser_iframe_editor_media_browser" iframe.
$this->getSession()->switchToIFrame('entity_browser_iframe_editor_media_browser');
$actual = $this->getSession()->getPage()->getText();
$actual = preg_replace('/\s+/u', ' ', $actual);
$regex = '/' . preg_quote($text, '/') . '/ui';
if (!preg_match($regex, $actual)) {
throw new \Exception(sprintf('The text "%s" was not found anywhere in the text of the current page.', $text));
}
// Switch back too the page from the "entity_browser_iframe_editor_media_browser" iframe.
$this->getSession()->switchToIFrame(NULL);
}
/**
* Find an image with the title text attribute.
*
* Varbase Context #varbase.
*
* Example 1: Then I should see image with the "Flag Earth" title text.
*
* @Then /^I should see image with the "([^"]*)" title text under (?:|the )editor media browser(?:| modal window)$/
*/
public function iShouldSeeImageWithTheTitleTextUnderTheEditorMediaBrowser($titleText) {
// Switch to the "entity_browser_iframe_editor_media_browser" iframe.
$this->getSession()->switchToIFrame('entity_browser_iframe_editor_media_browser');
// Find an image with the title.
$element = $this->getSession()->getPage()->find('xpath', "//img[contains(@title, '" . $titleText . "')]");
if (empty($element)) {
throw new \Exception('The editor media browser dose not have an image with the [ ' . $titleText . ' ] title text.');
}
// Switch back too the page from the "entity_browser_iframe_editor_media_browser" iframe.
$this->getSession()->switchToIFrame(NULL);
}
/**
* Check if we can NOT see a text under the editor media browser.
*
* Varbase Context #varbase.
*
* Example 1: Then I should not see "this text" under editor media browser
* Example 2: Then I should not see "this text" under the editor media browser modal window.
*
* @Then /^I should not see "([^"]*)" under (?:|the )editor media browser(?:| modal window)$/
*/
public function iShouldNotSeeTextUnderTheeEitorMediaBrowser($text) {
// Switch to the "entity_browser_iframe_editor_media_browser" iframe.
$this->getSession()->switchToIFrame('entity_browser_iframe_editor_media_browser');
$actual = $this->getSession()->getPage()->getText();
$actual = preg_replace('/\s+/u', ' ', $actual);
$regex = '/' . preg_quote($text, '/') . '/ui';
if (preg_match($regex, $actual)) {
throw new \Exception(sprintf('The text "%s" was not found anywhere in the text of the current page.', $text));
}
// Switch back too the page from the "entity_browser_iframe_editor_media_browser" iframe.
$this->getSession()->switchToIFrame(NULL);
}
/*
* ===========================================================================
* Rich text editor Functions CKEditor 4.
* ===========================================================================
*/
/**
* Fill in a rich text editor field WYSIWYG with content.
*
* Using the name of the field.
* Varbase Context #varbase.
*
* Example: When I fill in the rich text editor field "Body" with "Test Body text"
*
* @When /^I fill in the rich text editor field "([^"]*)" with "([^"]*)"$/
*/
public function iFillInTheRichTextEditorField($locator, $value) {
$el = $this->getSession()->getPage()->findField($locator);
$fieldId = $el->getAttribute('id');
if (empty($fieldId)) {
throw new \Exception('Could not find an id for the rich text editor field : ' . $locator);
}
$this->getSession()->executeScript("Drupal.CKEditor5Instances.get(document.getElementById(\"$fieldId\").dataset[\"ckeditor5Id\"]).setData(\"$value\");");
}
/**
* Click a command button in the rich text editor.
*
* Varbase Context #varbase.
*
* Example 1: When I click on "bold" command button in the rich text editor field "Body"
* Example 2: When I click on "media" command button in the rich text editor field "Body".
*
* @When /^I click on "([^"]*)" command button in the rich text editor field "([^"]*)"$/
*/
public function iClickOnCommandButtonInTheRichTextEditorField($selectorCommand, $locator) {
$el = $this->getSession()->getPage()->findField($locator);
$fieldId = $el->getAttribute('id');
if (empty($fieldId)) {
throw new \Exception('Could not find an id for the rich text editor field : ' . $locator);
}
// Find the command button by the select command.
$element = $this->getSession()->getPage()->find('xpath', "//button[span[text()='$selectorCommand']]");
if (is_null($element)) {
throw new \Exception("The $selectorCommand command button in the rich text editor field $locator was not found");
}
$element->click();
}
/**
* Append text at the end of a rich text editor field WYSIWYG with content.
*
* Using the name of the field.
*
* Varbase Context #varbase.
*
* Example #1: When I append after the rich text editor field "Body" with "Test Body text"
* Example #2: When I append the rich text editor field "Body" with "Test Body text"
*
* @When /^(?:|I )append(?:| after) the rich text editor field "([^"]*)" with "([^"]*)"$/
*/
public function appendTheRichTextEditorField($locator, $value) {
$el = $this->getSession()->getPage()->findField($locator);
$fieldId = $el->getAttribute('id');
if (empty($fieldId)) {
throw new \Exception('Could not find an id for the rich text editor field : ' . $locator);
}
$this->getSession()->executeScript("Drupal.CKEditor5Instances.get(document.getElementById(\"$fieldId\").dataset[\"ckeditor5Id\"]).setData(Drupal.CKEditor5Instances.get(document.getElementById(\"$fieldId\").dataset[\"ckeditor5Id\"]).getData()+\"$value\");");
}
/**
* Add append text at the end of rich text editor field WYSIWYG with content.
*
* Using the name of the field.
* Varbase Context #varbase.
*
* Example #1: When I prepend before the rich text editor field "Body" with "Test Body text"
* Example #2: When I prepend the rich text editor field "Body" with "Test Body text"
*
* @When /^(?:|I )prepend(?:| before) the rich text editor field "([^"]*)" with "([^"]*)"$/
*/
public function prependTheRichTextEditorField($locator, $value) {
$el = $this->getSession()->getPage()->findField($locator);
$fieldId = $el->getAttribute('id');
if (empty($fieldId)) {
throw new \Exception('Could not find an id for the rich text editor field : ' . $locator);
}
$this->getSession()->executeScript("CKEDITOR.instances[\"$fieldId\"].setData(\"$value\"+CKEDITOR.instances[\"$fieldId\"].getData());");
}
/**
* Move the focus to selected rich text editor field.
*
* Varbase Context #varbase.
*
* Example #1: When I move focus to "Title" rich text editor field
* Example #2: And I move focus to "Body" rich text editor field.
*
* @When /^(?:|I )move focus to "(?P<selectedField>[^"]*)" rich text editor field$/
*/
public function moveFocusToTheRichTextEditorField($selectedField) {
$el = $this->getSession()->getPage()->findField($selectedField);
$fieldId = $el->getAttribute('id');
if (empty($fieldId)) {
throw new \Exception('Could not find an id for the rich text editor field : ' . $selectedField);
}
$this->getSession()->getDriver()->evaluateScript("Drupal.CKEditor5Instances.get(document.getElementById(\"" . $fieldId ."\").dataset[\"ckeditor5Id\"]).editing.view.focus()");
}
/**
* Select all text in selected field input element.
*
* Varbase Context #varbase.
*
* Example #1: When I select all text in "Body" rich text editor field
* Example #2: And I select all text in "Body" rich text editor field
*
* @When /^(?:|I )select all text in "(?P<selectedField>[^"]*)" rich text editor field$/
*/
public function selectAllTextInTheRichTextEditorField($selectedField) {
$el = $this->getSession()->getPage()->findField($selectedField);
$fieldId = $el->getAttribute('id');
if (empty($fieldId)) {
throw new \Exception('Could not find an id for the rich text editor field : ' . $selectedField);
}
$this->getSession()->getDriver()->evaluateScript("Drupal.CKEditor5Instances.get(document.getElementById(\"$fieldId\").dataset[\"ckeditor5Id\"]).execute( 'selectAll' );");
}
/**
* Click on the save button in the the rich text editor field
*
* Varbase Context #varbase.
*
* Example 1: I click on the save button in "Body" rich text editor field
*
* @Given /^I click on the save button in "(?P<selectedField>[^"]*)" rich text editor field$/
*/
public function iClickOnTheSaveButtonInTheEditor($selectedField) {
$selectorFieldElement = $this->getSession()->getPage()->findField($selectedField);
$fieldId = $selectorFieldElement->getAttribute('id');
if (empty($fieldId)) {
throw new \Exception('Could not find an id for the rich text editor field : ' . $selectedField);
}
// Find the save button for the current selected field
$element = $this->getSession()->getPage()->find('css', "button.ck.ck-button.ck-off.ck-button-save");
if (empty($element)) {
throw new \Exception("No save button for the " . $selectedField . " rich text editor field.");
}
// Click the save button.
$element->click();
}
/**
* =========================================================.
*
* Section Configuration Functions
*
* =========================================================.
*/
/**
* Add a basic section at the end of layout.
*
* Varbase Context #varbase
*
* Example #1: When I add a basic "4 Cols" section at the end of layout
* Example #2: And I add a basic section at the end of layout
*
* @When I add a basic section at the end of layout
* @When I add a basic :arg1 section at the end of layout
* @And I add a basic (?:|arg1 ) section at the end of layout
*/
public function iAddABasicSectionAtTheEndOfLayout($cols = "1 Col") {
$this->iScrollToBottom();
$this->iWaitForSeconds(1);
$end_layout = $this->getSession()->getPage()->find('xpath', "//a[contains(@class, 'use-ajax layout-builder__link layout-builder__link--add') and contains(., 'at end of layout')]")->click();
$this->iWaitForSeconds(1);
$section = $this->getSession()->getPage()->find('xpath', "//*[contains(., '$cols') and contains(@class, 'use-ajax')]");
if (is_null($section)) {
throw new \Exception('The ' . $cols . ' option was not found or not visible');
}
$section->click();
$this->iWaitForSeconds(1);
}
/**
* @When I save the section
*/
public function iSaveTheSection() {
$save = $this->getSession()->getPage()->find('xpath', "//*[contains(@value, 'Add section')]");
if (is_null($save)) {
throw new \Exception('The "Add section" button was not found or not visible');
}
$save->click();
}
/**
* Select a section container type.
*
* Varbase Context #varbase
*
* Example #1: When I select the "Edge to Edge" container type
* Example #2: And I select the "Boxed" container type with a "Tiny" width
*
* @When I select the :type container type
* @When I select the :type container type with a :width width
*/
public function iSelectTheContainerType($type, $width = NULL) {
$element = $this->getSession()->getPage()->find('xpath', "//label[contains(.,'$type') and contains(@for, 'edit-layout-settings-ui-tab-content-layout-container-type')]");
if (is_null($element)) {
throw new \Exception("The $type option was not found or not visible. \nThese are the available container types:\n\tFull (Default)\n\tEdge to Edge\n\tBoxed\n");
}
$element->click();
if ($type === "Boxed" && isset($width)) {
$this->iSelectTheContainerWidth($width);
}
}
/**
* Select a section container width.
*
* Varbase Context #varbase
*
* Example #1: When I select the "Tiny" container width
* Example #2: And I select the "Narrow" container width
*
* @When I select the :width container width
*/
public function iSelectTheContainerWidth($width) {
$element = $this->getSession()->getPage()->find('xpath', "//label[contains(.,'$width') and contains(@for, 'edit-layout-settings-ui-tab-content-layout-container-width')]");
if (is_null($element)) {
throw new \Exception("The $width option was not found or not visible. \nThese are the available container widths:\n\tWide (Default)\n\tMedium\n\tNarrow\n\tTiny\n");
}
$element->click();
}
/**
* Select a section breakpoint.
*
* Varbase Context #varbase
*
* Example #1: When I select the "md" "33% 67%" section breakpoint
* Example #2: And I select the "xs" "75% 25%" section breakpoint
*
* @When I select the :size :point section breakpoint
*/
public function iSelectTheSectionBreakpoint($size, $point) {
$screen_size = $this->getSession()->getPage()->find('xpath', "//*[contains(@class, '$size')]");
if (is_null($screen_size)) {
throw new \Exception("The $size option was not found or not visible. \nThese are the available screen sizes:\n\tlg\n\tmd\n\tsm\n\txs\n");
}
$break = $this->getSession()->getPage()->find('xpath', "//*[contains(., '$point')]");
if (is_null($break)) {
throw new \Exception('The break point selected was not found or not suitable for the screen size selected');
}
$element = $this->getSession()->getPage()->find('xpath', "//*[contains(@class,'$size') and contains(.,'$point')]")->click();
}
/**
* Select with gutters option for section.
*
* Varbase Context #varbase
*
* Example #1: When I add section gutters
* Example #2: And I add section gutters
*
* @When I add section gutters
*/
public function iAddSectionGutters() {
$with_gutters = $this->getSession()->getPage()->find('xpath', "//label[contains(., 'With Gutters')]");
if (is_null($with_gutters)) {
throw new \Exception('The "With Gutters" option was not found or not visible');
}
$with_gutters->click();
}
/**
* Remove gutters between columns.
*
* Varbase Context #varbase
*
* Example #1: When I remove gutters between section columns
* Example #2: And I remove gutters between section columns
*
* @When I remove gutters between section columns
*/
public function iRemoveGuttersBetweenSectionColumns() {
$no_gutters = $this->getSession()->getPage()->find('xpath', "//*[contains(@class, 'vlb_gutters_between')]");
if (is_null($no_gutters)) {
throw new \Exception('The "Keep gutters between columns" checkbox was not found or not visible');
}
$no_gutters->click();
}
/**
* Move to the section styles tab.
*
* Varbase Context #varbase
*
* Example #1: When I move to the section styles tab
* Example #2: And I move to the section styles tab
*
* @When I move to the section styles tab
*/
public function iMoveToTheSectionStylesTab() {
$styles_tab = $this->getSession()->getPage()->find('xpath', "//a[contains(@data-target, 'appearance')]");
if (is_null($styles_tab)) {
throw new \Exception('The section styles tab was not found or not visible');
}
$styles_tab->click();
}
/**
* Open a specific setting menu under styles tab in section configuration.
*
* Varbase Context #varbase
*
* Example #1: When I open the section "Background" settings menu
* Example #2: And I open the section "Border" settings menu
*
* @When I open the section :menu settings menu
*/
public function iOpenTheSectionSettingsMenu($menu) {
$this->iMoveToTheSectionStylesTab();
$js = <<<JS
var title = "$menu";
const xpath = "//span[contains(text(),'" + title + "')]";
const js_menu = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
js_menu.closest("details").setAttribute("open", "");
JS;
try {
$this->getSession()->executeScript($js);
}
catch (Exception $e) {
throw new \Exception('The "' . $menu . '" menu was not found or not visible');
}
}
/**
* Select a section background color.
*
* Varbase Context #varbase
*
* Example #1: When I select the "Primary" section background color
* Example #2: And I select the "Light" section background color
*
* @When I select the :color section background color
*/
public function iSelectTheSectionBackgroundColor($color) {
$this->iSwitchToSectionBackgroundColorSettings();
$bg_color = $this->getSession()->getPage()->find('xpath', "//label[contains(., '$color') and contains(@for, 'edit-layout-settings-ui-tab-content-appearance-background-background-color')]");
if (is_null($bg_color)) {
throw new \Exception('The "' . $color . '" option was not found or not visible');
}
$bg_color->click();
}
/**
* Uncheck the Edge to Edge Background option.
*
* Varbase Context #varbase
*
* Example #1: When I uncheck the Edge to Edge Background
* Example #2: And I uncheck the Edge to Edge Background
*
* @When I uncheck the Edge to Edge Background
*/
public function iUncheckTheEdgeToEdgeBackground() {
$this->iOpenTheSectionSettingsMenu("Background");
$e2e = $this->getSession()->getPage()->find('xpath', "//input[contains(@class, 'field-background-edge-to-edge')]");
if (is_null($e2e)) {
throw new \Exception('The "Edge to Edge Background" checkbox was not found or not visible');
}
$e2e->click();
}
/**
* Select a section text color.
*
* Varbase Context #varbase
*
* Example #1: When I select the "Dark" section text color
* Example #2: And I select the "White" section text color
*
* @When I select the :color section text color
*/
public function iSelectTheSectionTextColor($color) {
$this->iOpenTheSectionSettingsMenu("Typography");
$text_color = $this->getSession()->getPage()->find('xpath', "//label[contains(., '$color') and contains(@for, 'edit-layout-settings-ui-tab-content-appearance-typography-text-color-text')]");
if (is_null($text_color)) {
throw new \Exception('The "' . $color . '" option was not found or not visible');
}
$text_color->click();
}
/**
* Set alignment of text.
*
* Varbase Context #varbase
*
* Example #1: When I set the alignment to "End"
* Example #2: And I set the alignment to "Start"
*
* @When I set the alignment to :align
*/
public function iSetTheAlignmentTo($align) {
$this->iOpenTheSectionSettingsMenu("Typography");
$alignment = $this->getSession()->getPage()->find('xpath', "//label[contains(., '$align') and contains(@for, 'edit-layout-settings-ui-tab-content-appearance-typography-text-alignment')]");
if (is_null($alignment)) {
throw new \Exception("The alignment option selected was not found or not visible. \nSelect one of these: \n\tStart\n\tCenter\n\tEnd\n\tJustify\n");
}
$alignment->click();
}
/**
* Set the section blocks alignment.
*
* #Varbase Context #varbase
*
* Example #1: When I set the blocks vertical alignment to "Align middle"
* Example #2: When I set the blocks horizontal alignment to "Align start"
*
* @When I set the blocks :orientation alignment to :align
*/
public function iSetTheBlocksAlignmentTo($orientation, $align) {
$this->iOpenTheSectionSettingsMenu("Blocks alignment");
$alignment = $this->getSession()->getPage()->find('xpath', "//label[contains(., '$align') and contains(@for, 'edit-layout-settings-ui-tab-content-appearance-alignment-$orientation')]");
if (is_null($alignment)) {
throw new \Exception("The alignment option selected was not found or not visible\nAlignment options for vertical orientation are: \n\tAlign top\n\tAlign middle\n\tAlign bottom\n\nAlignment options for horizontal orientation are: \n\tAlign start\n\tAlign center\n\tAlign end\n");
}
$alignment->click();
}