Repository: nightwatchjs/nightwatch
Pull request #2759 (2021-06-22 12:05:13)

added selenium-webdriver proxy
### Changes
- ~~remove proxy from http library of nightwatch~~ --> refator http library of nightwatch to work with selenium-webriver proxy.
- added proxy in the driver options
- added test cases for each driver


Patch:
diff --git a/lib/transport/selenium-webdriver/index.js b/lib/transport/selenium-webdriver/index.js
index d0fa203c..bf769a98 100644
--- a/lib/transport/selenium-webdriver/index.js
+++ b/lib/transport/selenium-webdriver/index.js
@@ -163,7 +163,7 @@ class SeleniumWebdriver extends WebdriverTransport {
               try {
                 body = JSON.stringify(data);
               } catch (err) {
-
+                // 
               }
             }
             const resp = new http.Response(statusCode, headers, body);
@@ -251,7 +251,8 @@ class SeleniumWebdriver extends WebdriverTransport {
 
     this
       .addHeadlessOption({options, argv})
-      .addWindowSizeOption({options});
+      .addWindowSizeOption({options})
+      .addProxyOption({options});
 
     return options;
   }
@@ -272,6 +273,13 @@ class SeleniumWebdriver extends WebdriverTransport {
     return this;
   }
 
+  addProxyOption({options}) {
+    if (this.capabilities.getProxy() && (options instanceof Capabilities) && options.setProxy) {
+      const proxy = require('selenium-webdriver/proxy');
+      options.setProxy(proxy.manual(this.capabilities.getProxy()));
+    }
+  }
+
   setBuilderOptions({builder, options}) {
     switch (this.capabilities.getBrowserName()) {
       case Browser.CHROME:


Test patch:
diff --git a/test/src/index/testChromeOptions.js b/test/src/index/testChromeOptions.js
index dfb59f6f..31612de6 100644
--- a/test/src/index/testChromeOptions.js
+++ b/test/src/index/testChromeOptions.js
@@ -109,6 +109,26 @@ describe('Test chrome options', function () {
     assert.strictEqual(options instanceof ChromeOptions, true);
     assert.deepStrictEqual(options.options_.args, ['window-size=100,100']);
   });
- 
+
+  it('proxy option', function(){
+    const client = Nightwatch.createClient({
+      desiredCapabilities: {
+        browserName: 'chrome',
+        proxy: {
+          https: 'localhost:8888'
+        }
+      }
+    });
+    const options = client.transport.createOptions();
+    
+    assert.strictEqual(options instanceof ChromeOptions, true);
+    assert.deepStrictEqual(options.getProxy(), {
+      proxyType: 'manual',
+      sslProxy: 'localhost:8888',
+      ftpProxy: undefined,
+      httpProxy: undefined,
+      noProxy: undefined
+    });
+  });
   
 });
diff --git a/test/src/index/testEdgeOptions.js b/test/src/index/testEdgeOptions.js
index d632cc2e..5d60cc79 100644
--- a/test/src/index/testEdgeOptions.js
+++ b/test/src/index/testEdgeOptions.js
@@ -110,5 +110,27 @@ describe('Test edge option', function(){
     assert.strictEqual(options instanceof EdgeOptions, true);
     assert.strictEqual(options.options_.androidPackage, 'com.android.edge');
   });
-  
+
+  it('proxy option', function(){
+    const client =  Nightwatch.createClient({
+      desiredCapabilities: {
+        browserName: 'edge',
+        proxy: {
+          https: 'localhost:8888'
+        }
+      }
+    });
+    const options = client.transport.createOptions();
+
+    assert.strictEqual(options instanceof EdgeOptions, true);
+    assert.deepStrictEqual(options.getProxy(), {
+      proxyType: 'manual',
+      sslProxy: 'localhost:8888',
+      ftpProxy: undefined,
+      httpProxy: undefined,
+      noProxy: undefined
+    });
+  });
+
+
 });
diff --git a/test/src/index/testFirefoxOptions.js b/test/src/index/testFirefoxOptions.js
index 3841f392..1112b72a 100644
--- a/test/src/index/testFirefoxOptions.js
+++ b/test/src/index/testFirefoxOptions.js
@@ -95,5 +95,26 @@ describe('Firefox driver options', function(){
     assert.strictEqual(options instanceof FirefoxOptions, true);
     assert.deepStrictEqual(options.get('moz:firefoxOptions').args, ['--width=100', '--height=100']);
   });
- 
+  
+  it('proxy options', function(){
+    const client = Nightwatch.createClient({
+      desiredCapabilities: {
+        proxy: {
+          https: 'localhost:8888'
+        },
+        browserName: 'firefox'
+      }
+    });
+    const options = client.transport.createOptions();
+
+    assert.strictEqual(options instanceof FirefoxOptions, true);
+    assert.deepStrictEqual(options.getProxy(), {
+      proxyType: 'manual',
+      sslProxy: 'localhost:8888',
+      ftpProxy: undefined,
+      httpProxy: undefined,
+      noProxy: undefined
+    });
+  });
+
 });
diff --git a/test/src/index/testSafariOptions.js b/test/src/index/testSafariOptions.js
index 248a2f48..6cc209d0 100644
--- a/test/src/index/testSafariOptions.js
+++ b/test/src/index/testSafariOptions.js
@@ -20,5 +20,27 @@ describe('Test safari options', function(){
     assert.strictEqual(options instanceof SafariOptions, true);
     assert.deepStrictEqual(options.options_.technologyPreview, true);
   });
-    
+
+  it('proxy option', function(){
+    const client =  Nightwatch.createClient({
+      desiredCapabilities: {
+        proxy: {
+          https: 'localhost:8888'
+        },
+        browserName: 'safari'
+      }
+    });
+    const options = client.transport.createOptions();
+
+    assert.strictEqual(options instanceof SafariOptions, true);
+    assert.deepStrictEqual(options.getProxy(), {
+      proxyType: 'manual',
+      sslProxy: 'localhost:8888',
+      ftpProxy: undefined,
+      httpProxy: undefined,
+      noProxy: undefined
+    });
+  });
+  
+
 });
\ No newline at end of file