簡體   English   中英

在Specflow中創建頁面對象實例

[英]Creating Page Object instances in Specflow

我一直在努力解決自己的問題,因此決定尋求幫助。 因此,我正在編寫我的GitHub存儲庫,以便可以將其放入我的CV中。 問題是我決定將BDD與我的Selenium一起使用,並且我不知道如何在“步驟定義”中停止重復自己的操作。 在面向頁面對象模型中創建代碼時,我會不斷重復自己在每個步驟中創建對象實例的過程。 我發現我可以使用ScenarioContext.Current之類的東西,但是老實說,我一定一直在錯誤地使用它,因為它看起來並不像我想要的那樣好。 我希望就如何使我的代碼變得更好提出一些建議。 這是一個示例:

using TechTalk.SpecFlow;

namespace SampleAutomationTests.StepDefinitions
{
    [Binding]
    public sealed class AuthenticationFeatureSteps
    {
        [Given(@"I opened the home page")]
        public void GivenIOpenedTheHomePage()
        {
            HomePage page = new HomePage(Hooks.Driver);
            ScenarioContext.Current["Home Page"] = page;
            page.GoTo();
        }

        [Given(@"I navigated to Basic Auth link")]
        public void GivenINavigatedToBasicAuthLink()
        {
            HomePage page = (HomePage)ScenarioContext.Current["Home Page"];
            AuthenticationPage authenticationPage = page.GoToAuthenticationPage();
            ScenarioContext.Current["authenticationPage"] = authenticationPage;

        }
}
}

我通常將PageObjects作為Steps類的屬性,然后在Steps類的Constructor中對其進行初始化。 之后,我可以對這些對象執行任何操作。

我將實例化頁面如下:

using TechTalk.SpecFlow;

namespace SampleAutomationTests.StepDefinitions
{
[Binding]
public sealed class AuthenticationFeatureSteps
{

    HomePage page = new HomePage(Hooks.Driver);
    AuthenticationPage authpage = new AuthernticationPage(Hooks.Driver);

    [Given(@"I opened the home page")]
    public void GivenIOpenedTheHomePage()
    {
        page.GoTo();
    }

    [Given(@"I navigated to Basic Auth link")]
    public void GivenINavigatedToBasicAuthLink()
    {
        page.GoToAuthenticationPage();
    }
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM