質問

Currently, openGoogle() does get called for each test case with the correct parameters. The problem is that setBrowser does not appear to be working properly. It does set the first time and completes the test successfully. However, when openGoogle() gets invoked for the second time it continues to use the first browser instead of using the new browser specified.

using NFramework = NUnit.Framework; ...

   [NFramework.TestFixture]
    public class SampleTest : FluentAutomation.FluentTest
    {
        string path;
        private Action<TinyIoCContainer> currentRegistration;
        public TestContext TestContext { get; set; }

        [NFramework.SetUp]
        public void Init()
        {
            FluentAutomation.Settings.ScreenshotOnFailedExpect = true;
            FluentAutomation.Settings.ScreenshotOnFailedAction = true;
            FluentAutomation.Settings.DefaultWaitTimeout = TimeSpan.FromSeconds(1);
            FluentAutomation.Settings.DefaultWaitUntilTimeout = TimeSpan.FromSeconds(30);
            FluentAutomation.Settings.MinimizeAllWindowsOnTestStart = false;
            FluentAutomation.Settings.ScreenshotPath = path = "C:\\ScreenShots";
        }

        [NFramework.Test]
        [NFramework.TestCase(SeleniumWebDriver.Browser.Firefox)]
        [NFramework.TestCase(SeleniumWebDriver.Browser.InternetExplorer)]
        public void openGoogle(SeleniumWebDriver.Browser browser)
        {
            setBrowser(browser);

            I.Open("http://www.google.com/");
            I.WaitUntil(() => I.Expect.Exists("body"));
            I.Enter("Unit Testing").In("input[name=q]");
            I.TakeScreenshot(browser + "EnterText");

            I.Click("button[name=btnG]");
            I.WaitUntil(() => I.Expect.Exists(".mw"));
            I.TakeScreenshot(browser + "ClickSearch");
        }

        public SampleTest()
        {
            currentRegistration = FluentAutomation.Settings.Registration;
        }

        private void setBrowser(SeleniumWebDriver.Browser browser)
        {
            switch (browser)
            {
                case SeleniumWebDriver.Browser.InternetExplorer:
                case SeleniumWebDriver.Browser.Firefox:
                    FluentAutomation.SeleniumWebDriver.Bootstrap(browser);
                    break;
            }
        }
    }

Note: Doing it this way below DOES work correctly - opening a separate browser for each test.

public class SampleTest : FluentAutomation.FluentTest { string path; private Action currentRegistration; public TestContext TestContext { get; set; }

private void ie()
{
    FluentAutomation.SeleniumWebDriver.Bootstrap(FluentAutomation.SeleniumWebDriver.Browser.InternetExplorer);
}
private void ff()
{
    >FluentAutomation.SeleniumWebDriver.Bootstrap(FluentAutomation.SeleniumWebDriver.Browser.Firefox);
}

public SampleTest()
{
    //ff
    FluentAutomation.SeleniumWebDriver.Bootstrap();
    currentRegistration = FluentAutomation.Settings.Registration;
}

[TestInitialize]
public void Initialize()
{
    FluentAutomation.Settings.ScreenshotOnFailedExpect = true;
    FluentAutomation.Settings.ScreenshotOnFailedAction = true;
    FluentAutomation.Settings.DefaultWaitTimeout = TimeSpan.FromSeconds(1);
    FluentAutomation.Settings.DefaultWaitUntilTimeout = TimeSpan.FromSeconds(30);
    FluentAutomation.Settings.MinimizeAllWindowsOnTestStart = false;
    path = TestContext.TestResultsDirectory;
    FluentAutomation.Settings.ScreenshotPath = path;
}

[TestMethod]
public void OpenGoogleIE()
{
    ie();
    openGoogle("IE");
}
[TestMethod]
public void OpenGoogleFF()
{
    ff();
    openGoogle("FF");
}
private void openGoogle(string browser)
{
    I.Open("http://www.google.com/");
    I.WaitUntil(() => I.Expect.Exists("body"));
    I.Enter("Unit Testing").In("input[name=q]");
    I.TakeScreenshot(browser + "EnterText");

    I.Click("button[name=btnG]");
    I.WaitUntil(() => I.Expect.Exists(".mw"));
    I.TakeScreenshot(browser + "ClickSearch");

} }
役に立ちましたか?

解決

Dev branch: The latest bits in the Dev branch play nicely with NUnit's parameterized test cases in my experience.

Just move the Bootstrap call inside the testcase itself and be sure that you manually call I.Dispose() at the end. This allows for proper browser creation when run in this context.

Here is an example that you should be able to copy/paste and run, if you pull latest from GitHub on the dev branch.

    [TestCase(FluentAutomation.SeleniumWebDriver.Browser.InternetExplorer)]
    [TestCase(FluentAutomation.SeleniumWebDriver.Browser.Chrome)]
    public void CartTest(FluentAutomation.SeleniumWebDriver.Browser browser)
    {
        FluentAutomation.SeleniumWebDriver.Bootstrap(browser);
        I.Open("http://automation.apphb.com/forms");
        I.Select("Motorcycles").From(".liveExample tr select:eq(0)"); // Select by value/text
        I.Select(2).From(".liveExample tr select:eq(1)"); // Select by index
        I.Enter(6).In(".liveExample td.quantity input:eq(0)");
        I.Expect.Text("$197.70").In(".liveExample tr span:eq(1)");

        // add second product
        I.Click(".liveExample button:eq(0)");
        I.Select(1).From(".liveExample tr select:eq(2)");
        I.Select(4).From(".liveExample tr select:eq(3)");
        I.Enter(8).In(".liveExample td.quantity input:eq(1)");
        I.Expect.Text("$788.64").In(".liveExample tr span:eq(3)");

        // validate totals
        I.Expect.Text("$986.34").In("p.grandTotal span");

        // remove first product
        I.Click(".liveExample a:eq(0)");

        // validate new total
        I.WaitUntil(() => I.Expect.Text("$788.64").In("p.grandTotal span"));
        I.Dispose();
    }

It should find its way to NuGet in the next release which I'm hoping happens this week.

NuGet v2.0: Currently only one call to Bootstrap is supported per test. In v1 we had built-in support for running the same test against all the browsers supported by a provider but found that users preferred to split it out into multiple tests.

The way I manage it with v2 is to have a 'Base' TestClass that has the TestMethods in it. I then extend that once per browser I want to target, and override the constructor to call the appropriate Bootstrap method.

A bit more verbose but very easy to manage.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top