How To Run Your Selenium Tests on Chrome Browser Java, Python, Ruby, C#

Yosuva ArulanthuC#, Java, Python, Ruby, Selenium, Test Automation, TestingLeave a Comment

It’s straightforward to get your tests running locally against Firefox. But when you want to run them against a different browser like Chrome, you download the ChromeDriver and mention the file path before creating the WebDriver object.

Chrome Driver can be downloaded from the below path

Chrome Driver – Take Me to Download Page

For a detailed selenium setup, check https://yosuva.com/2019/08/03/how-to-start-with-selenium

Let’s check an example using ChromeDriver.

Example

Java

// filename: ChromeDriverExample.java
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;

public class ChromeDriverExample {
    WebDriver driver;

    @Before
    public void setUp() throws Exception {
        System.setProperty("webdriver.chrome.driver",
                System.getProperty("user.dir") + "/../../vendor/chrome-driver-2.15/chromedriver_mac32");
        driver = new ChromeDriver();
    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
    }
    @Test
    public void chromeDriverTest() {
        driver.get("http://the-internet.herokuapp.com/");
        assertThat(driver.getTitle(), is(equalTo("The Internet")));
    }

}

Python

# filename: chrome.py
import os
import unittest
from selenium import webdriver


class Chrome(unittest.TestCase):

    def setUp(self):
        chromedriver_path = os.getcwd() + '/vendor/chromedriver'
        self.driver = webdriver.Chrome(chromedriver_path)

    def tearDown(self):
        self.driver.quit()
    def test_example_1(self):
        driver = self.driver
        driver.get('http://the-internet.herokuapp.com/upload')
        assert driver.title == 'The Internet'

if __name__ == "__main__":
    unittest.main()

Ruby

# filename: chrome.rb

require 'selenium-webdriver'
require 'rspec/expectations'
include RSpec::Matchers

def setup
  Selenium::WebDriver::Chrome::Service.executable_path = File.join(Dir.pwd, 'chromedriver')
  @driver = Selenium::WebDriver.for :chrome
end

def teardown
  @driver.quit
end

def run
  setup
  yield
  teardown
end
run do
  @driver.get 'http://the-internet.herokuapp.com/'
  expect(@driver.title).to eql 'The Internet'
end

C#

// filename: Chrome.cs
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System.IO;

public class Chrome
{
    IWebDriver Driver;
    string VendorDirectory = Directory.GetParent(
        Path.GetDirectoryName(typeof(Chrome).Assembly.Location)).
            Parent.FullName + @"\Vendor";

    [SetUp]
    public void SetUp()
    {
        Driver = new ChromeDriver(VendorDirectory);
    }

    [TearDown]
    public void TearDown()
    {
        Driver.Quit();
    }
    [Test]
    public void PageLoads()
    {
        Driver.Navigate().GoToUrl("http://the-internet.herokuapp.com");
        Assert.That(Driver.Title.Equals("The Internet"));
    }
}

When we save this file and run it, it will launch an instance of Chrome, visit the homepage of the-internet, and assert that the page title loaded.

Leave a Reply

Your email address will not be published.