@@ -77,42 +77,74 @@ export class UIElement {
7777 }
7878
7979 /**
80- * Returns if an element is selected
80+ * Returns if an element is selected
81+ */
82+ public async isSelected ( ) {
83+ const el = ( await this . element ( ) ) ;
84+ if ( ! el ) return false ;
85+ if ( this . _args . isAndroid ) {
86+ try {
87+ await el . getAttribute ( "selected" ) ;
88+ } catch ( error ) {
89+ console . error ( "Check if this is the correct element!" ) ;
90+ }
91+ }
92+
93+ try {
94+ return await el . isSelected ( ) ;
95+ } catch ( ex ) {
96+ console . warn ( "'selected' attr is not reachable on this element!" ) ;
97+ }
98+
99+ console . warn ( "Trying use 'value' attr!" ) ;
100+ try {
101+ const attrValue = await el . getAttribute ( "value" ) ;
102+ return attrValue === "1" || attrValue === "true" || attrValue === true ;
103+ } catch ( error ) {
104+ return false ;
105+ }
106+ }
107+
108+ /**
109+ * Selected an element
81110 */
82111 public async select ( retries : number = 3 ) {
83112 ( await ( await this . element ( ) ) ) . click ( ) ;
84113 let el = ( await this . element ( ) ) ;
85- if ( ! el ) return el ;
86-
87- const hasSelectedAttr = await ( await this . element ( ) ) . getAttribute ( "selected" ) ;
88- if ( hasSelectedAttr ) {
89- let isSelected = await el . isSelected ( ) ;
90- while ( retries >= 0 && ! isSelected ) {
91- ( await ( await this . element ( ) ) ) . click ( ) ;
92- isSelected = await el . isSelected ( ) ;
93- retries -- ;
94- await this . _driver . sleep ( 200 ) ;
95- }
96- } else {
97- console . log ( `This element doesn't contains selected attribute!` ) ;
114+ if ( ! el ) return el ;
115+
116+ let isSelected = await this . isSelected ( ) ;
117+ while ( retries >= 0 && ! isSelected ) {
118+ ( await ( await this . element ( ) ) ) . click ( ) ;
119+ isSelected = await this . isSelected ( ) ;
120+ retries -- ;
121+ await this . _driver . sleep ( 200 ) ;
98122 }
99123
100124 return el ;
101125 }
102126
103127 /**
104- * Returns if an element is selected
128+ * Returns if an element is checked
105129 */
106- public async isSelected ( ) {
130+ public async isChecked ( ) {
107131 const el = ( await this . element ( ) ) ;
108- if ( ! el ) return false ;
132+ if ( ! el ) return false ;
133+ if ( this . _args . isAndroid ) {
134+ try {
135+ const isChecked = await el . getAttribute ( "checked" ) ;
136+ return isChecked === "true" || isChecked === true ;
137+ } catch ( error ) {
138+ console . error ( "Check if this is the correct element!" ) ;
139+ }
140+ }
109141
110- const hasSelectedAttr = await ( await this . element ( ) ) . getAttribute ( "selected ") ;
111- if ( ! hasSelectedAttr ) {
112- console . log ( `This element doesn't contains selected attribute! Skip check!` ) ;
113- return true ;
114- } else {
115- return await ( await this . element ( ) ) . isSelected ( ) ;
142+ console . warn ( "Trying use 'value' attr! ") ;
143+ try {
144+ const attrValue = await el . getAttribute ( "value" ) ;
145+ return attrValue === "1" || attrValue === "true" || attrValue === true ;
146+ } catch ( error ) {
147+ return false ;
116148 }
117149 }
118150
0 commit comments