mastercool
mastercool

Reputation: 523

Can AWS EC2 be used for running automated Selenium tests on a remote location

At work we are running automated Selenium tests on a remote location (BrowserStack). This is how the remote web driver is instantiated and these tests also use testNG and the way the project is set up is that each test passes a row number into this DriverInit constructor and will then tests things that are on that row (that part isn't shown in the code). The problem is we don't have the money to run 100 parallel tests on BrowserStack and we are all unfamiliar with this.

After reading, it seems like AWS ec2 would be a good option but I have no idea how it works even after watching videos on it. Does ec2 have the capability of taking a project like this and running a testNG suite? What is the easiest way to do this? We don't need any of the fancy stuff that BrowserStack or SaucyLabs has. We simply need to run browser tests in the background, but we do not need to have a video recording or any testing information. We really just need the CPU power to run a lot of parallel tests remotely.

Ideally we would like to be able to just replace the URL with another url and run tests like that if possible.

public class DriverInit{

       public WebDriver driver;
       public ChromeOptions chromeOptions;
       public DesiredCapabilities caps;

       public static final String USERNAME = "my_name";
       public static final String AUTOMATE_KEY = "blah_blah_blah";
       public static final String URL = "https://" + USERNAME + ":" + AUTOMATE_KEY + "@hub-cloud.browserstack.com/wd/hub";
       
        DriverInit(int row) throws MalformedURLException {

            // for BrowserStack testing
               caps = new DesiredCapabilities();
               caps.setCapability("os", "Windows");
               caps.setCapability("os_version", "10");
               caps.setCapability("browser", "Chrome");
               caps.setCapability("browser_version", "80.0 beta");
               caps.setCapability("browserstack.local", "false");
               caps.setCapability("browserstack.selenium_version", "3.5.2");

               caps.setCapability("name", "selenium test for row " + row);
               this.driver = new RemoteWebDriver(new URL(URL), caps);
               this.chromeOptions = new ChromeOptions();

               String chromeDriverPath = "resources/chromedriver.exe";
               System.setProperty("webdriver.chrome.driver", chromeDriverPath); 
        }

Upvotes: 2

Views: 9000

Answers (2)

QA Square
QA Square

Reputation: 253

To run your remotely on AWS or Any other remote machine,

  1. Make sure you have a selenium server running on the machine.
  2. Provide IP and Port (on which selenium server is running) of your AWS machine to web driver as URL (Make sure your machine has access to that AWS machine)

This should doe your job. Below code should work.

try {
            DesiredCapabilities capabilities = DesiredCapabilities.chrome();

            ChromeOptions chromOpt = new ChromeOptions();
            chromOpt.addArguments("Proxy","null");
            chromOpt.setExperimentalOption("useAutomationExtension", false);
            chromOpt.addArguments("--disable-dev-shm-usage");
            chromOpt.addArguments("--headless");
            chromOpt.addArguments("--no-sandbox");


            capabilities.setCapability(ChromeOptions.CAPABILITY,chromOpt );

            driver = new RemoteWebDriver(new URL("http://" + AWS_SERVER_URL + ":" + AWS_SERVER_PORT + "/wd/hub"),
                    capabilities);
        } catch (Exception e) {
            e.printStackTrace();
        }

Alternatively, you can use Selenium Grid also. Start a Selenium Server on your machine as a hub, and server on your AWS machine as a node. And run it. Code will be similar which I have pasted.

Upvotes: 4

olli_kahn
olli_kahn

Reputation: 167

you have to setup your project on AWS to be able to run tests there.
so basically: setup OS, install chrome, install chromedriver, install project, install project dependencies.

it's better to be done automatically via some CI/CD (e.g. Jenkins)

Upvotes: 2

Related Questions