Browse Source

Refactor acceptance tests, move boilerplate to a standalone `core` module, add simple dragdrop test

Artur Paikin 9 years ago
parent
commit
644d6b6759
4 changed files with 101 additions and 78 deletions
  1. 43 0
      test/acceptance/core.js
  2. 34 0
      test/acceptance/dragdrop.spec.js
  3. 16 78
      test/acceptance/i18n.spec.js
  4. 8 0
      test/acceptance/index.js

+ 43 - 0
test/acceptance/core.js

@@ -0,0 +1,43 @@
+var webdriver = require('selenium-webdriver')
+var firefox = require('selenium-webdriver/firefox')
+var By = webdriver.By
+var path = require('path')
+var chalk = require('chalk')
+
+// Monitor for errors, and dump them
+function monitorErrors (driver) {
+  var promise = driver.executeScript('return window.JSErrorCollector_errors.pump()')
+  promise.then(function (errors) {
+    if (!errors || !errors.length) {
+      return
+    }
+    errors.forEach(function (error) {
+      console.error([
+        '[browser-error]',
+        chalk.magenta(error.sourceName),
+        chalk.dim('#' + error.lineNumber),
+        chalk.red(error.errorMessage)
+      ].join(' '))
+    })
+  })
+}
+
+var profile = new firefox.Profile()
+profile.addExtension(path.join(__dirname, 'xpi', 'firebug-2.0.16.xpi'))
+profile.addExtension(path.join(__dirname, 'xpi', 'JSErrorCollector.xpi'))
+profile.setPreference('extensions.firebug.showChromeErrors', true)
+
+var options = new firefox.Options().setProfile(profile)
+var driver = new firefox.Driver(options)
+
+monitorErrors(driver)
+
+var consoleElement = driver.findElement(By.id('console-log'))
+var hexoServer = 'http://localhost:4000'
+
+module.exports = {
+  driver,
+  consoleElement,
+  By,
+  hexoServer
+}

+ 34 - 0
test/acceptance/dragdrop.spec.js

@@ -0,0 +1,34 @@
+var test = require('tape')
+var path = require('path')
+var core = require('./core')
+
+var config = {
+  dragDropTestUrl: 'http://localhost:4000/examples/dragdrop/',
+  dragDropInputSelector: '.UppyDragDrop-One .UppyDragDrop-input',
+  imageAbsolutePath: path.join(__dirname, 'image.jpg')
+}
+
+test('dragdrop: make sure DragDrop accepts and uploads 1 file via input', function (t) {
+  t.plan(1)
+
+  // Go to the example URL
+  core.driver.get(config.dragDropTestUrl)
+
+  // Find input by css selector
+  var input = core.driver.findElement(core.By.css(config.dragDropInputSelector))
+
+  // Pass absolute image path to the input
+  input.sendKeys(config.imageAbsolutePath)
+
+  // Wait for a while for upload to go through
+  core.driver.sleep(3000)
+
+  // Get console elements’s value, then check if it has “Download” there somewhere,
+  // if it does, then test passes
+  core.consoleElement.getAttribute('value').then(function (value) {
+    var isFileUploaded = value.indexOf('Download') !== -1
+    t.equal(isFileUploaded, true)
+  })
+
+  core.driver.quit()
+})

+ 16 - 78
test/acceptance/i18n.spec.js

@@ -1,89 +1,27 @@
-// Docs aren't that great to find. Mostly JAVA based. Here are few helpful resources:
-// - https://github.com/SeleniumHQ/selenium/wiki/WebDriverJs
-// - http://seleniumhq.github.io/selenium/docs/api/javascript/
-// - http://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/firefox/index.html
-// - http://selenium.googlecode.com/git/docs/api/javascript/namespace_webdriver_By.html
-// - http://selenium.googlecode.com/git/docs/api/javascript/class_webdriver_WebElement.html
-
 var test = require('tape')
 var path = require('path')
-var chalk = require('chalk')
-var webdriver = require('selenium-webdriver')
-var firefox = require('selenium-webdriver/firefox')
-var By = webdriver.By
-
-var profile = new firefox.Profile()
-profile.addExtension(path.join(__dirname, 'xpi', 'firebug-2.0.16.xpi'))
-profile.addExtension(path.join(__dirname, 'xpi', 'JSErrorCollector.xpi'))
-profile.setPreference('extensions.firebug.showChromeErrors', true)
-
-var options = new firefox.Options().setProfile(profile)
-var driver = new firefox.Driver(options)
+var core = require('./core')
 
-// A remote driver could look something like this:
-// var driver = new (require('selenium-webdriver')).Builder()
-//   .forBrowser('firefox')
-//   .usingServer('http://127.0.0.1:4444/wd/hub')
-//   .setFirefoxOptions(options)
-//   .build()
-
-var hexoServer = 'http://localhost:4000'
+var config = {
+  i18nTestUrl: 'http://localhost:4000/examples/i18n',
+  dragDropLabelSelector: '.UppyDragDrop-label',
+  imageAbsolutePath: path.join(__dirname, 'image.jpg')
+}
 
 test('make sure Uppy loads with Russian language pack', function (t) {
-  driver.get(hexoServer + '/examples/i18n/')
-
-  // Monitor for errors, and dump them
-  const promise = driver.executeScript('return window.JSErrorCollector_errors.pump()')
-  promise.then(function (errors) {
-    if (!errors || !errors.length) {
-      return
-    }
-    errors.forEach(function (error) {
-      console.error([
-        '[browser-error]',
-        chalk.magenta(error.sourceName),
-        chalk.dim('#' + error.lineNumber),
-        chalk.red(error.errorMessage)
-      ].join(' '))
-    })
-  })
-
-  // Our success/fail message will be logged in the console element
-  var consoleElement = driver.findElement(By.id('console-log'))
+  core.driver.get(config.i18nTestUrl)
 
-  // Wait 8 seconds, for our upload message to be logged
-  driver.wait(isLoaded.bind(this, consoleElement), 8000)
+  // Wait 3 seconds, for the page to load
+  core.driver.sleep(3000)
 
-  driver.findElement(By.css('.UppyDragDrop-label')).getText().then(function (val) {
-    console.dir({val: val})
-    t.equal(val, 'Выберите файл или перенесите его сюда')
-  })
+  core.driver.findElement(core.By.css(config.dragDropLabelSelector))
+    .getText()
+    .then(function (val) {
+      console.dir({val: val})
+      t.equal(val, 'Выберите файл или перенесите его сюда')
+    })
 
-  driver.quit()
+  core.driver.quit()
 
   t.end()
-
-  /**
-   * Check if uploading is finished by looking for a result message
-   * in the example's console output element.
-   * @return {Boolean} If uploading is complete
-   */
-  function isLoaded (consoleElement) {
-    return getElementValue(consoleElement)
-      .then(function (value) {
-        return value.indexOf('-->') !== -1
-      })
-  }
-
-  /**
-   * Get value attribute of element
-   * @param  {webdriver.WebElement} element Selenium element object
-   * @return {webdriver.promise} Promise resolving to element value
-   */
-  function getElementValue (element) {
-    return element.getAttribute('value')
-      .then(function (value) {
-        return value
-      })
-  }
 })

+ 8 - 0
test/acceptance/index.js

@@ -1,2 +1,10 @@
+// Docs aren't that great to find. Mostly JAVA based. Here are few helpful resources:
+// - https://github.com/SeleniumHQ/selenium/wiki/WebDriverJs
+// - http://seleniumhq.github.io/selenium/docs/api/javascript/
+// - http://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/firefox/index.html
+// - http://selenium.googlecode.com/git/docs/api/javascript/namespace_webdriver_By.html
+// - http://selenium.googlecode.com/git/docs/api/javascript/class_webdriver_WebElement.html
+
 require('babel-register')
 require('./i18n.spec.js')
+require('./dragdrop.spec.js')