multipart.spec.js 1.7 KB

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