问题
I have some web ui tests in C# which are executed through selenium in browserstack. Currently these are just simple unit ms tests and they are executed againts different browsers. I want to port the test to specflow, but I don't really know how to do the multiple browser testing bit.
At the moment, to execute these tests in multiple browsers I am using a DataSource attribute, which basically takes different inputs for the same tests from an XML file
[TestMethod]
[Ignore]
[DeploymentItem("JLL.Specs\\Browsers.xml")]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\Browsers.xml", "Row", DataAccessMethod.Sequential)]
public void LoadHomePageAndFindSearchBox()
{
...
}
The problem is that Specflow doesn't support the DataSource attribute. Any idea?
This is the XML file:
<?xml version="1.0" encoding="utf-8" ?>
<Rows>
<Row>
<browser>Firefox</browser>
<browser_version>31.0</browser_version>
<os>Windows</os>
<os_version>7</os_version>
<resolution>1280x1024</resolution>
<browserName></browserName>
<platform></platform>
<device></device>
</Row>
<Row>
<browser>Chrome</browser>
<browser_version>36.0</browser_version>
<os>Windows</os>
<os_version>XP</os_version>
<resolution>1024x768</resolution>
<browserName></browserName>
<platform></platform>
<device></device>
</Row>
...
回答1:
You could use a web driver abstraction framework to write driver agnostic test code. To do this I've used Coypu. Using this library you can easily switch between different web drivers (e.g. Selenium, Phantom JS, WaitN) without needing to write different driver code.
In a web driver factory class you could then use code like (see here for more information):
sessionConfiguration.Driver = Type.GetType("Coypu.Drivers.Selenium.SeleniumWebDriver, Coypu");
sessionConfiguration.Browser = Drivers.Browser.Parse("firefox");
to get an instance of Selenium for FireFox or you could replace "firefox" with "chrome" to get an instance of Chrome.
I've used this approach to run a suite of tests for different browsers and it worked well. However this has meant executing a suite of tests for one browser, and then upon completion running them for another.
回答2:
Best is to use Baseclass.Contrib.SpecFlow.Selenium.NUnit dll
.
see the Example:
@Browser:IE
@Browser:Chrome
@Browser:Firefox
Scenario Outline: Add Two Numbers
You can use @Browser:..........
in your feature file. This attribute will list 3 tests in your test explorer for each browser and you can run whichever you wanted to use.
For detailed information , go here.
来源:https://stackoverflow.com/questions/27784357/datasource-attribute-with-specflow-for-multiple-browser-testing