1

I'd like to load a browser page that is a static HTML test harness for a page running QUnit tests.

I'd like to get the values from the success/failure <span>s and test with those.

How can I load a page and interrogate elements on it using MSTest/MSUnit?

StuperUser
  • 10,555
  • 13
  • 78
  • 137

1 Answers1

1

If you don't mind starting actual browser, and you don't want to put dependency on SeleniumRC (which is required for C#), you can use WatiN. Below little example from WatiN.

[Test] 
public void SearchForWatiNOnGoogle()
{
  using (var browser = new IE("http://www.google.com"))
  {
    browser.TextField(Find.ByName("q")).TypeText("WatiN");
    browser.Button(Find.ByName("btnG")).Click();

    Assert.IsTrue(browser.ContainsText("WatiN"));
  }
}

Or if you don't want to start real browser on machine you can try Selenium, and HtmlUnit. With Selenium you start HtmlUnit, tell it to load given page, and you read what you need via xpath. For example this is example from Selenium documentation how to do something similar:

using OpenQA.Selenium;
using OpenQA.Selenium.Remote;

class Example
{
    static void Main(string[] args)
    {

        ICapabilities desiredCapabilities = DesiredCapabilities.HtmlUnit();
        IWebDriver driver = new RemoteWebDriver(desiredCapabilities);

        driver.Navigate().GoToUrl("http://google.ca/");

        IWebElement element = driver.FindElement(By.Name("q"));
        element.SendKeys("Cheese!");
        element.Submit();
        System.Console.WriteLine("Page title is: " + driver.Title);
        driver.Quit();
        System.Console.ReadLine();
    }
}  

BTW with selenium you can use real browser also.

On the other hand if this page with results is local file, you just read the file and filter data you need.

yoosiba
  • 2,196
  • 1
  • 18
  • 27
  • Can you move WatiN to the top of this answer as it's much simpler and cleaner. – StuperUser Feb 18 '11 at 10:42
  • I did, although I would try Selenium anyway :P – yoosiba Feb 18 '11 at 15:55
  • I used Selenium when I was in QA and tried to get it introduced, great for recording, but WatiN is perfect for what I needed to get up and running. I'll investigate Selenium to see what it can do that WatiN can't. Cheers yoosiba. – StuperUser Feb 21 '11 at 08:57
  • Well, with Selenium you have SeleniumRC - you can run tests on remote machine. Nice if you have CI server that is shared between projects. But WatiN has better support for dialog windows, like authentication window. Other example is file upload: http://seleniumdeal.blogspot.com/2009/05/uploading-files-using-selenium-with-c.html In general there are some differences, that may have various impact on your work. It all depends on your whole project ecosystem. Recording feature in all tools sucks. For me that did more evil than good for test automation. – yoosiba Feb 21 '11 at 10:17