Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pw on IOS changes #47

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions lib/core/AppiumSocket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// AppiumSocket.js
'use strict';

const EventEmitter = require('events');
const logger = require('../util/loggerFactory');
const appiumSessionDriver = require('../util/AppiumSession');
const { kSendMessage } = require('../config/constants');

/**zzxx
* AppiumSocket handles specific events for Appium communication.
*/
class AppiumSocket extends EventEmitter {
constructor() {
super();
this.registerListeners();
}

/**
* Registers listeners for the AppiumSocket.
*/
registerListeners() {
this.on(kSendMessage, this.handleSendMessage.bind(this));
}

/**
* Handles the kSendMessage event.
*
* @param {string} msg - The message to log.
*/
async handleSendMessage(msg) {
logger.info(`AppiumSocket received message: ${typeof msg} ${msg}`);

const { method, params } = JSON.parse(msg);

if (method === 'initialize') {
await appiumSessionDriver();
}
}
}

module.exports = AppiumSocket;
26 changes: 15 additions & 11 deletions lib/core/Context.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const EventEmitter = require('events');
const IncomingWebSocket = require('./IncomingWebSocket');
const OutgoingWebSocket = require('./OutgoingWebSocket');
const AppiumSocket = require('./AppiumSocket');
const logger = require('../util/loggerFactory');
const { createTarget } = require('../util/util');
const {
Expand Down Expand Up @@ -55,7 +56,8 @@ class Context extends EventEmitter {
this.connectionId = connectionId;
this.incomingSocket = null;
this.outgoingSocket = null;
this.incomingLock = true;
this.incomingLock = false;
this.appiumSocket = null;
this.outgoingLock = false;
}

Expand All @@ -68,13 +70,15 @@ class Context extends EventEmitter {
addNewConnection(socket, request) {
if (isUndefined(this.incomingSocket)) {
this.incomingSocket = new IncomingWebSocket(socket, request);
this.outgoingSocket = new OutgoingWebSocket(
createTarget(request.url),
request.headers
);
this.outgoingSocket.addSocket();
logger.info(`[RUTVIK] - Incoming socket added, ${request.url}`);
this.appiumSocket = new AppiumSocket();
// this.outgoingSocket = new OutgoingWebSocket(
// createTarget(request.url),
// request.headers
// );
// this.outgoingSocket.addSocket();
this.registerIncomingListeners();
this.registerOutgoingListeners();
// this.registerOutgoingListeners();
} else {
this.incomingSocket.setSocket(socket, request);
this.outgoingSocket.emit(kDequeueMessage);
Expand All @@ -91,7 +95,7 @@ class Context extends EventEmitter {
if (this.incomingLock) {
logger.debug(`${INCOMING} [${this.connectionId}] [QUEUE] - ${msg}`);
this.incomingSocket.addToQueue(msg);
} else this.outgoingSocket.emit(kSendMessage, msg);
} else this.appiumSocket.emit(kSendMessage, msg);
});

this.incomingSocket.on(kError, (error) => {
Expand All @@ -114,15 +118,15 @@ class Context extends EventEmitter {
});

this.incomingSocket.on(kDrainMessage, (msg) => {
this.outgoingSocket.emit(kSendMessage, msg);
this.appiumSocket.emit(kSendMessage, msg);
});

this.incomingSocket.on(kConnectionOpened, () => {
if (isNotUndefined(this.upstreamCloseTimer)) {
clearTimeout(this.upstreamCloseTimer);
this.upstreamCloseTimer = null;
}
this.outgoingSocket.emit(kDequeueMessage);
this.appiumSocket.emit(kDequeueMessage);
logger.debug(`${INCOMING} [${this.connectionId}] [OPEN]`);
incrNewConnect();
incrActiveConnectionCount();
Expand All @@ -132,7 +136,7 @@ class Context extends EventEmitter {
logger.info(
`${INCOMING} [${this.connectionId}] [CLOSE] - Socket closed with ${code} and ${msg}`
);
this.outgoingSocket.emit(kQueueMessage);
this.appiumSocket.emit(kQueueMessage);
this.upstreamCloseTimer = setTimeout(
this.closingOutgoingSocket.bind(this, code, msg),
config.closeTimer
Expand Down
1 change: 1 addition & 0 deletions lib/core/Proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ class Proxy {
return;
}
const proxyReq = http.request(options, (proxyResponse) => {
// logger.info(`[RUTVIK] - ${this.body}`);
response.writeHead(proxyResponse.statusCode, proxyResponse.headers);
proxyResponse.pipe(response, {
end: true,
Expand Down
53 changes: 53 additions & 0 deletions lib/util/AppiumSession.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const wd = require('wd');

// Define build name
const BUILD_NAME = 'PW iOS - POC';

// Configure capabilities
const capabilities = {
'browserName': 'Safari',
'device': 'iPhone 14',
'os_version': '16',
'real_mobile': 'true',
'build': BUILD_NAME,
'project': 'iOS Safari Test',
'browserstack.debug': 'true',
'browserstack.console': 'info',
'browserstack.interactiveDebugging': 'true',
'appium:automationName': 'XCUITest',
'appium:safariInitialUrl': "about:blank",
'appium:nativeWebTap': true,
'appium:autoAcceptAlerts': true,
'appium:safariAllowPopups': true
};


// Create a new browser instance
const driver = wd.promiseChainRemote({
host: 'hub.browserstack.com',
port: 80,
user: "rutvikchandla_2MEern",
pwd: "3PpCxycsXsg1DzMFauGe"
});

// Start the session and navigate to a URL
async function initialiseDriver() {
try {
// Initialize the session
await driver.init(capabilities);
console.log('Session started');

} catch (error) {
console.error('Error:', error);
// Make sure to end the session even if there's an error
try {
await driver.quit();
} catch (quitError) {
console.error('Error ending session:', quitError);
}
}
}

module.exports = initialiseDriver;


Loading
Loading