查看: 5324|回复: 21

Getting Started with Selenium Grid

[复制链接]
认证徽章
论坛徽章:
1054
紫蜘蛛
日期:2015-09-22 15:53:22紫蜘蛛
日期:2015-10-15 13:48:52紫蜘蛛
日期:2015-10-15 14:45:48紫蜘蛛
日期:2015-10-15 14:47:47紫蜘蛛
日期:2015-10-15 14:48:45九尾狐狸
日期:2015-09-22 15:53:22九尾狐狸
日期:2015-10-15 13:50:37九尾狐狸
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:47:47九尾狐狸
日期:2015-10-15 14:48:45
发表于 2010-12-23 17:21 | 显示全部楼层 |阅读模式
In this article, by David Burns, author of Selenium 1.0 Testing Tools, we are going to have a look at what Selenium Grid is and how we can set it up in different environments, including how to set it up on Amazon EC2. This will abstract the topography of where the tests are located so that your tests only have to worry about one address.
In this article we shall cover the following topics:
  • Setting up the Selenium Grid Hub
  • Setting up the Selenium Grid Remote Controls
  • Creating tests for the Grid
So let's get on with it...
认证徽章
论坛徽章:
1054
紫蜘蛛
日期:2015-09-22 15:53:22紫蜘蛛
日期:2015-10-15 13:48:52紫蜘蛛
日期:2015-10-15 14:45:48紫蜘蛛
日期:2015-10-15 14:47:47紫蜘蛛
日期:2015-10-15 14:48:45九尾狐狸
日期:2015-09-22 15:53:22九尾狐狸
日期:2015-10-15 13:50:37九尾狐狸
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:47:47九尾狐狸
日期:2015-10-15 14:48:45
发表于 2010-12-23 17:22 | 显示全部楼层
Important preliminary points

For this section you will need to have Apache Ant on the machine that you are going to have running Grid instances. You can get this from http://ant.apache.org/bindownload.cgi for Windows and Mac. If you have Ubuntu you can simply do sudo apt-get install ant1.8, which will install all the relevant items that are needed onto your Linux machine. You will also have to download the latest  Selenium Grid from http://selenium-grid.seleniumhq.org/download.html.

使用道具 举报

回复
认证徽章
论坛徽章:
1054
紫蜘蛛
日期:2015-09-22 15:53:22紫蜘蛛
日期:2015-10-15 13:48:52紫蜘蛛
日期:2015-10-15 14:45:48紫蜘蛛
日期:2015-10-15 14:47:47紫蜘蛛
日期:2015-10-15 14:48:45九尾狐狸
日期:2015-09-22 15:53:22九尾狐狸
日期:2015-10-15 13:50:37九尾狐狸
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:47:47九尾狐狸
日期:2015-10-15 14:48:45
发表于 2010-12-23 17:24 | 显示全部楼层
Understanding Selenium Grid

Selenium Grid is a version of Selenium that allows teams to set up a number of Selenium instances and then have one central point to send your Selenium commands to. This differs from what we saw in Selenium Remote Control (RC)  where we always had to explicitly say where the Selenium RC is as well as know what browsers that Remote Control can handle.

With Selenium Grid, we just ask for a specific browser, and then the hub that is part of Selenium Grid will route all the Selenium commands through to the Remote Control you want.

0264-09-01.png

Selenium Grid also allows us to, with the help of the configuration file, assign friendly names to the Selenium RC instances so that when the tests want to run against Firefox on Linux, the hub will find a free instance and then route all the Selenium Commands from your test through to the instance that is registered with that environment. We can see an example of this in the next diagram.

0264-09-02.png

We will see how to create tests for this later in the chapter, but for now let's have a look at making sure we have all the necessary items ready for the grid.

使用道具 举报

回复
认证徽章
论坛徽章:
1054
紫蜘蛛
日期:2015-09-22 15:53:22紫蜘蛛
日期:2015-10-15 13:48:52紫蜘蛛
日期:2015-10-15 14:45:48紫蜘蛛
日期:2015-10-15 14:47:47紫蜘蛛
日期:2015-10-15 14:48:45九尾狐狸
日期:2015-09-22 15:53:22九尾狐狸
日期:2015-10-15 13:50:37九尾狐狸
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:47:47九尾狐狸
日期:2015-10-15 14:48:45
发表于 2010-12-23 17:26 | 显示全部楼层
Checking that we have the necessary items for Selenium Grid

Now that you have downloaded Selenium Grid and Ant, it is always good to run a sanity check on Selenium Grid to make sure that we are ready to go. To do this we run a simple command in a console or Command Prompt.
Let's see this in action.

Time for action – doing a sanity check on Selenium Grid
  • Open a Command Prompt or console window.
  • Run the command ant sanity-check. When it is complete you should see something similar to the next screenshot:

    0264-09-03.png
What just happened?

We have just checked whether we have all the necessary items to run Selenium Grid. If there was something that Selenium relied on, the sanity check script would output what was needed so that you could easily correct this. Now that everything is ready, let us start setting up the Grid.

使用道具 举报

回复
认证徽章
论坛徽章:
1054
紫蜘蛛
日期:2015-09-22 15:53:22紫蜘蛛
日期:2015-10-15 13:48:52紫蜘蛛
日期:2015-10-15 14:45:48紫蜘蛛
日期:2015-10-15 14:47:47紫蜘蛛
日期:2015-10-15 14:48:45九尾狐狸
日期:2015-09-22 15:53:22九尾狐狸
日期:2015-10-15 13:50:37九尾狐狸
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:47:47九尾狐狸
日期:2015-10-15 14:48:45
发表于 2010-12-23 17:30 | 显示全部楼层
Selenium Grid Hub

Selenium Grid works by having a central point that tests can connect to, and commands are then pushed to the Selenium Remote Control instances connected to that hub. The hub has a web interface that tells you about the Selenium Remote Control instances that are connected to the Hub, and whether they are currently in use.

Time for action – launching the hub

Now that we are ready to start working with Selenium Grid we need to set up the Grid.    This is a simple command that we run in the console or Command Prompt.

  • Open a Command Prompt or console window.
  • Run the command ant launch-hub. When that happens you should see something similar to the following screenshot:
    0264-09-04.png

We can see that this is running in the command prompt or console. We can also see the hub running from within a browser.

If we put http://nameofmachine:4444/console where nameofmachine is the name of the machine with the hub. If it is on your machine then you can place http://localhost:4444/console. We can see that in the next screenshot:

0264-09-05.png

What just happened?
We have successfully started Selenium Grid Hub. This is the central point of our tests and Selenium Grid instances. We saw that when we start Selenium Grid it showed us what items were available according to the configuration file that is with the normal install.
We then had a look at how we can see what the Grid is doing by having a look at the hub in a browser. We did this by putting the URL http://nameofmachine:4444/console where nameofmachine is the name of the machine that we would like to access with the hub. It shows what configured environments the hub can handle, what grid instances are available and which instances are currently active.

Now that we have the hub ready we can have a look at starting up instances.

使用道具 举报

回复
认证徽章
论坛徽章:
1054
紫蜘蛛
日期:2015-09-22 15:53:22紫蜘蛛
日期:2015-10-15 13:48:52紫蜘蛛
日期:2015-10-15 14:45:48紫蜘蛛
日期:2015-10-15 14:47:47紫蜘蛛
日期:2015-10-15 14:48:45九尾狐狸
日期:2015-09-22 15:53:22九尾狐狸
日期:2015-10-15 13:50:37九尾狐狸
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:47:47九尾狐狸
日期:2015-10-15 14:48:45
发表于 2010-12-24 08:11 | 显示全部楼层
Adding instances to the hub

Now that we have successfully started the Selenium Grid Hub, we will need to have a look at how we can start adding Selenium Remote Controls to the hub so that it starts forming the grid of computers that we are expecting. As with everything in Selenium Grid, we need Ant to start the instances that connect. In the next few Time for action sections we will see the different arguments needed to start instances to join the grid.

Time for action – adding a remote control with the defaults

In this section we are going to launch Selenium Remote Control and get it to register with the hub. We are going to assume that the browser you would like it to register for is Firefox, and the hub is on the same machine as the Remote Control. We will pass in only one required argument, which is the port that we wish it to run on. However, when starting instances, we will always need to pass in the port since Selenium cannot work out if there are any free ports on the host machine.

  • Open a Command Prompt or console window.
  • Enter the command ant –Dport=5555 launch-remote-control and press Return. You should see the following in your Command Prompt or console:
    0264-09-06.png
  • And this in the Selenium Grid Hub site:
    0264-09-07.png

What just happened?

We have added the first machine to our own Selenium Grid. It has used all the defaults that are in the Ant build script and it has created a Selenium Remote Control that will take any Firefox requests, located on the same machine as the host of Selenium Remote Control Grid. This is a useful way to set up the grid if you just want a large number of Firefox-controlling Selenium Remote Controls.

使用道具 举报

回复
认证徽章
论坛徽章:
1054
紫蜘蛛
日期:2015-09-22 15:53:22紫蜘蛛
日期:2015-10-15 13:48:52紫蜘蛛
日期:2015-10-15 14:45:48紫蜘蛛
日期:2015-10-15 14:47:47紫蜘蛛
日期:2015-10-15 14:48:45九尾狐狸
日期:2015-09-22 15:53:22九尾狐狸
日期:2015-10-15 13:50:37九尾狐狸
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:47:47九尾狐狸
日期:2015-10-15 14:48:45
发表于 2010-12-24 08:13 | 显示全部楼层
Adding Selenium Remote Controls for different machines

Selenium Grid is most powerful when you can add it to multiple operating systems. This allows us to check, for instance, whether Firefox on Windows and Firefox on Linux is doing the same thing during a test. To register new remote controls to the grid from a machine other than the one hosting the hub, we need to tell it where the hub is. We do this by passing in the   –DhubURL argument when calling the Ant script. We also need to pass in the –Dhost argument  with the name of the machine so that we can see where it is being hosted.

Let's see this in action.

Time for action – adding Selenium Remote Controls for different machines

For this Time for action you will need to have another machine available for you to use. This could be the Ubuntu machine that you needed for the previous chapter. I suggest giving the –Dhost argument the name of the machine that it is running on. If you have a small Grid then you can name them according to the operating system that it is run on.
  • Open a Command Prompt or console.
  • Run the command ant –Dport=9999 –DhubURL=http://nameofmachine: port –Dhost=nameofcurrentmachine launch-remote-control.
  • When you have run this, your Grid site should appear as follows:
    0264-09-08.png


What just happened?

We have added a new remote control to the grid from a machine other than where the Selenium Grid Hub is running. This is the first time that we have been able to set up our remote control instances in a grid. We learnt about the  –DhubURL argument and the  –Dhost argument that is needed when launching the remote control. We then saw that it has updated the Grid site that is running on the hub.

Now that we have this working as we expect, let us have a look at setting up browsers other than Firefox.

使用道具 举报

回复
认证徽章
论坛徽章:
1054
紫蜘蛛
日期:2015-09-22 15:53:22紫蜘蛛
日期:2015-10-15 13:48:52紫蜘蛛
日期:2015-10-15 14:45:48紫蜘蛛
日期:2015-10-15 14:47:47紫蜘蛛
日期:2015-10-15 14:48:45九尾狐狸
日期:2015-09-22 15:53:22九尾狐狸
日期:2015-10-15 13:50:37九尾狐狸
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:47:47九尾狐狸
日期:2015-10-15 14:48:45
发表于 2010-12-24 08:15 | 显示全部楼层
Adding Selenium Remote Control for different browsers

Selenium Grid is extremely powerful when we start using different browsers on the grid, since we can't run all the different browsers on a single machine due to operating systems and browser combinations. There are currently up to nine different combinations that are used by most people, so getting Selenium Grid to help with this can give you the test coverage that you need. To do this we pass in the   –Denvironment argument in our Ant call. The value that we assign to this has to be Selenium Grid configuration. The Selenium Grid configuration comes with a number of preset items. This is visible from the Selenium Grid Hub page that we have seen already. Let us now see how we can set the items.

Time for action – setting the Environment when starting Selenium Remote Control

Now that we need to get Internet Explorer Selenium Remote controls added to our grid. We have to add the –Denvironment argument to our call with the target on the configured environments. Since we want an Internet Explorer remote control we can use the IE on Windows targets.
  • Open a console or Command Prompt window.
  • Run the command ant –Dport=9998 –DhubURL=addressofhub –Dhost=nameofremotehost –Denvir launch-remote-control.
  • When it is running, your hub page should appear as follows:
    0264-09-09.png


What just happened?

We have just seen how we can create more verbose environment names such as "IE on Windows". We also saw how we can start a remote control for different browsers. This is quite useful when we need to test a large amount of browser and operating system combinations.

使用道具 举报

回复
认证徽章
论坛徽章:
1054
紫蜘蛛
日期:2015-09-22 15:53:22紫蜘蛛
日期:2015-10-15 13:48:52紫蜘蛛
日期:2015-10-15 14:45:48紫蜘蛛
日期:2015-10-15 14:47:47紫蜘蛛
日期:2015-10-15 14:48:45九尾狐狸
日期:2015-09-22 15:53:22九尾狐狸
日期:2015-10-15 13:50:37九尾狐狸
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:47:47九尾狐狸
日期:2015-10-15 14:48:45
发表于 2010-12-24 08:22 | 显示全部楼层
Updating the Selenium Grid Configuration

The Configured Environments come with a standard installation but there are times where it would be useful to set up your own targets. This could be when browsers that used to work on a specific operating system now work on multiple operating systems. We have already seen this happen with Google Chrome.

To update the configuration we need to have a look at the grid_configuration.yml. This is a YAML file that contains all the configurations that Selenium grid has. We need to add a new name and browser for new items. For example:
  1. -name: "Google Chrome on Windows"
  2. Browser: "*googlechrome"
复制代码


Time for action – adding new items to the Grid Configuration

As we saw from the default configuration on that is distributed, it doesn't have the different flavors of Google Chrome. We can add this to the file this time so we can extend the coverage that we need.
  • Open grid_configuration.yml in a text editor.
    Add the following to the file:
    1. -name: "Google Chrome on Windows"
    2. Browser: "*googlechrome"
    复制代码
  • Start the hub.
  • In another console or Command Prompt, run the newly created item. While running, your Grid should appear as follows:
    0264-09-10.png

What just happened?

We have just added our first item to the Grid Configuration. This could be renaming an item so that it makes a lot more sense, or if there is something missing that is needed. We can give each of the items meaningful names that then point to a specific browser. We can also use this to set up custom browsers if need be.

使用道具 举报

回复
认证徽章
论坛徽章:
1054
紫蜘蛛
日期:2015-09-22 15:53:22紫蜘蛛
日期:2015-10-15 13:48:52紫蜘蛛
日期:2015-10-15 14:45:48紫蜘蛛
日期:2015-10-15 14:47:47紫蜘蛛
日期:2015-10-15 14:48:45九尾狐狸
日期:2015-09-22 15:53:22九尾狐狸
日期:2015-10-15 13:50:37九尾狐狸
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:47:47九尾狐狸
日期:2015-10-15 14:48:45
发表于 2010-12-24 08:23 | 显示全部楼层
Pop quiz – doing the thing
  • What is the command required to start the Hub?
  • What is the URL where one can see what is happening on the Grid?
  • How do you specify the port that the Remote Control is running on?
  • How do you specify which browser you would like the Remote Control to be registered with?

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 
京ICP备09055130号-4  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表