How to use getScreenshot method in Appium Base Driver

Best JavaScript code snippet using appium-base-driver

Run Appium Base Driver automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

browserElement_BadScreenshot.js

Source: browserElement_BadScreenshot.js Github

copy
1/* Any copyright is dedicated to the public domain.
2   http://creativecommons.org/publicdomain/zero/1.0/ */
3
4// Bug 800170 - Test that we get errors when we pass bad arguments to
5// mozbrowser's getScreenshot.
6"use strict";
7
8SimpleTest.waitForExplicitFinish();
9browserElementTestHelpers.setEnabledPref(true);
10browserElementTestHelpers.addPermission();
11
12var iframe;
13var numPendingTests = 0;
14
15// Call iframe.getScreenshot with the given args.  If expectSuccess is true, we
16// expect the screenshot's onsuccess handler to fire.  Otherwise, we expect
17// getScreenshot() to throw an exception.
18function checkScreenshotResult(expectSuccess, args) {
19  var req;
20  try {
21    req = iframe.getScreenshot.apply(iframe, args);
22  }
23  catch(e) {
24    ok(!expectSuccess, "getScreenshot(" + JSON.stringify(args) + ") threw an exception.");
25    return;
26  }
27
28  numPendingTests++;
29  req.onsuccess = function() {
30    ok(expectSuccess, "getScreenshot(" + JSON.stringify(args) + ") succeeded.");
31    numPendingTests--;
32    if (numPendingTests == 0) {
33      SimpleTest.finish();
34    }
35  };
36
37  // We never expect to see onerror.
38  req.onerror = function() {
39    ok(false, "getScreenshot(" + JSON.stringify(args) + ") ran onerror.");
40    numPendingTests--;
41    if (numPendingTests == 0) {
42      SimpleTest.finish();
43    }
44  };
45}
46
47function runTest() {
48  iframe = document.createElement('iframe');
49  SpecialPowers.wrap(iframe).mozbrowser = true;
50  document.body.appendChild(iframe);
51  iframe.src = 'data:text/html,<html>' +
52    '<body style="background:green">hello</body></html>';
53
54  iframe.addEventListener('mozbrowserfirstpaint', function() {
55    // This one should succeed.
56    checkScreenshotResult(true, [100, 100]);
57
58    // These should fail.
59    checkScreenshotResult(false, []);
60    checkScreenshotResult(false, [100]);
61    checkScreenshotResult(false, ['a', 100]);
62    checkScreenshotResult(false, [100, 'a']);
63    checkScreenshotResult(false, [-1, 100]);
64    checkScreenshotResult(false, [100, -1]);
65
66    if (numPendingTests == 0) {
67      SimpleTest.finish();
68    }
69  });
70}
71
72addEventListener('testready', runTest);
73
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Appium Base Driver on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)