01
Oct

Dependency injection with Guice modules

Google Guice injection (Module)

Pre-requisites:
==================

Create an interface
Create implementations of above interface
Create Google Guice Module for above


eg. 

Step 1: Interface
==================

package DIwithGuice;

public interface EnvInfo {
	String getBrowserFlavor();
}

Step 2: Implementation of interface
====================================

package DIwithGuice;

public class DefaultEnvInfo implements EnvInfo {

	@Override
	public String getBrowserFlavor() {
		return "chrome";
	}
}


Step 3: Guice module to above implementation
=============================================
package DIwithGuice;

import com.google.inject.Binder;
import com.google.inject.Module;

public class DefaultParentModule implements Module {
	@Override
	public void configure(Binder binder) {
		binder.bind(EnvInfo.class).to(DefaultEnvInfo.class);
	}
}


Step 4: Second interface:
=========================

package DIwithGuice;

import org.openqa.selenium.remote.RemoteWebDriver;

public interface WebDriverProducer {
	RemoteWebDriver newInstance();
}


Step 5: second interface implementation (inject's first interface)
==================================================================

package DIwithGuice;

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.RemoteWebDriver;

import javax.inject.Inject;

public class DefaultWebDriverProducerImpl implements WebDriverProducer {

	private String flavor;

	@Inject
	public DefaultWebDriverProducerImpl(EnvInfo info) {
		this.flavor = info.getBrowserFlavor().trim().toLowerCase();
	}

	@Override
	public RemoteWebDriver newInstance() {
		switch (flavor) {
		case "firefox":
			return new FirefoxDriver();
		default:
			return new ChromeDriver();
		}

	}
}


Step 6: Guice module for 2nd interface
=======================================

package DIwithGuice;

import com.google.inject.Binder;
import com.google.inject.Module;

public class WebDriverModule implements Module {
	@Override
	public void configure(Binder binder) {
		binder.bind(WebDriverProducer.class).to(DefaultWebDriverProducerImpl.class);
	}
}

Step 7:  your Actual test:
===========================

package DIwithGuice;

import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;

import javax.inject.Inject;

@Guice(modules = {WebDriverModule.class, DefaultParentModule.class})
@Test
public class SampleTestClass {

	private RemoteWebDriver driver;

	@Inject
	public SampleTestClass(WebDriverProducer producer) {
		System.setProperty("webdriver.chrome.driver", "C:\\Browserdrivers_Selenium\\chromedriver.exe");
		driver = producer.newInstance();
	}

	public void testMethod() {
		driver.get("http://www.google.co.in/");
		Assert.assertEquals("Google", driver.getTitle());
	}

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