本文介绍了什么是使用Selenium PageObject设计模式的最佳途径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建使用Selenium 2网络驱动器,采用C#.NET测试。通过大量的硒文档看完后,我离开仍然感觉上如何去使用PageObject设计模式测试不确定。

I'm creating tests using Selenium 2 Web Driver with C#.Net. After reading through a lot of the Selenium documentation, I am left still feeling unsure on how to go about testing using the PageObject design patterns.

许多硒例子只在Java显示和面向.NET的API绑定并不总是像人们可能会认为他们是由于限制,类似和标准设置某些语言。

Many of the selenium examples are only shown in Java and the API bindings for .Net are not always as similar as one would think they are due to limitations and the standards set by certain languages.

什么是使用与PageFactory的PageObject设计模式在.net中硒网络驱动器的最佳方式?

What is the best way to use the PageObject design pattern with PageFactory in .Net Selenium Web Driver?

最后,我希望我的PageObjects处理更多的功能,而不是我的NUnit的测试使用PageObject IWebElements。

Eventually, I want my PageObjects to handle more functionality, rather than my NUnit tests using the PageObject IWebElements.

下面是我怎么现在要创建我的测试示例。前进

Below is an example of how I am currently going to create my tests moving forward.

public class LoginPage
{
    private IWebDriver webDriver;

    [FindsBy(How = How.Id, Using = "ctl00_ctl00_ctl00_insideForm_insideForm_content_txtPassword")]
    public IWebElement Password { get; set; }

    [FindsBy(How = How.Id, Using = "ctl00_ctl00_ctl00_insideForm_insideForm_content_cmdSubmit")]
    public IWebElement SubmitButton { get; set; }

    [FindsBy(How = How.Id, Using = "ctl00_ctl00_ctl00_insideForm_insideForm_content_txtUserName")]
    public IWebElement UserName { get; set; }

    public LoginPage() { }

    public LoginPage(IWebDriver webDriver)
    {
        this.webDriver = webDriver;


        if(!webDriver.Url.Contains("Login.aspx"))
        {
            throw new StaleElementReferenceException("This is not the login page");
        }
        PageFactory.InitElements(webDriver, this);
    }

    public HomePage signIn(string username, string password)
    {
        UserName.SendKeys(username);
        Password.SendKeys(password);
        SubmitButton.Submit();

        // Even if i create a NUnit test for this
        // Issue with page loading still occures when I try and return new object
        HomePage homePage = new HomePage(webDriver);
        PageFactory.InitElements(webDriver, homePage);
        return homePage;
    }
}



目前,这是我目前用做NUnit的:

At the moment this is what I am currently doing with NUnit:

[TestFixture]
public class LoginPageTest : TestBase
{
    private IWebDriver driver;
    private LoginPage loginPage;
    private HomePage homePage;

    [SetUp]
    [Description("Sets up the test fixture page objects and navigates to the login page.")]
    public void SetUp()
    {
        driver = StartDriver();
        Log.Info("Driver started");
        driver.Navigate().GoToUrl("http://" + Environment + ");
        loginPage = new LoginPage();
        PageFactory.InitElements(driver, loginPage);
        //driver.Navigate().Refresh();
    }

    [Test]
    [Description("Enters invalid credentials and asserts that a correct error message is displayed.")]
    public void SubmitFormInvalidCredentials()
    {
        loginPage.UserName.SendKeys("invalid");
        loginPage.Password.SendKeys("invalid");
        loginPage.SubmitButton.Click();
        IWebElement invalidCredentials = driver.FindElement(By.Id("ctl00_ctl00_ctl00_insideForm_insideForm_ctl02_title"));
        Assert.AreEqual("Invalid user name or password", invalidCredentials.Text);
    }

    [Test]
    [Description("Enters valid credentials and asserts that the user is taken to the home page.")]
    public void SubmitFormValidCredentials()
    {
        loginPage.UserName.SendKeys("valid");
        loginPage.Password.SendKeys("valid");
        loginPage.SubmitButton.Click();

        homePage = new HomePage();
        PageFactory.InitElements(driver, homePage);
        Assert.AreEqual("pattest", homePage.Username.Text);
    }

 }



大部分文章和博客文章我发现硒的webdriver设计模式发出矛盾,以前的文章,我觉得。

Most of the articles and blog posts I find for selenium webdriver Design Patterns give off contradictions to previous posts I find.

那么,什么是正确的做法?

So, what is the right way?

要顶部这一关,我甚至给了PageObject设计模式一试。

To top this off, I even gave the PageObject design pattern a try.

    [Test]
    [Description("Login using PageObject Design Pattern")]
    public void Login()
    {
        loginPage = new LoginPage(driver);
        HomePage signIn = loginPage.SignIn("pattest", "pattest");
    }

在我LoginPage

Inside my LoginPage

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

        if (!driver.Url.Contains("Login.aspx"))
        {
            throw new ElementNotFoundException("This is not the login page");
        }
        PageFactory.InitElements(driver, this);
    }

    public HomePage SignIn(string username, string password)
    {
        UserName.SendKeys(username);
        Password.SendKeys(password);
        SubmitButton.Click();
        return new HomePage(driver);
    }

和,以显示如何我的主页应该有它的构造启动:

And, of course to show how my HomePage should be initiated with its Constructor:

public HomePage(IWebDriver d)
    {
        webDriver = d;
        // I need to use this as its not freaking waiting for my Page to load when I pass the webdriver in the consturctor.
        var wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(60));

        try
        {
            wait.Until(driver => driver.FindElement(By.Id("ctl00_ctl00_ctl00_insideForm_insideForm_loginStatus")));
        }
        catch(Exception e)
        {
            throw new ElementNotFoundException("This is not the home page.");
        }
        PageFactory.InitElements(webDriver, this);
    }



我如何有效地使用webdriver的PageObject设计模式与测试。
我不知道这一点。

How do I use WebDriver PageObject design pattern effectively with testing.I can't figure this out.

推荐答案

使用 PageFactory.InitElements(_driver,这一点);您基本页类的构造函数:

Use PageFactory.InitElements(_driver, this); on the constructor of your base page class:

public class Page
{
    public IWebDriver _driver;

    public Page(IWebDriver driver)
    {
        this._driver = driver;
        PageFactory.InitElements(_driver, this);
    }
}

请看到的

这篇关于什么是使用Selenium PageObject设计模式的最佳途径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 18:16