| OLD | NEW |
| (Empty) |
| 1 library webdriver_test; | |
| 2 import 'dart:async' show getAttachedStackTrace; | |
| 3 import 'package:webdriver/webdriver.dart'; | |
| 4 import 'package:unittest/unittest.dart'; | |
| 5 import 'package:unittest/vm_config.dart'; | |
| 6 | |
| 7 WebDriver web_driver; | |
| 8 | |
| 9 /** | |
| 10 * These tests are not expected to be run as part of normal automated testing, | |
| 11 * as they are slow and they have external dependencies. | |
| 12 */ | |
| 13 main() { | |
| 14 useVMConfiguration(); | |
| 15 | |
| 16 var web_driver = new WebDriver('localhost', 4444, '/wd/hub'); | |
| 17 var session = null; | |
| 18 | |
| 19 var exceptionHandler = (error) { | |
| 20 var trace = getAttachedStackTrace(error); | |
| 21 String traceString = trace == null ? "" : trace.toString(); | |
| 22 if (error is TestFailure) { | |
| 23 currentTestCase.fail(error.message, traceString); | |
| 24 } else { | |
| 25 currentTestCase.error("Unexpected error: ${error}", traceString); | |
| 26 } | |
| 27 if (session != null) { | |
| 28 var s = session; | |
| 29 session = null; | |
| 30 s.close(); | |
| 31 } | |
| 32 }; | |
| 33 | |
| 34 group('Sessionless tests', () { | |
| 35 test('Get status', () { | |
| 36 return web_driver.getStatus() | |
| 37 .then((status) { | |
| 38 expect(status['sessionId'], isNull); | |
| 39 }) | |
| 40 .catchError(exceptionHandler); | |
| 41 }); | |
| 42 }); | |
| 43 | |
| 44 group('Basic session tests', () { | |
| 45 test('Create session/get capabilities', () { | |
| 46 return web_driver.newSession('chrome') | |
| 47 .then((_session) { | |
| 48 expect(_session, isNotNull); | |
| 49 session = _session; | |
| 50 return session.getCapabilities(); | |
| 51 }) | |
| 52 .then((capabilities) { | |
| 53 expect(capabilities, isNotNull); | |
| 54 expect(capabilities['browserName'], equals('chrome')); | |
| 55 return session.close(); | |
| 56 }) | |
| 57 .then((_) { | |
| 58 session = null; | |
| 59 }) | |
| 60 .catchError(exceptionHandler); | |
| 61 }); | |
| 62 | |
| 63 test('Create and get multiple sessions', () { | |
| 64 var session2 = null; | |
| 65 return web_driver.newSession('chrome') | |
| 66 .then((_session) { | |
| 67 session = _session; | |
| 68 return web_driver.newSession('chrome'); | |
| 69 }) | |
| 70 .then((_session) { | |
| 71 session2 = _session; | |
| 72 return web_driver.getSessions(); | |
| 73 }) | |
| 74 .then((sessions) { | |
| 75 expect(sessions.length, greaterThanOrEqualTo(2)); | |
| 76 return session2.close(); | |
| 77 }) | |
| 78 .then((_) { | |
| 79 return session.close(); | |
| 80 }) | |
| 81 .then((_) { | |
| 82 session = null; | |
| 83 }) | |
| 84 .catchError(exceptionHandler); | |
| 85 }); | |
| 86 | |
| 87 test('Set/get url', () { | |
| 88 return web_driver.newSession('chrome') | |
| 89 .then((_session) { | |
| 90 session = _session; | |
| 91 return session.setUrl('http://translate.google.com'); | |
| 92 }) | |
| 93 .then((_) { | |
| 94 return session.getUrl(); | |
| 95 }) | |
| 96 .then((u) { | |
| 97 expect(u, equals('http://translate.google.com/')); | |
| 98 return session.close(); | |
| 99 }) | |
| 100 .then((_) { | |
| 101 session = null; | |
| 102 }) | |
| 103 .catchError(exceptionHandler); | |
| 104 }); | |
| 105 | |
| 106 test('Navigation', () { | |
| 107 return web_driver.newSession('chrome') | |
| 108 .then((_session) { | |
| 109 session = _session; | |
| 110 return session.setUrl('http://translate.google.com'); | |
| 111 }) | |
| 112 .then((_) { | |
| 113 return session.setUrl('http://www.google.com'); | |
| 114 }) | |
| 115 .then((_) { | |
| 116 return session.navigateBack(); | |
| 117 }) | |
| 118 .then((_) { | |
| 119 return session.getUrl(); | |
| 120 }) | |
| 121 .then((u) { | |
| 122 expect(u, equals('http://translate.google.com/')); | |
| 123 return session.navigateForward(); | |
| 124 }) | |
| 125 .then((_) { | |
| 126 return session.getUrl(); | |
| 127 }) | |
| 128 .then((url) { | |
| 129 expect(url, equals('http://www.google.com/')); | |
| 130 return session.close(); | |
| 131 }) | |
| 132 .then((_) { | |
| 133 session = null; | |
| 134 }) | |
| 135 .catchError(exceptionHandler); | |
| 136 }); | |
| 137 | |
| 138 test('Take a Screen shot', () { | |
| 139 return web_driver.newSession('chrome') | |
| 140 .then((_session) { | |
| 141 session = _session; | |
| 142 return session.setUrl('http://translate.google.com'); | |
| 143 }) | |
| 144 .then((_) { | |
| 145 return session.getScreenshot(); | |
| 146 }) | |
| 147 .then((image) { | |
| 148 expect(image.length, greaterThan(10000)); | |
| 149 return session.close(); | |
| 150 }) | |
| 151 .then((_) { | |
| 152 session = null; | |
| 153 }) | |
| 154 .catchError(exceptionHandler); | |
| 155 }); | |
| 156 }); | |
| 157 | |
| 158 group('Window tests', () { | |
| 159 test('Window position and size', () { | |
| 160 // We don't validate the results. Setting size and position is flaky. | |
| 161 // I tried with the Selenium python client code and found it had the | |
| 162 // same issue, so this is not a bug in the Dart code. | |
| 163 var window; | |
| 164 return web_driver.newSession('chrome') | |
| 165 .then((_session) { | |
| 166 session = _session; | |
| 167 return session.getWindow(); | |
| 168 }) | |
| 169 .then((_window) { | |
| 170 window = _window; | |
| 171 return window.setSize(220, 400); | |
| 172 }) | |
| 173 .then((_) { | |
| 174 return window.getSize(); | |
| 175 }) | |
| 176 .then((ws) { | |
| 177 return window.setPosition(100, 80); | |
| 178 }) | |
| 179 .then((_) { | |
| 180 return window.getPosition(); | |
| 181 }) | |
| 182 .then((wp) { | |
| 183 return session.close(); | |
| 184 }) | |
| 185 .then((_) { | |
| 186 session = null; | |
| 187 }) | |
| 188 .catchError(exceptionHandler); | |
| 189 }); | |
| 190 }); | |
| 191 | |
| 192 group('Cookie tests', () { | |
| 193 test('Create/query/delete', () { | |
| 194 var window; | |
| 195 var numcookies; | |
| 196 return web_driver.newSession('chrome') | |
| 197 .then((_session) { | |
| 198 session = _session; | |
| 199 // Must go to a web page first to set a cookie. | |
| 200 return session.setUrl('http://translate.google.com'); | |
| 201 }) | |
| 202 .then((_) { | |
| 203 return session.getCookies(); | |
| 204 }) | |
| 205 .then((cookies) { | |
| 206 numcookies = cookies.length; | |
| 207 return session.setCookie({ 'name': 'foo', 'value': 'bar'}); | |
| 208 }) | |
| 209 .then((_) { | |
| 210 return session.getCookies(); | |
| 211 }) | |
| 212 .then((cookies) { | |
| 213 expect(cookies.length, equals(numcookies + 1)); | |
| 214 expect(cookies, someElement(allOf( | |
| 215 containsPair('name', 'foo'), | |
| 216 containsPair('value', 'bar')))); | |
| 217 return session.deleteCookie('foo'); | |
| 218 }) | |
| 219 .then((_) { | |
| 220 return session.getCookies(); | |
| 221 }) | |
| 222 .then((cookies) { | |
| 223 expect(cookies.length, numcookies); | |
| 224 expect(cookies, everyElement(isNot(containsPair('name', 'foo')))); | |
| 225 return session.deleteCookie('foo'); | |
| 226 }) | |
| 227 .then((_) { | |
| 228 return session.getCookies(); | |
| 229 }) | |
| 230 .then((cookies) { | |
| 231 expect(cookies.length, numcookies); | |
| 232 return session.close(); | |
| 233 }) | |
| 234 .then((_) { | |
| 235 session = null; | |
| 236 }) | |
| 237 .catchError(exceptionHandler); | |
| 238 }); | |
| 239 }); | |
| 240 | |
| 241 group('Storage tests', () { | |
| 242 test('Local Storage Create/query/delete', () { | |
| 243 var window; | |
| 244 var numkeys; | |
| 245 return web_driver.newSession('firefox', { 'webStorageEnabled': true }) | |
| 246 .then((_session) { | |
| 247 session = _session; | |
| 248 return session.getCapabilities(); | |
| 249 }) | |
| 250 .then((capabilities) { | |
| 251 expect(capabilities, isNotNull); | |
| 252 expect(capabilities['webStorageEnabled'], isTrue); | |
| 253 // Must go to a web page first. | |
| 254 return session.setUrl('http://translate.google.com'); | |
| 255 }) | |
| 256 .then((_) { | |
| 257 return session.getLocalStorageKeys(); | |
| 258 }) | |
| 259 .then((keys) { | |
| 260 numkeys = keys.length; | |
| 261 return session.setLocalStorageItem('foo', 'bar'); | |
| 262 }) | |
| 263 .then((_) { | |
| 264 return session.getLocalStorageKeys(); | |
| 265 }) | |
| 266 .then((keys) { | |
| 267 expect(keys.length, equals(numkeys + 1)); | |
| 268 expect(keys, someElement(equals('foo'))); | |
| 269 return session.getLocalStorageCount(); | |
| 270 }) | |
| 271 .then((count) { | |
| 272 expect(count, equals(numkeys + 1)); | |
| 273 return session.getLocalStorageValue('foo'); | |
| 274 }) | |
| 275 .then((value) { | |
| 276 expect(value, equals('bar')); | |
| 277 return session.setLocalStorageItem('bar', 'foo'); | |
| 278 }) | |
| 279 .then((_) { | |
| 280 return session.getLocalStorageKeys(); | |
| 281 }) | |
| 282 .then((keys) { | |
| 283 expect(keys.length, equals(numkeys + 2)); | |
| 284 expect(keys, someElement(equals('bar'))); | |
| 285 expect(keys, someElement(equals('foo'))); | |
| 286 return session.deleteLocalStorageValue('foo'); | |
| 287 }) | |
| 288 .then((_) { | |
| 289 return session.getLocalStorageKeys(); | |
| 290 }) | |
| 291 .then((keys) { | |
| 292 expect(keys.length, equals(numkeys + 1)); | |
| 293 expect(keys, everyElement(isNot(equals('foo')))); | |
| 294 return session.setLocalStorageItem('foo', 'bar'); | |
| 295 }) | |
| 296 .then((_) { | |
| 297 return session.clearLocalStorage(); | |
| 298 }) | |
| 299 .then((_) { | |
| 300 return session.getLocalStorageKeys(); | |
| 301 }) | |
| 302 .then((keys) { | |
| 303 expect(keys.length, isZero); | |
| 304 return session.close(); | |
| 305 }) | |
| 306 .then((_) { | |
| 307 session = null; | |
| 308 }) | |
| 309 .catchError(exceptionHandler); | |
| 310 }); | |
| 311 | |
| 312 test('Session Storage Create/query/delete', () { | |
| 313 var window; | |
| 314 var numkeys; | |
| 315 return web_driver.newSession('chrome', { 'webStorageEnabled': true }) | |
| 316 .then((_session) { | |
| 317 session = _session; | |
| 318 // Must go to a web page first. | |
| 319 return session.setUrl('http://translate.google.com'); | |
| 320 }) | |
| 321 .then((_) { | |
| 322 return session.getSessionStorageKeys(); | |
| 323 }) | |
| 324 .then((keys) { | |
| 325 numkeys = keys.length; | |
| 326 return session.setSessionStorageItem('foo', 'bar'); | |
| 327 }) | |
| 328 .then((_) { | |
| 329 return session.getSessionStorageKeys(); | |
| 330 }) | |
| 331 .then((keys) { | |
| 332 expect(keys.length, equals(numkeys + 1)); | |
| 333 expect(keys, someElement(equals('foo'))); | |
| 334 return session.getSessionStorageCount(); | |
| 335 }) | |
| 336 .then((count) { | |
| 337 expect(count, equals(numkeys + 1)); | |
| 338 return session.getSessionStorageValue('foo'); | |
| 339 }) | |
| 340 .then((value) { | |
| 341 expect(value, equals('bar')); | |
| 342 return session.deleteSessionStorageValue('foo'); | |
| 343 }) | |
| 344 .then((_) { | |
| 345 return session.getSessionStorageKeys(); | |
| 346 }) | |
| 347 .then((keys) { | |
| 348 expect(keys.length, equals(numkeys)); | |
| 349 expect(keys, everyElement(isNot(equals('foo')))); | |
| 350 return session.setSessionStorageItem('foo', 'bar'); | |
| 351 }) | |
| 352 .then((_) { | |
| 353 return session.clearSessionStorage(); | |
| 354 }) | |
| 355 .then((_) { | |
| 356 return session.getSessionStorageKeys(); | |
| 357 }) | |
| 358 .then((keys) { | |
| 359 expect(keys.length, isZero); | |
| 360 return session.close(); | |
| 361 }) | |
| 362 .then((_) { | |
| 363 session = null; | |
| 364 }) | |
| 365 .catchError(exceptionHandler); | |
| 366 }); | |
| 367 | |
| 368 }); | |
| 369 | |
| 370 group('Script tests', () { | |
| 371 // This test is just timing out eventually with a 500 response. | |
| 372 | |
| 373 test('Sync script', () { | |
| 374 return web_driver.newSession('chrome') | |
| 375 .then((_session) { | |
| 376 session = _session; | |
| 377 return session.setUrl('http://translate.google.com'); | |
| 378 }) | |
| 379 .then((_) { | |
| 380 return session.execute('return arguments[0] * arguments[1];', | |
| 381 [2, 3]); | |
| 382 }) | |
| 383 .then((value) { | |
| 384 expect(value, equals(6)); | |
| 385 return session.close(); | |
| 386 }) | |
| 387 .then((_) { | |
| 388 session = null; | |
| 389 }) | |
| 390 .catchError(exceptionHandler); | |
| 391 }); | |
| 392 }); | |
| 393 | |
| 394 group('Element tests', () { | |
| 395 test('Elements', () { | |
| 396 var w; | |
| 397 return web_driver.newSession('chrome') | |
| 398 .then((_session) { | |
| 399 session = _session; | |
| 400 return session.setUrl('http://duckduckgo.com'); | |
| 401 }) | |
| 402 .then((_) { | |
| 403 return session.findElement('id', 'search_form_input_homepage'); | |
| 404 }) | |
| 405 .then((_w) { | |
| 406 w = _w['ELEMENT']; | |
| 407 return session.sendKeyStrokesToElement(w, | |
| 408 [ 'g', 'o', 'o', 'g', 'l', 'e' ]); | |
| 409 }) | |
| 410 .then((_) { | |
| 411 return session.submit(w); | |
| 412 }) | |
| 413 .then((_) { | |
| 414 return session.findElements('class name', | |
| 415 'links_zero_click_disambig'); | |
| 416 }) | |
| 417 .then((divs) { | |
| 418 expect(divs.length, greaterThan(0)); | |
| 419 return session.close(); | |
| 420 }) | |
| 421 .then((_) { | |
| 422 session = null; | |
| 423 }) | |
| 424 .catchError(exceptionHandler); | |
| 425 }); | |
| 426 }); | |
| 427 | |
| 428 group('Log tests', () { | |
| 429 // Currently, getLogTypes returns a 404, and attempts to get the | |
| 430 // logs either return 500 with 'Unknown log type' or 500 with | |
| 431 // 'Unable to convert logEntries'. | |
| 432 | |
| 433 test('Log retrieval', () { | |
| 434 return web_driver.newSession('firefox') | |
| 435 .then((_session) { | |
| 436 session = _session; | |
| 437 return session.getLogTypes(); | |
| 438 }) | |
| 439 .then((logTypes) { | |
| 440 expect(logTypes, someElement(equals('client'))); | |
| 441 expect(logTypes, someElement(equals('server'))); | |
| 442 return session.getLogs('driver'); | |
| 443 }) | |
| 444 .then((logs) { | |
| 445 expect(logs.length, greaterThan(0)); | |
| 446 return session.close(); | |
| 447 }) | |
| 448 .then((_) { | |
| 449 session = null; | |
| 450 }) | |
| 451 .catchError(exceptionHandler); | |
| 452 }); | |
| 453 }); | |
| 454 | |
| 455 group('Cleanup', () { | |
| 456 test('Cleanup', () { | |
| 457 web_driver = null; | |
| 458 if (session != null) { | |
| 459 var s = session; | |
| 460 session = null; | |
| 461 s.close(); | |
| 462 } | |
| 463 }); | |
| 464 }); | |
| 465 } | |
| 466 | |
| OLD | NEW |