Skip to content

Commit 1566880

Browse files
committed
[Java] Updated Design Patterns pages to use codeblocks
1 parent 1309acf commit 1566880

File tree

10 files changed

+1029
-1331
lines changed

10 files changed

+1029
-1331
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
public class ActionBot {
2+
private final WebDriver driver;
3+
4+
public ActionBot(WebDriver driver) {
5+
this.driver = driver;
6+
}
7+
8+
public void click(By locator) {
9+
driver.findElement(locator).click();
10+
}
11+
12+
public void submit(By locator) {
13+
driver.findElement(locator).submit();
14+
}
15+
16+
/**
17+
* Type something into an input field. WebDriver doesn't normally clear these
18+
* before typing, so this method does that first. It also sends a return key
19+
* to move the focus out of the element.
20+
*/
21+
public void type(By locator, String text) {
22+
WebElement element = driver.findElement(locator);
23+
element.clear();
24+
element.sendKeys(text + "\n");
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.example.webdriver;
2+
3+
import org.openqa.selenium.By;
4+
import org.openqa.selenium.WebDriver;
5+
import org.openqa.selenium.WebElement;
6+
7+
public class EditIssue {
8+
9+
private final WebDriver driver;
10+
11+
public EditIssue(WebDriver driver) {
12+
this.driver = driver;
13+
}
14+
15+
public void setTitle(String title) {
16+
WebElement field = driver.findElement(By.id("issue_title")));
17+
clearAndType(field, title);
18+
}
19+
20+
public void setBody(String body) {
21+
WebElement field = driver.findElement(By.id("issue_body"));
22+
clearAndType(field, body);
23+
}
24+
25+
public void setHowToReproduce(String howToReproduce) {
26+
WebElement field = driver.findElement(By.id("issue_form_repro-command"));
27+
clearAndType(field, howToReproduce);
28+
}
29+
30+
public void setLogOutput(String logOutput) {
31+
WebElement field = driver.findElement(By.id("issue_form_logs"));
32+
clearAndType(field, logOutput);
33+
}
34+
35+
public void setOperatingSystem(String operatingSystem) {
36+
WebElement field = driver.findElement(By.id("issue_form_operating-system"));
37+
clearAndType(field, operatingSystem);
38+
}
39+
40+
public void setSeleniumVersion(String seleniumVersion) {
41+
WebElement field = driver.findElement(By.id("issue_form_selenium-version"));
42+
clearAndType(field, logOutput);
43+
}
44+
45+
public void setBrowserVersion(String browserVersion) {
46+
WebElement field = driver.findElement(By.id("issue_form_browser-versions"));
47+
clearAndType(field, browserVersion);
48+
}
49+
50+
public void setDriverVersion(String driverVersion) {
51+
WebElement field = driver.findElement(By.id("issue_form_browser-driver-versions"));
52+
clearAndType(field, driverVersion);
53+
}
54+
55+
public void setUsingGrid(String usingGrid) {
56+
WebElement field = driver.findElement(By.id("issue_form_selenium-grid-version"));
57+
clearAndType(field, usingGrid);
58+
}
59+
60+
public IssueList submit() {
61+
driver.findElement(By.cssSelector("button[type='submit']")).click();
62+
return new IssueList(driver);
63+
}
64+
65+
private void clearAndType(WebElement field, String text) {
66+
field.clear();
67+
field.sendKeys(text);
68+
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.example.webdriver;
2+
import org.openqa.selenium.By;
3+
import org.openqa.selenium.WebDriver;
4+
import org.openqa.selenium.WebElement;
5+
import org.openqa.selenium.support.FindBy;
6+
import org.openqa.selenium.support.PageFactory;
7+
8+
import static junit.framework.Assert.assertTrue;
9+
10+
public class EditIssue extends LoadableComponent<EditIssue> {
11+
12+
private final WebDriver driver;
13+
14+
// By default the PageFactory will locate elements with the same name or id
15+
// as the field. Since the issue_title element has an id attribute of "issue_title"
16+
// we don't need any additional annotations.
17+
private WebElement issue_title;
18+
19+
// But we'd prefer a different name in our code than "issue_body", so we use the
20+
// FindBy annotation to tell the PageFactory how to locate the element.
21+
@FindBy(id = "issue_body") private WebElement body;
22+
23+
public EditIssue(WebDriver driver) {
24+
this.driver = driver;
25+
26+
// This call sets the WebElement fields.
27+
PageFactory.initElements(driver, this);
28+
}
29+
30+
@Override
31+
protected void load() {
32+
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+");
33+
}
34+
35+
@Override
36+
protected void isLoaded() throws Error {
37+
String url = driver.getCurrentUrl();
38+
assertTrue("Not on the issue entry page: " + url, url.endsWith("/new"));
39+
}
40+
41+
public void setHowToReproduce(String howToReproduce) {
42+
WebElement field = driver.findElement(By.id("issue_form_repro-command"));
43+
clearAndType(field, howToReproduce);
44+
}
45+
46+
public void setLogOutput(String logOutput) {
47+
WebElement field = driver.findElement(By.id("issue_form_logs"));
48+
clearAndType(field, logOutput);
49+
}
50+
51+
public void setOperatingSystem(String operatingSystem) {
52+
WebElement field = driver.findElement(By.id("issue_form_operating-system"));
53+
clearAndType(field, operatingSystem);
54+
}
55+
56+
public void setSeleniumVersion(String seleniumVersion) {
57+
WebElement field = driver.findElement(By.id("issue_form_selenium-version"));
58+
clearAndType(field, logOutput);
59+
}
60+
61+
public void setBrowserVersion(String browserVersion) {
62+
WebElement field = driver.findElement(By.id("issue_form_browser-versions"));
63+
clearAndType(field, browserVersion);
64+
}
65+
66+
public void setDriverVersion(String driverVersion) {
67+
WebElement field = driver.findElement(By.id("issue_form_browser-driver-versions"));
68+
clearAndType(field, driverVersion);
69+
}
70+
71+
public void setUsingGrid(String usingGrid) {
72+
WebElement field = driver.findElement(By.id("issue_form_selenium-grid-version"));
73+
clearAndType(field, usingGrid);
74+
}
75+
76+
public IssueList submit() {
77+
driver.findElement(By.cssSelector("button[type='submit']")).click();
78+
return new IssueList(driver);
79+
}
80+
81+
private void clearAndType(WebElement field, String text) {
82+
field.clear();
83+
field.sendKeys(text);
84+
}
85+
}
86+
87+
EditIssue page = new EditIssue(driver).get();
88+
89+
// Further Updates
90+
91+
@Override
92+
protected void load() {
93+
securedPage.get();
94+
95+
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+");
96+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import org.junit.jupiter.api.Test;
2+
3+
public class FooTest {
4+
private EditIssue editIssue;
5+
6+
// Removed @Before
7+
public void prepareComponents() {
8+
WebDriver driver = new FirefoxDriver();
9+
10+
ProjectPage project = new ProjectPage(driver, "selenium");
11+
SecuredPage securedPage = new SecuredPage(driver, project, "example", "top secret");
12+
editIssue = new EditIssue(driver, securedPage);
13+
}
14+
15+
// Removed @Test
16+
public void demonstrateNestedLoadableComponents() {
17+
editIssue.get();
18+
19+
editIssue.title.sendKeys('Title');
20+
editIssue.body.sendKeys('What Happened');
21+
editIssue.setHowToReproduce('How to Reproduce');
22+
editIssue.setLogOutput('Log Output');
23+
editIssue.setOperatingSystem('Operating System');
24+
editIssue.setSeleniumVersion('Selenium Version');
25+
editIssue.setBrowserVersion('Browser Version');
26+
editIssue.setDriverVersion('Driver Version');
27+
editIssue.setUsingGrid('I Am Using Grid');
28+
}
29+
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.example.webdriver;
2+
3+
import org.openqa.selenium.WebDriver;
4+
5+
import static org.junit.Assert.assertTrue;
6+
7+
public class ProjectPage extends LoadableComponent<ProjectPage> {
8+
9+
private final WebDriver driver;
10+
private final String projectName;
11+
12+
public ProjectPage(WebDriver driver, String projectName) {
13+
this.driver = driver;
14+
this.projectName = projectName;
15+
}
16+
17+
@Override
18+
protected void load() {
19+
driver.get("http://" + projectName + ".googlecode.com/");
20+
}
21+
22+
@Override
23+
protected void isLoaded() throws Error {
24+
String url = driver.getCurrentUrl();
25+
26+
assertTrue(url.contains(projectName));
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.example.webdriver;
2+
3+
import org.openqa.selenium.By;
4+
import org.openqa.selenium.NoSuchElementException;
5+
import org.openqa.selenium.WebDriver;
6+
import org.openqa.selenium.WebElement;
7+
8+
import static org.junit.Assert.fail;
9+
10+
public class SecuredPage extends LoadableComponent<SecuredPage> {
11+
12+
private final WebDriver driver;
13+
private final LoadableComponent<?> parent;
14+
private final String username;
15+
private final String password;
16+
17+
public SecuredPage(WebDriver driver, LoadableComponent<?> parent, String username, String password) {
18+
this.driver = driver;
19+
this.parent = parent;
20+
this.username = username;
21+
this.password = password;
22+
}
23+
24+
@Override
25+
protected void load() {
26+
parent.get();
27+
28+
String originalUrl = driver.getCurrentUrl();
29+
30+
// Sign in
31+
driver.get("https://www.google.com/accounts/ServiceLogin?service=code");
32+
driver.findElement(By.name("Email")).sendKeys(username);
33+
WebElement passwordField = driver.findElement(By.name("Passwd"));
34+
passwordField.sendKeys(password);
35+
passwordField.submit();
36+
37+
// Now return to the original URL
38+
driver.get(originalUrl);
39+
}
40+
41+
@Override
42+
protected void isLoaded() throws Error {
43+
// If you're signed in, you have the option of picking a different login.
44+
// Let's check for the presence of that.
45+
46+
try {
47+
WebElement div = driver.findElement(By.id("multilogin-dropdown"));
48+
} catch (NoSuchElementException e) {
49+
fail("Cannot locate user name link");
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)