1

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); 
        }
halfer
  • 19,824
  • 17
  • 99
  • 186
mastercool
  • 463
  • 12
  • 35

2 Answers2

3

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.

QA Square
  • 245
  • 1
  • 3
  • Thanks! The code in your example is how to use Selenium grid with AWS? Or is that different? Currently the project uses testNG with each test being a test for a different user. Can I still keep my testNG suite and just run it with the above and it’ll still run on different threads? – mastercool Sep 04 '20 at 11:20
  • Also where can I find the AWS server url? I just set up an instance but I have no idea what this means. Next step it’s asking me to connect to it through SSH – mastercool Sep 04 '20 at 11:26
  • This code will work fine with your TestNg suite. Infact this is similar code you the code you have mentioned in your question. – QA Square Sep 04 '20 at 11:35
  • You can go to AWS console, it will show you the instace, clicking on its detail you will get the IP of your instance which you can use as AWS server IP. – QA Square Sep 04 '20 at 11:36
  • It’s that simple? I don’t need to add files to AWS to run this? Can I please message you in a few hours if I don’t figure it out? – mastercool Sep 04 '20 at 11:40
  • Yes,what all you need is, run a selenium server on AWS machine. For you AWS machine or a machine in your network, both are similar..there is not any change in the code. – QA Square Sep 04 '20 at 12:10
  • Sorry for the excessive questions. So your saying I need two machines? My local pc with the tests to run and then another machine that acts as a server? Is the second machine the remote AWS machine? If so, how do I even start a selenium server. I don’t see anywhere to do that in the AWS console – mastercool Sep 04 '20 at 13:21
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/220979/discussion-between-mastercool-and-qa-square). – mastercool Sep 04 '20 at 13:50
  • @QA Square, I got one question on the above code, I am able to run tests in EC2 but I use driver=new ChromeDriver(path); My problem is, when I do a VNC connection from local to EC2 (after setting up a ssh connection on port 5900 and exporting display), I see a black screen on VNC viewer. I am expecting to see Chrome test window like how we see on Grid VNC. Is it because I using ChromeDriver and not remotedriver? – Afsal Sep 20 '20 at 11:35
  • Here's a detailed explanation of my issue. @QA Square https://stackoverflow.com/q/63935935/4113833 – Afsal Sep 20 '20 at 11:48
2

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)

olli_kahn
  • 157
  • 6
  • 1
    Could you please send me a link to an article that would show me this? I am so clueless that I am not even sure what I need. When you say to setup OS, install chrome, ect, does that mean on the ec2 virtual machine? Also, when setting up ec2, there are so many things like CodeDeploy, ect. Do I need all that? – mastercool Sep 03 '20 at 22:15
  • smth like this https://medium.com/faun/setup-an-automated-testing-server-with-jenkins-and-aws-device-farm-bb2dfc5661ae but you have to figure out on your own what steps are needed as each project setup is unique – olli_kahn Sep 03 '20 at 22:30
  • So I actually just got an instance up and working. Now it says to connect to the ec2 instance using SSH. Do you know what this means or what this will do for me? – mastercool Sep 03 '20 at 22:37