multipart.spec.js 1.8 KB

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