使用Selenium和BrowserStack进行分布式自动浏览器测试

本文介绍了如何使用BrowserStack作为Selenium云服务,进行分布式自动浏览器测试。作者强调BrowserStack的便利性,如其跨平台和多浏览器支持,以及与Visual Studio的集成。文章还提及了使用Python进行Selenium测试,并提供了设置和运行测试的基本步骤。
摘要由CSDN通过智能技术生成

I'm a huge fan of BrowserStack. They are a cloud-based browser testing service that lets you remote into effectively any browser version on any version of any operating system. They've even got Visual Studio integration so you can just hit F5, choose the browser you want, and start debugging.

我是BrowserStack的忠实粉丝。 它们是基于云的浏览器测试服务,可让您有效地远程访问任何操作系统的任何版本上的任何浏览器版本。 他们甚至已经集成Visual Studio,因此您只需按F5键,选择所需的浏览器,然后开始调试即可。

COUPON: BrowserStack is a service (a cloud of machines do the work) but you can get 3 months free from the IE folks at Modern.ie. To be clear: I'm not affiliated with either of these folks, just letting you know that it's cool. I do personally have a BrowserStack subscription that I pay for.

优惠券: BrowserStack是一项服务(由机器组成的云可以完成工作),但是您可以从Modern.ie的IE人员那里免费获得3个月的服务需要说明的是:我与这些人都不隶属,只是让您知道它很酷。 我个人确实有一个付费的BrowserStack订阅。

I've long been a proponent of integration testing websites by putting the browser on a string, like a puppet. I used Watir (Web Application Testing in Ruby) almost 10 (!) years ago, and write WatirMaker (and later WatirRecorder) to make that easier.

通过将浏览器放在字符串(如木偶),我一直是集成测试网站的支持者。 大约10年前,我使用了Watir(Ruby中的Web应用程序测试),并编写了WatirMaker (以及后来的WatirRecorder )使其变得更容易。

I also spent time with Selenium as early as 2007, although it was a very different Selenium than it is today. I also interviewed Jim Evans from the Selenium Team on my podcast.

我也曾在2007年Selenium一起度过时光,尽管它与今天的Selenium截然不同。 我还通过播客采访了Selenium团队的Jim Evans

BrowserStack作为具有RemoteDriver的Selenium云 (BrowserStack as a Selenium Cloud with RemoteDriver)

Selenium today uses a "Selenium Server" and a "WebDriver." WebDrivers are language-specific bindings to drive a browser - to put strings on your browser and control it.

Selenium今天使用“ Selenium服务器”和“ WebDriver”。 WebDrivers是特定于语言的绑定,用于驱动浏览器-在浏览器中放置字符串并对其进行控制。

Now, there's dozens of choices to make and dozens of ways you can get Selenium working. With much respect due to the Selenium team, the docs on their main page spend a LOT of time talking about different versions, older versions, history of the project, and there's no real "Getting Started FAQ" with stuff like "I'm a Windows person, get this and this and that." Or "I'm on a Mac and like Ruby, just get this and this." There is a fairly extensive Wiki, though, but it's still a lot of terms to understand.

现在,有数十种选择可供选择,并且有数十种方法可以使Selenium工作。 出于对Selenium团队的敬意,他们主页上的文档花费了大量时间讨论不同的版本,较旧的版本,项目的历史,并且没有诸如“我是Windows的人,得到这个,那个,那个。” 或“我在Mac上,并且像Ruby,只要得到这个就可以了。” 虽然有相当广泛的Wiki ,但是仍然有很多术语需要理解。

Do expect to spend a few hours exploring and messing up, but do know that there's a few dozen ways to be successful with Selenium, which is part of its charm.

不要指望花上几个小时探索和搞乱了,知道有几十个方法是成功的Selenium,这就是它的魅力的一部分。

First, you can write tests in whatever language you like. So, C#/NUnit, or Ruby, or Python, for example.

首先,您可以使用任何喜欢的语言编写测试。 因此,例如C#/ NUnit或Ruby或Python

You can download "drivers" for a browser and run them directly, having them listen a port, and make them available to yourself or others in your company.

您可以下载浏览器的“驱动程序”并直接运行它们,让它们侦听端口,并将其提供给您自己或您公司中的其他人。

When writing tests, you can ask for browsers ("drivers") directly by asking for them from your test language of choice, like instantiating a "ChromeDriver" or an "IEDriver."

编写测试时,您可以通过选择测试语言来直接询问浏览器(“驱动程序”),例如实例化“ ChromeDriver”或“ IEDriver”。

But, you can also launch a more general-purpose server that will present itself as a "WebDriver," then you give it a list of the capabilities you want and it will then find and drive a browser for you. This is what BrowserStack's cloud does. You can also set these up inside your own company, although it's a bit of a hassle.

但是,您还可以启动一个更通用的服务器,该服务器将自己显示为“ WebDriver”,然后为其提供所需功能列表,然后它将为您查找并驱动浏览器。 这就是BrowserStack的云所做的。 您也可以在自己的公司内部进行设置,尽管这有点麻烦。

There's 8 different OSes. 20 mobile devices, 300 browser/version combos. For the free automated trial you get 100 minutes of "drive time" free. Also, it's half the price if you only want desktop browsers.

有8种不同的操作系统。 20个移动设备,300个浏览器/版本组合。 对于免费的自动试用,您可以免费获得100分钟的“行驶时间”。 此外,如果您只需要台式机浏览器,则价格仅为一半。

The Web Interface for BrowserStack showing the tests I've run

I like to use Python for Selenium Tests, for some odd reason. Again, you can use whatever you like. Doesn't matter.

由于某些奇怪的原因,我喜欢将Python用于Selenium测试。 同样,您可以使用任何您喜欢的东西。 没关系

If you don't have Python...

如果您没有Python ...

  • Get Python 2.7 - I'm on Windows and I got the x86 one.

    获得Python 2.7-我在Windows上,并且得到了x86。

  • Get setuptools for 2.7 - note the py2.7 in the file name.

    获取setuptools的2.7 -注意在文件名中py2.7。

  • Get the latest Pip for 2.7 - Note the py2.7 in the file name.

    获取最新的2.7点子-注意文件名中的py2.7。

  • Then run "pip install -U selenium"

    然后运行“ pip install -U selenium”

Your Python code might look like this. Here I'm using BrowserStack's cloud and asking for IE7 on Windows XP.

您的Python代码可能如下所示。 在这里,我正在使用BrowserStack的云,并要求在Windows XP上使用IE7。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

desired_cap = {'os': 'Windows',
'os_version': 'xp',
'browser': 'IE',
'browser_version': '7.0',
'browserstack.debug': 'true' }

driver = webdriver.Remote(
command_executor='http://hanselman:mysecretkey@hub.browserstack.com:80/wd/hub',
desired_capabilities=desired_cap)

driver.get("http://www.google.com")
if not "Google" in driver.title:
raise Exception("Unable to load google page!")
elem = driver.find_element_by_name("q")
elem.send_keys("Hanselman")
elem.submit()
print driver.title
driver.quit()

Note I have "browserstack.debug" on, so I can actually go to the BrowserStack site and see screenshots of each step!

注意,我已经打开了“ browserstack.debug”,因此我实际上可以转到BrowserStack站点,并查看每个步骤的屏幕截图!

Screenshot of BrowserStack automatically typing Hanselman into Google on IE7 on XP

Here's the next step...

这是下一步...

The results of the Google Search for Hanselman

Details on how this automation works are all up at https://www.browserstack.com/automate. Again you can use any language you want. Here's the same thing in C#:

有关此自动化工作方式的详细信息,请访问https://www.browserstack.com/automate 。 同样,您可以使用任何想要的语言。 这是C#中的同一件事:

using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;

namespace SeleniumTest {
class Program {
static void Main(string[] args) {
IWebDriver driver;
DesiredCapabilities capability = DesiredCapabilities.Firefox();
capability.SetCapability("browserstack.user", "hanselman");
capability.SetCapability("browserstack.key", "superfancypantssecretapikey");

driver = new RemoteWebDriver(
new Uri("http://hub.browserstack.com/wd/hub/"), capability
);
driver.Navigate().GoToUrl("http://www.google.com/ncr");
Console.WriteLine(driver.Title);

IWebElement query = driver.FindElement(By.Name("q"));
query.SendKeys("hanselman");
query.Submit();
Console.WriteLine(driver.Title);

driver.Quit();
}
}
}

If your site is only available at localhost, you can make a temporary tunnel and make is accessible to BrowserStack, as well. If you've got Chrome or Firefox, there are extensions to make this even easier.

如果您的站点仅在localhost可用,则您可以建立一个临时隧道,并且使BrowserStack也可以访问该站点。 如果您使用的是Chrome或Firefox,可以通过一些扩展程序来简化操作。

I hope you check out both BrowserStack and Selenium, and perhaps consider how you're testing your site today (humans who click?) and if you could be more effective with different tools?

我希望您同时查看BrowserStack和Selenium,也许考虑一下您今天如何测试您的网站(点击的人?),以及如果使用其他工具可以更有效?

翻译自: https://www.hanselman.com/blog/distributed-automated-browser-testing-with-selenium-and-browserstack

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值