使用带有页面对象模型框架的Browserstack

时间:2017-08-03 15:53:15

标签: selenium specflow browserstack

您好我正在尝试将自动化测试集成到浏览器堆栈中。不幸的是,有任何在线可以帮助我使用POM框架。

我有seen this这是有用的,但它是一个基本模板,不使用页面对象模型来传递驱动程序。 我不断将对象引用设置为对象的实例'错误。

using Macmillan.Specflow.Support.Models;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using System;
using System.Configuration;
using System.Diagnostics;
using TechTalk.SpecFlow;

namespace Macmillan.Specflow.Support.Steps
{
    [Binding]
    public class BaseSteps
    {
        public IWebDriver Driver { get; private set; }

        [BeforeScenario]
        public void BeforeScenario()
        {
            if (Process.GetProcessesByName("BrowserStackLocal").Length == 0)
                new Process
                {
                    StartInfo = new ProcessStartInfo
                    {
                        FileName = "BrowserStackLocal.exe",
                        Arguments = ConfigurationManager.AppSettings["browserstack.key"] + " -forcelocal"
                    }
                }.Start();

            var capabilities = new DesiredCapabilities();

            capabilities.SetCapability(CapabilityType.Version, "33");
            capabilities.SetCapability("os", "windows");
            capabilities.SetCapability("os_version", "8");
            capabilities.SetCapability("browserName", "firefox");

            capabilities.SetCapability("browserstack.user", ConfigurationManager.AppSettings["browserstack.user"]);
            capabilities.SetCapability("browserstack.key", ConfigurationManager.AppSettings["browserstack.key"]);

            capabilities.SetCapability("project", "LCI");
            capabilities.SetCapability("name", ScenarioContext.Current.ScenarioInfo.Title);

            capabilities.SetCapability("browserstack.local", true);


            Driver = new RemoteWebDriver(new Uri(ConfigurationManager.AppSettings["browserstack.hub"]), capabilities);
            Driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(1));
            ScenarioContext.Current["driver"] = Driver;
        }


        [AfterScenario]
        public void AfterScenario()
        {
            Driver.Quit();
        }
    }


using Macmillan.Specflow.Support.Models;
using OpenQA.Selenium;
using TechTalk.SpecFlow;

namespace Macmillan.Specflow.Steps.Steps
{
    public class BaseSteps : Support.Steps.BaseSteps
    {
        #region Properties

        public HomePage HomePage { get; private set; }
        public EnglandPage EnglandPage { get; set; }

        #endregion

        #region Methods

        [Given(@"I browse to Home page")]
        public void GivenIBrowseToHomePage()
        {
            HomePage = new HomePage(Driver).BrowseTo();
        }

        [Given(@"I navigate to England Page")]
        public void GivenINavigateToEnglandPage()
        {
            EnglandPage = HomePage.HomePageMenuBar.NavigateToEnglandPage();
        }



        #endregion
    }
}


using Macmillan.Specflow.Support.Models.Partials;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.PageObjects;
using System.Configuration;
using TechTalk.SpecFlow;

namespace Macmillan.Specflow.Support.Models
{
    public class HomePage 
    {
        #region Constructors

        public HomePage(IWebDriver driver)
        {
            Driver = driver;
            Driver = (IWebDriver)ScenarioContext.Current["driver"];
            PageFactory.InitElements(Driver, this);

            HeaderNavigation = new HeaderNavigation(Driver);
            HomePageMenuBar = new HomePageMenuBar(Driver);
        }

        #endregion

        #region fields
        [FindsBy(How = How.ClassName, Using = "text-headline")]
        private IWebElement _header;

        #endregion

        #region properties
        public HomePageMenuBar HomePageMenuBar { get; }
        public HeaderNavigation HeaderNavigation { get; }

        public IWebDriver Driver { get; set; }

        #endregion


        #region Methods

        /// <summary>
        /// Use the browser to browse to the Home Page.
        /// </summary>
        /// <returns>An instance of the Home page.</returns>
        public HomePage BrowseTo()
        {
            Driver.Navigate().GoToUrl(ConfigurationManager.AppSettings["baseUrl"]);
            return this;
        }

        #endregion Methods
    }
}

using Macmillan.Specflow.Support.Models;
using NUnit.Framework;
using System;
using System.Text.RegularExpressions;
using TechTalk.SpecFlow;

namespace Macmillan.Specflow.Steps.Steps
{
    [Binding]
    public class LCIEnglandSteps : BaseSteps
    {
        #region properties

        public AboutPage AboutPage { get; set; }
        public ContactUsPage ContactUsPage { get; set; }
        public FAQSPage FAQSPage { get; set; }
        public IncidenceDetailsPage IncidenceDetailsPage { get; set; }
        public MortalityDetailsPage MortalityDetailsPage { get; set; }
        public PrevalenceDetailsPage PrevalenceDetailsPage { get; set; }

        #endregion properties

        #region methods

        #region Givens

        [Given(@"I navigate to the Macmillan Homepage")]
        public void GivenINavigateToTheMacmillanHomepage()
        {
            HomePage.HeaderNavigation.NavigateToMacmillanPage();
        }

        [Given(@"I want to contact Macmillan")]
        public void GivenIWantToContactMacmillan()
        {
            ContactUsPage = EnglandPage.EnglandPageMenuBar.NavigateToContactUs();
        }

        [Given(@"I want to filter the data by both CCG (.*) and Cancer (.*)")]
        public void GivenIWantToFilterTheDataByBothCCGAndCancer(string p0, string p1)
        {
            EnglandPage.CcgFilter = p0;
            EnglandPage.NavigateIteminCCGList(p0);
            EnglandPage.CancerTypeFilter = p1;
            EnglandPage.NavigateIteminCancerTypeList(p1);
            EnglandPage.ApplyFilter();
        }

        [Given(@"I want to filter the data by Cancer (.*)")]
        public void GivenIWantToFilterTheDataByCancer(string p0)
    `enter code here`    {`enter code here`
            EnglandPage.CancerTypeFilter = p0;
            EnglandPage.NavigateIteminCancerTypeList(p0);
            EnglandPage.ApplyFilter();
        }

1 个答案:

答案 0 :(得分:0)

您的问题是您创建了新的驱动程序实例,而不是将现有的驱动程序实例传递给

public HomePage(IWebDriver driver)
    {
        Driver = driver;
        Driver = (IWebDriver)ScenarioContext.Current["driver"];
        PageFactory.InitElements(Driver, this);

        HeaderNavigation = new HeaderNavigation(Driver);
        HomePageMenuBar = new HomePageMenuBar(Driver);
    }

我使用了https://github.com/browserstack/specflow-browserstack/blob/master/SpecFlow-BrowserStack/BrowserStack.cs

并且有一行使IWebDriver成为私有,因此您无法传递它,将其公开并将其传递给您的Page对象。

public MainPageObjects(BrowserStackDriver driver)
    {

       PageFactory.InitElements(driver.driver, this);

    }