automatictester

my thoughts on test automation

Selenium and new OperaChromiumDriver

Three months ago I’ve posted an article about how Opera and Selenium work together. OperaChromiumDriver is still in early beta, and this clearly impacts what can be achieved. Nevertheless I’ve managed to provide basic usage examples so that you can start working with it on your own. First of all, you will need the binaries, which can be downloaded here.

Local WebDriver

New Opera driver is based on ChromeDriver, which will let us do some hacks. Just pretend you are using ChromeDriver and only replace path to ChromeDriver binary with path to OperaChromiumDriver:

package com.wordpress.automatictester.opera.local;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class OperaDriverExample {

    private WebDriver driver;

    @BeforeClass
    public void setup() {
        System.setProperty("webdriver.chrome.driver", "/path/to/operadriver");
        driver = new ChromeDriver();
    }

    @Test
    public void testMethod() {
        driver.get("http://www.google.com");
        driver.findElement(By.name("q")).sendKeys("OperaDriver");
        driver.findElement(By.name("q")).submit();
    }

    @AfterClass
    public void quit() {
        driver.quit();
    }
}

RemoteWebDriver

Start OperaDriver:

./operadriver
Starting OperaDriver beta (v 0.1.498e0af382ca0283abd01832a17569991f2ecd52) on port 9515
Only local connections are allowed

Now you can run your test in Opera:

package com.wordpress.automatictester.opera.remote;

import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.net.MalformedURLException;
import java.net.URL;

public class OperaDriverExample {

    private RemoteWebDriver driver;

    @BeforeClass
    public void setup() throws MalformedURLException {
        URL hubUrl = new URL("http://localhost:9515");
        DesiredCapabilities capabilities = new DesiredCapabilities();
        driver = new RemoteWebDriver(hubUrl, capabilities);
    }

    @Test
    public void testMethod() {
        driver.get("http://www.google.com");
        driver.findElement(By.name("q")).sendKeys("OperaDriver");
        driver.findElement(By.name("q")).submit();
    }

    @AfterClass
    public void quit() {
        driver.quit();
    }
}

Please note URL format is different from typical RemoteWebDriver setup. DesiredCapabilities don’t need to specify browser.

Service

You can also run ChromeDriverService and do yet another hack – specify path to OperaDriver:

package com.wordpress.automatictester.opera.service;

import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;

public class OperaDriverExample {

    private ChromeDriverService service;
    private RemoteWebDriver driver;

    @BeforeClass
    public void setup() throws IOException {
        service = new ChromeDriverService.Builder()
                .usingDriverExecutable(new File("/path/to/operadriver"))
                .usingAnyFreePort()
                .build();
        service.start();

        DesiredCapabilities capabilities = new DesiredCapabilities();
        driver = new RemoteWebDriver(service.getUrl(), capabilities);
    }

    @Test
    public void testMethod() {
        driver.get("http://www.google.com");
        driver.findElement(By.name("q")).sendKeys("OperaDriver");
        driver.findElement(By.name("q")).submit();
    }

    @AfterClass
    public void quit() {
        driver.quit();
        service.stop();
    }
}

Also here DesiredCapabilities are irrelevant, although required by constructor.

Advertisements

One response to “Selenium and new OperaChromiumDriver

  1. Luke Kende February 12, 2015 at 9:14 pm

    Thanks for the info! Just started to explore this OperaChromiumDriver option since we haven’t been able to automate testing any of the Opera versions since 12 (Presto based). Too bad we have to skip versions through 25, but glad Opera is throwing us some kind of bone 🙂

    Anyway, wanted to share my code to show use of ChromeOptions which is working except for the “start-maximized” flag. Mainly want to show how one can set the binary path when the operadriver is launched in another process.

    DesiredCapabilities capabilities = DesiredCapabilities.opera();

    ChromeOptions options = new ChromeOptions();
    options.addArguments(“start-maximized”); //does not work – have to use driver.manage().window().maximize();
    options.setBinary(“/path/to/opera.exe”);
    options.addArguments(“proxy-pac-url=http://mywebsite.com/myproxypac”);
    capabilities.setCapability(ChromeOptions.CAPABILITY, options);
    WebDriver driver = new RemoteWebDriver(new URL(“http://127.0.0.1:9515”),capabilities);

    This is working for both Opera 26 & 27 on all Windows and Mac OSs for me.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: