Validating the text from a text box using Selenium RC

Here is the html code input id=”qu” and the text in the text input box is Selenium Wiki.

To validate the text from a text box using Selenium RC you will have to use the Selenium command selenium.getValue(). This will get the value in the location input id=”qu”.
selenium.getValue(“//input[@id=’qu’]”)

If you found this solution helpful or have something extra to add, feel free to share it here by commenting below.

How to verify if there are even number of xpaths in Selenium RC

There was a requirement to check if there are even number of a particular element on a web page. Here is the code to check if there are even number of element xpath on the page. The code is in Visual Studio C#.
try
{
Assert.IsTrue(Convert.ToInt32(selenium.GetXpathCount("//div[@id='elementid']")) % 2 == 0);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}

Overriding the SendKeys Selenium 2 Webdriver

Here is a function I have written that checks if an element is present and then executes the Type/SendKeys command.

public void type(String xpath, String value)
{ if (driver.FindElement(By.XPath(xpath))!=null)
{
driver.FindElement(By.XPath(xpath)).SendKeys(value);
}
else
{
Console.WriteLine("The element is not found: " + xpath);
}
}

Using the function in the test script.

type("//input[@name='p']", "Selenium Wiki");

Using verifyTextPresent in Selenium 2 Webdriver

There is no verifyTextPresent in Selenium 2 Webdriver; I have created a function that you use to verify if a particular text is present on a web page. You can change the name of the function to anything you want.
public void verifyTextPresent(String value)
{
driver.PageSource.Contains(value); ;
}

Using the function in your test script.

try
{
Assert.IsTrue(verifyTextPresent("Selenium Wiki"));
Console.WriteLine("Selenium Wiki test is present on the home page");
}
catch (Exception)
{
Console.WriteLine("Selenium Wiki test is not present on the home page");
}

If you found this solution helpful or have something extra to add, feel free to share it here by commenting below.

Creating getText function for Selenium 2 in C Sharp

As most of you know there is no getText() function in Selenium 2 C#; I have created a function that you can use to get the text associated with a element/xpath.

public String getText(String xpath)
{
String x = driver.FindElement(By.XPath(xpath)).Text;
return x;
}

Here is how I have used the getText() function in my test script:
Console.WriteLine(getText(“//div[@id=’fll’]/a[1]”));
Printing the text assoicated with the xpath “//div[@id=’fll’]/a[1]”