How to use MessageBox.Show in Selenium C# script to Debug the script 

Here is an example that show you how you can use MessageBox.Show in Selenium C# script to Debug the script.
First Add the reference: System.Windows.Forms
In your script use using System.Windows.Forms;
MessageBox.Show(“The text is Present”);
Here is the script

using System;
using System.Windows.Forms;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using NUnit.Framework;
using Selenium;
namespace SeleniumTests
{
[TestFixture]
public class NewTest
{
private ISelenium selenium;
private StringBuilder verificationErrors;
[SetUp]
public void SetupTest()
{
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "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());
}
public void writepass(string passtext)
{
System.IO.StreamWriter passmsg = new System.IO.StreamWriter("C:\\selenium\\pass.txt", false);
passmsg.WriteLine("[" + System.DateTime.Now.ToShortDateString() + " " + System.DateTime.Now.ToLongTimeString() + "] " + "Pass: " + passtext);
//passmsg.WriteLine(passtext);
passmsg.Close();
}
public void writefail(string failtext)
{
System.IO.StreamWriter failmsg = new System.IO.StreamWriter("C:\\selenium\\fail.txt", false);
failmsg.WriteLine("[" + System.DateTime.Now.ToShortDateString() + " " + System.DateTime.Now.ToLongTimeString() + "] " + "Fail: " + failtext);
//failmsg.WriteLine(failtext);
failmsg.Close();
}
[Test]
public void TheNewTest()
{
selenium.Open("/");
selenium.Type("q", "selenium rc");
selenium.Click("btnG");
selenium.WaitForPageToLoad("30000");
if (selenium.IsTextPresent("Selenium eRemote-Control"))
{
MessageBox.Show("The text is Present");
writepass("the text is present");
// passmsg.Close();
}
else
{
writefail("the text is not present");
//passmsg.Close();
}
selenium.Type("q", "Selenium Remote-Control");
selenium.Click("btnG");
selenium.WaitForPageToLoad("30000");
try
{
Assert.AreEqual("Selenium Remote-Control", selenium.GetText("//ol[@id='rso']/li[1]/h3/a"));
writepass("the text is present");
}
catch (AssertionException)
{
writefail("the text is not present");
}
}
}
}

Submitted By: Pavandeep Puddupakkam


Author: Pavandeep Puddupakkam on July 19, 2010
Category: Selenium RC, Software Testing
Tags: , , , ,

Last articles