Selenium Grid on Headless Ubuntu



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()
    chrome_options.add_argument('headless') #mandatory
    chrome_options.add_argument('window-size=1920x1080') #can be set to any window size
    capabilities = {'browserName': 'chrome'}
    capabilities.update(chrome_options.to_capabilities())
    driver = webdriver.Remote(command_executor = 'http://<PublicIpAddressOfGridInstance>:4444/wd/hub', desired_capabilities = capabilities)
    driver.get("https://www.whatismybrowser.com")
    driver.save_screenshot('screenshot.png')
    driver.quit()

    Firefox:
    from selenium import webdriver
    
    firefox_options = webdriver.FirefoxOptions()
    firefox_options.add_argument('--headless') #mandatory
    firefox_options.add_argument('--window-size=1920x1080') #can be set to any window size
    capabilities = {'browserName': 'firefox'}
    capabilities.update(firefox_options.to_capabilities())
    driver = webdriver.Remote(command_executor = 'http://<PublicIpAddressOfGridInstance>:4444/wd/hub', desired_capabilities = capabilities)
    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();
          chromeOptions.addArguments("headless"); // mandatory
          chromeOptions.addArguments("window-size=1920x1080"); // can be set to any window size
    
          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();
          firefoxOptions.addArguments("--headless"); // mandatory
          firefoxOptions.addArguments("--window-size=1920x1080"); // can be set to any window size
    
          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');
    
    // can be set to any window size
    const screen = {
      width: 1500,
      height: 700
    };
    
    (async function samplechrome() {
        let driver = new Builder()
            .forBrowser('chrome')
            .usingServer("http://<PublicIpAddressOfGridInstance>:4444/wd/hub")   
            .setChromeOptions(new chrome.Options().headless().windowSize(screen))
            .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');
    
    // can be set to any window size
    const screen = {
      width: 1500,
      height: 700
    };
    
    (async function samplefirefox() {
        let driver = new Builder()
            .forBrowser('firefox')
            .usingServer("http://<PublicIpAddressOfGridInstance>:4444/wd/hub")   
            .setFirefoxOptions(new firefox.Options().headless().windowSize(screen))
            .build();
        try {
            await driver.get('https://www.whatismybrowser.com');
        } finally {
            await driver.quit();
        }
    })();
  • 4  For more sample code snippets, visit our sample code site.


Manage Grid server:

  • 1  Connect to the terminal via SSH and log into user 'ubuntu'. See here how.
  • 2  On every boot of the instance a script gets executed which starts the Selenium grid server and two headless Chrome and two headless Firefox nodes. The script is located in /home/ubuntu/Selenium directory. To modify the script type
    nano /home/ubuntu/Selenium/startseleniumgrid.sh
    To remove the autostart remove the installed crontab by
    crontab -e
    and deleting of last line.
  • 3  To be able to run the grid server and the nodes in parallel, all Selenium jobs run in a separate 'screen' thread. To show all threads type
    screen -ls
    To go into one screen type
    screen -r <ScreenID>

    To exit from a screen press Ctrl+a+d on your keyboard. For more information on the functionality of screens in Ubuntu visit linuxize.com.
  • 4  For more information how to configure your Selenium grid server, visit the official Selenium documentation.