How to use createReadStream method in Playwright Internal

Best JavaScript code snippet using playwright-internal

Run Playwright Internal automation tests on LambdaTest cloud grid

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

insertImages.js

Source: insertImages.js Github

copy
1const fs = require('fs')
2
3const fetch = require('node-fetch')
4const FormData = require('form-data')
5
6const { API_URL } = require('./constants')
7
8exports.insertImages = async token => {
9    const formData = new FormData()
10
11    formData.append('image', fs.createReadStream(`${__dirname}/images/1.jpg`))
12    formData.append('image', fs.createReadStream(`${__dirname}/images/2.jpg`))
13    formData.append('image', fs.createReadStream(`${__dirname}/images/3.jpg`))
14    formData.append('image', fs.createReadStream(`${__dirname}/images/4.jpg`))
15    formData.append('image', fs.createReadStream(`${__dirname}/images/5.png`))
16    formData.append('image', fs.createReadStream(`${__dirname}/images/6.png`))
17    formData.append('image', fs.createReadStream(`${__dirname}/images/7.jpg`))
18    formData.append('image', fs.createReadStream(`${__dirname}/images/8.jpg`))
19    formData.append('image', fs.createReadStream(`${__dirname}/images/9.png`))
20    formData.append('image', fs.createReadStream(`${__dirname}/images/10.jpg`))
21    formData.append('image', fs.createReadStream(`${__dirname}/images/11.jpg`))
22    formData.append('image', fs.createReadStream(`${__dirname}/images/12.jpg`))
23    formData.append('image', fs.createReadStream(`${__dirname}/images/13.jpg`))
24    formData.append('image', fs.createReadStream(`${__dirname}/images/14.png`))
25    formData.append('image', fs.createReadStream(`${__dirname}/images/15.png`))
26    formData.append('image', fs.createReadStream(`${__dirname}/images/16.png`))
27    formData.append('image', fs.createReadStream(`${__dirname}/images/17.png`))
28    formData.append('image', fs.createReadStream(`${__dirname}/images/18.png`))
29    formData.append('image', fs.createReadStream(`${__dirname}/images/19.png`))
30    formData.append('image', fs.createReadStream(`${__dirname}/images/20.png`))
31    formData.append('image', fs.createReadStream(`${__dirname}/images/21.png`))
32    formData.append('image', fs.createReadStream(`${__dirname}/images/22.png`))
33    formData.append('image', fs.createReadStream(`${__dirname}/images/23.png`))
34    formData.append('image', fs.createReadStream(`${__dirname}/images/24.jpg`))
35    formData.append('image', fs.createReadStream(`${__dirname}/images/25.jpg`))
36    formData.append('image', fs.createReadStream(`${__dirname}/images/26.jpg`))
37    formData.append('image', fs.createReadStream(`${__dirname}/images/27.jpg`))
38    formData.append('image', fs.createReadStream(`${__dirname}/images/28.png`))
39    formData.append('image', fs.createReadStream(`${__dirname}/images/29.png`))
40    formData.append('image', fs.createReadStream(`${__dirname}/images/30.jpg`))
41    formData.append('image', fs.createReadStream(`${__dirname}/images/31.jpg`))
42    formData.append('image', fs.createReadStream(`${__dirname}/images/32.jpg`))
43    formData.append('image', fs.createReadStream(`${__dirname}/images/33.jpg`))
44    formData.append('image', fs.createReadStream(`${__dirname}/images/34.jpg`))
45    formData.append('image', fs.createReadStream(`${__dirname}/images/35.jpg`))
46    formData.append('image', fs.createReadStream(`${__dirname}/images/36.jpg`))
47    formData.append('image', fs.createReadStream(`${__dirname}/images/37.jpg`))
48    formData.append('image', fs.createReadStream(`${__dirname}/images/38.jpg`))
49
50    const response = await fetch(`${API_URL}/image`, {
51        method: 'POST',
52        headers: {
53            Authorization: `Bearer ${token}`,
54        },
55        body: formData,
56    })
57
58    console.log(response)
59
60    const data = await response.json()
61
62    console.log(data)
63}
64
Full Screen

uploads.js

Source: uploads.js Github

copy
1
2import fs from 'fs';
3
4function setup() {
5  console.log("Creating uploads directory\n");
6  if (!fs.existsSync('uploads/')){
7    fs.mkdirSync('uploads/');
8  }
9
10  console.log("Copying logo file for tenants\n");
11
12  // Tenant1
13  fs.createReadStream('tmp_uploads/tenant1.png')
14    .pipe(fs.createWriteStream('uploads/tenant1.png'));
15  fs.createReadStream('tmp_uploads/tenant1.jpg')
16    .pipe(fs.createWriteStream('uploads/tenant1.jpg'));
17  fs.createReadStream('tmp_uploads/tenant1_da1.jpg')
18    .pipe(fs.createWriteStream('uploads/tenant1_da1.jpg'));
19
20  // Tenant2
21  fs.createReadStream('tmp_uploads/tenant2.png')
22    .pipe(fs.createWriteStream('uploads/tenant2.png'));
23  fs.createReadStream('tmp_uploads/tenant2.jpg')
24    .pipe(fs.createWriteStream('uploads/tenant2.jpg'));
25  fs.createReadStream('tmp_uploads/tenant2_ma1.jpg')
26    .pipe(fs.createWriteStream('uploads/tenant2_ma1.jpg'));
27
28  fs.createReadStream('tmp_uploads/staples.png')
29    .pipe(fs.createWriteStream('uploads/staples.png'));
30  fs.createReadStream('tmp_uploads/keurig.png')
31    .pipe(fs.createWriteStream('uploads/keurig.png'));
32
33  fs.createReadStream('tmp_uploads/rajeev.png')
34    .pipe(fs.createWriteStream('uploads/rajeev.png'));
35  fs.createReadStream('tmp_uploads/vinod.png')
36    .pipe(fs.createWriteStream('uploads/vinod.png'));
37
38  fs.createReadStream('tmp_uploads/staples_ma.jpg')
39    .pipe(fs.createWriteStream('uploads/staples_ma.jpg'));
40  fs.createReadStream('tmp_uploads/staples_da.jpg')
41    .pipe(fs.createWriteStream('uploads/staples_da.jpg'));
42
43  fs.createReadStream('tmp_uploads/keurig_ma.jpg')
44    .pipe(fs.createWriteStream('uploads/keurig_ma.jpg'));
45  fs.createReadStream('tmp_uploads/keurig_da.jpg')
46    .pipe(fs.createWriteStream('uploads/keurig_da.jpg'));
47
48  fs.createReadStream('tmp_uploads/venn.jpg')
49    .pipe(fs.createWriteStream('uploads/venn.jpg'));
50  fs.createReadStream('tmp_uploads/bubble1.png')
51    .pipe(fs.createWriteStream('uploads/bubble1.png'));
52  fs.createReadStream('tmp_uploads/bubble2.png')
53    .pipe(fs.createWriteStream('uploads/bubble2.png'));
54  fs.createReadStream('tmp_uploads/empty_nesters.jpeg')
55    .pipe(fs.createWriteStream('uploads/empty_nesters.jpeg'));
56  fs.createReadStream('tmp_uploads/momnbaby.jpeg')
57    .pipe(fs.createWriteStream('uploads/momnbaby.jpeg'));
58  fs.createReadStream('tmp_uploads/young_singles.jpeg')
59    .pipe(fs.createWriteStream('uploads/young_singles.jpeg'));
60  fs.createReadStream('tmp_uploads/young_couples.jpeg')
61    .pipe(fs.createWriteStream('uploads/young_couples.jpeg'));
62
63  fs.createReadStream('tmp_uploads/rfv_leastval.jpg')
64    .pipe(fs.createWriteStream('uploads/rfv_leastval.jpg'));
65  fs.createReadStream('tmp_uploads/rfv_mostval.jpg')
66    .pipe(fs.createWriteStream('uploads/rfv_mostval.jpg'));
67
68  fs.createReadStream('tmp_uploads/scheme_lifestage_small.jpg')
69    .pipe(fs.createWriteStream('uploads/scheme_lifestage_small.jpg'));
70   fs.createReadStream('tmp_uploads/scheme_potential_small.jpg')
71    .pipe(fs.createWriteStream('uploads/scheme_potential_small.jpg'));
72  fs.createReadStream('tmp_uploads/scheme_rfv_small.png')
73    .pipe(fs.createWriteStream('uploads/scheme_rfv_small.png'));
74  fs.createReadStream('tmp_uploads/scheme_missions_small.png')
75    .pipe(fs.createWriteStream('uploads/scheme_missions_small.png'));
76  fs.createReadStream('tmp_uploads/scheme_lifestyle_small.jpg')
77    .pipe(fs.createWriteStream('uploads/scheme_lifestyle_small.jpg'));
78  fs.createReadStream('tmp_uploads/mombaby.jpg')
79    .pipe(fs.createWriteStream('uploads/mombaby.jpg'));
80  fs.createReadStream('tmp_uploads/enthusiats.jpg')
81    .pipe(fs.createWriteStream('uploads/enthusiats.jpg'));
82  fs.createReadStream('tmp_uploads/vip.jpg')
83    .pipe(fs.createWriteStream('uploads/vip.jpg'));
84  fs.createReadStream('tmp_uploads/matures.jpg')
85    .pipe(fs.createWriteStream('uploads/matures.jpg'));
86
87  fs.createReadStream('tmp_uploads/dashboard_young_single.png')
88    .pipe(fs.createWriteStream('uploads/dashboard_young_single.png'));
89  fs.createReadStream('tmp_uploads/dashboard_young_couples.png')
90    .pipe(fs.createWriteStream('uploads/dashboard_young_couples.png'));
91  fs.createReadStream('tmp_uploads/dashboard_vip.png')
92    .pipe(fs.createWriteStream('uploads/dashboard_vip.png'));
93  fs.createReadStream('tmp_uploads/dashboard_value_for_money.png')
94    .pipe(fs.createWriteStream('uploads/dashboard_value_for_money.png'));
95  fs.createReadStream('tmp_uploads/dashboard_technology_savvy.png')
96    .pipe(fs.createWriteStream('uploads/dashboard_technology_savvy.png'));
97  fs.createReadStream('tmp_uploads/dashboard_on_the_go.png')
98    .pipe(fs.createWriteStream('uploads/dashboard_on_the_go.png'));
99  fs.createReadStream('tmp_uploads/dashboard_mom_baby.png')
100    .pipe(fs.createWriteStream('uploads/dashboard_mom_baby.png'));
101  fs.createReadStream('tmp_uploads/dashboard_matures.png')
102    .pipe(fs.createWriteStream('uploads/dashboard_matures.png'));
103  fs.createReadStream('tmp_uploads/dashboard_lunch_time.png')
104    .pipe(fs.createWriteStream('uploads/dashboard_lunch_time.png'));
105  fs.createReadStream('tmp_uploads/dashboard_low.png')
106    .pipe(fs.createWriteStream('uploads/dashboard_low.png'));
107  fs.createReadStream('tmp_uploads/dashboard_least_eng.png')
108    .pipe(fs.createWriteStream('uploads/dashboard_least_eng.png'));
109  fs.createReadStream('tmp_uploads/dashboard_high.png')
110    .pipe(fs.createWriteStream('uploads/dashboard_high.png'));
111  fs.createReadStream('tmp_uploads/dashboard_explorers.png')
112    .pipe(fs.createWriteStream('uploads/dashboard_explorers.png'));
113  fs.createReadStream('tmp_uploads/dashboard_enthusiats.png')
114    .pipe(fs.createWriteStream('uploads/dashboard_enthusiats.png'));
115  fs.createReadStream('tmp_uploads/dashboard_empty_nester.png')
116    .pipe(fs.createWriteStream('uploads/dashboard_empty_nester.png'));
117  fs.createReadStream('tmp_uploads/dashboard_dinner_party.png')
118    .pipe(fs.createWriteStream('uploads/dashboard_dinner_party.png'));
119  fs.createReadStream('tmp_uploads/dashboard_deal_shoppers.png')
120    .pipe(fs.createWriteStream('uploads/dashboard_deal_shoppers.png'));
121  fs.createReadStream('tmp_uploads/dashboard_convenience.png')
122    .pipe(fs.createWriteStream('uploads/dashboard_convenience.png'));
123  fs.createReadStream('tmp_uploads/dashboard_brand_conscious.png')
124    .pipe(fs.createWriteStream('uploads/dashboard_brand_conscious.png'));
125  fs.createReadStream('tmp_uploads/dashboard_booze_cruise.png')
126    .pipe(fs.createWriteStream('uploads/dashboard_booze_cruise.png'));
127	fs.createReadStream('tmp_uploads/dashboard_occasional_spenders.png')
128    .pipe(fs.createWriteStream('uploads/dashboard_occasional_spenders.png'));
129  fs.createReadStream('tmp_uploads/dashboard_potential_vips.png')
130    .pipe(fs.createWriteStream('uploads/dashboard_potential_vips.png'));
131
132	fs.createReadStream('tmp_uploads/dashboard_potential_high.jpg')
133    .pipe(fs.createWriteStream('uploads/dashboard_potential_high.jpg'));
134	fs.createReadStream('tmp_uploads/dashboard_potential_medium.jpg')
135    .pipe(fs.createWriteStream('uploads/dashboard_potential_medium.jpg'));
136  fs.createReadStream('tmp_uploads/dashboard_potential_low.jpg')
137    .pipe(fs.createWriteStream('uploads/dashboard_potential_low.jpg'));
138
139
140}
141
142export { setup };
143
Full Screen

read.js

Source: read.js Github

copy
1'use strict'
2
3const assert = require('chai').assert
4const proxyquire = require('proxyquire')
5const spooks = require('spooks')
6
7const modulePath = '../../src/read'
8
9suite('read:', () => {
10  test('require does not throw', () => {
11    assert.doesNotThrow(() => {
12      require(modulePath)
13    })
14  })
15
16  test('require returns function', () => {
17    assert.isFunction(require(modulePath))
18  })
19
20  suite('require:', () => {
21    let log, results, read
22
23    setup(() => {
24      log = {}
25      results = {
26        parse: [ {} ],
27        createReadStream: [ {} ]
28      }
29      read = proxyquire(modulePath, {
30        fs: {
31          createReadStream: spooks.fn({
32            name: 'createReadStream',
33            log: log,
34            results: results.createReadStream
35          })
36        },
37        './parse': spooks.fn({
38          name: 'parse',
39          log: log,
40          results: results.parse
41        })
42      })
43    })
44
45    test('read expects two arguments', () => {
46      assert.lengthOf(read, 2)
47    })
48
49    test('read does not throw', () => {
50      assert.doesNotThrow(() => {
51        read()
52      })
53    })
54
55    test('parse was not called', () => {
56      assert.strictEqual(log.counts.parse, 0)
57    })
58
59    test('fs.createReadStream was not called', () => {
60      assert.strictEqual(log.counts.createReadStream, 0)
61    })
62
63    suite('read:', () => {
64      let path, options, result
65
66      setup(() => {
67        path = {}
68        options = { foo: 'bar', ndjson: true }
69        result = read(path, options)
70      })
71
72      test('fs.createReadStream was called once', () => {
73        assert.strictEqual(log.counts.createReadStream, 1)
74      })
75
76      test('fs.createReadStream was called correctly', () => {
77        assert.lengthOf(log.args.createReadStream[0], 2)
78        assert.strictEqual(log.args.createReadStream[0][0], path)
79        assert.lengthOf(Object.keys(log.args.createReadStream[0][0]), 0)
80        assert.strictEqual(log.args.createReadStream[0][1], options)
81        assert.lengthOf(Object.keys(log.args.createReadStream[0][1]), 2)
82      })
83
84      test('parse was called once', () => {
85        assert.strictEqual(log.counts.parse, 1)
86      })
87
88      test('parse was called correctly', () => {
89        assert.isUndefined(log.these.parse[0])
90        assert.lengthOf(log.args.parse[0], 2)
91        assert.strictEqual(log.args.parse[0][0], results.createReadStream[0])
92        assert.lengthOf(Object.keys(log.args.parse[0][0]), 0)
93        assert.notStrictEqual(log.args.parse[0][1], options)
94        assert.deepEqual(log.args.parse[0][1], { foo: 'bar', ndjson: false })
95      })
96
97      test('parse result was returned', () => {
98        assert.strictEqual(result, results.parse[0])
99      })
100    })
101  })
102})
103
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 Playwright Internal 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)