Skip to content

Commit d7b2296

Browse files
authored
Add frontend functional tests for testrules (#43)
* Add frontend functional tests for testrules - All functional tests for testrules fronend has been implemented. - Fix minor frontend framework to support firefox download - Fix minor frontend js and html code for uploading to support easier selenium testing
1 parent c736f3a commit d7b2296

File tree

15 files changed

+579
-96
lines changed

15 files changed

+579
-96
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ log/
1616
target/
1717

1818
# Others
19-
derby.log
19+
derby.log
20+
EIBackendInstancesInformation.json

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,12 @@
8989
<dependency>
9090
<groupId>org.seleniumhq.selenium</groupId>
9191
<artifactId>selenium-firefox-driver</artifactId>
92-
<version>3.12.0</version>
92+
<version>3.9.1</version>
9393
</dependency>
9494
<dependency>
9595
<groupId>org.seleniumhq.selenium</groupId>
9696
<artifactId>selenium-server</artifactId>
97-
<version>3.12.0</version>
97+
<version>3.9.1</version>
9898
</dependency>
9999
<!-- https://mvnrepository.com/artifact/org.mock-server/mockserver-client-java -->
100100
<dependency>

src/functionaltest/java/com/ericsson/ei/config/SeleniumConfig.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
package com.ericsson.ei.config;
22

33
import com.ericsson.ei.frontend.exception.OSNotSupportedException;
4+
import com.google.common.io.Files;
45

5-
import java.util.concurrent.TimeUnit;
6+
import java.io.File;
67

78
import org.apache.commons.lang3.SystemUtils;
89
import org.openqa.selenium.firefox.FirefoxDriver;
910
import org.openqa.selenium.firefox.FirefoxOptions;
11+
import org.openqa.selenium.firefox.FirefoxProfile;
1012
import org.slf4j.Logger;
1113
import org.slf4j.LoggerFactory;
1214

1315
public class SeleniumConfig {
1416

1517
private static final Logger log = LoggerFactory.getLogger(SeleniumConfig.class);
18+
private static File tempDownloadDirectory = Files.createTempDir();
1619

1720
public static FirefoxDriver getFirefoxDriver() throws OSNotSupportedException {
1821
FirefoxDriver driver;
22+
FirefoxProfile firefoxProfile = new FirefoxProfile();
23+
24+
firefoxProfile.setPreference("browser.download.folderList",2);
25+
firefoxProfile.setPreference("browser.download.dir", tempDownloadDirectory.getPath());
26+
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/json");
27+
1928
if (SystemUtils.IS_OS_LINUX) {
2029
System.setProperty("webdriver.gecko.driver", "src/functionaltest/resources/geckodriver");
2130
} else if (SystemUtils.IS_OS_WINDOWS) {
@@ -24,13 +33,18 @@ public static FirefoxDriver getFirefoxDriver() throws OSNotSupportedException {
2433
log.error("OS currently not supported.");
2534
throw new OSNotSupportedException();
2635
}
27-
FirefoxOptions firefoxOptions = new FirefoxOptions().setHeadless(true);
36+
FirefoxOptions firefoxOptions = new FirefoxOptions()
37+
.setHeadless(true)
38+
.setProfile(firefoxProfile);
2839

2940
driver = new FirefoxDriver(firefoxOptions);
30-
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
3141
return driver;
3242
}
3343

44+
public static File getTempDownloadDirectory() {
45+
return tempDownloadDirectory;
46+
}
47+
3448
public static String getBaseUrl(int randomServerPort) {
3549
String baseUrl = "http://localhost:" + randomServerPort;
3650
return baseUrl;

src/functionaltest/java/com/ericsson/ei/frontend/SeleniumBaseClass.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.junit.Assert.fail;
44

5+
import java.io.File;
56
import java.io.IOException;
67
import java.nio.charset.StandardCharsets;
78
import java.nio.file.Files;
@@ -10,13 +11,14 @@
1011
import com.ericsson.ei.config.SeleniumConfig;
1112

1213
import org.apache.http.impl.client.CloseableHttpClient;
14+
import org.apache.tomcat.util.http.fileupload.FileUtils;
1315
import org.junit.After;
1416
import org.junit.Before;
1517
import org.junit.Ignore;
1618
import org.junit.runner.RunWith;
1719
import org.mockito.InjectMocks;
1820
import org.mockito.MockitoAnnotations;
19-
import org.openqa.selenium.WebDriver;
21+
import org.openqa.selenium.firefox.FirefoxDriver;
2022
import org.springframework.beans.factory.annotation.Autowired;
2123
import org.springframework.boot.test.context.SpringBootTest;
2224
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
@@ -41,7 +43,7 @@ public class SeleniumBaseClass {
4143
@Autowired
4244
WebController webController;
4345

44-
protected WebDriver driver;
46+
protected FirefoxDriver driver;
4547
protected String baseUrl;
4648

4749
private StringBuffer verificationErrors = new StringBuffer();
@@ -50,20 +52,24 @@ public class SeleniumBaseClass {
5052
public void setUp() throws Exception {
5153
MockitoAnnotations.initMocks(this);
5254
webController.setFrontendServicePort(randomServerPort);
55+
5356
driver = SeleniumConfig.getFirefoxDriver();
5457
baseUrl = SeleniumConfig.getBaseUrl(randomServerPort);
5558
}
5659

5760
@After
5861
public void tearDown() throws Exception {
62+
File tempDownloadDirectory = SeleniumConfig.getTempDownloadDirectory();
63+
FileUtils.deleteDirectory(tempDownloadDirectory);
64+
5965
driver.quit();
6066
String verificationErrorString = verificationErrors.toString();
6167
if (!verificationErrorString.equals("")) {
6268
fail(verificationErrorString);
6369
}
6470
}
6571

66-
protected String getResponseObjectFromFile(String filepath) throws IOException {
67-
return new String(Files.readAllBytes(Paths.get(filepath)), StandardCharsets.UTF_8);
72+
protected String getJSONStringFromFile(String filepath) throws IOException {
73+
return new String(Files.readAllBytes(Paths.get(filepath)), StandardCharsets.UTF_8).replaceAll("[\\r\\n ]", "");
6874
}
6975
}

src/functionaltest/java/com/ericsson/ei/frontend/TemplateTestCase.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.ericsson.ei.frontend;
22

33
import org.junit.*;
4+
import org.openqa.selenium.support.ui.WebDriverWait;
45

56
import static org.junit.Assert.*;
67

@@ -18,13 +19,14 @@ public void testTemplateTestCase() throws Exception {
1819
assertEquals("Eiffel Intelligence", indexPageObject.getTitle());
1920

2021
//The lines below contains selenium interaction with mocking
21-
String response = this.getResponseObjectFromFile(
22+
String response = this.getJSONStringFromFile(
2223
"src/functionaltest/resources/responses/SubscriptionObjects.json");
24+
new WebDriverWait(driver, 10).until((webdriver) -> indexPageObject.presenceOfReloadButton());
2325
indexPageObject.clickReloadButton(response);
2426

2527
//Click on test rules page button and verify that it is opened
2628
TestRulesPage testRulesPage = indexPageObject.clickTestRulesPage();
27-
assertEquals("Test Rules", testRulesPage.getTestRulesHeader());
29+
new WebDriverWait(driver, 10).until((webdriver) -> testRulesPage.presenceOfTestRulesHeader());
2830
}
2931

3032
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.ericsson.ei.frontend;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.io.File;
6+
import java.nio.file.Files;
7+
import java.nio.file.Paths;
8+
import java.util.concurrent.TimeUnit;
9+
10+
import org.junit.Test;
11+
import org.openqa.selenium.support.ui.WebDriverWait;
12+
13+
import com.ericsson.ei.config.SeleniumConfig;
14+
import com.ericsson.ei.frontend.pageobjects.IndexPage;
15+
import com.ericsson.ei.frontend.pageobjects.TestRulesPage;
16+
17+
public class TestRulesFunctionality extends SeleniumBaseClass {
18+
19+
private static final String DOWNLOADEDRULESTEMPLATEFILEPATH = String.join(
20+
File.separator, SeleniumConfig.getTempDownloadDirectory().getPath(), "rulesTemplate.json");
21+
22+
private static final String RULESTEMPLATEFILEPATH = String.join(
23+
File.separator, "src", "functionaltest", "resources", "responses", "RulesTemplateObject.json");
24+
private static final String DOWNLOADEDRULESFILEPATH = String.join(File.separator, SeleniumConfig.getTempDownloadDirectory().getPath(), "rules.json");
25+
26+
private static final String DOWNLOADEDEVENTSTEMPLATEFILEPATH = String.join(
27+
File.separator, SeleniumConfig.getTempDownloadDirectory().getPath(), "eventsTemplate.json");
28+
private static final String EVENTSTEMPLATEFILEPATH = String.join(
29+
File.separator, "src", "functionaltest", "resources", "responses", "EventsTemplateObject.json");
30+
31+
private static final String AGGREGATEDOBJECTFILEPATH = String.join(
32+
File.separator, "src", "functionaltest", "resources", "responses", "AggregatedObjectResponse.json");
33+
@Test
34+
public void testJourneyToFindAggregatedObjectButton() throws Exception {
35+
// Load index page and wait for it to load
36+
IndexPage indexPageObject = new IndexPage(mockedHttpClient, driver, baseUrl);
37+
indexPageObject.loadPage();
38+
TimeUnit.SECONDS.sleep(3);
39+
40+
// Verify that we can navigate to test rules page
41+
TestRulesPage testRulesPage = indexPageObject.clickTestRulesPage();
42+
new WebDriverWait(driver, 10).until((webdriver) -> testRulesPage.presenceOfTestRulesHeader());
43+
44+
// Verify that "download rules template" button works
45+
String mockedResponse = this.getJSONStringFromFile(RULESTEMPLATEFILEPATH);
46+
new WebDriverWait(driver, 10).until((webdriver) -> testRulesPage.presenceOfClickDownloadRulesTemplateButton());
47+
testRulesPage.clickDownloadRulesTemplate(mockedResponse);
48+
new WebDriverWait(driver, 10).until((webdriver) -> Files.exists(Paths.get(DOWNLOADEDRULESTEMPLATEFILEPATH)));
49+
String downloadedRulesTemplate = this.getJSONStringFromFile(DOWNLOADEDRULESTEMPLATEFILEPATH);
50+
assertEquals(mockedResponse, downloadedRulesTemplate);
51+
52+
// Verify that uploading the downloaded template file works.
53+
testRulesPage.uploadRulesTemplate(DOWNLOADEDRULESTEMPLATEFILEPATH);
54+
new WebDriverWait(driver, 10).until((webdriver) -> testRulesPage.presenceOfRuleNumber(2));
55+
String firstRule = testRulesPage.getFirstRuleText();
56+
assertEquals(true, downloadedRulesTemplate.contains(firstRule));
57+
58+
// Verify that it is possible to download rules
59+
testRulesPage.clickDownloadRulesButton();
60+
new WebDriverWait(driver, 10).until((webdriver) -> Files.exists(Paths.get(DOWNLOADEDRULESFILEPATH)));
61+
String downloadedRules = this.getJSONStringFromFile(DOWNLOADEDRULESFILEPATH);
62+
assertEquals(downloadedRulesTemplate, downloadedRules);
63+
64+
// Verify that add rule button works
65+
testRulesPage.clickAddRuleButton();
66+
new WebDriverWait(driver, 10).until((webdriver) -> testRulesPage.presenceOfRuleNumber(3));
67+
68+
// Verify that removing a rule works
69+
testRulesPage.clickRemoveRuleNumber(3);
70+
new WebDriverWait(driver, 10).until((webdriver) -> testRulesPage.presenceOfRuleNumber(3) == false);
71+
72+
// Verify that "download events template" button works
73+
String downloadEventsTemplateMockedResponse = this.getJSONStringFromFile(EVENTSTEMPLATEFILEPATH);
74+
testRulesPage.clickDownloadEventsTemplate(downloadEventsTemplateMockedResponse);
75+
new WebDriverWait(driver, 10).until((webdriver) -> Files.exists(Paths.get(DOWNLOADEDEVENTSTEMPLATEFILEPATH)));
76+
String downloadedEventsTemplate = this.getJSONStringFromFile(DOWNLOADEDEVENTSTEMPLATEFILEPATH);
77+
assertEquals(downloadEventsTemplateMockedResponse, downloadedEventsTemplate);
78+
79+
// Verify that uploading the downloaded template file works.
80+
testRulesPage.uploadEventsTemplate(DOWNLOADEDEVENTSTEMPLATEFILEPATH);
81+
new WebDriverWait(driver, 10).until((webdriver) -> testRulesPage.presenceOfEventNumber(2));
82+
String firstEvent = testRulesPage.getFirstEventText();
83+
assertEquals(true, downloadedEventsTemplate.contains(firstEvent));
84+
85+
// Verify that add rule button works
86+
testRulesPage.clickAddEventButton();
87+
new WebDriverWait(driver, 10).until((webdriver) -> testRulesPage.presenceOfEventNumber(3));
88+
89+
// Verify that removing a rule works
90+
testRulesPage.clickRemoveEventNumber(3);
91+
new WebDriverWait(driver, 10).until((webdriver) -> testRulesPage.presenceOfEventNumber(3) == false);
92+
93+
// Verify that find aggregated object button works
94+
String findAggregatedObjectResponse = this.getJSONStringFromFile(AGGREGATEDOBJECTFILEPATH);
95+
testRulesPage.clickFindAggregatedObject(findAggregatedObjectResponse);
96+
assertEquals(findAggregatedObjectResponse, testRulesPage.getAggregatedResultData());
97+
}
98+
}

src/functionaltest/java/com/ericsson/ei/frontend/pageobjects/IndexPage.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
import org.apache.http.impl.client.CloseableHttpClient;
77
import org.mockito.Mockito;
88
import org.openqa.selenium.By;
9-
import org.openqa.selenium.WebDriver;
9+
import org.openqa.selenium.NoSuchElementException;
1010
import org.openqa.selenium.WebElement;
11+
import org.openqa.selenium.firefox.FirefoxDriver;
1112

1213
import java.io.IOException;
1314

1415
public class IndexPage extends PageBaseClass {
15-
public IndexPage(CloseableHttpClient mockedHttpClient, WebDriver driver,
16+
public IndexPage(CloseableHttpClient mockedHttpClient, FirefoxDriver driver,
1617
String baseUrl) throws ClientProtocolException, IOException {
1718
super(mockedHttpClient, driver, baseUrl);
1819
}
@@ -44,4 +45,13 @@ public void clickReloadButton(String responseData) throws ClientProtocolExceptio
4445
WebElement reloadButton = driver.findElement(By.className("table_reload"));
4546
reloadButton.click();
4647
}
48+
49+
public Object presenceOfReloadButton() {
50+
try {
51+
driver.findElement(By.className("table_reload"));
52+
return true;
53+
} catch (NoSuchElementException e){
54+
return false;
55+
}
56+
}
4757
}

src/functionaltest/java/com/ericsson/ei/frontend/pageobjects/PageBaseClass.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import org.apache.http.message.BasicStatusLine;
1616
import org.mockito.Mockito;
1717
import org.openqa.selenium.JavascriptExecutor;
18-
import org.openqa.selenium.WebDriver;
18+
import org.openqa.selenium.firefox.FirefoxDriver;
1919
import org.openqa.selenium.support.PageFactory;
2020
import org.openqa.selenium.support.ui.ExpectedCondition;
2121
import org.openqa.selenium.support.ui.WebDriverWait;
@@ -24,11 +24,11 @@ public class PageBaseClass {
2424
CloseableHttpClient mockedHttpClient;
2525
CloseableHttpResponse mockedHttpResponse;
2626

27-
protected WebDriver driver;
27+
protected FirefoxDriver driver;
2828
protected String baseUrl;
2929

3030
public PageBaseClass(CloseableHttpClient mockedHttpClient,
31-
WebDriver driver, String baseUrl) throws ClientProtocolException, IOException {
31+
FirefoxDriver driver, String baseUrl) throws ClientProtocolException, IOException {
3232
super();
3333
this.mockedHttpClient = mockedHttpClient;
3434
this.driver = driver;

0 commit comments

Comments
 (0)