Commits

Bob Swift committed db7cd59

GINT-46: Some selenium improvements for screenshots.

  • Participants
  • Parent commits 25e1273

Comments (0)

Files changed (2)

File src/main/groovy/org/swift/tools/Helper.groovy

      * @return Map of attributes to values
      */
     static public Map<String, String> getWebElementAttributes(final element) {
-        return [
-            type: element.getAttribute('type') ?: '',
-            id: element.getAttribute('id') ?: '',
-            name: element.getAttribute('name') ?: '',
-            class: element.getAttribute('class') ?: '',
-            style: element.getAttribute('style') ?: '',
-            value: element.getAttribute('value') ?: '',
-            text: element.getText() ?: '',
-            selected: element.getAttribute('selected') ? true : false,
-            disabled: element.getAttribute('disabled') ? true : false,
-        ]
+        try {
+            return [
+                type: element.getAttribute('type') ?: '',
+                id: element.getAttribute('id') ?: '',
+                name: element.getAttribute('name') ?: '',
+                class: element.getAttribute('class') ?: '',
+                style: element.getAttribute('style') ?: '',
+                value: element.getAttribute('value') ?: '',
+                text: element.getText() ?: '',
+                selected: element.getAttribute('selected') ? true : false,
+                disabled: element.getAttribute('disabled') ? true : false,
+            ]
+        } catch (Exception exception) {
+            if (isVerbose()) {
+                message 'error', 'Ignore error accessing web element. Error: ' + exception
+            }
+            return [:]
+        }
     }
 
     /**

File src/main/groovy/org/swift/tools/SeleniumHelper.groovy

                     exception.printStackTrace()
                 }
             },
+            finalClosure: { testcase ->
+                if (testcase.success == false) {
+                    screenshot(name: testcase.name + '_failed')
+                }
+            }
         ]
     }
 
      * png will be appended to a name that does not have an extension
      * Parameters
      * - driver optional
-     * - name of file
-     * Example: finalClosure: { testcase -> screenshot(saved.driver, testcase.name)},
+     * - name - used to generate file name
+     * Example: finalClosure: { testcase -> screenshot(name: testcase.name)},
      */
-    public def screenshot(final Map parameters = null) {
-        if ((parameters == null) || (parameters?.screenshot != null) && (parameters?.screenshot != false)) {
+    public def screenshot(final Map parameters) {
+        if (parameters?.screenshot != false) {
             def myDriver = helper.getValueHandleClosure(parameters?.driver ?: driver ?: parameters?.base)
             assert myDriver // driver is null for screenshot
-            def name = parameters?.screenshot == true ? parameters?.name : parameters?.screenshot
+            def name = parameters?.screenshot == true ? parameters?.name : (parameters?.screenshot ?: parameters?.name)
             name = name ?: 'screenshot'  // default name
             def postfix = helper.getExtension(name) == '' ? '.png' : '' //
             def toFile = binding.gint?.getOutputFile(name, postfix) ?: name + postfix
             toFile = generateNextFileName(toFile)
             binding.ant.copy(file: ((TakesScreenshot) myDriver).getScreenshotAs(OutputType.FILE), tofile: toFile)
+            message 'info', "Screenshot: ${toFile}"
         }
     }