Skip to content

Instantly share code, notes, and snippets.

@maxmalakhov
Last active December 29, 2015 17:29
Show Gist options
  • Save maxmalakhov/7704647 to your computer and use it in GitHub Desktop.
Save maxmalakhov/7704647 to your computer and use it in GitHub Desktop.
@Guice(modules = { CommonModule.class })
@Listeners({LogTestListener.class, HTMLReporter.class})
abstract public class AbstractTest<T extends AbstractPage, S extends PageAssertion> {
protected static final Logger LOG = LoggerFactory.getLogger(AbstractTest.class);
private static final BrowserKind BROWSER = BrowserKind.FireFox;
private final WebDriver driver;
private final Class<T> pageClass;
private final Class<S> assertionInterface;
private T page;
private S assertion;
@Inject
private ControlService control;
@Inject
private DomService dom;
@Inject
private ConfigurationService configuration;
protected AbstractTest() {
this.driver = BROWSER.getDriver();
this.pageClass = (Class<T>)
((ParameterizedType)getClass()
.getGenericSuperclass())
.getActualTypeArguments()[0];
this.assertionInterface = (Class<S>)
((ParameterizedType)getClass()
.getGenericSuperclass())
.getActualTypeArguments()[1];
}
protected T page() {
return page;
}
protected S assertion() {
return assertion;
}
private T createPage(Class<T> clazz) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Constructor<T> constructor = clazz.getConstructor(WebDriver.class, ConfigurationService.class, ControlService.class, DomService.class);
return constructor.newInstance(driver, configuration, control, dom);
}
private S createAssertion(Class<S> interfaze) throws NoSuchFieldException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
if(interfaze!=null) {
String fieldName;
switch (AppVersion.getCurrentVersion()) {
case NTG4_5: fieldName = "IMPL_4_5"; break;
case NTG5_0: fieldName = "IMPL_5_0"; break;
default: throw new IllegalArgumentException("Not found a class of AssertionImpl for version "+AppVersion.getCurrentVersion());
}
Field field = interfaze.getField(fieldName);
Class<? extends AbstractAssertion> clazz = (Class<? extends AbstractAssertion>) field.get(interfaze);
Constructor<? extends AbstractAssertion> assertionConstructor = clazz.getConstructor(pageClass);
return (S) assertionConstructor.newInstance(page);
} else {
return null;
}
}
public AbstractPage getPage() {
return page;
};
public WebDriver getDriver() {
return driver;
}
@BeforeSuite
public void init(ITestContext context) {
// Init settings
SettingsUtil.load(context.getCurrentXmlTest().getAllParameters());
SettingsUtil.setEnvironment();
// Set service driver
control.setDriver(driver);
dom.setDriver(driver);
configuration.setDriver(driver);
// Set params
driver.get(Pages.ROOT.getUrl());
configuration.initParams(SettingsUtil.getParams());
// Hack by specific version
configuration.hack();
}
@BeforeClass
public void prepare(ITestContext context) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, ClassNotFoundException, NoSuchFieldException {
// create page
page = createPage(pageClass);
page.open();
// create assertion
assertion = createAssertion(assertionInterface);
// logging
LOG.debug("Test is running with current parameters:");
LOG.debug("URL = '{}' ",driver.getCurrentUrl());
LOG.debug("VIN = '{}' ",configuration.get(ParameterType.VIN));
LOG.debug("MARKET = '{}' ",configuration.get(ParameterType.MARKET));
LOG.debug("LANGUAGE = '{}' ",configuration.get(ParameterType.LANGUAGE));
LOG.debug("LOCATION = '{}' ",configuration.get(ParameterType.LOCATION));
LOG.debug("DESTINATION = '{}' ",configuration.get(ParameterType.DESTINATION));
}
@AfterSuite
public void closeDriver() {
driver.quit();
}
// @BeforeMethod
public void waiter() {
try {
LOG.debug("Waiting before test");
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment