使用C#在多个浏览器中运行Selenium测试

使用C#在多个浏览器中运行Selenium测试,第1张

使用C#在多个浏览器中运行Selenium测试

我目前使用NUnit的方式。我遇到了同样的问题,找不到使用MSTest的好方法。

我正在做的是:

如您所见,我只是为每个浏览器创建一个新的TestFixture。

[TestFixture(typeof(ChromeDriver))][TestFixture(typeof(InternetExplorerDriver))][TestFixture(typeof(FirefoxDriver))]public class LoginTests<TWebDriver> where TWebDriver : IWebDriver, new(){[SetUp]public void Init(){   Driver.Initialize<TWebDriver>();}[Test]public void Failed_login(){    LoginPage.GoTo();    LoginPage.LoginAs("user").WithPassword("pass").WithDatasource("db").Login();    Assert.IsTrue(LoginFail.IsAt, "Login failure is incorrect");}[Test]public void Admin_User_Can_Login(){    LoginPage.GoTo();    LoginPage.LoginAs("user").WithPassword("pass").WithDatasource("db").Login();    Assert.IsTrue(HomePage.IsAt, "Failed to login.");}[TearDown]public void Cleanup(){  Driver.Close();}}}

驾驶舱

 public class Driver<TWebDriver> where TWebDriver : IWebDriver, new() {    public static IWebDriver Instance { get; set; }    public static void Initialize()    {        if (typeof(TWebDriver) == typeof(ChromeDriver))        {         var browser = DesiredCapabilities.Chrome();     System.Environment.SetEnvironmentVariable("webdriver.chrome.driver", "C:/Users/jm/documents/Visual Studio 2013/Projects/VNextAutomation - Copy - Copy (3)/packages/WebDriverChromeDriver.2.10/tools/chromedriver.exe");     ChromeOptions options = new ChromeOptions() { BinaryLocation = "C:/Users/jm/AppData/Local/Google/Chrome/Application/chrome.exe" };     browser.SetCapability(ChromeOptions.Capability, options);     Console.Write("Testing in Browser: " + browser.BrowserName);     Instance = new RemoteWebDriver(new Uri("http://127.0.0.1:4444/wd/hub"), browser); } else {    Console.Write("Testing in Browser: "+ browser.BrowserName);    Instance = new RemoteWebDriver(new Uri("http://127.0.0.1:4444/wd/hub"), browser);}}        Instance.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(15));    }}

我已尝试将其适合您的代码。



欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5121863.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-17
下一篇2022-11-17

发表评论

登录后才能评论

评论列表(0条)

    保存