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 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.

Using Selenium RC commands in Webdriver Selenium 2

In this article I will explain you how you can use your existing Selenium RC commands in Webdriver with some changes made to the code. This is more like Webdriver backward compatability with Selenium RC.
Here is a google search example that does a search for selenium wiki and validating the same on the google search results page.

package Htmltests;
import com.thoughtworks.selenium.Selenium;
import org.openqa.selenium.*;
import org.openqa.selenium.htmlunit.*;
import org.openqa.selenium.firefox.*;
import org.openqa.selenium.chrome.*;
import org.openqa.selenium.ie.*;
import org.junit.*;
import static org.junit.Assert.*;
@SuppressWarnings("unused")
public class SW {
public static void main(String[] args) throws InterruptedException { //update this
//public void setUp() { // remove the setUp
//driver = new FirefoxDriver();
// selenium = new WebDriverBackedSelenium(driver, "http://www.google.com/");
//}
WebDriver driver = new FirefoxDriver(); // add this
String baseUrl = "http://www.google.com.au"; // add this
// Create the Selenium implementation
Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl); // add this
//public void tearDown() { // remove the tearDown
// driver.close();
//}
//public void testUntitled() { // remove this
selenium.open("");
selenium.type("q", "selenium wiki");
selenium.click("btnG");
Thread.sleep(5000);
try
{
assertTrue(selenium.isTextPresent("Selenium Wiki"));
System.out.println("Selenium Wiki is present on the google search results page");
}
catch (Exception e)
{
System.out.println("Selenium Wiki is present on the google search results page");
}
driver.close(); // add this to close the webdriver
//} // remove this
}
}

Here is the template that I have created for you removing all the commented sections.


package Htmltests;
import com.thoughtworks.selenium.Selenium;
import org.openqa.selenium.*;
import org.openqa.selenium.htmlunit.*;
import org.openqa.selenium.firefox.*;
import org.openqa.selenium.chrome.*;
import org.openqa.selenium.ie.*;
import org.junit.*;
import static org.junit.Assert.*;
@SuppressWarnings("unused")
public class SW {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
String baseUrl = "http://www.seleniumwiki.com"; // Change the url
// Create the Selenium implementation
Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);
//add your selenium RC test script code here
driver.close(); // add this to close the webdriver
}
}

How to install Selenium RC and run using Ruby code

Here are the steps that you need to follow to install and run Selenium RC using Ruby code.
1. Download the latest version of Selenium RC
2. Unzip the file save it C:\selenium-remote-control-1.0.3
3. command prompt>java -version –> Version should grater than 1.6.0_5
4. command prompt go to –> C:\selenium-remote-control-1.0.3\selenium-server-1.0.3>java -jar selenium-server.jar
5. Download the RubyInstaller – The Ruby Installer is currently available only for the Windows platform. The version that I have downloaded was Ruby 1.9.2-p0
6. Install the RubyInstaller by double slicking on the .exe file. A new Ruby folder is created in your C drive Ruby192
7. Download the selenium-client to c:\Ruby192\bin. The version I have downloaded was selenium-client 1.2.18
8. Set the CLASSPATH C:\Ruby192\bin – This is done by adding System Variables
9. Go to Cmd Prompt –> c:\Ruby192\bin>gem install selenium-client-1.2.18.gem
Once you install selenium-client you should get the following conformation message:
‘1 gem installed
Installing ri documentation for selenium-client-1.2.18…
Installing RDoc documentation for selenium-client-1.2.18…

10. Open a text editor and copy the following code and save the file as googlesearch.rb under your test script folder in this example c:\seleniumrubytest


require "test/unit"
require "rubygems"
gem "selenium-client"
require "selenium/client"
class Untitled < Test::Unit::TestCase def setup @verification_errors = [] @selenium = Selenium::Client::Driver.new \ :host => "localhost",
:port => 4444,
:browser => "*chrome",
:url => "http://www.google.com.au/",
:timeout_in_second => 60
@selenium.start_new_browser_session
end
def teardown
@selenium.close_current_browser_session
assert_equal [], @verification_errors
end
def test_untitled
@selenium.open "/"
@selenium.type "q", "selenium wiki"
@selenium.click "btnG"
@selenium.wait_for_page_to_load "30000"
begin
assert @selenium.is_text_present("Selenium Wiki")
rescue Test::Unit::AssertionFailedError
@verification_errors << $! end end end

11. Go to c:\seleniumrubytest where you have the ruby script and execute the command ruby googlesearch.rb. Also make sure that you have the Selenium RC server running.
12. The selenium script will get executed and you will see the results on the command prompt.
Loaded suite googlesearch
Started
.
Finished in 11.593750 seconds.
1 tests, 2 assertions, 0 failures, 0 errors, 0 skips
Test run options: --seed 23438

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

What is Selenium WebDriver

WebDriver uses a different underlying framework from Selenium’s javascript Selenium-Core. It also provides an alternative API with functionality not supported in Selenium-RC. WebDriver does not depend on a javascript core embedded within the browser, therefore it is able to avoid some long-running
Selenium limitations.
WebDriver’s goal is to provide an API that establishes
• A well-designed standard programming interface for web-app testing.
• Improved consistency between browsers.
• Additional functionality addressing testing problems not well-supported in Selenium 1.0.
The Selenium developers strive to continuously improve Selenium. Integrating WebDriver is another step in that process. The developers of Selenium and of WebDriver felt they could make significant gains for the Open Source test automation community be combining forces and merging their ideas and
technologies. Integrating WebDriver into Selenium is the current result of those efforts.