multipart.spec.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. var test = require('tape')
  2. var webdriver = require('selenium-webdriver')
  3. var By = webdriver.By
  4. test('upload two files', function (t) {
  5. var driver = new webdriver.Builder()
  6. .withCapabilities(webdriver.Capabilities.chrome())
  7. .build()
  8. driver.get('http://localhost:4000/examples/multipart/')
  9. driver.findElement(By.id('myfile1')).sendKeys(__dirname + '/image.jpg')
  10. driver.findElement(By.id('myfile2')).sendKeys(__dirname + '/dummy.css')
  11. driver.findElement(By.id('myupload')).click()
  12. // Our success/fail message will be logged in the console element
  13. var consoleElement = driver.findElement(By.id('console-log'))
  14. // Wait for our upload message to be logged
  15. driver.wait(isUploadComplete.bind(this, consoleElement))
  16. // Get the result of our upload and test it
  17. getElementValue(consoleElement)
  18. .then(function (value) {
  19. var result = value.split('\n')[0]
  20. t.equal(result, 'DEBUG LOG: Upload result -> success!')
  21. })
  22. driver.quit()
  23. t.end()
  24. /**
  25. * Check if uploading is finished by looking for a result message
  26. * in the example's console output element.
  27. * @return {Boolean} If uploading is complete
  28. */
  29. function isUploadComplete (consoleElement) {
  30. return getElementValue(consoleElement)
  31. .then(function (value) {
  32. return value.indexOf('DEBUG LOG: Upload result ->') !== -1
  33. })
  34. }
  35. /**
  36. * Get value attribute of element
  37. * @param {webdriver.WebElement} element Selenium element object
  38. * @return {webdriver.promise} Promise resolving to element value
  39. */
  40. function getElementValue (element) {
  41. return element.getAttribute('value')
  42. .then(function (value) {
  43. return value
  44. })
  45. }
  46. })