×
Community Blog Develop Function Compute by Running Selenium Java

Develop Function Compute by Running Selenium Java

In this tutorial, you will learn how to develop Function Compute by running Selenium Java.

By Yi Xian

This tutorial discusses how to develop Function Compute by running Selenium Java, but before we get down into it, let's first take a look at several important concepts mentioned in this article.

Background Information

First, there's Alibaba Cloud's Function Compute, which is an event-driven service that allows users to write and upload code without having to manage server health or consider some other factors. Function Compute prepares and auto scales to the appropriate amount of computing resources necessary to run user code. The user only pays for the resources required to run their code.

Next, there's Fun, which is a deployment tool for serverless applications. It helps you manage resources, such as Function Compute, API Gateway, and Log Service. You can use Fun to develop, build, and deploy resources by describing specified resources in the template.yml file.

Note: The techniques described in this article are applicable to Fun 2.10.2 or later.

Dependent Tools

This tutorial discusses how to develop Function Compute by running Selenium Java. The project outlined in this tutorial is developed on macOS but it also involves platform-independent tools, which are compatible with Linux and Windows. Before proceeding to this example, make sure that Docker, Fun, and Fcli are correctly installed, updated to the latest version and properly configured. You can download, update, and configure these tools with the instructions and resources on the pages linked here.

Fun and Fcli depend on Docker to simulate the local environment. MacOS users can use homebrew to install these tools:

brew cask install docker
brew tap vangie/formula
brew install fun
brew install fcli

Windows and Linux users can refer to Installation to learn how to install these tools. After the installation, don't forget to first run fun config to initialize the configuration.

Note: If you have already installed Fun, make sure it is version 2.10.1 or higher.

$ fun --version
2.10.1

Running Selenium Java

You can use the fun init command to easily initialize this template project to a local environment.

fun init vangie/selenium-java-example

Then you can use the fun install command to install dependencies.

$ fun install
...

Text, you can conduct some local testing. To do so, you can use the ChromeDemo test code, whose segment content is as follows:

public class ChromeDemo implements StreamRequestHandler {


    public void handleRequest(InputStream inputStream,
                              OutputStream outputStream,
                              Context context) throws IOException {

        System.setProperty("webdriver.chrome.driver", "/code/chromedriver");

        ChromeOptions options = new ChromeOptions();
        options.setBinary("/code/headless-chromium");
        options.addArguments("--disable-extensions"); // disabling extensions
        options.addArguments("--disable-gpu"); // applicable to windows os only
        options.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems
        options.addArguments("--no-sandbox"); // Bypass OS security model
        options.addArguments("--headless");

        WebDriver driver = new ChromeDriver(options);

        driver.get("https://ide.fc.aliyun.com");

        outputStream.write(("Page title is: " + driver.getTitle() + "\n").getBytes());

        driver.quit();

    }

}

Next, you can trey the code locally by using the following command.

$ mvn package && fun local invoke selenium
...
FC Invoke Start RequestId: 68c83b4c-b053-479c-9b0e-9503582ccb56
handle user request is com.aliyun.fc.selenium.ChromeDemo::handleRequest
cache is null!
Starting ChromeDriver 2.35.528139 (47ead77cb35ad2a9a83248b292151462a66cd881) on port 20652
Only local connections are allowed.
Mar 05, 2019 11:34:27 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Page title is: Cloud-integrated development environment
FC Invoke End RequestId: 68c83b4c-b053-479c-9b0e-9503582ccb56


RequestId: 68c83b4c-b053-479c-9b0e-9503582ccb56          Billed Duration: 5265 ms        Memory Size: 1998 MB    Max Memory Used: 240 MB

Then, deploy it using the fun deploy command.

$ mvn package && fun deploy

Last, you can run it.

$  fcli function invoke -s chrome -f selenium
  Page title is: 云端集成开发环境

File size

Because the size of chromedriver and headless-chromium after the compression is very close to 50 MB and little space is left for the jar, a highly compressed version is also created. The file is 32.7 MB after compressed by using the Brotli algorithm which features a higher compression ratio. When running the project, use initializer for the decompression, which takes about 3.7s.

References

  1. Headless Chrome + Selenium Webdriver in AWS Lambda
  2. Running Selenium and Headless Chrome on AWS Lambda
0 0 0
Share on

Alibaba Cloud Serverless

97 posts | 7 followers

You may also like

Comments

Alibaba Cloud Serverless

97 posts | 7 followers

Related Products