Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

0 like 0 dislike
1.2k views
by The go-to Tester (473 points)
retagged by
what are the languages supported by selenium webdriver?

1 Answer

0 like 0 dislike
by The go-to Tester (181 points)

Selenium WebDriver supports below languages.

  • C#
  • Haskell
  • Java
  • JavaScript
  • Objective-C
  • Perl
  • PHP
  • Python
  • R
  • Ruby

Here is the sample code from each language I have worked with.

C# sample:

using System.IO;
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
 
namespace WebDriverExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize the Chrome Driver
            using (var driver = new ChromeDriver())
            {
                // Go to the home page
                driver.Navigate().GoToUrl("http://mayurshah.in");
 
                // Take a screenshot and save it into screen.png
                driver.GetScreenshot().SaveAsFile(@"screen.png", ImageFormat.Png);
            }
        }
    }
}
 
Java sample:
 
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.firefox.FirefoxDriver;
 
import java.net.URL;
 
public class WebDriverExample {
 
  
  public static void main(String[] args) throws Exception {
 
   
    WebDriver driver = new FirefoxDriver();
 
    driver.get("http://mayurshah.in");
    System.out.println("title of page is: " + driver.getTitle());
 
    driver.quit();
  }
}
 
Javascript example:
 
var webdriver = require('selenium-webdriver'),
    By = webdriver.By,
    until = webdriver.until;
 
var driver = new webdriver.Builder()
    .forBrowser('firefox')
    .build();
 
driver.get('http://mayurshah.in');
driver.wait(until.titleIs('Mayur Shah), 1000);
driver.quit();
 
Perl sample:
 
use WWW::Selenium;
my $sel = WWW::Selenium->new( host => "localhost",
                                  port => 4444,
                                  browser => "*iexplore",
                                  browser_url => "http://mayurshah.in",
                                );
$sel->start;
$sel->open("http://mayurshah.in");
print $sel->get_title;
$sel->stop;
 
PHP Sample:
 
<?php
class GitHubTest extends PHPUnit_Framework_TestCase {
 
    protected $webDriver;
 
public function setUp()
    {
        $capabilities = array(\WebDriverCapabilityType::BROWSER_NAME => 'firefox');
        $this->webDriver = RemoteWebDriver::create('http://localhost:4444/wd/hub', $capabilities);
    }
 
    protected $url = 'https://mayurshah.in';
 
    public function testGitHubHome()
    {
        $this->webDriver->get($this->url);
        $this->assertContains('Mayur', $this->webDriver->getTitle());
    }    
 
}
?>
 
Python sample
 
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
 
driver = webdriver.Firefox()
driver.get("http://mayurshah.in")
assert "Mayur Shah" not in driver.page_source
driver.close()
 
Ruby Sample:
 
driver = Selenium::WebDriver.for :firefox
driver.navigate.to "http://mayurshah.in"
driver.quit

 

Hope that helps!


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

1.4k questions

1.6k answers

866 comments

1.9k users

...