-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSeleniumBasedRegressionTest.java
More file actions
195 lines (159 loc) · 8.12 KB
/
SeleniumBasedRegressionTest.java
File metadata and controls
195 lines (159 loc) · 8.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package examples;
import com.google.common.collect.ImmutableList;
import com.testfabrik.webmate.javasdk.*;
import com.testfabrik.webmate.javasdk.browsersession.*;
import com.testfabrik.webmate.javasdk.testmgmt.TestRun;
import com.testfabrik.webmate.javasdk.testmgmt.TestRunInfo;
import com.testfabrik.webmate.javasdk.testmgmt.spec.*;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.runners.MethodSorters;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;
import static examples.MyCredentials.*;
import static examples.helpers.Helpers.waitForElement;
import static org.junit.Assert.assertEquals;
/**
* Simple test that shows how to compare two Selenium based browser sessions (aka "expeditions")
* using webmate.
* One of the most interesting use cases for this functionality is to compare
* two versions of a web page or web application, e.g. a stable release deployed at the
* beginning of a sprint and a nightly version.
* <p>
* The following example provides two methods that may individually be executed as a JUnit test.
* <p>
* The first method ("createReferenceExpedition") performs a Selenium test on a web page. After
* the test finishes, the corresponding BrowserSessionId (an alternative / forthcoming terminology
* for this is "ExpeditionId") is saved to the home directory of the executing user in the
* file "webmate_referencesession_id.txt".
* <p>
* The second method ("createExpeditionAndCompareWithReference") performs the same
* Selenium test as above, returns its BrowserSessionId and starts a webmate TestRun of type
* "ExpeditionComparison", which compares the layout of the latter Selenium test with
* that whose ID is stored in "webmate_referencesession_id.txt".
* <p>
* Usually, if both methods are executed in direct succession, webmate won't find any
* differences because the web page did not change between those runs. If you want to
* see differences, you may change the Selenium script for the second run to add synthetic
* deviations.
*/
@RunWith(JUnit4.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class SeleniumBasedRegressionTest {
private WebmateAPISession webmateSession;
private static final String REFERENCE_FILENAME = "webmate_referencesession_id.txt";
private static final BrowserType BROWSERTYPE = BrowserType.CHROME;
private static final String BROWSERVERSION = "106";
private static final Platform PLATFORM = new Platform(PlatformType.WINDOWS, "11", "64");
@Before
public void setup() throws URISyntaxException {
WebmateAuthInfo authInfo = new WebmateAuthInfo(MyCredentials.WEBMATE_APIKEY);
webmateSession = new WebmateAPISession(
authInfo,
WebmateEnvironment.create(new URI(WEBMATE_API_URI)),
WEBMATE_PROJECTID);
}
private BrowserSessionId getReferenceExpeditionId() throws IOException {
String home = System.getProperty("user.home");
Path path = Paths.get(home + "/" + REFERENCE_FILENAME);
if (Files.notExists(path)){
createExpeditionReference();
}
File file = path.toFile();
FileReader reader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(reader);
String browserSessionIdStr = bufferedReader.readLine();
return new BrowserSessionId(UUID.fromString(browserSessionIdStr));
}
private void saveReferenceSessionId(BrowserSessionId expeditionId) throws IOException {
String home = System.getProperty("user.home");
File file = new File(home + "/" + REFERENCE_FILENAME);
FileWriter writer = new FileWriter(file);
writer.write(expeditionId.getValueAsString());
writer.close();
}
public void createExpeditionReference() throws IOException {
BrowserSessionId referenceExpedition = executeTest();
saveReferenceSessionId(referenceExpedition);
}
@Test
public void createExpeditionAndCompareWithReference() throws IOException {
BrowserSessionId compareExpeditionId = executeTest();
BrowserSessionId referenceExpeditionId = getReferenceExpeditionId();
TestRun testRun = webmateSession.testMgmt.startExecutionWithBuilder(ExpeditionComparisonSpec.ExpeditionComparisonCheckBuilder.builder(
"Example Regression Test",
new OfflineExpeditionSpec(referenceExpeditionId),
ImmutableList.of(new OfflineExpeditionSpec(compareExpeditionId))
).withTag("Selenium").withTag("Sprint", "22"));
System.out.println("Starting layout analysis.");
TestRunInfo testRunInfo = testRun.waitForCompletion();
System.out.println("Comparison is finished: " + testRunInfo);
System.out.println("The result is available at: https://app.webmate.io/#/projects/" +
testRunInfo.getProjectId().toString() + "/testlab/testruns/" + testRunInfo.getTestRunId());
}
/**
* Perform Selenium test.
*/
public BrowserSessionId executeTest() throws MalformedURLException {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("browserName", BROWSERTYPE.getValue());
caps.setCapability("version", BROWSERVERSION);
caps.setCapability("platform", PLATFORM.toString());
caps.setCapability(WebmateCapabilityType.API_KEY, WEBMATE_APIKEY);
caps.setCapability(WebmateCapabilityType.PROJECT, WEBMATE_PROJECTID.toString());
RemoteWebDriver driver = new RemoteWebDriver(new URL(WEBMATE_SELENIUM_URL), caps);
BrowserSessionRef browserSession = webmateSession.browserSession
.getBrowserSessionForSeleniumSession(driver.getSessionId().toString());
try {
driver.get("http://www.examplepage.org/version/future");
System.out.println("Selecting some elements....");
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(".container"))).click();
browserSession.createState("after click");
System.out.println("Clicking on something that will redirect us...");
waitForElement(driver, "goto-examplepage").click();
assertEquals("Cross Browser Issues Example", driver.getTitle());
driver.get("http://www.examplepage.org/form_interaction");
System.out.println("Click on link");
waitForElement(driver, "lk").click();
assertEquals("Link Clicked!", waitForElement(driver, ".success").getText());
browserSession.createState("after link");
System.out.println("Clicking on Button");
waitForElement(driver, "bn").click();
System.out.println("Clicking on Checkbox");
waitForElement(driver, "ck").click();
System.out.println("Clicking on RadioButton");
waitForElement(driver, "rd").click();
browserSession.createState("after radio button");
System.out.println("Clicking on Element with a Hover Event");
waitForElement(driver, "mover").click();
System.out.println("Entering some Text...");
waitForElement(driver, "text-input").click();
waitForElement(driver, "text-input").sendKeys("Test test");
System.out.println("Entering more Text...");
waitForElement(driver, "area").click();
waitForElement(driver, "area").sendKeys("Here some more test");
System.out.println("Selenium expedition completed");
} catch(Throwable e) {
e.printStackTrace();
} finally {
driver.quit();
}
return browserSession.browserSessionId;
}
}