gpt4 book ai didi

c# - 如何同时在多个浏览器上运行测试? Selenium 网格、C#、Specflow、NUnit

转载 作者:太空宇宙 更新时间:2023-11-03 14:41:21 25 4
gpt4 key购买 nike

几天来,我一直在尝试在现有项目上实现 Selenium Grid 2 的指南和 YouTube 视频之间来回切换,但我遇到了困难,请帮忙!

我们的框架是 Specflow 3.0.220、Selenium WebDriver 3.141.0、C#、NUnit 3.12.0、Selenium Grid selenium-server-standalone-3.141.59。

我实现 Selenium Grid 2 的最初目标如下:

  1. 在我的本地机器上设置集线器和节点 = 完成。
  2. 通过其中一个节点运行测试 = 完成。
  3. 同时在所有节点上运行测试 = 令人头疼。

关于第2项,我设置了两个节点,一个是Chrome节点,一个是Firefox节点。我可以对它们进行测试,但不能同时进行。

我觉得我在这里漏掉了一 block 拼图。

这是设置:

Scenario Outline: Log in
Given I launch the site for <profile> and <environment> and <parallelEnvironment>
When I log in to the Normal account
Then I see that I am logged in

Examples:
| profile | environment | parallelEnvironment |
| parallel | Chrome75 | grid |

如果配置文件是并行的并且 parallelEnvironment 是网格,环境将被忽略。 parallelEnvironment 的原因是因为我们在设置 Selenium Grid 的过程中可能仍然会使用 Browserstack。

这些步骤使用相关的步骤文件等和页面文件(但不使用页面对象模型,因为它已被弃用)。

驱动设置如下:

namespace OurAutomation
{
[Binding]
public sealed class BrowserStack
{
private BrowserStackDriver bsDriver;
public static BrowserStackDriver bdriver;

[BeforeScenario]
public void BeforeScenario()
{
bsDriver = new BrowserStackDriver();
bdriver = bsDriver;
}

[AfterScenario]
public void AfterScenario()
{
bsDriver.Cleanup();
}
}

public class CustomRemoteWebDriver : RemoteWebDriver
{
public CustomRemoteWebDriver(Uri remoteAddress, ChromeOptions options) : base(remoteAddress, options)
{
}

public string getSessionID()
{
return base.SessionId.ToString();
}
}

public class BrowserStackDriver
{
private IWebDriver driver;
public static bool isBrowserStack = false;
public static string Platform;
public static string theEnvironment;
public static string sessionId;

public BrowserStackDriver()
{

}

public string GetString(string property)
{
if (TestContext.Parameters[property] == null)
{
throw new ArgumentException("Property does not exist, does not have a value, or a test setting is not selected. You may need to add the .runsettings file in Visual Studio (Test > Test Settings > Select Test Settings File).");
}
else
{
return TestContext.Parameters[property].ToString();
}
}

public IWebDriver Init(string profile, string environment, string parallelEnvironment)
{
String testString = GetString("BuildNumber");

theEnvironment = environment;

NameValueCollection caps = ConfigurationManager.GetSection("capabilities/" + profile) as NameValueCollection;
NameValueCollection settings = ConfigurationManager.GetSection("environments/" + environment) as NameValueCollection;

ChromeOptions chromeOptions = new ChromeOptions();

if (profile == "single")
{
// logic to invoke relevant browser locally based on Specflow parameter 'profile'
Thread.Sleep(3000);
}
else if (profile == "parallel")
{
if (parallelEnvironment == "browserstack")
{
foreach (string key in caps.AllKeys)
{
chromeOptions.AddAdditionalCapability(key, caps[key]);
}

foreach (string key in settings.AllKeys)
{
chromeOptions.AddAdditionalCapability(key, settings[key]);
}

string username = Environment.GetEnvironmentVariable("BROWSERSTACK_USERNAME");

if (username == null)
{
username = ConfigurationManager.AppSettings.Get("user");
}

string accesskey = Environment.GetEnvironmentVariable("BROWSERSTACK_ACCESS_KEY");

if (accesskey == null)
{
accesskey = ConfigurationManager.AppSettings.Get("key");
}

chromeOptions.AddAdditionalCapability("browserstack.user", username);
chromeOptions.AddAdditionalCapability("browserstack.key", accesskey);
chromeOptions.AddAdditionalCapability("browserstack.local", "true");
chromeOptions.AddAdditionalCapability("build", GetString("BuildNumber"));
chromeOptions.AddAdditionalCapability("name", TestContext.CurrentContext.Test.MethodName);
chromeOptions.AddAdditionalCapability("project", GetString("Project"));

BrowserStackDriver.isBrowserStack = true;

driver = new CustomRemoteWebDriver(
new Uri("http://" + ConfigurationManager.AppSettings.Get("server") + "/wd/hub/"), chromeOptions);

CustomRemoteWebDriver browserRemoteDriver = driver as CustomRemoteWebDriver;
sessionId = browserRemoteDriver.getSessionID();
}
else if (parallelEnvironment == "grid")
{
driver = new RemoteWebDriver(new Uri("http://000.00.00.00:4444/wd/hub"), chromeOptions);
}
}

return driver;
}

public void Cleanup()
{
Thread.Sleep(2000);
if (isBrowserStack)
{
Log.Status status = (TestContext.CurrentContext.Result.Message == null) ? Log.Status.Passed : Log.Status.Failed;
string reason = (TestContext.CurrentContext.Result.Message == null) ? "Passed" : "Error see exception";

Log.UpdateTestStatus(status, reason, sessionId);
}

driver.Quit();
driver = null;
}
}
}

所以在这里...

                else if (parallelEnvironment == "grid")
{
driver = new RemoteWebDriver(new Uri("http://000.00.00.00:4444/wd/hub"), chromeOptions);
}

...我输入其中一个节点的地址并进行测试。但是,我只想将测试发送到集线器,然后让它在相关浏览器中的所有事件节点上同时执行该测试。我该如何实现?这些指南和视频似乎只让我了解了这么多。

谢谢

更新:

所以我认为我正在朝着正确的方向前进。不得不将其回归基础,这样我才能看到如何在我现有的项目中实现它。我已经在我的网格中完成了这项工作:https://github.com/teixeira-fernando/Parallel-Execution-with-Selenium-Grid

但是我注意到我需要向测试添加属性(同时在多个浏览器上运行一个测试)...

    namespace Tutorial_parallel_execution
{
[TestFixture(BrowserType.Chrome)]
[TestFixture(BrowserType.Firefox)]
[TestFixture(BrowserType.Opera)]
[TestFixture(BrowserType.IE)]
[Parallelizable(ParallelScope.Fixtures)]
public class GoogleTesting : Hooks
{
public GoogleTesting(BrowserType browser) : base(browser)
{

}

[Test]
public void GoogleTest()
{
Driver.Navigate().GoToUrl("http://www.google.com");
Driver.FindElement(By.Name("q")).SendKeys("selenium");
Driver.FindElement(By.Name("btnK")).Click();
Assert.That(Driver.PageSource.Contains("Selenium"), Is.EqualTo(true),
"The text selenium doenst exist");
}
}

}

但是,自从我的项目开始出现与此类似的提示 SpecFlow Visual Studio extension attempted to use SpecFlow code-behind generator 1.9 ,我开始使用 SpecFlow.Tools.MsBuild.Generation 并失去了对测试(代码隐藏文件)的访问权限以添加属性。我可以添加的唯一属性是 [Parallelizable(ParallelScope.Fixtures)] 但我必须将它放在 AssemblyInfo.cs 中 - 其他属性不能添加到那里。

我是否需要降级 Specflow/Selenium 等版本才能使其正常工作?

最佳答案

我能够从 https://github.com/minhhoangvn/AutomationFramework 中删除使用 ThreadLocal 实现并行执行所需的代码

关于c# - 如何同时在多个浏览器上运行测试? Selenium 网格、C#、Specflow、NUnit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56807439/

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