Selenium is an excellent tool for browser automation. Using Selenium IDE, you can record sequences of commands (such as click, drag, and enter), validate the results, and ultimately store this automated test for later use. This is ideal for active development in the browser. But when you want to integrate these tests with CI/CD streams, it’s time to use Selenium WebDriver.

WebDriver exposes an API that binds to a number of programming languages and allows you to integrate browser tests with other tests. This article shows you how to run WebDriver in a container and use it with Python programs.

Run Selenium using Podman

Podman is the container runtime for the following example. See the previous article for information on how to get started with Podman.

This example uses a separate container for Selenium, which contains the WebDriver server and the browser itself. To start the server container in the background, run the following command:

$ podman run -d --network host --privileged --name server docker.io/selenium/standalone-firefoxCopy the code

When you run the container using the privilege flag and host network, you can connect to the container later from Python. You don’t need to use Sudo.

Use Selenium in Python

Now you can provide a simple program that uses this server. This program is small, but should give you an idea of what can be done:

The from selenium.webdriver.com mon. Desired_capabilities import DesiredCapabilities server = "http://127.0.0.1:4444/wd/hub" driver = webdriver.Remote(command_executor=server, desired_capabilities=DesiredCapabilities.FIREFOX) print("Loading page..." ) driver.get("https://fedoramagazine.org/") print("Loaded") assert "Fedora" in driver.title driver.quit() print("Done.")Copy the code

First, the program connects to the container you’ve started. It then loads the Fedora Magazine web page and determines that “Fedora” is part of the page title. Finally, it exits the session.

You need a Python binding to run this program. Since you’re already using containers, why not do it in containers? Save the following to a Dockerfile:

FROM fedora:29
RUN dnf -y install python3
RUN pip3 install seleniumCopy the code

Then use Podman to build the container image in the same folder as Dockerfile:

$ podman build -t selenium-python .Copy the code

To run the program in a container, mount the file containing Python code as a volume while running the container:

$ podman run -t --rm --network host \
 -v $(pwd)/browser-test.py:/browser-test.py:z \
 selenium-python python3 browser-test.pyCopy the code

The output looks like this:

Loading page...
Loaded
Done.Copy the code

What to do next

The example program above is minimal and may not be as useful. But that’s just the surface! See the documentation for Selenium and Python bindings. There, you’ll find examples of how to find elements in a page, handle pop-ups, or fill out forms. Drag and drop is also possible, and of course wait for events.

After you’ve implemented some good tests, you might want to include them in your CI/CD process. Fortunately, this is fairly straightforward, as everything is containerized.

You might also be interested in setting up a Grid to run tests in parallel. This not only helps speed things up, but also allows you to test multiple different browsers at the same time.

Clean up the

When you run out of containers, you can use the following command to stop and delete individual containers:

$ podman stop server
$ podman rm serverCopy the code

If you still want to free up disk space, run the following command to delete the image:

$ podman rmi docker.io/selenium/standalone-firefox
$ podman rmi selenium-python fedora:29Copy the code

conclusion

In this article, you’ve seen how easy it is to get started using Selenium using container technology. It allows you to automate interactions with your site, as well as test interactions. Podman allows you to run the containers you need without superuser privileges or Docker daemons. Finally, Python bindings allow you to interact with the browser using plain Python code.


via: https://fedoramagazine.org/automate-web-browser-selenium/

By Lennart Jern (lujun9972

This article is originally compiled by LCTT and released in Linux China