How to use _connectAsync method in Cypress

Best JavaScript code snippet using cypress

Run Cypress automation tests on LambdaTest cloud grid

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

conversation-db.js

Source: conversation-db.js Github

copy
1import sql from 'mssql';
2import dbConfig from './config/db.config';
3
4export default class ConversationDb {
5
6  constructor() {
7    this.isConnected = false;
8    this.connectionLock = false;
9  }
10
11  _connect() {
12    return new Promise(function (resolve, reject) {
13      sql.connect(dbConfig)
14        .then(pool => {
15          resolve(pool);
16        })
17        .catch(err => reject(err));
18    });
19  }
20
21  async _connectAsync() {
22    try {
23      while (this.connectionLock) {}
24      if (this.isConnected) return;
25      this.connectionLock = true;
26      this.db = await this._connect();
27      this.isConnected = true;
28      this.connectionLock = false;
29    } catch (err) {
30      console.log(err);
31    }
32  }
33
34  async getDealerIdByEmail(email) {
35    if (!this.isConnected) await this._connectAsync();
36
37    return await this.db.request()
38      .input('email_address', email)
39      .query('select TOP 1 * from dealers where EmailAddress = @email_address');
40  }
41
42  async saveNewDealer(email, dealerName, contactName) {
43    if (!this.isConnected) await this._connectAsync();
44
45    var result = await this.db.request()
46      .input('email_address', email)
47      .input('dealer_name', dealerName)
48      .input('contact_name', contactName)
49      .output('dealer_id', sql.Int)
50      .query('insert into dealers(DealerName, ContactName, EmailAddress) VALUES(@dealer_name, @contact_name, @email_address); SELECT @@IDENTITY as dealer_id');
51
52    return result.recordset[0].dealer_id;
53  }
54
55  async saveNewConversation(hash, customerId, dealerId) {
56    if (!this.isConnected) await this._connectAsync();
57
58    return await this.db.request()
59      .input('hash', hash)
60      .input('customer_id', customerId)
61      .input('dealer_id', dealerId)
62      .query('insert into conversations(Hash, CustomerId, DealerId) VALUES(@hash,@customer_id,@dealer_id)');
63  }
64
65  async getCustomerByEmail(email) {
66    if (!this.isConnected) await this._connectAsync();
67
68    return await this.db.request()
69      .input('email_address', email)
70      .query('select TOP 1 * from customers where EmailAddress = @email_address');
71  }
72
73  async saveNewCustomer(email, name) {
74    if (!this.isConnected) await this._connectAsync();
75
76    var result = await this.db.request()
77      .input('email_address', email)
78      .input('name', name)
79      .output('customer_id', sql.Int)
80      .query('insert into customers(Name, EmailAddress) VALUES(@name,@email_address); SELECT @@IDENTITY as customer_id');
81
82    return result.recordset[0].customer_id;
83  }
84
85  async saveNewMessage(conversationHash, customerId, dealerId, message) {
86    if (!this.isConnected) await this._connectAsync();
87
88    return await this.db.request()
89      .input('conversation_hash', conversationHash)
90      .input('customer_id', customerId)
91      .input('dealer_id', dealerId)
92      .input('message', message)
93      .input('timestamp', new Date())
94      .query('insert into messages(ConversationHash, CustomerId, DealerId, Message, Timestamp) VALUES(@conversation_hash,@customer_id,@dealer_id,@message, @timestamp)');
95  }
96
97  close() {
98    this.db.close();
99    this.isConnected = false;
100  }
101
102}
Full Screen

protocol.js

Source: protocol.js Github

copy
1const _ = require('lodash')
2const CRI = require('chrome-remote-interface')
3const { connect } = require('../../../network')
4const errors = require('../errors')
5const Promise = require('bluebird')
6const la = require('lazy-ass')
7const is = require('check-more-types')
8const debug = require('debug')('cypress:server:protocol')
9
10function _getDelayMsForRetry (i) {
11  if (i < 10) {
12    return 100
13  }
14
15  if (i < 18) {
16    return 500
17  }
18}
19
20function _connectAsync (opts) {
21  return Promise.fromCallback((cb) => {
22    connect.createRetryingSocket({
23      getDelayMsForRetry: _getDelayMsForRetry,
24      ...opts,
25    }, cb)
26  })
27  .then((sock) => {
28    // can be closed, just needed to test the connection
29    sock.end()
30  })
31  .catch((err) => {
32    errors.throw('CDP_COULD_NOT_CONNECT', opts.port, err)
33  })
34}
35
36/**
37 * Waits for the port to respond with connection to Chrome Remote Interface
38 * @param {number} port Port number to connect to
39 */
40const getWsTargetFor = (port) => {
41  debug('Getting WS connection to CRI on port %d', port)
42  la(is.port(port), 'expected port number', port)
43
44  return _connectAsync({ port })
45  .tapCatch((err) => {
46    debug('failed to connect to CDP %o', { port, err })
47  })
48  .then(() => {
49    debug('CRI.List on port %d', port)
50
51    // what happens if the next call throws an error?
52    // it seems to leave the browser instance open
53    return CRI.List({ port })
54  })
55  .then((targets) => {
56    debug('CRI List %o', { numTargets: targets.length, targets })
57    // activate the first available id
58    // find the first target page that's a real tab
59    // and not the dev tools or background page.
60    // since we open a blank page first, it has a special url
61    const newTabTargetFields = {
62      type: 'page',
63      url: 'about:blank',
64    }
65
66    const target = _.find(targets, newTabTargetFields)
67
68    la(target, 'could not find CRI target')
69
70    debug('found CRI target %o', target)
71
72    return target.webSocketDebuggerUrl
73  })
74}
75
76module.exports = {
77  _connectAsync,
78  _getDelayMsForRetry,
79  getWsTargetFor,
80}
81
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 Cypress 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)