如何使用 FluentAutomation 和 MSTest 组织测试和 PageObject 类(无法解析符号 "I" 错误)
How to organize tests and PageObject classes with FluentAutomation and MSTest (Cannot resolve symbol "I" error)
我正在尝试 运行 文档中的示例 here。
我在 MSTest 中使用 Visual Studio,所以我稍微修改了代码,现在看起来像这样:
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/");
}
}
}
我收到如下错误:
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
感觉像是一个愚蠢的问题,但我坚持了下来。知道我做错了什么吗? 属性 "I" 似乎在 FluentTest class 中,那么文档如何在从 PageObject 派生的 classes 中使用它?
atm 站点上的文档存在问题。真的需要把它修好。缺少通用位!
将 PageObject
更改为 PageObject<BingSearchResultsPage>
并且 I
应该可以解决。
有同样的问题,这是我想出的:
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;
}
}
}
我正在尝试 运行 文档中的示例 here。
我在 MSTest 中使用 Visual Studio,所以我稍微修改了代码,现在看起来像这样:
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/");
}
}
}
我收到如下错误:
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
感觉像是一个愚蠢的问题,但我坚持了下来。知道我做错了什么吗? 属性 "I" 似乎在 FluentTest class 中,那么文档如何在从 PageObject 派生的 classes 中使用它?
atm 站点上的文档存在问题。真的需要把它修好。缺少通用位!
将 PageObject
更改为 PageObject<BingSearchResultsPage>
并且 I
应该可以解决。
有同样的问题,这是我想出的:
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;
}
}
}