1
+ interface FindToolbarButtonByTextOptions {
2
+ queryShadowButton ?: boolean ;
3
+ }
4
+
1
5
declare global {
2
6
// eslint-disable-next-line @typescript-eslint/no-namespace
3
7
namespace Cypress {
@@ -23,7 +27,24 @@ declare global {
23
27
* @example cy.get('[ui5-tab-container]').findUi5TabOpenPopoverButtonByText('Tab 1.1');
24
28
* @param {string } text The text of the sub-tab that should be queried.
25
29
*/
26
- findUi5TabOpenPopoverButtonByText ( text : string , options ?: Partial < ClickOptions > ) : Chainable < Element > ;
30
+ findUi5TabOpenPopoverButtonByText ( text : string ) : Chainable < Element > ;
31
+
32
+ /**
33
+ * Returns the element that represents the `ui5-toolbar-button`.
34
+ * When `options.queryShadowButton` is `true`, the internal `button` element is returned, otherwise the `ui5-button` element.
35
+ *
36
+ * __Note:__ This query can either be chained to a `ui5-toolbar` or not be chained at all.
37
+ *
38
+ * __Note:__ The `text` next needs to be unique in the respective scope.
39
+ *
40
+ * @example cy.findToolbarButtonByText("Button Text")
41
+ * @example cy.get('[ui5-toolbar]').findToolbarButtonByText("Button Text")
42
+ *
43
+ * @param {string } text - The text of the button to search for. This text should be unique within the toolbar to ensure a single button is returned.
44
+ * @param {FindToolbarButtonByTextOptions } [options] - An optional object containing configuration options for the query.
45
+ * @param {boolean } [options.queryShadowButton=false] - If set to `true`, the internal `button` element will be returned instead of the `ui5-button` element.
46
+ */
47
+ findToolbarButtonByText ( text : string , options ?: FindToolbarButtonByTextOptions ) : Chainable < Element > ;
27
48
// private
28
49
/**
29
50
* Returns the internal input element inside the shadow-root.
@@ -36,6 +57,41 @@ declare global {
36
57
}
37
58
}
38
59
60
+ Cypress . Commands . addQuery ( 'findToolbarButtonByText' , function ( text , options ) {
61
+ return ( subject ) => {
62
+ if ( subject !== undefined && ! subject ?. [ 0 ] ?. hasAttribute ( 'ui5-toolbar' ) ) {
63
+ const err = `findToolbarButtonByText() needs to be chained to a \`ui5-toolbar\`, or not be chained at all.` ;
64
+ throw new TypeError ( err ) ;
65
+ }
66
+ const container = subject ? [ subject [ 0 ] ] : document . querySelectorAll ( '[ui5-toolbar]' ) ;
67
+
68
+ const toolbarBtns : HTMLElement [ ] = [ ] ;
69
+ container . forEach ( ( el ) => {
70
+ if ( el ) {
71
+ const toolbarDom = el . getDomRef ( ) ;
72
+ const buttons = Cypress . $ ( toolbarDom ) . find ( '[ui5-button]' ) ;
73
+ const matchingButtons = buttons . filter ( function ( ) {
74
+ return Cypress . $ ( this ) . text ( ) === text ;
75
+ } ) ;
76
+
77
+ toolbarBtns . push ( ...matchingButtons ) ;
78
+ }
79
+ } ) ;
80
+
81
+ if ( toolbarBtns . length > 1 ) {
82
+ const err = `Multiple ui5-toolbar-button elements with the same text ("${ text } ") were found.` ;
83
+ throw new TypeError ( err ) ;
84
+ }
85
+
86
+ let toolbarBtn = toolbarBtns [ 0 ] ;
87
+ if ( options ?. queryShadowButton ) {
88
+ toolbarBtn = toolbarBtn . shadowRoot ! . querySelector ( 'button' ) ! ;
89
+ }
90
+
91
+ return Cypress . $ ( toolbarBtn ) ;
92
+ } ;
93
+ } ) ;
94
+
39
95
Cypress . Commands . addQuery ( 'findShadowInput' , function ( ) {
40
96
const getShadow = cy . now ( 'shadow' ) ;
41
97
const getInput = cy . now ( 'find' , 'input' ) ;
0 commit comments