Last active
August 27, 2016 13:52
-
-
Save ova2/0d92d5c0cb95f14440ac12941b768c94 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class AbstractSeleniumTest { | |
// configurable base URL | |
private final String baseUrl = System.getProperty("selenium.baseUrl", | |
"http://localhost:8080/contextRoot/"); | |
private final WebDriver driver; | |
public AbstractSeleniumTest() { | |
// create desired WebDriver | |
driver = new ChromeDriver(); | |
// you can also set here desired capabilities and so on | |
... | |
} | |
/** | |
* The single entry point to prepare and run test flow. | |
*/ | |
@Test | |
public void testIt() throws Exception { | |
LoadablePage lastPageInFlow = null; | |
List <Method> methods = new ArrayList<>(); | |
// Seach methods annotated with FlowOnPage in this and all super classes | |
Class c = this.getClass(); | |
while (c != null) { | |
for (Method method: c.getDeclaredMethods()) { | |
if (method.isAnnotationPresent(FlowOnPage.class)) { | |
FlowOnPage flowOnPage = method.getAnnotation(FlowOnPage.class); | |
// add the method at the right position | |
methods.add(flowOnPage.step() - 1, method); | |
} | |
} | |
c = c.getSuperclass(); | |
} | |
for (Method m: methods) { | |
Class<?>[] pTypes = m.getParameterTypes(); | |
LoadablePage loadablePage = null; | |
if (pTypes != null && pTypes.length > 0) { | |
loadablePage = (LoadablePage) pTypes[0].newInstance(); | |
} | |
if (loadablePage == null) { | |
throw new IllegalArgumentException( | |
"No Page Object as parameter has been found for the method " + | |
m.getName() + ", in the class " + this.getClass().getName()); | |
} | |
// initialize Page Objects Page-Objekte and set parent-child relationship | |
loadablePage.init(this, m, lastPageInFlow); | |
lastPageInFlow = loadablePage; | |
} | |
if (lastPageInFlow == null) { | |
throw new IllegalStateException("Page Object to start the test was not found"); | |
} | |
// start test | |
lastPageInFlow.get(); | |
} | |
/** | |
* Executes the test flow logic on a given page. | |
* | |
* @throws AssertionError can be thrown by JUnit assertions | |
*/ | |
public void executeFlowOnPage(LoadablePage page) { | |
Method m = page.getMethod(); | |
if (m != null) { | |
// execute the method annotated with FlowOnPage | |
try { | |
m.setAccessible(true); | |
m.invoke(this, page); | |
} catch (Exception e) { | |
throw new AssertionError("Method invocation " + m.getName() + | |
", in the class " + page.getClass().getName() + ", failed", e); | |
} | |
} | |
} | |
@After | |
public void tearDown() { | |
// close browser | |
driver.quit(); | |
} | |
/** | |
* This method is invoked by LoadablePage. | |
*/ | |
public String getUrlToGo(String path) { | |
return baseUrl + path; | |
} | |
public WebDriver getDriver() { | |
return driver; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment