AWS EC2可以用于在远程位置运行自动化的Selenium测试吗?

huangapple go评论84阅读模式
英文:

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

问题

在工作中,我们在远程位置(BrowserStack)上运行自动化的Selenium测试。以下是远程WebDriver的实例化方式,这些测试还使用了testNG。项目的设置方式是,每个测试将一行编号传递给这个DriverInit构造函数,然后将测试针对该行上的内容(代码中未显示此部分)。问题是,我们没有足够的资金在BrowserStack上运行100个并行测试,并且我们对此并不熟悉。

阅读后,似乎AWS的EC2是一个不错的选择,但即使在观看了相关视频后,我仍然不知道它是如何工作的。EC2是否有能力承载这样的项目并运行testNG套件?最简单的实现方式是什么?我们不需要BrowserStack或SaucyLabs等的高级功能。我们只需在后台运行浏览器测试,不需要视频录制或任何测试信息。我们真正需要的只是远程运行大量并行测试的CPU计算能力。

理想情况下,如果可能的话,我们希望能够只需将URL替换为另一个网址,然后运行类似的测试。

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 {

        // 用于BrowserStack测试
        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); 
    }
英文:

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); 
	    }

</details>


# 答案1
**得分**: 3

```plaintext
要在AWS或任何其他远程机器上运行您的代码,
1. 确保您在机器上运行了一个Selenium服务器。
2. 将您的AWS机器的IP和端口(Selenium服务器运行的端口)作为URL提供给WebDriver(确保您的机器可以访问该AWS机器)。

这应该完成您的工作。以下代码应该有效。

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();
}

或者,您也可以使用Selenium Grid。在您的机器上作为主节点启动一个Selenium服务器,并在您的AWS机器上作为节点启动另一个服务器。然后运行它。代码将类似于我贴出的内容。
英文:

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.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

try {
			DesiredCapabilities capabilities = DesiredCapabilities.chrome();

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


			capabilities.setCapability(ChromeOptions.CAPABILITY,chromOpt );

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

<!-- end snippet -->

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.

答案2

得分: 2

你需要在AWS上设置你的项目,以便能够在那里运行测试。<br>基本上:设置操作系统,安装Chrome,安装Chromedriver,安装项目,安装项目依赖项。

最好通过一些CI/CD工具(例如Jenkins)自动完成。

英文:

you have to setup your project on AWS to be able to run tests there. <br>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)

huangapple
  • 本文由 发表于 2020年9月4日 05:21:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63731790.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定