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");

How to add proxy setting to webdriver

I had to run the Webdriver scripts on a Firefox and I had to use a particular proxy to get access to the QA/Dev environment. Here is the code that you can use to change the proxy settings.

FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference("network.proxy.type", 1);
firefoxProfile.setPreference("network.proxy.http", "100.00.100.100");
firefoxProfile.setPreference("network.proxy.http_port", 80);
firefoxProfile.setPreference("network.proxy.ssl", "100.00.100.100");
firefoxProfile.setPreference("network.proxy.ssl_port", 80);
firefoxProfile.setPreference("network.proxy.no_proxies_on", "");
driver = new FirefoxDriver(firefoxProfile);

Make changes to the PROXY_HOST – “100.00.100.100” and the PROXY_PORT – 80. The PROXY_PORT is an Integer and PROXY_HOST is a String.

Top Selenium Tips

Here are top Selenium Tips:


Using Selenium RC commands in Webdriver Selenium 2


Selenium Data Driven Testing with C#


Selenium RC with Visual Studio 2010 and NUnit


Create a Java Selenium RC test script and executing the script


How to install Selenium RC and run using Ruby code


Selenium RC with Java and JUnit


Sample Selenium RC Script using TestNG Eclipse


How to use contains to get the attribute value in xpath

Using Selenium IsSomethingSelected

Here is an example of using Selenium IsSomethingSelected. This example is written is Visual Studio C#.

try
{
Assert.IsTrue(selenium.IsSomethingSelected("ctl00_WebsiteId"));
string selectedLabel = selenium.GetSelectedLabel("ctl00_WebsiteId");
string selectedItem = selenium.GetSelectedValue("ctl00_WebsiteId");
Console.WriteLine(selectedLabel);
Console.WriteLine(selectedItem);
}
catch (Exception e)
{
verificationErrors.Append(e.Message);
}

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

Using Selenium WaitForCondition

In this example I will tell you how you can use selenium WaitForCondition command. Below is the code for waitForElementPresent. You can do the same with WaitForCondition and pass the element id.
Here I will waiting for element “//div[@id=’ctl00_contentSection_LoginPanel’]/h2” then once the element is found continue with testing other things.
This is waitForElementPresent code for element “//div[@id=’ctl00_contentSection_LoginPanel’]/h2”

for (int second = 0;; second++) {
if (second >= 60) Assert.Fail("timeout");
try
{
if (selenium.IsElementPresent
("//div[@id='ctl00_contentSection_LoginPanel']/h2"))
break;
}
catch (Exception)
{}
Thread.Sleep(1000);
}

Alternatively you can use WaitForCondition command and to do the same thing as waitForElementPresent but in a single line.

selenium.WaitForCondition("selenium.isElementPresent
(\"//div[@id='ctl00_contentSection_LoginPanel']/h2\")", "60000");

Thought the code is in C# the command that we are using is selenium.isElementPresent which is Java selenium command. If you use selenium.IsElementPresent you will get the following error “Selenium.SeleniumException : selenium.IsElementPresent is not a function”
[ad#PostAD]