This repository was archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathTodoAppTest.java
More file actions
154 lines (117 loc) · 6.33 KB
/
TodoAppTest.java
File metadata and controls
154 lines (117 loc) · 6.33 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
// Getting started: http://docs.seleniumhq.org/docs/03_webdriver.jsp
// API details: https://github.com/SeleniumHQ/selenium#selenium
// Unirest is the recommended way to interact with RESTful APIs in Java
// http://unirest.io/java.html
// runs test against http://crossbrowsertesting.github.io/todo-app.html
import java.net.URL;
import java.util.List;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import static org.junit.Assert.*;
class TodoAppTest {
static String username = "user%40email.com"; // Your username
static String authkey = "12345"; // Your authkey
String testScore = "unset";
public static void main(String[] args) throws Exception {
TodoAppTest myTest = new TodoAppTest();
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("name", " Todo App Example");
caps.setCapability("build", "1.0");
caps.setCapability("browserName", "Chrome"); // request the latest version of chrome by default
caps.setCapability("platform", "Windows 10"); // To specify version, setCapability("version", "desired version")
caps.setCapability("screen_resolution", "1366x768");
caps.setCapability("record_video", "true");
caps.setCapability("record_network", "false");
RemoteWebDriver driver = new RemoteWebDriver(new URL("http://" + username + ":" + authkey +"@hub.crossbrowsertesting.com:80/wd/hub"), caps);
System.out.println(driver.getSessionId());
// we wrap the test in a try catch loop so we can log assert failures in our system
try {
// load the page url
System.out.println("Loading Url");
driver.get("http://crossbrowsertesting.github.io/todo-app.html");
// maximize the window - DESKTOPS ONLY
//System.out.println("Maximizing window");
//driver.manage().window().maximize();
System.out.println("Checking Box");
driver.findElement(By.name("todo-4")).click();
System.out.println("Checking Another Box");
driver.findElement(By.name("todo-5")).click();
// If both clicks worked, then the following List should be have length 2
List elems = driver.findElements(By.className("done-true"));
// So we'll assert that this is correct.
assertEquals(2, elems.size());
System.out.println("Entering Text");
driver.findElement(By.id("todotext")).sendKeys("Run your first Selenium Test");
driver.findElement(By.id("addbutton")).click();
// Let's also assert that the todo we added is present in the list.
String spanText = driver.findElementByXPath("/html/body/div/div/div/ul/li[6]/span").getText();
assertEquals("Run your first Selenium Test", spanText);
System.out.println("Archiving old todos");
driver.findElement(By.linkText("archive")).click();
// If our archive link worked, then the following list should have length 4.
elems = driver.findElements(By.className("done-false"));
// So will assert that this is true as well.
assertEquals(4, elems.size());
System.out.println("Taking Snapshot");
myTest.takeSnapshot(driver.getSessionId().toString());
// if we get to this point, then all the assertions have passed
// that means that we can set the score to pass in our system
myTest.testScore = "pass";
}
catch(AssertionError ae) {
// if we have an assertion error, take a snapshot of where the test fails
// and set the score to "fail"
String snapshotHash = myTest.takeSnapshot(driver.getSessionId().toString());
myTest.setDescription(driver.getSessionId().toString(), snapshotHash, ae.toString());
myTest.testScore = "fail";
}
finally {
System.out.println("Test complete: " + myTest.testScore);
// here we make an api call to actually send the score
myTest.setScore(driver.getSessionId().toString(), myTest.testScore);
// and quit the driver
driver.quit();
}
}
public JsonNode setScore(String seleniumTestId, String score) throws UnirestException {
// Mark a Selenium test as Pass/Fail
HttpResponse<JsonNode> response = Unirest.put("http://crossbrowsertesting.com/api/v3/selenium/{seleniumTestId}")
.basicAuth(username, authkey)
.routeParam("seleniumTestId", seleniumTestId)
.field("action","set_score")
.field("score", score)
.asJson();
return response.getBody();
}
public String takeSnapshot(String seleniumTestId) throws UnirestException {
/*
* Takes a snapshot of the screen for the specified test.
* The output of this function can be used as a parameter for setDescription()
*/
HttpResponse<JsonNode> response = Unirest.post("http://crossbrowsertesting.com/api/v3/selenium/{seleniumTestId}/snapshots")
.basicAuth(username, authkey)
.routeParam("seleniumTestId", seleniumTestId)
.asJson();
// grab out the snapshot "hash" from the response
String snapshotHash = (String) response.getBody().getObject().get("hash");
return snapshotHash;
}
public JsonNode setDescription(String seleniumTestId, String snapshotHash, String description) throws UnirestException{
/*
* sets the description for the given seleniemTestId and snapshotHash
*/
HttpResponse<JsonNode> response = Unirest.put("http://crossbrowsertesting.com/api/v3/selenium/{seleniumTestId}/snapshots/{snapshotHash}")
.basicAuth(username, authkey)
.routeParam("seleniumTestId", seleniumTestId)
.routeParam("snapshotHash", snapshotHash)
.field("description", description)
.asJson();
return response.getBody();
}
}