使用Selenium 2的IWebDriver与页面上的元素进行交互 [英] Using Selenium 2's IWebDriver to interact with elements on the page
问题描述
我使用Selenium的 IWebDriver
来写C#单元测试
就是这样一个例子:
IWebDriver defaultDriver =新InternetExplorerDriver();
VAR DDL = driver.FindElements(By.TagName(选择));
最后一行检索选择
HTML元素包裹在一个 IWebElement
。
我需要一种方法来模拟选择一个特定的选项
在选择
列表,但我无法弄清楚如何做到这一点。
当一些的研究,我发现那里的人都使用 ISelenium DefaultSelenium
类来完成以下的例子,但我不利用这个类,因为我与 IWebDriver
做的一切和 INavigation
(从 defaultDriver .Navigate()
)。
我也注意到, ISelenium DefaultSelenium
包含一吨的不在 IWebDriver
的具体实现可用其他方法。
那么,有没有办法,我可以使用 IWebDriver
和 INavigation
会同 ISelenium DefaultSelenium
<? / p>
由于ZloiAdun提到,有一个在OpenQA.Selenium.Support.UI命名空间中一个可爱的新选择类。这就是访问一个选择元件的最好方法之一,它的选择,因为它的API是那么容易。比方说,你已经有了一个网页,看起来像这样
<!DOCTYPE HTML>
< HEAD>
<标题>一次性页< /标题>
< /头>
<身体GT;
<选择一个id =选择>
<期权价值=沃尔沃>沃尔沃< /选项>
<期权价值=萨博>萨博< /选项>
<期权价值=奔驰>奔驰< /选项>
<期权价值=奥迪>奥迪< /选项>
< /选择>
< /身体GT;
< / HTML>
你代码访问的选择是这样的。请注意我是如何通过传递一个正常IWebElement它的构造函数创建选择对象。你有足够的选择对象的方法。 看看。在源了解更多信息,直到它得到正确记录
使用OpenQA.Selenium.Support.UI;
使用OpenQA.Selenium;
使用System.Collections.Generic;
使用OpenQA.Selenium.IE;
命名空间Selenium2
{
类SelectExample
{
公共静态无效的主要(字串[] args)
{
IWebDriver司机=新InternetExplorerDriver();
driver.Navigate()GoToUrl(www.example.com);
//注意这里怎么是我在传递一个正常IWebElement到选择
//构造
选择选择=新选择(driver.FindElement(By.Id(选择)));
&IList的LT; IWebElement>选项= select.GetOptions();
的foreach(期权IWebElement选项)
{
的System.Console.WriteLine(option.Text);
}
select.SelectByValue(奥迪);
//这是只有在这里让你有时间看输出和
System.Console.ReadLine();
driver.Quit();
}
}
}
一对夫妇事情需要注意有关支持类但是。即使你下载了最新的测试版,支持DLL不会在那里。支持包在Java库一个比较长的历史(这就是生活PageObject),但它仍然在净司机很新鲜。幸运的是,它真的很容易从源代码编译。我从SVN拉则引用从公测下载WebDriver.Common.dll和以C#速成2008年此类尚未很好地测试一些其他类的,但我的例子在Internet Explorer和Firefox的工作。
有一个几根据您的代码,我要指出其他的事情上面。首先,你用找到选择元素的行
driver.FindElements(By.TagName(选择));
是要找到全部选择元素。你应该使用 driver.FindElement
,如果没有的。
此外,很少会使用INavigation直。你会做大部分的导航像 driver.Navigate()GoToUrl(http://example.com);
最后, DefaultSelenium
是访问旧硒的1.x的API的方式。 2硒是硒1相当显著背离,所以,除非你想老的测试迁移到新的硒2 API(通常简称的webdriver API)你不会使用DefaultSelenium。
I'm using Selenium's IWebDriver
to write Unit Tests in C#.
Such is an example:
IWebDriver defaultDriver = new InternetExplorerDriver();
var ddl = driver.FindElements(By.TagName("select"));
The last line retrieves the select
HTML element wrapped in a IWebElement
.
I need a way to simulate selection to a specific option
in that select
list but I can't figure out how to do it.
Upon some research, I found examples where people are using the ISelenium DefaultSelenium
class to accomplish the following, but I am not making use of this class because I'm doing everything with IWebDriver
and INavigation
(from defaultDriver.Navigate()
).
I also noticed that ISelenium DefaultSelenium
contains a ton of other methods that aren't available in the concrete implementations of IWebDriver
.
So is there any way I can use IWebDriver
and INavigation
in conjunction with ISelenium DefaultSelenium
?
As ZloiAdun mentions, there is a lovely new Select class in the OpenQA.Selenium.Support.UI namespace. That's one of the best ways to access a selection element and it's options because it's api is so easy. Let's say you've got a web page that looks something like this
<!DOCTYPE html>
<head>
<title>Disposable Page</title>
</head>
<body >
<select id="select">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
</body>
</html>
You're code to access the select would look like this. Note how I create the Select object by passing a normal IWebElement to it's constructor. You have plenty of methods on the Select object. Take a look at the source for more information, until it gets properly documented.
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium;
using System.Collections.Generic;
using OpenQA.Selenium.IE;
namespace Selenium2
{
class SelectExample
{
public static void Main(string[] args)
{
IWebDriver driver = new InternetExplorerDriver();
driver.Navigate().GoToUrl("www.example.com");
//note how here's i'm passing in a normal IWebElement to the Select
// constructor
Select select = new Select(driver.FindElement(By.Id("select")));
IList<IWebElement> options = select.GetOptions();
foreach (IWebElement option in options)
{
System.Console.WriteLine(option.Text);
}
select.SelectByValue("audi");
//This is only here so you have time to read the output and
System.Console.ReadLine();
driver.Quit();
}
}
}
A couple things to note about the Support class however. Even if you downloaded the latest beta, the support DLL won't be there. The Support package has a relatively long history in the Java libraries (that's where PageObject lives) but it's still pretty fresh in the .Net driver. Fortunately, it's really easy to build from source. I pulled from SVN then referenced the WebDriver.Common.dll from the beta download and built in C# Express 2008. This class hasn't been as well tested as some of the other classes, but my example worked in Internet Explorer and Firefox.
There's a few other things that I should point out based on your code above. Firstly the line you were using to find the select element
driver.FindElements(By.TagName("select"));
is going to find all select elements. you should probably use driver.FindElement
, without the 's'.
Also, very rarely would you use INavigation directly. You'll do most of your navigation like driver.Navigate().GoToUrl("http://example.com");
Lastly, DefaultSelenium
is the way to access the older Selenium 1.x apis. Selenium 2 is a pretty significant departure from Selenium 1, so unless you're trying to migrate old tests to the new Selenium 2 api (often referred to as the WebDriver api) you won't use DefaultSelenium.
这篇关于使用Selenium 2的IWebDriver与页面上的元素进行交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!