Generating HTML results for Selenium RC scripts

Here is an example of Google search and the results are saved as a HTML file:

Code for GoogleSearch.cs file:

using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Selenium;
using System.Diagnostics;
namespace SeleniumTests
{
[TestFixture]
public class GoogleSearch
{
private ISelenium selenium;
private StringBuilder verificationErrors;
[SetUp]
public void SetupTest()
{
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.google.com/");
selenium.Start();
verificationErrors = new StringBuilder();
}
[TearDown]
public void TeardownTest()
{
try
{
selenium.Stop();
}
catch (Exception)
{
// Ignore errors if unable to close the browser
}
Assert.AreEqual("", verificationErrors.ToString());
}
[Test]
public void GoogleSearchTest()
{
int stepsPassed = 0;
int stepsFailed = 0;
string TestCase = "Google Search";
Results.writeTCHeading(TestCase);
selenium.Open("/");
selenium.Type("q", "seleniumwiki");
selenium.Click("btnG");
selenium.WaitForPageToLoad("30000");
try
{
Assert.IsTrue(selenium.IsTextPresent("Selenium Wiki"));
Results.passMSG("The text Selenium Wiki is present on the search results page"); //pass message
stepsPassed++;
}
catch (AssertionException e)
{
verificationErrors.Append(e.Message);
Results.failMSG("The text Selenium Wiki is not present on the search results page"); //fail message
stepsFailed++;
}
//write report for the total number of steps passed and failed.
int totalsteps = stepsPassed + stepsFailed;
if (stepsFailed > 0)
{
Results.writereport(TestCase + "test case failed - " + stepsFailed + " of " + totalsteps + " step(s) FAILED.");
}
else
{
Results.writereport(TestCase + "test case passed - " + stepsPassed + "  step(s) PASSED.");
}

selenium.Close();
selenium.ShutDownSeleniumServer();
}
}
}

Code for the Results.cs file:


using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Selenium;

namespace SeleniumTests
{
public class Results
{
static String reporttime = System.DateTime.Now.ToString("yyyy-dd-MM.hh.mm.ss");
public void writeTCHeading(string testcasename)
{
System.String resultsFileName = @"\\c:\Testing\Selenium_Tests\Results\ Test Report Summary.html"; // location were you save the file
System.IO.StreamWriter reportmsg = new System.IO.StreamWriter(resultsFileName, true);
reportmsg.WriteLine("<h2>");
reportmsg.WriteLine(testcasename);
reportmsg.WriteLine("</h2>");
reportmsg.Close();
}

public void passMSG(string passMessage)
{
System.String resultsFileName = @"\\c:\Testing\Selenium_Tests\Results\ Test Report Summary.html"; // location were you save the file
System.IO.StreamWriter reportmsg = new System.IO.StreamWriter(resultsFileName, true);
reportmsg.WriteLine("<br>");
reportmsg.WriteLine("[" + System.DateTime.Now.ToShortDateString() + " " + System.DateTime.Now.ToLongTimeString() + "] " + "<font color='green'>Pass: " + passMessage + "</font>");
reportmsg.Close();
}

public void failMSG(string failMessage)
{
System.String resultsFileName = @"\\c:\Testing\Selenium_Tests\Results\ Test Report Summary.html"; // location were you save the file
System.IO.StreamWriter reportmsg = new System.IO.StreamWriter(resultsFileName, true);
reportmsg.WriteLine("<br>");
reportmsg.WriteLine("[" + System.DateTime.Now.ToShortDateString() + " " + System.DateTime.Now.ToLongTimeString() + "] " + "<font color='red'>Fail: " + failMessage + "</font>");
reportmsg.Close();
}

public void writereport(string reporttext)
{
System.String testSummaryFileName = @"\\c:\Testing\Selenium_Tests\Results\ Test Report Summary.html"; // location were you save the file;
System.IO.StreamWriter reportmsg = new System.IO.StreamWriter(testSummaryFileName, true);
reportmsg.WriteLine("<br>");
reportmsg.WriteLine("[" + " " + System.DateTime.Now.ToShortDateString() + " " + System.DateTime.Now.ToLongTimeString() + "] " + reporttext);
reportmsg.Close();
}
}
}


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>