简体   繁体   中英

How to organize tests and PageObject classes with FluentAutomation and MSTest (Cannot resolve symbol “I” error)

I am trying to run the example in the documentation here .

I am using Visual Studio with MSTest, so I modified the code a little bit and it looks like this now:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using FluentAutomation;
using FluentAutomation.Interfaces;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;

namespace UnitTestProject1
{
    public class BingSearchPage : PageObject
    {
        public BingSearchPage(FluentTest test)
            : base(test)
    {
        Url = "http://bing.com/";
        At = () => I.Expect.Exists(SearchInput); //Documentation says "At = () =>; I.Expect.Exists(SearchInput);" but I think that's a typo
    }

        public BingSearchResultsPage Search(string searchText)
        {
            I.Enter(searchText).In(SearchInput);
            I.Press("{ENTER}");
            return this.Switch();
        }

        private const string SearchInput = "input[title='Enter your search term']";
    }

    public class BingSearchResultsPage : PageObject
    {
        public BingSearchResultsPage(FluentTest test)
            : base(test)
        {
            At = () => I.Expect.Exists(SearchResultsContainer);
        }

        public BingSearchResultsPage FindResultUrl(string url)
        {
            I.Expect.Exists(string.Format(ResultUrlLink, url));
            return this;
        }

        private const string SearchResultsContainer = "#b_results";
        private const string ResultUrlLink = "a[href='{0}']";
    }

    [TestClass]
    public class UnitTest1 : FluentTest
    {
        public UnitTest1()
        {
            SeleniumWebDriver.Bootstrap(SeleniumWebDriver.Browser.Chrome);
        }

        [TestMethod]
        public void SearchForFluentAutomation()
        {
            new BingSearchPage(this)
                .Go()
                .Search("FluentAutomation")
                .FindResultUrl("http://fluent.stirno.com/blog/FluentAutomation-scriptcs/");
        }
    }
}

I get errors like:

Error 1 'FluentAutomation.PageObject' does not contain a constructor that takes 1 arguments C:\\Users\\Shitij\\Documents\\Visual Studio 2013\\Projects\\UnitTestProject1\\UnitTestProject1\\UnitTest1.cs 13 15 UnitTestProject1

Error 2 The name 'I' does not exist in the current context C:\\Users\\Shitij\\Documents\\Visual Studio 2013\\Projects\\UnitTestProject1\\UnitTestProject1\\UnitTest1.cs 16 20 UnitTestProject1

Error 3 'UnitTestProject1.BingSearchPage' does not contain a definition for 'Switch' and no extension method 'Switch' accepting a first argument of type 'UnitTestProject1.BingSearchPage' could be found (are you missing a using directive or an assembly reference?) C:\\Users\\Shitij\\Documents\\Visual Studio 2013\\Projects\\UnitTestProject1\\UnitTestProject1\\UnitTest1.cs 23 25 UnitTestProject1

Feels like a stupid question, but I am stuck on it. Any idea what I am doing wrong? The property "I" seems to be in FluentTest class, so how is the documentation using that in classes derived from PageObject?

Theres an issue with the docs on the site atm. Really need to get it fixed. The generic bits are missing!

change PageObject to PageObject<BingSearchResultsPage> and I should resolve.

Had the same problem, this is what I came up with:

using System;
using FluentAutomation;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace HelloWorldTests
{
    [TestClass]
    public class WithPageObjectTests : FluentTest
    {
        public WithPageObjectTests()
        {
            SeleniumWebDriver
                .Bootstrap(SeleniumWebDriver.Browser.InternetExplorer);
        }

        [TestInitialize]
        public void TestInitialisze()
        {
            // Global wait timer
            Config.Settings.WaitUntilTimeout = TimeSpan.FromSeconds(5);

            Config.Settings.ScreenshotOnFailedAction = true;
            Config.Settings.ScreenshotOnFailedAssert = true;
            Config.Settings.ScreenshotOnFailedExpect = true;
        }

        [TestMethod]
        public void SearchForFluentAutomation()
        {
            new BingSearchPage(this)
                .Go()
                .Search("FluentAutomation")
                .FindResultUrl("http://fluent.stirno.com/blog/FluentAutomation-scriptcs/");
        }
    }

    /// <summary>
    /// Page model
    /// </summary>
    public class BingSearchPage : PageObject<BingSearchPage>
    {
        //private const string SearchInput = "input[title='Enter your search term']";
        private const string SearchInput = "#sb_form_q";

        public BingSearchPage(FluentTest test)
            : base(test)
        {
            Url = "http://bing.com/";
            At = () => I.Expect.Exists(SearchInput);
        }

        public BingSearchResultsPage Search(string searchText)
        {
            I.Enter(searchText).In(SearchInput);
            I.Press("{ENTER}");
            return this.Switch<BingSearchResultsPage>();
        }
    }

    /// <summary>
    /// Page model
    /// </summary>
    public class BingSearchResultsPage : PageObject<BingSearchResultsPage>
    {
        private const string SearchResultsContainer = "#b_results";
        private const string ResultUrlLink = "a[href='{0}']";

        public BingSearchResultsPage(FluentTest test)
            : base(test)
        {
            At = () => I.Expect.Exists(SearchResultsContainer);
        }

        public BingSearchResultsPage FindResultUrl(string url)
        {
            I.Expect.Exists(string.Format(ResultUrlLink, url));
            return this;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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