Selenium remote server on Windows



Run test scripts clientside:

  • 1  Once the instance has been booted, we need to open port 4444 on the instance to allow incoming Selenium connections. In the AWS management console click on the associated security group of your instance. Then click on 'Edit inbound rules'. Click on 'Add rule' and open custom TCP port 4444. In 'Source' dropdown choose 'My IP'. Caution: Allowing traffic from anywhere makes your Selenium grid available for everyone without any further protection.
  • 2  Install Selenium libraries in the language you want to use on your local machine. For more information see the official Selenium documentation.
  • 3  The following sample code show you how you can connect to the instance to run tests on it.
    Chrome:
    from selenium import webdriver
    
    chrome_options = webdriver.ChromeOptions()
    driver = webdriver.Remote(command_executor = 'http://<PublicIpAddressOfGridInstance>:4444/wd/hub',
        options=chrome_options)
    driver.get("https://www.whatismybrowser.com")
    driver.save_screenshot('screenshot.png')
    driver.quit()

    Firefox:
    from selenium import webdriver
    
    firefox_options = webdriver.FirefoxOptions()
    driver = webdriver.Remote(command_executor = 'http://<PublicIpAddressOfGridInstance>:4444/wd/hub',
        options=firefox_options)
    driver.get("https://www.whatismybrowser.com")
    driver.save_screenshot('screenshot.png')
    driver.quit()
    Chrome:
    import java.io.IOException;
    import java.net.URL;
    
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.remote.RemoteWebDriver;
    
    public class samplechrome {
    
      public static void main(String[] args) throws IOException, InterruptedException {
        try {
          ChromeOptions chromeOptions = new ChromeOptions();
          WebDriver driver =
              new RemoteWebDriver(new URL("http://<PublicIpAddressOfGridInstance>:4444/wd/hub"), chromeOptions);
    
          driver.get("https://www.whatismybrowser.com");
    
          driver.quit();
        } catch (Exception ex) {
          System.out.println(ex.getMessage());
        }
      }
    }
    

    Firefox:
    import java.io.IOException;
    import java.net.URL;
    
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxOptions;
    import org.openqa.selenium.remote.RemoteWebDriver;
    
    public class samplefirefox {
    
      public static void main(String[] args) throws IOException, InterruptedException {
        try {
          FirefoxOptions firefoxOptions = new FirefoxOptions();
    
          WebDriver driver =
              new RemoteWebDriver(new URL("http://<PublicIpAddressOfGridInstance>:4444/wd/hub"), firefoxOptions);
    
          driver.get("https://www.whatismybrowser.com");
    
          driver.quit();
        } catch (Exception ex) {
          System.out.println(ex.getMessage());
        }
      }
    }
    Chrome:
    const { Builder, Capabilities } = require("selenium-webdriver");
    var chrome = require('selenium-webdriver/chrome');
    
    (async function samplechrome() {
        let driver = new Builder()
            .forBrowser('chrome')
            .usingServer("http://<PublicIpAddressOfGridInstance>:4444/wd/hub")   
            .build();
        try {
            await driver.get('https://www.whatismybrowser.com');
        } finally {
            await driver.quit();
        }
    })();

    Firefox:
    const { Builder, Capabilities } = require("selenium-webdriver");
    var firefox = require('selenium-webdriver/firefox');
    
    (async function samplefirefox() {
        let driver = new Builder()
            .forBrowser('firefox')
            .usingServer("http://<PublicIpAddressOfGridInstance>:4444/wd/hub")   
            .build();
        try {
            await driver.get('https://www.whatismybrowser.com');
            console.log("done");
        } finally {
            await driver.quit();
        }
    })();
  • 4  For more sample code snippets, visit our sample code site.


Manage Grid server:

  • 1  Connect to your instance via RDP. See here how.
  • 2  On every boot of the instance a script gets executed which starts the Selenium remote server. The script is located at Desktop->Selenium->startselenium.cmd. To modify the script right click on it and click 'Edit'. To remove the autostart, remove the installed scheduled task by opening 'Task Scheduler' app.
    Afterwards search for 'startselenium' task and double click on it to modify the task.
  • 3  For more information how to configure your Selenium grid server, visit the official Selenium documentation.