简体   繁体   English

C# generics 用于在 Selenium+specflow 框架中链接页面对象

[英]C# generics for linking page objects in Selenium+specflow framework

I am trying to create a POM using Specflow and Selenium.我正在尝试使用 Specflow 和 Selenium 创建一个 POM。 I am following a course and he used C# generics to call pages.我正在学习一门课程,他使用 C# generics 来调用页面。 Can someone help me understand how this following code works:有人可以帮助我了解以下代码的工作原理:

class BasePage
{
    public T As<T>()where T: BasePage
    {
        return(T) this;
    }
}

public class PropertiesCollection
{
    private static BasePage _currentPage;
    
    public static BasePage currentPage
    {
        get{return _currentPage;}
        set{
            ScenarioContext.current["class"]=value;
            _currentPage=ScenarioContext.Current.Get<BasePage>("class");
           }
    }
}

code in step definition file:(instead of creating an object for every page and calling methods from it the code is written as follows)步骤定义文件中的代码:(而不是为每个页面创建一个 object 并从中调用方法,代码编写如下)

[Then(@"I should be asked to enter Username and password")]
public void ThenIShouldBeAskedToEnterUsernameAndPassword()
{
    PropertiesCollection.currentPage.As<LoginPage>().Login(username,password);
}

The PropertiesCollection (above line of code) cannot read LoginPage until i create object (PropertiesCollection.currentPage = new LoginPage();) before the steps.在我创建 object (PropertiesCollection.currentPage = new LoginPage();) 之前,PropertiesCollection(代码行上方)无法读取LoginPage

and also its not reading the next page而且它没有阅读下一页

[Then(@"I should Login and see Welcome page")]
public void ThenIShouldLoginAndSeeWelcomePage()
{
PropertiesCollection.currentPage.As<WelcomePage>().WelcomeLabel();
}

its throwing an error: Unable to cast Login page to Welcome Page.它抛出一个错误:无法将登录页面转换为欢迎页面。

When i debug and check its(currentPage) is still in the LoginPage当我调试并检查其(当前页面)是否仍在登录页面中

I'll make an assumption about what the scenario looks like:我将对场景的样子做出一个假设:

Scenario: Logging in
    When I log
    Then should be logged in and see the welcome page

The key here is setting up your page object model correctly.这里的关键是正确设置您的页面 object model。 Methods that cause a new page to load should return the page model for that next page.导致新页面加载的方法应为该下一页返回页面 model。 When logging in, the LoginPage.Login(...) method should return an instance of WelcomePage.登录时,LoginPage.Login(...) 方法应返回 WelcomePage 的实例。 Your SpecFlow steps should call these page model methods and reassign the currentPage property whenever you expect the user to navigate to a new page.您的 SpecFlow 步骤应调用这些页面 model 方法,并在您希望用户导航到新页面时重新分配currentPage属性。

Here is how to modify your LoginPage class:以下是如何修改您的 LoginPage class:

public class LoginPage
{
    private readonly IWebDriver driver;

    public LoginPage(IWebDriver driver)
    {
        this.driver = driver;
    }

    ///
    /// <summary>
    /// Logs in and redirects to the welcome screen
    /// </summary>
    /// <param name="username"></param>
    /// <param name="password"></param>
    /// <returns>A page object model representing the welcome screen</returns>
    public WelcomePage Login(string username, string password)
    {
        // log in

        return new WelcomePage(driver);
    }
}

Notice that the Login() method does not just type the username and password into the form fields, then click the Log In button.请注意,Login() 方法不只是在表单字段中输入用户名和密码,然后单击“登录”按钮。 It returns an instance of a page object that you expect the user to navigate to after successfully logging in.它返回您希望用户在成功登录后导航到的页面 object 的实例。

Some example step definitions that set the currentPage property:设置currentPage属性的一些示例步骤定义:

[When(@"I log in")]
public void WhenILogIn()
{
    // Initialize a new login page object
    var loginPage = new LoginPage(driver);

    // Log the user in, and assign reference to the next expected page
    // to a local variable.
    var welcomePage = loginPage.Login(username, password);

    // Set the "current page" the user should be on
    PropertiesCollection.currentPage = welcomePage;
}

[Then(@"should be logged in and see welcome page")]
public void ThenShouldBeLoggedInAndSeeWelcomePage()
{
    // Get the "current page" and cast it to the expected type:
    var welcomePage = PropertiesCollection.currentPage.As<WelcomePage>();

    // Make your assertion:
    Assert.AreEqual("Welcome!", welcomePage.WelcomeLabel());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM