How to use driver.saveScreenshot method in Appium

Best JavaScript code snippet using appium

Run Appium automation tests on LambdaTest cloud grid

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

screenshots.js

Source: screenshots.js Github

copy
1module.exports = function screenshots(webdriver, driver, shoot) {
2	return [
3		shoot('screenshots/pages/asides.html', function(done) {
4			setTimeout(function waitforaside() {
5				driver.saveScreenshot('asides.png').then(done);
6			}, 2000);
7		}),
8
9		shoot('screenshots/pages/bars.html', function(done) {
10			driver.saveScreenshot('bars.png').then(done);
11		}),
12
13		shoot('screenshots/pages/buttons.html', function(done) {
14			driver.saveScreenshot('buttons.png').then(done);
15		}),
16
17		shoot('screenshots/pages/forms.html', function(done) {
18			driver.saveScreenshot('forms.png').then(done);
19		}),
20
21		shoot('screenshots/pages/panels.html', function(done) {
22			setTimeout(function waitforaside() {
23				driver.saveScreenshot('panels.png').then(done);
24			}, 2000);
25		})
26	];
27};
28
Full Screen

sn-totalhip.js

Source: sn-totalhip.js Github

copy
1var fs = require('fs');
2var webdriver = require('browserstack-webdriver');
3
4var capabilities = {
5
6	'project': 'Schedule Nurse',
7	'build': 'Joint - Total Hip version(20150713)',
8  'browserName': 'IE',
9  'browser_version' : '10.0',
10  'os' : 'Windows',
11  'os_version' : '8',
12  'browserstack.user' : 'bsUser3210',
13  'browserstack.key' : 'jr8VGaiPmN544yvEJurp',
14  'browserstack.debug': true // for visual logs
15}
16
17var driver = new webdriver.Builder().
18  usingServer('http://hub.browserstack.com/wd/hub').
19  withCapabilities(capabilities).
20  build();
21
22  var testdata = {
23    url: 'https://dev.invivolink.com/schedule-nurse/#/',
24    screenshots: 'screenshots/schedulenurse/totalhip/',
25  	user: {
26  		name: 'sashley',
27  		pass: 'devpass1',
28  		ssn: '415253632',
29  		dob: '10/15/1960',
30  		firstname: 'Bob',
31  		lastname: 'Hope3632',
32  		feet: 6,
33  		inches: 1,
34  		weight: 225,
35  		proceduredate: '07/25/2015'
36  	}
37  };
38
39webdriver.WebDriver.prototype.saveScreenshot = function(filename) {
40  return driver.takeScreenshot().then(function(data) {
41      fs.writeFile(filename, data.replace(/^data:image\/png;base64,/,''), 'base64', function(err) {
42          if(err) throw err;
43      });
44  })
45};
46
47/*** BEGIN TEST SUITE ***/
48console.log("this is a selenium automation test for " + capabilities.project + "[" + capabilities.build +  "] \n >>> Go to https://https://www.browserstack.com/automate to see logs...")
49
50driver.saveScreenshot(testdata.screenshots + '1_login.png');
51
52// #1 Login
53driver.get(testdata.url);
54driver.findElement(webdriver.By.name('username')).sendKeys(testdata.user.name);
55driver.findElement(webdriver.By.name('password')).sendKeys(testdata.user.pass);
56driver.findElement(webdriver.By.xpath("//button[@type='submit']")).click();
57
58driver.sleep(5000);
59driver.saveScreenshot(testdata.screenshots + '2_home.png');
60
61// #3 Schecdule Hip Surgery
62driver.findElement(webdriver.By.xpath('//*[@id="ng-app"]/body/div[2]/div[2]/div/table/thead/tr[1]/th[2]/div[1]/span[2]/a/img')).click();
63driver.saveScreenshot(testdata.screenshots + '3_grid.png');
64
65driver.sleep(5000);
66
67// #4 Patient Form
68driver.findElement(webdriver.By.name('ssn')).click();
69driver.findElement(webdriver.By.name('ssn')).sendKeys(testdata.user.ssn);
70driver.findElement(webdriver.By.name('dob')).sendKeys(testdata.user.dob);
71driver.sleep(5000);
72driver.findElement(webdriver.By.name('firstname')).sendKeys(testdata.user.firstname);
73driver.findElement(webdriver.By.name('lastname')).sendKeys(testdata.user.lastname);
74driver.findElement(webdriver.By.xpath('//*[@id="ng-app"]/body/div[2]/div[2]/div/div[2]/form/fieldset/div[1]/div/div[2]/table/tbody/tr[5]/td[2]/div/label[1]/input')).click(); // gener value=M
75driver.findElement(webdriver.By.name('feet')).sendKeys(testdata.user.feet);
76driver.findElement(webdriver.By.name('inches')).sendKeys(testdata.user.inches);
77driver.findElement(webdriver.By.name('weight')).sendKeys(testdata.user.weight);
78
79driver.sleep(3000);
80driver.saveScreenshot(testdata.screenshots + '4_patient.png');
81driver.findElement(webdriver.By.xpath('//*[@id="ng-app"]/body/div[2]/div[2]/div/div[2]/form/fieldset/div[1]/div/div[2]/table/tbody/tr[16]/td[2]/div')).click(); // save patient
82driver.findElement(webdriver.By.xpath('//*[@id="ng-app"]/body/div[2]/div[2]/div/div[2]/form/fieldset/div[3]/div[2]')).click(); // goto procedure
83
84driver.sleep(5000);
85
86// #5 Procedure Form
87driver.findElement(webdriver.By.xpath('//*[@id="ng-app"]/body/div[2]/div[2]/div/div[2]/form/fieldset/div[1]/div[2]/div/div[2]/table/tbody/tr[2]/td[2]/select/option[12]')).click(); // TOTAL HIP
88driver.findElement(webdriver.By.xpath('//*[@id="ng-app"]/body/div[2]/div[2]/div/div[2]/form/fieldset/div[1]/div[2]/div/div[2]/table/tbody/tr[4]/td[2]/select/option[6]')).click(); // Physician17593, Release
89driver.sleep(4000);
90driver.findElement(webdriver.By.xpath('//*[@id="ng-app"]/body/div[2]/div[2]/div/div[2]/form/fieldset/div[1]/div[2]/div/div[2]/table/tbody/tr[6]/td[2]/select/option[3]')).click(); // Hospital 606946
91driver.findElement(webdriver.By.name('procedureSchedule')).sendKeys(testdata.user.proceduredate);
92driver.findElement(webdriver.By.xpath('//*[@id="ng-app"]/body/div[2]/div[2]/div/div[2]/form/fieldset/div[1]/div[3]/div/div[2]/table/tbody/tr[1]/td[2]/select/option[2]')).click(); // Acute Fracture
93driver.findElement(webdriver.By.xpath('//*[@id="ng-app"]/body/div[2]/div[2]/div/div[2]/form/fieldset/div[1]/div[1]/div/div[2]/div[1]/div/label[1]/input')).click(); // Operative Side: Left
94driver.sleep(1000);
95driver.saveScreenshot(testdata.screenshots + '5_procedure.png');
96driver.findElement(webdriver.By.xpath('//*[@id="ng-app"]/body/div[2]/div[2]/div/div[2]/form/fieldset/div[2]/div[2]')).click(); // goto insurance
97
98driver.sleep(5000);
99
100// #6 Insurance Form
101driver.findElement(webdriver.By.xpath('//*[@id="ng-app"]/body/div[2]/div[2]/div/div[2]/form/div[1]/div/div[2]/table/tbody/tr[1]/td[2]/select/option[2]')).click(); // Commercial Payor
102driver.sleep(1000);
103driver.saveScreenshot(testdata.screenshots + '6_insurance.png');
104driver.findElement(webdriver.By.xpath('//*[@id="ng-app"]/body/div[2]/div[2]/div/div[2]/div[2]/div[2]')).click(); // goto implants
105
106driver.sleep(5000);
107
108// #7 Implants Form
109driver.findElement(webdriver.By.xpath('//*[@id="preference"]/option[2]')).click(); // High - Preference A
110driver.sleep(1000);
111driver.saveScreenshot(testdata.screenshots + '7_implants.png');
112
113driver.findElement(webdriver.By.xpath('//*[@id="ng-app"]/body/div[2]/div[2]/div/div[2]/form/div[2]/div[3]/div')).click(); // High - Preference A
114driver.sleep(8000);
115
116// #8 Home / Grid
117driver.saveScreenshot(testdata.screenshots + '8_saved.png');
118
119driver.quit();
120/*** END TEST SUITE ***/
121
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 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)