使用Pytest,Selenium Grid和Docker创建测试基础架构

基于这个
article,我成功地创建了可扩展的硒网格.

然后我想用Pytest运行我的测试套件[用Python编写]到Grid中.

我是Docker的新手,并试图找到将我的测试程序迁移到微服务架构的最佳方法.基本上我想让开发人员能够在他们的PC上本地设置一个完整的测试基础设施.

所以,我有4个Dockerfiles和1个docker-compose.yml.

BASE dockerfile:

FROM ubuntu

ENV SEL_VERSION 2.44.0

# Update repos for Java
RUN apt-get update -qqy \
&& apt-get -qqy --no-install-recommends install \
software-properties-common \
&& rm -rf /var/lib/apt/lists/*
RUN add-apt-repository -y ppa:webupd8team/java

RUN echo debconf shared/accepted-oracle-license-v1-1 select true | debconf-set-selections
RUN echo debconf shared/accepted-oracle-license-v1-1 seen true | debconf-set-selections

# Install Java7
RUN apt-get update -qqy \
&& apt-get -qqy --no-install-recommends install \
oracle-java7-installer \
&& rm -rf /var/lib/apt/lists/*

# Download Selenium Server
RUN wget http://selenium-release.storage.googleapis.com/${SEL_VERSION%.*}/selenium-server-standalone-${SEL_VERSION}.jar

HUB dockerfile:

FROM org/grid:base

EXPOSE 4444

# Add and set permissions to the script that will launch the hub
ADD register-hub.sh /var/register-hub.sh
RUN chmod 755 /var/register-hub.sh

# start a shell and run the script
#WORKDIR /
CMD ["/bin/bash", "/var/register-hub.sh"]

NODE dockerfile:

FROM org/grid:base

# set the FF version to use
ENV FIREFOX_MINOR 34.0.5

# Update and install what's needed
RUN apt-get update -qqy \
&& apt-get -qqy --no-install-recommends install \
firefox \
xvfb \
bzip2 \
&& rm -rf /var/lib/apt/lists/*

# setup FF
RUN [ -e /usr/bin/firefox ] && rm /usr/bin/firefox
ADD https://ftp.mozilla.org/pub/mozilla.org/firefox/releases/${FIREFOX_MINOR}/linux-x86_64/en-US/firefox-${FIREFOX_MINOR}.tar.bz2 /tmp/
RUN apt-get install -q -y libdbus-glib-1-2
RUN tar -xvjf /tmp/firefox-${FIREFOX_MINOR}.tar.bz2 -C /opt/
RUN chmod -R +x /opt/firefox/
RUN ln -s /opt/firefox/firefox /usr/bin/firefox

# add and set permissions for the bash script to register the node
ADD register-node.sh /var/register-node.sh
RUN chmod 755 /var/register-node.sh

# start a shell and run the script
CMD ["/bin/bash", "/var/register-node.sh"]

和PYTEST dockerfile:

# Starting from base image
FROM ubuntu

# Set the Github personal token [to clone the QA code]
#todo------secret------
ENV GH_TOKEN some_token_some_token_some_token_

# Install Python & pip
RUN apt-get update
RUN apt-get upgrade -y
RUN apt-get install -y python python-pip python-dev && pip install --upgrade pip

# Install GIT
RUN apt-get update -y && apt-get install git -y

# [in the / folder] Create the folder and add the whole project from the repo to the container
RUN git clone https://$GH_TOKEN:x-oauth-basic@github.com/org/org_QA.git /org_QA_folder

# Install dependencies via pip
WORKDIR /org_QA_folder
RUN pip install -r dependencies.txt

#
CMD /bin/bash

和docker-compose文件:

hub:
  image: org/grid:hub # image must already exist
  ports:
    - "4444:4444" # HOST:CONTAINER
  external_links: # link to a container created outside of this YAML file
    - pytest
  volumes_from:
    - pytest # must be created first

firefox:
  image: org/grid:nodeff # image must already exist
  links:
    - hub
  expose:
    - "5555" # grid console open to public

所以…我无法理解我应该如何运行“py.test /org_QA_folder/testcase.py”之类的东西并在网格上运行[本质上是节点].

我首先运行pytest容器
docker run -dit -v / org_QA_folder –name pytest schoox / grid:py和其他服务与docker-compose up -d.

我尝试了多种方法:

>不使用Pytest容器,而是克隆集线器中的代码.但在这种情况下,HUB容器运行网格的中心[每个容器指令一个CMD]. [编辑:我选择了像Docker建议的微服务架构]
>我离开集线器和节点容器以运行其指定的服务,并创建一个单独的容器[pytest]以从那里发送测试.但是测试不会运行导致这个容器没有xvfb.[编辑:我刚刚安装了xvfb并且工作]
>我尝试使用pytest容器作为集线器的卷,但仍然存在从已经运行服务的集线器容器运行测试的问题.[编辑:为了开发目的,我从pytest容器安装代码并使用其控制台测试其他容器上的东西.之后我将使用pytest容器将代码分开]

我应该在其他容器中安装该节点容器所有的所有软件包吗?理想情况下,我想使用pytest容器控制台[至少开发基础设施]来运行将发送到集线器并运行到许多节点的测试.[编辑:我没有与集线器和节点混在一起,我刚安装pytest容器上的xvfb]

你会怎么建议这样做?我错过了什么吗?

编辑[解决方案的步骤]:

我首先启动pytest容器[docker run -dit -v / org / org_QA_folder –name pytest org / grid:py]然后我运行docker-compose up -d来启动网格

在compose文件中,我使用external_links和volumes_from用于开发目的.设置完成后,我无法从任何容器内部工作.

我更改了Pytest dockerfile并添加了这个:

# Install xvfb [THAT WAS NEEDED!!!!!]
RUN apt-get install -y xvfb

我运行我的一个测试,从pytest容器内部到我已经运行的Grid [在EC2实例上]作为概念证明并且工作正常.它仍然是发送到dockerized网格.

最佳答案 我想你差不多了.

我会在docker-compose.yml中添加一个pytest服务,并从中心图像中删除volumes_from和external_links(这样它看起来更像是你链接的博客文章中的例子).

pytest服务将运行python代码,但它应连接到集线器以运行selenium.我相信org_QA_folder中的某个地方有配置试图在本地启动selenium.需要重新配置它以使用中心服务器.

点赞