github.com/saucelabs/saucectl@v0.175.1/internal/cypress/code/parser_test.go (about) 1 package code 2 3 import ( 4 "reflect" 5 "testing" 6 ) 7 8 func TestParse(t *testing.T) { 9 tests := []struct { 10 name string 11 input string 12 want []TestCase 13 }{ 14 { 15 name: "basic test case match", 16 input: ` 17 context('Actions', () => { 18 beforeEach(() => { 19 cy.visit('https://example.cypress.io/commands/actions') 20 }) 21 it('.type() - type into a DOM element', () => { 22 // https://on.cypress.io/type 23 cy.get('.action-email') 24 .type('fake@email.com').should('have.value', 'fake@email.com') 25 }) 26 }) 27 `, 28 want: []TestCase{ 29 { 30 Title: "Actions", 31 Tags: "", 32 }, 33 { 34 Title: ".type() - type into a DOM element", 35 Tags: "", 36 }, 37 }, 38 }, 39 { 40 name: "parse test case with multiple tags", 41 input: ` 42 context('Actions', () => { 43 beforeEach(() => { 44 cy.visit('https://example.cypress.io/commands/actions') 45 }) 46 it('.type() - type into a DOM element', { tags: ['@tag1', "@tag2"] }, () => { 47 // https://on.cypress.io/type 48 cy.get('.action-email') 49 .type('fake@email.com').should('have.value', 'fake@email.com') 50 }) 51 }) 52 `, 53 want: []TestCase{ 54 { 55 Title: "Actions", 56 Tags: "", 57 }, 58 { 59 Title: ".type() - type into a DOM element", 60 Tags: "@tag1 @tag2", 61 }, 62 }, 63 }, 64 { 65 name: "parse test case with single tag", 66 input: ` 67 context('Actions', () => { 68 beforeEach(() => { 69 cy.visit('https://example.cypress.io/commands/actions') 70 }) 71 it('.type() - type into a DOM element', { tags: '@tag1' }, () => { 72 // https://on.cypress.io/type 73 cy.get('.action-email') 74 .type('fake@email.com').should('have.value', 'fake@email.com') 75 }) 76 }) 77 `, 78 want: []TestCase{ 79 { 80 Title: "Actions", 81 Tags: "", 82 }, 83 { 84 Title: ".type() - type into a DOM element", 85 Tags: "@tag1", 86 }, 87 }, 88 }, 89 { 90 name: "parse test case with complex test object", 91 input: ` 92 context('Actions', () => { 93 beforeEach(() => { 94 cy.visit('https://example.cypress.io/commands/actions') 95 }) 96 it('.type() - type into a DOM element', { 97 tags: [ 98 '@tag1', 99 '@tag2' 100 ], 101 }, (() => { 102 // https://on.cypress.io/type 103 cy.get('.action-email') 104 .type('fake@email.com').should('have.value', 'fake@email.com') 105 }) 106 }) 107 `, 108 want: []TestCase{ 109 { 110 Title: "Actions", 111 Tags: "", 112 }, 113 { 114 Title: ".type() - type into a DOM element", 115 Tags: "@tag1 @tag2", 116 }, 117 }, 118 }, 119 { 120 name: "parse multiple test cases", 121 input: ` 122 context('Actions', function () { 123 beforeEach(function () { 124 cy.visit('https://example.cypress.io/commands/actions'); 125 }); 126 127 // https://on.cypress.io/interacting-with-elements 128 129 it('.type() - type into a DOM element', 130 { 131 tags: [ 132 '@tag1', 133 '@tag2', 134 ], 135 }, 136 function () { 137 // https://on.cypress.io/type 138 cy.get('.action-email') 139 .type('fake@email.com').should('have.value', 'fake@email.com') 140 141 // .type() with special character sequences 142 .type('{leftarrow}{rightarrow}{uparrow}{downarrow}') 143 .type('{del}{selectall}{backspace}') 144 145 // .type() with key modifiers 146 .type('{alt}{option}') //these are equivalent 147 .type('{ctrl}{control}') //these are equivalent 148 .type('{meta}{command}{cmd}') //these are equivalent 149 .type('{shift}') 150 151 // Delay each keypress by 0.1 sec 152 .type('slow.typing@email.com', { delay: 100 }) 153 .should('have.value', 'slow.typing@email.com'); 154 155 cy.get('.action-disabled') 156 // Ignore error checking prior to type 157 // like whether the input is visible or disabled 158 .type('disabled error checking', { force: true }) 159 .should('have.value', 'disabled error checking'); 160 } 161 ); 162 163 it('.focus() - focus on a DOM element', 164 { 165 tags: '@tag1', 166 otherAttr: 'somevalue', 167 object: { 168 hoo: 'hah', 169 }, 170 }, 171 function () { 172 // https://on.cypress.io/focus 173 cy.get('.action-focus').focus() 174 .should('have.class', 'focus') 175 .prev().should('have.attr', 'style', 'color: orange;'); 176 } 177 ); 178 `, 179 want: []TestCase{ 180 { 181 Title: "Actions", 182 Tags: "", 183 }, 184 { 185 Title: ".type() - type into a DOM element", 186 Tags: "@tag1 @tag2", 187 }, 188 { 189 Title: ".focus() - focus on a DOM element", 190 Tags: "@tag1", 191 }, 192 }, 193 }, 194 } 195 196 for _, tt := range tests { 197 t.Run(tt.name, func(t *testing.T) { 198 if got := Parse(tt.input); !reflect.DeepEqual(got, tt.want) { 199 t.Errorf("parseTitle() = \"%v\", want \"%v\"", got, tt.want) 200 } 201 }) 202 } 203 }