Skip to content

Commit 7a66859

Browse files
authored
Update consider_using_a_fluent_api.en.md (SeleniumHQ#1297)
* Update consider_using_a_fluent_api.en.md The Fluent Interface can be implemented much simpler to the same effect. FindBy should no longer be encouraged in examples with Selenium 4.x. Lambdas as wait conditions can be encouraged in examples as the more modern approach with Selenium 4.x. * Fix constructor in example * Remove Assertion from page object Selenium encourages to "generally not throw assertions" within pages objects or page components. The WebDriverWait throws an exception to the same effect. * Update code example in Fluent API (EN) * Update code example in Fluent API (JA) * Update code example in Fluent API (PT-BR) * Update code example in Fluent API (ZH-CN) [deploy site]
1 parent 0772d80 commit 7a66859

File tree

4 files changed

+60
-180
lines changed

4 files changed

+60
-180
lines changed

website_and_docs/content/documentation/test_practices/encouraged/consider_using_a_fluent_api.en.md

Lines changed: 15 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -25,59 +25,29 @@ The Google page object class with this fluent behavior
2525
might look like this:
2626

2727
```java
28-
public class GoogleSearchPage extends LoadableComponent<GoogleSearchPage> {
29-
private final WebDriver driver;
30-
private GSPFluentInterface gspfi;
28+
public abstract class BasePage {
29+
protected WebDriver driver;
3130

32-
public class GSPFluentInterface {
33-
private GoogleSearchPage gsp;
34-
35-
public GSPFluentInterface(GoogleSearchPage googleSearchPage) {
36-
gsp = googleSearchPage;
31+
public BasePage(WebDriver driver) {
32+
this.driver = driver;
3733
}
34+
}
3835

39-
public GSPFluentInterface clickSearchButton() {
40-
gsp.searchButton.click();
41-
return this;
36+
public class GoogleSearchPage extends BasePage {
37+
public GoogleSearchPage(WebDriver driver) {
38+
super(driver);
39+
// Generally do not assert within pages or components.
40+
// Effectively throws an exception if the lambda condition is not met.
41+
new WebDriverWait(driver, Duration.ofSeconds(3)).until(d -> d.findElement(By.id("logo")));
4242
}
4343

44-
public GSPFluentInterface setSearchString( String sstr ) {
45-
clearAndType( gsp.searchField, sstr );
44+
public GoogleSearchPage setSearchString(String sstr) {
45+
driver.findElement(By.id("gbqfq")).sendKeys(sstr);
4646
return this;
4747
}
48-
}
49-
50-
@FindBy(id = "gbqfq") private WebElement searchField;
51-
@FindBy(id = "gbqfb") private WebElement searchButton;
52-
public GoogleSearchPage(WebDriver driver) {
53-
gspfi = new GSPFluentInterface( this );
54-
this.get(); // If load() fails, calls isLoaded() until page is finished loading
55-
PageFactory.initElements(driver, this); // Initialize WebElements on page
56-
}
57-
58-
public GSPFluentInterface withFluent() {
59-
return gspfi;
60-
}
61-
62-
public void clickSearchButton() {
63-
searchButton.click();
64-
}
65-
66-
public void setSearchString( String sstr ) {
67-
clearAndType( searchField, sstr );
68-
}
69-
70-
@Override
71-
protected void isLoaded() throws Error {
72-
Assert.assertTrue("Google search page is not yet loaded.", isSearchFieldVisible() );
73-
}
7448

75-
@Override
76-
protected void load() {
77-
if ( isSFieldPresent ) {
78-
Wait<WebDriver> wait = new WebDriverWait( driver, Duration.ofSeconds(3) );
79-
wait.until( visibilityOfElementLocated( By.id("gbqfq") ) ).click();
49+
public void clickSearchButton() {
50+
driver.findElement(By.id("gbqfb")).click();
8051
}
81-
}
8252
}
8353
```

website_and_docs/content/documentation/test_practices/encouraged/consider_using_a_fluent_api.ja.md

Lines changed: 15 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -23,59 +23,29 @@ gsp.withFluent().setSearchString().clickSearchButton();
2323
この流暢な動作を持つGoogleページオブジェクトクラスは次のようになります。
2424

2525
```java
26-
public class GoogleSearchPage extends LoadableComponent<GoogleSearchPage> {
27-
private final WebDriver driver;
28-
private GSPFluentInterface gspfi;
26+
public abstract class BasePage {
27+
protected WebDriver driver;
2928

30-
public class GSPFluentInterface {
31-
private GoogleSearchPage gsp;
32-
33-
public GSPFluentInterface(GoogleSearchPage googleSearchPage) {
34-
gsp = googleSearchPage;
29+
public BasePage(WebDriver driver) {
30+
this.driver = driver;
3531
}
32+
}
3633

37-
public GSPFluentInterface clickSearchButton() {
38-
gsp.searchButton.click();
39-
return this;
34+
public class GoogleSearchPage extends BasePage {
35+
public GoogleSearchPage(WebDriver driver) {
36+
super(driver);
37+
// Generally do not assert within pages or components.
38+
// Effectively throws an exception if the lambda condition is not met.
39+
new WebDriverWait(driver, Duration.ofSeconds(3)).until(d -> d.findElement(By.id("logo")));
4040
}
4141

42-
public GSPFluentInterface setSearchString( String sstr ) {
43-
clearAndType( gsp.searchField, sstr );
42+
public GoogleSearchPage setSearchString(String sstr) {
43+
driver.findElement(By.id("gbqfq")).sendKeys(sstr);
4444
return this;
4545
}
46-
}
47-
48-
@FindBy(id = "gbqfq") private WebElement searchField;
49-
@FindBy(id = "gbqfb") private WebElement searchButton;
50-
public GoogleSearchPage(WebDriver driver) {
51-
gspfi = new GSPFluentInterface( this );
52-
this.get(); // If load() fails, calls isLoaded() until page is finished loading
53-
PageFactory.initElements(driver, this); // Initialize WebElements on page
54-
}
55-
56-
public GSPFluentInterface withFluent() {
57-
return gspfi;
58-
}
59-
60-
public void clickSearchButton() {
61-
searchButton.click();
62-
}
63-
64-
public void setSearchString( String sstr ) {
65-
clearAndType( searchField, sstr );
66-
}
67-
68-
@Override
69-
protected void isLoaded() throws Error {
70-
Assert.assertTrue("Google search page is not yet loaded.", isSearchFieldVisible() );
71-
}
7246

73-
@Override
74-
protected void load() {
75-
if ( isSFieldPresent ) {
76-
Wait<WebDriver> wait = new WebDriverWait( driver, Duration.ofSeconds(3) );
77-
wait.until( visibilityOfElementLocated( By.id("gbqfq") ) ).click();
47+
public void clickSearchButton() {
48+
driver.findElement(By.id("gbqfb")).click();
7849
}
79-
}
8050
}
8151
```

website_and_docs/content/documentation/test_practices/encouraged/consider_using_a_fluent_api.pt-br.md

Lines changed: 15 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -25,59 +25,29 @@ A classe de objeto da página do Google com este comportamento fluente
2525
pode ser assim:
2626

2727
```java
28-
public class GoogleSearchPage extends LoadableComponent<GoogleSearchPage> {
29-
private final WebDriver driver;
30-
private GSPFluentInterface gspfi;
28+
public abstract class BasePage {
29+
protected WebDriver driver;
3130

32-
public class GSPFluentInterface {
33-
private GoogleSearchPage gsp;
34-
35-
public GSPFluentInterface(GoogleSearchPage googleSearchPage) {
36-
gsp = googleSearchPage;
31+
public BasePage(WebDriver driver) {
32+
this.driver = driver;
3733
}
34+
}
3835

39-
public GSPFluentInterface clickSearchButton() {
40-
gsp.searchButton.click();
41-
return this;
36+
public class GoogleSearchPage extends BasePage {
37+
public GoogleSearchPage(WebDriver driver) {
38+
super(driver);
39+
// Generally do not assert within pages or components.
40+
// Effectively throws an exception if the lambda condition is not met.
41+
new WebDriverWait(driver, Duration.ofSeconds(3)).until(d -> d.findElement(By.id("logo")));
4242
}
4343

44-
public GSPFluentInterface setSearchString( String sstr ) {
45-
clearAndType( gsp.searchField, sstr );
44+
public GoogleSearchPage setSearchString(String sstr) {
45+
driver.findElement(By.id("gbqfq")).sendKeys(sstr);
4646
return this;
4747
}
48-
}
49-
50-
@FindBy(id = "gbqfq") private WebElement searchField;
51-
@FindBy(id = "gbqfb") private WebElement searchButton;
52-
public GoogleSearchPage(WebDriver driver) {
53-
gspfi = new GSPFluentInterface( this );
54-
this.get(); // Se load() falhar, chama isLoaded() até que a página termine de carregar
55-
PageFactory.initElements(driver, this); // Inicializa WebElements na página
56-
}
57-
58-
public GSPFluentInterface withFluent() {
59-
return gspfi;
60-
}
61-
62-
public void clickSearchButton() {
63-
searchButton.click();
64-
}
65-
66-
public void setSearchString( String sstr ) {
67-
clearAndType( searchField, sstr );
68-
}
69-
70-
@Override
71-
protected void isLoaded() throws Error {
72-
Assert.assertTrue("Google search page is not yet loaded.", isSearchFieldVisible() );
73-
}
7448

75-
@Override
76-
protected void load() {
77-
if ( isSFieldPresent ) {
78-
Wait<WebDriver> wait = new WebDriverWait( driver, Duration.ofSeconds(3) );
79-
wait.until( visibilityOfElementLocated( By.id("gbqfq") ) ).click();
49+
public void clickSearchButton() {
50+
driver.findElement(By.id("gbqfb")).click();
8051
}
81-
}
8252
}
8353
```

website_and_docs/content/documentation/test_practices/encouraged/consider_using_a_fluent_api.zh-cn.md

Lines changed: 15 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -21,59 +21,29 @@ gsp.withFluent().setSearchString().clickSearchButton();
2121
Google页面对象类具有这种流畅行为后可能看起来像这样:
2222

2323
```java
24-
public class GoogleSearchPage extends LoadableComponent<GoogleSearchPage> {
25-
private final WebDriver driver;
26-
private GSPFluentInterface gspfi;
24+
public abstract class BasePage {
25+
protected WebDriver driver;
2726

28-
public class GSPFluentInterface {
29-
private GoogleSearchPage gsp;
30-
31-
public GSPFluentInterface(GoogleSearchPage googleSearchPage) {
32-
gsp = googleSearchPage;
27+
public BasePage(WebDriver driver) {
28+
this.driver = driver;
3329
}
30+
}
3431

35-
public GSPFluentInterface clickSearchButton() {
36-
gsp.searchButton.click();
37-
return this;
32+
public class GoogleSearchPage extends BasePage {
33+
public GoogleSearchPage(WebDriver driver) {
34+
super(driver);
35+
// Generally do not assert within pages or components.
36+
// Effectively throws an exception if the lambda condition is not met.
37+
new WebDriverWait(driver, Duration.ofSeconds(3)).until(d -> d.findElement(By.id("logo")));
3838
}
3939

40-
public GSPFluentInterface setSearchString( String sstr ) {
41-
clearAndType( gsp.searchField, sstr );
40+
public GoogleSearchPage setSearchString(String sstr) {
41+
driver.findElement(By.id("gbqfq")).sendKeys(sstr);
4242
return this;
4343
}
44-
}
45-
46-
@FindBy(id = "gbqfq") private WebElement searchField;
47-
@FindBy(id = "gbqfb") private WebElement searchButton;
48-
public GoogleSearchPage(WebDriver driver) {
49-
gspfi = new GSPFluentInterface( this );
50-
this.get(); // If load() fails, calls isLoaded() until page is finished loading
51-
PageFactory.initElements(driver, this); // Initialize WebElements on page
52-
}
53-
54-
public GSPFluentInterface withFluent() {
55-
return gspfi;
56-
}
57-
58-
public void clickSearchButton() {
59-
searchButton.click();
60-
}
61-
62-
public void setSearchString( String sstr ) {
63-
clearAndType( searchField, sstr );
64-
}
65-
66-
@Override
67-
protected void isLoaded() throws Error {
68-
Assert.assertTrue("Google search page is not yet loaded.", isSearchFieldVisible() );
69-
}
7044

71-
@Override
72-
protected void load() {
73-
if ( isSFieldPresent ) {
74-
Wait<WebDriver> wait = new WebDriverWait( driver, Duration.ofSeconds(3) );
75-
wait.until( visibilityOfElementLocated( By.id("gbqfq") ) ).click();
45+
public void clickSearchButton() {
46+
driver.findElement(By.id("gbqfb")).click();
7647
}
77-
}
7848
}
7949
```

0 commit comments

Comments
 (0)