gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 00:10:18 25 4
gpt4 key购买 nike

我有一个创建 2 个远程网络驱动程序的方法。一个用 chrome,另一个用 firefox:

驱动.cs

 public class Driver
{

public static IWebDriver Instance { get; set; }

public static void Initialize()
{
DesiredCapabilities[] browsers = {DesiredCapabilities.Firefox(),DesiredCapabilities.Chrome()};
foreach (DesiredCapabilities browser in browsers)
{
if (browser == DesiredCapabilities.Chrome())
{
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));
}

然后我有一个测试类:

[TestClass]
public class LoginTests
{
[TestInitialize]
public void Init()
{
Driver.Initialize();
}

[TestMethod]
public void Failed_login()
{
LoginPage.GoTo();
LoginPage.LoginAs("user").WithPassword("pass").WithDatasource("db").Login();

Assert.IsTrue(LoginFail.IsAt, "Login failure is incorrect");
}


[TestMethod]
public void Admin_User_Can_Login()
{
LoginPage.GoTo();
LoginPage.LoginAs("user").WithPassword("pass").WithDatasource("db").Login();

Assert.IsTrue(HomePage.IsAt, "Failed to login.");
}

[TestCleanup]
public void Cleanup()
{
Driver.Close();

}
}

问题是当调用 Driver.Intialize 时,它​​不会同时运行 chrome 和 firefox。我想要发生的是,当调用 Init 方法时,它会启动两个浏览器并在每个浏览器中运行测试方法。

最佳答案

我目前使用的方法是使用 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));
}
}

我已尝试将其融入您的代码。

关于c# - 使用 C# 在多个浏览器中运行 Selenium 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28480545/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com