Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use selenium+testng to realize web Automation in docker

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to use selenium+testng in docker to achieve web automation, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Construction of web Automation Environment for selenium+testng under Windows

People who have done automation must be very familiar with the construction of selenium web environment, especially the use of selenium in java.

First set up and install JDK, configure the java development environment (if you still don't know how to do this, but hit PP). Then download the jar package of the corresponding selenium from the official website and load it into the project, or use maven to modify the pom.xml file and load the dependent package of selenium directly:

Org.seleniumhq.selenium selenium-java 3.14.0

Then add the corresponding browser driver file, and the environment is basically done, and you can begin to automate the road to testing code.

Of course, you will also use the popular unit testing framework testng in the process of writing code. How to add testng environment on top of this? In fact, this is not a difficult task. The official website of testng (https://testng.org/doc/index.html) provides how to install plug-ins under Eclipse, IEDA and other editors. For example, Eclipse can go directly to Market to search for testng installation, while using maven, you need to add dependent packages to pom.xml:

Jcenterbintray http://jcenter.bintray.comorg.testngtestng6.10test

Mindless wait for the download and installation to complete, then write the code, and then you can spill it freely.

But what I'm going to introduce today is not the above, the construction of this environment is very simple, most people actually know it, and there are not many holes, it's just a code environment.

What is the trouble we have to deal with this time? In the increasingly developed docker mode, the above compilation environment can not run on the linux system, without the code editor, the interface, and the browser, what should we do? Let's have a look.

Installation and use of Docker

Again, java+selenium+testng is required, except that the environment has changed from windows to linux.

About the use of docker, here is a brief introduction to the installation and startup process:

Install directly using the yum install docker command

Service docker start starts the docker service

Then docker pull centos finishes the most basic docker image in centos version.

All current mirrors can be listed through the docker images command

After viewing the linux image, use the docker run-it-d image name to make the linux container run (note the parameter-it-d, if no container flashes by, it will hang)

If you enter the container, you can check all the currently running containers through docker ps. If it is not found, the container has not been started or has been closed. Docker ps will list the id of the container.

Use the docker attach container id to log in to the linux in the container.

Building java environment under Docker

The linux in the container is actually the same as the normal linux operation.

The first is to install jdk:

Download the jdk-8u181-linux-x64.tar.gz package

Decompress tar-xzvf jdk-8u181-linux-x64.tar.gz to get the jdk1.8.0_181 directory (assuming it is stored as / root/java/ directory)

Configure the environment variables for java:

Edit the environment variable file: vi / etc/profile

Add content:

Export JAVA_HOME=/root/java/jdk1.8.0_181 export JRE_HOME=$ {JAVA_HOME} / jre export CLASSPATH=.:$ {JAVA_HOME} / lib:$ {JRE_HOME} / lib export PATH=$ {JAVA_HOME} / bin:$PATH

Refresh the environment variable configuration:

Source / etc/profile

Confirm that the java version is valid:

Java-version

Here the java environment is solved, and there is no particular difficulty, just step by step.

Configuration of testng environment under Docker

Then there is testng, because the environment is similar to java and is also related to environment variables, so priority is given to:

Download the testng-6.14.3.jar package

Decompress tar-xzvf testng-6.14.3.jar to get the testng directory (suppose you put it in the / root/java/testng/ directory)

Continue editing the environment variable file:

Vi / etc/profile

Add content:

Export TESTNG_HOME=/root/java/testngexport CLASSPATH=.:$ {JAVA_HOME} / lib:$ {JRE_HOME} / lib:$CLASSPATH:$TESTNG_HOME/testng-6.14.3.jar

Refresh the environment variable configuration:

Source / etc/profile

Note the testng environment variable here, where CLASSPATH is appended to the java environment. If you configure it on windows, the delimiter should be replaced; and the form of mutual invocation of environment variables is different, there are some differences.

Well, some people may think, isn't it easy?

However, if you continue to implement at this time, you will find that you are beginning to report an error, that is, the pit is coming, which is because you are out of the entire development environment.

Let's first mention the execution steps of testng. Suppose you write a test.java, and remember to write the simplest one. Don't join other jar packages, including package name simple and class name test:

Javac test.java java org.testng.TestNG-testclass simple.test

It seems very simple, but when it comes to execution, there are errors everywhere. To execute javac test.java, the common errors are:

Java.lang.NoClassDefFoundError: com/beust/jcommander/

This is due to the lack of the jar package used at compile time, so we need to download jcommander-1.72.jar. We put it together in the jar package of testng (assuming / root/java/testng/ directory):

The CLASSPATH of the environment variable needs to be appended:

CLASSPATH=.:$ {JAVA_HOME} / lib:$ {JRE_HOME} / lib:$CLASSPATH:$TESTNG_HOME/testng-6.14.3.jar:$TESTNG_HOME/jcommander-1.72.jar

Continue to refresh the environment configuration:

Source / etc/profile

After that, it is generally possible to compile smoothly and the test.class file appears.

If other errors occur, such as the testng-related package does not exist, check that the environment variables are configured correctly.

Finally, it is the link of operation, and it is also the point where we will encounter the most problems.

First mention the structure, such as the above simple.test class, then you need to create a folder simple to store test.java.

In the environment variable of CLASSPATH, the top-level directory of the testng code file and package needs to be appended, assuming

/ root/java/testng/ / root/java/testng/simple/test.java

With such a structural premise, you can execute it through a command:

Java org.testng.TestNG-testclass simple.test

The result means that the operation is successful:

Total tests run: 1, Failures: 0, Skips: 0

If you have a test case @ Test in your code, but run appears 0, it still fails, but there is no clear error report.

Of course, being here doesn't mean it's over.

Because you may also encounter problems with the lack of various dependent jar packages:

You can introduce all these jar packages through parameters

/ root/java/testng/ / root/java/testng/simple/test.java

Remotely call the browser under windows

Do you think this will fix the environment and everything will be all right? One more knife, where's the browser? How to solve the problem without a browser?

Please use the server that comes with selenium to deploy to the windows machine.

Instead, the solution to this problem is relatively simple:

Download the selenium-server-standalone-3.14.0.jar package

Put it in the specified directory (for example, in the root directory of C disk)

Cd to this directory: cd C:\

Execute the command to open the selenium remote service:

Java-jar selenium-server-standalone-3.14.0.jar-port 6666

Next, someone will ask how to specify the browser.

Just use the following statement in the java code to invoke the service you just started in windows:

WebDriver driver = new RemoteWebDriver ("http://ip:6666/wd/hub/", DesiredCapabilities.chrome ()); this is how to use selenium+testng to automate web in docker. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report