Skip to content

Commit 3bc170f

Browse files
committed
[Java] Reformated Design Strategies to Use Codeblocks
1 parent 66b3050 commit 3bc170f

File tree

5 files changed

+1080
-1273
lines changed

5 files changed

+1080
-1273
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,284 @@
1+
package com.example.webdriver;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
import org.openqa.selenium.By;
6+
import org.openqa.selenium.NoSuchElementException;
7+
import org.openqa.selenium.WebDriver;
8+
import org.openqa.selenium.WebElement;
9+
import org.openqa.selenium.support.FindBy;
10+
import org.openqa.selenium.support.PageFactory;
11+
import org.openqa.selenium.support.ui.LoadableComponent;
12+
13+
public class EditIssue {
14+
15+
private final WebDriver driver;
16+
17+
public EditIssue(WebDriver driver) {
18+
this.driver = driver;
19+
}
20+
21+
public void setTitle(String title) {
22+
WebElement field = driver.findElement(By.id("issue_title"));
23+
clearAndType(field, title);
24+
}
25+
26+
public void setBody(String body) {
27+
WebElement field = driver.findElement(By.id("issue_body"));
28+
clearAndType(field, body);
29+
}
30+
31+
public void setHowToReproduce(String howToReproduce) {
32+
WebElement field = driver.findElement(By.id("issue_form_repro-command"));
33+
clearAndType(field, howToReproduce);
34+
}
35+
36+
public void setLogOutput(String logOutput) {
37+
WebElement field = driver.findElement(By.id("issue_form_logs"));
38+
clearAndType(field, logOutput);
39+
}
40+
41+
public void setOperatingSystem(String operatingSystem) {
42+
WebElement field = driver.findElement(By.id("issue_form_operating-system"));
43+
clearAndType(field, operatingSystem);
44+
}
45+
46+
public void setSeleniumVersion(String seleniumVersion) {
47+
WebElement field = driver.findElement(By.id("issue_form_selenium-version"));
48+
clearAndType(field, seleniumVersion);
49+
}
50+
51+
public void setBrowserVersion(String browserVersion) {
52+
WebElement field = driver.findElement(By.id("issue_form_browser-versions"));
53+
clearAndType(field, browserVersion);
54+
}
55+
56+
public void setDriverVersion(String driverVersion) {
57+
WebElement field = driver.findElement(By.id("issue_form_browser-driver-versions"));
58+
clearAndType(field, driverVersion);
59+
}
60+
61+
public void setUsingGrid(String usingGrid) {
62+
WebElement field = driver.findElement(By.id("issue_form_selenium-grid-version"));
63+
clearAndType(field, usingGrid);
64+
}
65+
66+
public IssueList submit() {
67+
driver.findElement(By.cssSelector("button[type='submit']")).click();
68+
return new IssueList(driver);
69+
}
70+
71+
private void clearAndType(WebElement field, String text) {
72+
field.clear();
73+
field.sendKeys(text);
74+
}
75+
}
76+
77+
public class IssueList extends LoadableComponent<IssueList> {
78+
private final WebDriver driver;
79+
80+
public IssueList(WebDriver driver) {
81+
this.driver = driver;
82+
}
83+
84+
}
85+
86+
public class EditIssueBetter extends LoadableComponent<EditIssue> {
87+
88+
private final WebDriver driver;
89+
90+
// By default the PageFactory will locate elements with the same name or id
91+
// as the field. Since the issue_title element has an id attribute of "issue_title"
92+
// we don't need any additional annotations.
93+
private WebElement issue_title;
94+
95+
// But we'd prefer a different name in our code than "issue_body", so we use the
96+
// FindBy annotation to tell the PageFactory how to locate the element.
97+
@FindBy(id = "issue_body") private WebElement body;
98+
99+
public EditIssueBetter(WebDriver driver) {
100+
this.driver = driver;
101+
102+
// This call sets the WebElement fields.
103+
PageFactory.initElements(driver, this);
104+
}
105+
106+
@Override
107+
protected void load() {
108+
driver.get("https://github.com/SeleniumHQ/selenium/issues/new?assignees=&labels=I-defect%2Cneeds-triaging&projects=&template=bug-report.yml&title=%5B%F0%9F%90%9B+Bug%5D%3A+");
109+
}
110+
111+
@Override
112+
protected void isLoaded() throws Error {
113+
String url = driver.getCurrentUrl();
114+
Assertions.assertTrue(url.endsWith("/new"), "Not on the issue entry page: " + url);
115+
}
116+
117+
public void setHowToReproduce(String howToReproduce) {
118+
WebElement field = driver.findElement(By.id("issue_form_repro-command"));
119+
clearAndType(field, howToReproduce);
120+
}
121+
122+
public void setLogOutput(String logOutput) {
123+
WebElement field = driver.findElement(By.id("issue_form_logs"));
124+
clearAndType(field, logOutput);
125+
}
126+
127+
public void setOperatingSystem(String operatingSystem) {
128+
WebElement field = driver.findElement(By.id("issue_form_operating-system"));
129+
clearAndType(field, operatingSystem);
130+
}
131+
132+
public void setSeleniumVersion(String seleniumVersion) {
133+
WebElement field = driver.findElement(By.id("issue_form_selenium-version"));
134+
clearAndType(field, seleniumVersion);
135+
}
136+
137+
public void setBrowserVersion(String browserVersion) {
138+
WebElement field = driver.findElement(By.id("issue_form_browser-versions"));
139+
clearAndType(field, browserVersion);
140+
}
141+
142+
public void setDriverVersion(String driverVersion) {
143+
WebElement field = driver.findElement(By.id("issue_form_browser-driver-versions"));
144+
clearAndType(field, driverVersion);
145+
}
146+
147+
public void setUsingGrid(String usingGrid) {
148+
WebElement field = driver.findElement(By.id("issue_form_selenium-grid-version"));
149+
clearAndType(field, usingGrid);
150+
}
151+
152+
public IssueList submit() {
153+
driver.findElement(By.cssSelector("button[type='submit']")).click();
154+
return new IssueList(driver);
155+
}
156+
157+
private void clearAndType(WebElement field, String text) {
158+
field.clear();
159+
field.sendKeys(text);
160+
}
161+
}
162+
163+
public class ProjectPage extends LoadableComponent<ProjectPage> {
164+
165+
private final WebDriver driver;
166+
private final String projectName;
167+
168+
public ProjectPage(WebDriver driver, String projectName) {
169+
this.driver = driver;
170+
this.projectName = projectName;
171+
}
172+
173+
@Override
174+
protected void load() {
175+
driver.get("http://" + projectName + ".googlecode.com/");
176+
}
177+
178+
@Override
179+
protected void isLoaded() throws Error {
180+
String url = driver.getCurrentUrl();
181+
182+
Assertions.assertTrue(url.contains(projectName));
183+
}
184+
}
185+
186+
public class SecuredPage extends LoadableComponent<SecuredPage> {
187+
188+
private final WebDriver driver;
189+
private final LoadableComponent<?> parent;
190+
private final String username;
191+
private final String password;
192+
193+
public SecuredPage(WebDriver driver, LoadableComponent<?> parent, String username, String password) {
194+
this.driver = driver;
195+
this.parent = parent;
196+
this.username = username;
197+
this.password = password;
198+
}
199+
200+
@Override
201+
protected void load() {
202+
parent.get();
203+
204+
String originalUrl = driver.getCurrentUrl();
205+
206+
// Sign in
207+
driver.get("https://www.google.com/accounts/ServiceLogin?service=code");
208+
driver.findElement(By.name("Email")).sendKeys(username);
209+
WebElement passwordField = driver.findElement(By.name("Passwd"));
210+
passwordField.sendKeys(password);
211+
passwordField.submit();
212+
213+
// Now return to the original URL
214+
driver.get(originalUrl);
215+
}
216+
217+
@Override
218+
protected void isLoaded() throws Error {
219+
// If you're signed in, you have the option of picking a different login.
220+
// Let's check for the presence of that.
221+
222+
try {
223+
WebElement div = driver.findElement(By.id("multilogin-dropdown"));
224+
} catch (NoSuchElementException e) {
225+
Assertions.fail("Cannot locate user name link");
226+
}
227+
}
228+
}
229+
230+
// public class FooTest {
231+
// private EditIssue editIssue;
232+
233+
// @Before
234+
// public void prepareComponents() {
235+
// WebDriver driver = new FirefoxDriver();
236+
237+
// ProjectPage project = new ProjectPage(driver, "selenium");
238+
// SecuredPage securedPage = new SecuredPage(driver, project, "example", "top secret");
239+
// editIssue = new EditIssue(driver, securedPage);
240+
// }
241+
242+
// @Test
243+
// public void demonstrateNestedLoadableComponents() {
244+
// editIssue.get();
245+
246+
// editIssue.title.sendKeys('Title');
247+
// editIssue.body.sendKeys('What Happened');
248+
// editIssue.setHowToReproduce('How to Reproduce');
249+
// editIssue.setLogOutput('Log Output');
250+
// editIssue.setOperatingSystem('Operating System');
251+
// editIssue.setSeleniumVersion('Selenium Version');
252+
// editIssue.setBrowserVersion('Browser Version');
253+
// editIssue.setDriverVersion('Driver Version');
254+
// editIssue.setUsingGrid('I Am Using Grid');
255+
// }
256+
257+
// }
258+
259+
public class ActionBot {
260+
private final WebDriver driver;
261+
262+
public ActionBot(WebDriver driver) {
263+
this.driver = driver;
264+
}
265+
266+
public void click(By locator) {
267+
driver.findElement(locator).click();
268+
}
269+
270+
public void submit(By locator) {
271+
driver.findElement(locator).submit();
272+
}
273+
274+
/**
275+
* Type something into an input field. WebDriver doesn't normally clear these
276+
* before typing, so this method does that first. It also sends a return key
277+
* to move the focus out of the element.
278+
*/
279+
public void type(By locator, String text) {
280+
WebElement element = driver.findElement(locator);
281+
element.clear();
282+
element.sendKeys(text + "\n");
283+
}
284+
}

0 commit comments

Comments
 (0)