如何在browserstack和本地浏览器上运行相同的测试?

时间:2018-02-27 09:53:55

标签: selenium selenium-webdriver automated-tests nunit browserstack

我已经设置了在BrowserStack上运行的NUnit测试(从此示例设置https://github.com/browserstack/nunit-browserstack

基类:

namespace Bloc.TestProject
{

    public class BrowserStackNUnitTest
    {
        protected IWebDriver driver;
        protected string profile;
        protected string environment;
        private Local browserStackLocal;

        public BrowserStackNUnitTest(string profile, string environment)
        {
            this.profile = profile;
            this.environment = environment;
        }

        [SetUp]
        public void Init()
        {
        ...

Browserstack测试:

namespace Bloc.TestProject
{
    [TestFixture("parallel", "chrome")]
    [TestFixture("parallel", "ie11")]
    [TestFixture("parallel", "iphoneX")]
    [TestFixture("parallel", "ipad")]
    [TestFixture("parallel", "samsungGalaxyS8")]
    [Parallelizable(ParallelScope.Fixtures)]
    public class OnTimeOnlineBooking : BrowserStackNUnitTest
    {
        WebDriverWait wait;
        public OnTimeOnlineBooking(string profile, string environment) : base(profile, environment)
        {

        }
... my tests ...

本地测试:

namespace Bloc.TestProject
{
    [TestFixture(typeof(PhantomJSDriver))]
    public class LocalBrowserTest<TWebDriver> where TWebDriver : IWebDriver, new()
{
    private IWebDriver driver;

    [SetUp]
    public void CreateDriver()
    {
        this.driver = new TWebDriver();
    }
    [TearDown]
    public void Cleanup()
    {
        driver.Quit();
    }


    ... my tests ... 

有没有什么方法可以构建我的测试,以便我可以运行测试,它将在本地和browserstack上运行,而不必复制测试代码?

2 个答案:

答案 0 :(得分:0)

我可以在Java中为这个案例建议一个解决方法,需要在C#中进行更改。

编写浏览器堆栈设置代码

NaN

然后编写启动浏览器的代码

public static browserstack_setup()  {

    DesiredCapabilities caps = new DesiredCapabilities();
    caps.setCapability("browserName", "chrome");
    caps.setCapability("version", "");
    caps.setCapability("platform", "windows");
    caps.setCapability("os_version", "8.1");

    WebDriver driver = new RemoteWebDriver(new URL(URL), caps);
    driver.get("http://www.google.com");
  }

答案 1 :(得分:0)

您可以从测试上下文中获取名称,并根据该信息启动本地驱动程序或远程驱动程序。对于repo和你的例子,我假设下面的代码应该工作。您可能还想查看 TestContext.CurrentContext.Test 下可用的其他API进行比较操作

[SetUp]
        public void Init()
        {
         if(TestContext.CurrentContext.Test.Name == "MyTestName"){
             this.driver = new TWebDriver();
         }
         else{
          NameValueCollection caps = ConfigurationManager.GetSection("capabilities/" + profile) as NameValueCollection;
          NameValueCollection settings = ConfigurationManager.GetSection("environments/" + environment) as NameValueCollection;

          DesiredCapabilities capability = new DesiredCapabilities();

          foreach (string key in caps.AllKeys)
          {
            capability.SetCapability(key, caps[key]);
          }

          foreach (string key in settings.AllKeys)
          {
            capability.SetCapability(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");
          }

          capability.SetCapability("browserstack.user", username);
          capability.SetCapability("browserstack.key", accesskey);

          if (capability.GetCapability("browserstack.local") != null && capability.GetCapability("browserstack.local").ToString() == "true")
          {
            browserStackLocal = new Local();
            List<KeyValuePair<string, string>> bsLocalArgs = new List<KeyValuePair<string, string>>();
            bsLocalArgs.Add(new KeyValuePair<string, string>("key", accesskey));
            browserStackLocal.start(bsLocalArgs);
          }

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

您需要确保已指定用于在Local和Grid上运行的测试装置。例如,如果在网格上的Safari上测试A并在本地浏览器上测试A