Skip to content

Commit

Permalink
Refactored GalenPageActionRunJavascriptTest to use MockedDriver
Browse files Browse the repository at this point in the history
  • Loading branch information
ishubin committed Feb 1, 2015
1 parent bd5f2ea commit 0962076
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package net.mindengine.galen.components.mocks.driver;

import com.fasterxml.jackson.databind.ObjectMapper;
import net.mindengine.galen.components.validation.MockedPage;
import net.mindengine.galen.utils.GalenUtils;
import net.mindengine.rainbow4j.Rainbow4J;
import org.openqa.selenium.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ public void submit() {
@Override
public void sendKeys(CharSequence... charSequences) {

StringBuilder builder = new StringBuilder();
for (CharSequence charSequence : charSequences) {
builder.append(charSequence.toString());
}

registerEvent("#sendKeys: " + builder.toString());
}

@Override
Expand Down Expand Up @@ -113,4 +119,13 @@ public Dimension getSize() {
public String getCssValue(String s) {
return null;
}

public List<String> getMockedEvents() {
return this.item.getMockedEvents();
}

private void registerEvent(String event) {
this.item.getMockedEvents().add(event);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.openqa.selenium.WebElement;

import java.lang.reflect.Field;
import java.util.LinkedList;
import java.util.List;

public class MockedPageItem {
private String locator;
Expand All @@ -28,6 +30,7 @@ public class MockedPageItem {
private String locatorType;
private String locatorValue;
private boolean visible = true;
private List<String> mockedEvents = new LinkedList<String>();

private String getLocatorType() {
return locatorType;
Expand Down Expand Up @@ -113,4 +116,8 @@ public boolean isVisible() {
public void setVisible(boolean visible) {
this.visible = visible;
}

public List<String> getMockedEvents() {
return this.mockedEvents;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,40 @@
package net.mindengine.galen.tests.action;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.is;
import static org.mockito.Matchers.contains;

import net.mindengine.galen.browser.Browser;
import net.mindengine.galen.browser.SeleniumBrowser;
import net.mindengine.galen.components.mocks.driver.MockedDriver;
import net.mindengine.galen.components.mocks.driver.MockedDriverElement;
import net.mindengine.galen.reports.TestReport;
import net.mindengine.galen.suite.GalenPageTest;
import net.mindengine.galen.suite.actions.GalenPageActionRunJavascript;
import net.mindengine.galen.tests.util.WebDriverFactory;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;

public class GalenPageActionRunJavascriptTest {
private static final String TEST_URL = "file://" + GalenPageActionCheckTest.class.getResource("/html/page-for-js-check.html").getPath();
private static final String TEST_URL = "/GalenPageActionRunJavascriptTest/page.json";

@Test public void shouldRun_javascriptFile_andPerformActions_onBrowser() throws Exception {
WebDriver driver = WebDriverFactory.getInstance();
MockedDriver driver = new MockedDriver();
Browser browser = new SeleniumBrowser(driver);
browser.load(TEST_URL);

WebElement element = driver.findElement(By.id("search-query"));
assertThat("Search input should not contain any text yet", element.getAttribute("value"), is(""));


GalenPageActionRunJavascript action = new GalenPageActionRunJavascript(getClass().getResource("/scripts/to-run-1.js").getFile());
action.setJsonArguments("{prefix: 'This was'}");

action.execute(new TestReport(), browser, new GalenPageTest(), null);

assertThat("Search input should contain text", element.getAttribute("value"), is("This was typed by a selenium from javascript text from imported script"));
// TODO tear down after class!!
WebDriverFactory.tearDown();


MockedDriverElement webElement = (MockedDriverElement) driver.findElement(By.id("search-query"));
assertThat("Mocked events of element should be",
webElement.getMockedEvents(),
hasItems("#sendKeys: This was typed by a selenium from javascript text from imported script"));
}

}
16 changes: 16 additions & 0 deletions src/test/resources/GalenPageActionRunJavascriptTest/page.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"title": "GalenPageActionCheckTest page",
"items": [
{
"locator": "id: header",
"area": [0, 0, 400, 140]
},


{
"locator": "id: search-query",
"area": [0, 160, 410, 47]
}

]
}

0 comments on commit 0962076

Please sign in to comment.