使用 Pytest、Selenium Grid 和 Docker 创建测试基础设施
Creating a testing infrastructure with Pytest , Selenium Grid and Docker
基于此article,我成功创建了可扩展的 selenium 网格。
然后我想 运行 我的测试套件 [用 Python] 使用 Pytest 进入那个网格。
我是 Docker 的新手,正在尝试找到将我的测试程序迁移到微服务架构中的最佳方法。本质上,我想让开发人员能够在他们的 PC 上本地设置完整的测试基础设施。
所以,我有 4 个 Docker 文件 和 1 个 docker-compose.yml 。
基础docker文件:
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 docker文件:
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"]
节点docker文件:
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 docker文件:
# 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 中克隆代码。但在这种情况下,HUB 容器 运行 是网格的中心 [每个容器指令一个 CMD]。 [编辑:我按照 Docker 建议选择了微服务架构]
我将集线器和节点容器留给 运行 它们指定的服务,并创建一个单独的容器 [pytest] 从那里发送测试。但是测试不会 运行 因为这个容器没有 xvfb。[编辑:我刚刚安装了 xvfb 并且工作了]
我尝试使用 pytest 容器作为集线器的卷,但仍然存在 运行 从已经有服务的集线器容器中测试的问题 运行ning .[编辑:出于开发目的,我从 pytest 容器安装代码并使用其控制台在其他容器上测试内容。之后我将使用pytest容器将代码分开]
我应该在其他容器中安装那个节点容器的所有包吗?理想情况下,我想使用 pytest 容器控制台 [至少开发基础设施] 运行 将发送到集线器和 运行 许多节点的测试。[编辑:我没有没有和hub和node混在一起,我只是在pytest容器上安装了xvfb]
您建议如何做到这一点?我错过了什么吗?
编辑[解决方案的步骤]:
我首先启动 pytest 容器 [docker run -dit -v /org/org_QA_folder --name pytest org/grid:py
] 然后我 运行 docker-compose up -d
启动网格
在撰写文件中,我使用 external_links
和 volumes_from
用于开发目的。安装完成后,我不会在任何容器内工作。
我更改了 Pytest docker 文件并添加了这个:
# Install xvfb [THAT WAS NEEDED!!!!!]
RUN apt-get install -y xvfb
我 运行 我的一个测试,从 pytest 容器内部,到我已经 运行ning Grid [在 EC2 实例上] 作为概念证明并且工作正常。它仍然是将它发送到 docker 化的网格。
我想你快到了。
我会向 docker-compose.yml
添加 pytest
服务,并从 hub
图像中删除 volumes_from
和 external_links
(这样它看起来更像是您链接的博客 post 中的示例。
pytest
服务将 运行 python 代码,但它应该连接到 运行 selenium 的集线器。我相信在 org_QA_folder
的某处有试图在本地启动 selenium 的配置。它需要重新配置以改为使用 hub
服务器。
我终于让它像这样工作了:
在我的代码中,我使用了这个:
class Fixtures_docker(unittest.TestCase):
def setUp(self, hub_ip="http://HUB_CONTAINER_IP", hub_port=4444):
print "--Fixtures_docker in config.py trying to run on--: %s:%s"%(hub_ip, hub_port)
if platform.system() == 'Linux':
from pyvirtualdisplay import Display
self.display = Display(visible=0, size=(1024, 768))
self.display.start()
if browser == "Firefox":
self.wd = webdriver.Remote(
command_executor=hub_ip + ':' + str(hub_port) + '/wd/hub',
desired_capabilities={
#FIREFOX
"browserName" : "firefox",
# "maxInstances" : 10,
"seleniumProtocol": "WebDriver",
"platform" : "ANY",
"node" : hub_port
})
# for debugging purposes from local machine
elif browser == "Firefox" and os == "Windows":
self.wd = webdriver.Remote(
command_executor=hub_ip + ':' + str(hub_port) + '/wd/hub',
desired_capabilities={
# FIREFOX -> WINDOWS
# "firefox_binary":"C:\firefox3\Firefox3.exe", # path to find another version of ff
"browserName" : "firefox",
#"version" : "3.0b3", # trying to run a specific ff version
"platform" : "WINDOWS",
"node" : hub_port,
"maxSession" : 5
})
self.wd.implicitly_wait(10)
def tearDown(self):
self.wd.quit()
if platform.system() == 'Linux':
self.display.stop()
并且在 docker-compose.yml:
hub:
image: org/grid:hub # image must already exist
ports:
- "8080:4444" # HOST:CONTAINER
external_links: # link to a container created outside of this YAML file
- pytest
volumes_from:
- pytest # must be created first
expose:
- "4444" # intercontainer communication [other containers with selenium server]
firefox:
image: org/grid:nodeff # image must already exist
links:
- hub
expose:
- "5555" # intercontainer communication [node registers to hub]
基于此article,我成功创建了可扩展的 selenium 网格。 然后我想 运行 我的测试套件 [用 Python] 使用 Pytest 进入那个网格。
我是 Docker 的新手,正在尝试找到将我的测试程序迁移到微服务架构中的最佳方法。本质上,我想让开发人员能够在他们的 PC 上本地设置完整的测试基础设施。
所以,我有 4 个 Docker 文件 和 1 个 docker-compose.yml 。
基础docker文件:
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 docker文件:
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"]
节点docker文件:
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 docker文件:
# 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 中克隆代码。但在这种情况下,HUB 容器 运行 是网格的中心 [每个容器指令一个 CMD]。 [编辑:我按照 Docker 建议选择了微服务架构]
我将集线器和节点容器留给 运行 它们指定的服务,并创建一个单独的容器 [pytest] 从那里发送测试。但是测试不会 运行 因为这个容器没有 xvfb。[编辑:我刚刚安装了 xvfb 并且工作了]
我尝试使用 pytest 容器作为集线器的卷,但仍然存在 运行 从已经有服务的集线器容器中测试的问题 运行ning .[编辑:出于开发目的,我从 pytest 容器安装代码并使用其控制台在其他容器上测试内容。之后我将使用pytest容器将代码分开]
我应该在其他容器中安装那个节点容器的所有包吗?理想情况下,我想使用 pytest 容器控制台 [至少开发基础设施] 运行 将发送到集线器和 运行 许多节点的测试。[编辑:我没有没有和hub和node混在一起,我只是在pytest容器上安装了xvfb]
您建议如何做到这一点?我错过了什么吗?
编辑[解决方案的步骤]:
我首先启动 pytest 容器 [docker run -dit -v /org/org_QA_folder --name pytest org/grid:py
] 然后我 运行 docker-compose up -d
启动网格
在撰写文件中,我使用 external_links
和 volumes_from
用于开发目的。安装完成后,我不会在任何容器内工作。
我更改了 Pytest docker 文件并添加了这个:
# Install xvfb [THAT WAS NEEDED!!!!!]
RUN apt-get install -y xvfb
我 运行 我的一个测试,从 pytest 容器内部,到我已经 运行ning Grid [在 EC2 实例上] 作为概念证明并且工作正常。它仍然是将它发送到 docker 化的网格。
我想你快到了。
我会向 docker-compose.yml
添加 pytest
服务,并从 hub
图像中删除 volumes_from
和 external_links
(这样它看起来更像是您链接的博客 post 中的示例。
pytest
服务将 运行 python 代码,但它应该连接到 运行 selenium 的集线器。我相信在 org_QA_folder
的某处有试图在本地启动 selenium 的配置。它需要重新配置以改为使用 hub
服务器。
我终于让它像这样工作了:
在我的代码中,我使用了这个:
class Fixtures_docker(unittest.TestCase):
def setUp(self, hub_ip="http://HUB_CONTAINER_IP", hub_port=4444):
print "--Fixtures_docker in config.py trying to run on--: %s:%s"%(hub_ip, hub_port)
if platform.system() == 'Linux':
from pyvirtualdisplay import Display
self.display = Display(visible=0, size=(1024, 768))
self.display.start()
if browser == "Firefox":
self.wd = webdriver.Remote(
command_executor=hub_ip + ':' + str(hub_port) + '/wd/hub',
desired_capabilities={
#FIREFOX
"browserName" : "firefox",
# "maxInstances" : 10,
"seleniumProtocol": "WebDriver",
"platform" : "ANY",
"node" : hub_port
})
# for debugging purposes from local machine
elif browser == "Firefox" and os == "Windows":
self.wd = webdriver.Remote(
command_executor=hub_ip + ':' + str(hub_port) + '/wd/hub',
desired_capabilities={
# FIREFOX -> WINDOWS
# "firefox_binary":"C:\firefox3\Firefox3.exe", # path to find another version of ff
"browserName" : "firefox",
#"version" : "3.0b3", # trying to run a specific ff version
"platform" : "WINDOWS",
"node" : hub_port,
"maxSession" : 5
})
self.wd.implicitly_wait(10)
def tearDown(self):
self.wd.quit()
if platform.system() == 'Linux':
self.display.stop()
并且在 docker-compose.yml:
hub:
image: org/grid:hub # image must already exist
ports:
- "8080:4444" # HOST:CONTAINER
external_links: # link to a container created outside of this YAML file
- pytest
volumes_from:
- pytest # must be created first
expose:
- "4444" # intercontainer communication [other containers with selenium server]
firefox:
image: org/grid:nodeff # image must already exist
links:
- hub
expose:
- "5555" # intercontainer communication [node registers to hub]