Ruby and Python webdriver bindings


From the launch date Nerrvana could run tests in PHP or Java. Some time ago we added Ruby and Python support, however we only had libraries supporting Selenium 1.

We are glad to tell you that Nerrvana now supports Ruby and Python tests using Selenium 2 (webdriver).


For Python we use version 2.33.0 of Selenium
For Ruby – selenium-webdriver 2.33.0

We also added Rspec framework for Ruby and nose for Python.

After registration, you will see examples of tests in each language we support. If you do not have experience with Selenium, you can use our demo tests as a starting point. If you are a guru, you can still check the examples, just to get an idea of how to kick off test execution, and what parameters are used to create a driver instance.

Or you can check our tests included in the demo account below. All tests go to Wikipedia, search ‘Selenium’ and check if the phrase “This article is about the chemical element.” exists on search results page.

In our sample tests I now keep only the important things. Notice, as before, your tests will most likely not require modification to run in Nerrvana. You only need to use the remote webdriver as a driver. As we mentioned in our documentation – despite the fact that some code uses capabilities with particular browser – Nerrvana will use browsers you specified via API or UI for this test run.

Ruby:

require "rubygems"
require "selenium-webdriver"
 
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome()
driver = Selenium::WebDriver.for(:remote, :desired_capabilities => capabilities)
driver.get "http://en.wikipedia.org"
driver.find_element(:id, "searchInput").send_keys "Selenium"
driver.find_element(:id, "searchInput").submit
if driver.find_element(:tag_name => "body").text.include?("This article is about the chemical element.")
    print 'Test passed'
else
    print 'Test failed'
end
driver.save_screenshot "./screenshot.png"
driver.quit

Python:

View Code PYTHON
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
 
driver = webdriver.Remote(desired_capabilities=webdriver.DesiredCapabilities.CHROME)
driver.get("http://en.wikipedia.org")
driver.find_element_by_id("searchInput").send_keys("Selenium")
driver.find_element_by_id("searchInput").submit()
 
time.sleep(1)
 
if "This article is about the chemical element." in driver.find_element_by_tag_name("body").text:
    print 'Test passed'
else:
    print 'Test failed'
 
driver.get_screenshot_as_file('./screenshot.png')
driver.quit()

Java:

import com.thoughtworks.selenium.*;
import java.io.*;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.*;
import java.net.URL;
 
public class SimpleTest {
    private void simpleWebDriverTest() {
        DesiredCapabilities capability = new DesiredCapabilities();
        capability.setCapability("browserName", "firefox);
 
        try {
            driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
            driver.get("http://en.wikipedia.org");
 
            WebElement element = driver.findElement(By.id("searchInput"));
            element.sendKeys("Selenium");
            element.submit();
 
            if (driver.getPageSource().contains("This article is about the chemical element.")) {
                System.out.println("Test passed");
            } else {
                System.out.println("Test failed");
            }
 
            driver.quit();
        } catch (Exception e) {
            if (driver != null) {
                driver.quit();
            }
            System.out.println("Test failed");
        }
    }
 
    public static void main(String[] args) {
        SimpleTest me = new SimpleTest();
        me.simpleWebDriverTest();
    }
}

PHP:
(note, Nerrvana does not include Selenium libraries for PHP now. So you can use any Selenium bindings you have included in your project files. Our example is for Selenium-RC binding)

require_once 'Selenium.php';
 
$selenium = new Testing_Selenium("*chrome", "http://en.wikipedia.org", "localhost", 4444);
$selenium->start();
$selenium->open("http://en.wikipedia.org");
$selenium->type("searchInput", "Selenium");
$selenium->submit("searchform");
$selenium->waitForPageToLoad("30000");
 
if ($selenium->isTextPresent("This article is about the chemical element.")) {
    echo("Test passed");
} else {
    echo("Test failed");
}
 
$selenium->stop();
Print this post | Home

Comments are closed.