packagedev.selenium.test_practices;importjava.time.Duration;importorg.junit.jupiter.api.Disabled;importorg.junit.jupiter.api.Test;importorg.openqa.selenium.By;importorg.openqa.selenium.WebDriver;importorg.openqa.selenium.chrome.ChromeDriver;importorg.openqa.selenium.support.ui.WebDriverWait;abstractclassBasePage{protectedWebDriverdriver;publicBasePage(WebDriverdriver){this.driver=driver;}}classGoogleSearchPageextendsBasePage{publicGoogleSearchPage(WebDriverdriver){super(driver);// Generally do not assert within pages or components.// Effectively throws an exception if the lambda condition is not met.newWebDriverWait(driver,Duration.ofSeconds(3)).until(d->d.findElement(By.id("logo")));}publicGoogleSearchPagesetSearchString(Stringsstr){driver.findElement(By.id("gbqfq")).sendKeys(sstr);returnthis;}publicvoidclickSearchButton(){driver.findElement(By.id("gbqfb")).click();}}publicclassFluentApiExample{@Test@Disabled("Illustrative only: targets Google's live, frequently changing search page markup")voidqueryTheGoogleSearchPageUsingAFluentApi(){WebDriverdriver=newChromeDriver();try{driver.get("http://www.google.com/webhp?hl=en&tab=ww");GoogleSearchPagegsp=newGoogleSearchPage(driver);gsp.setSearchString("cheese").clickSearchButton();}finally{driver.quit();}}}
この流暢な動作を持つGoogleページオブジェクトクラスは次のようになります。
abstractclassBasePage{protectedWebDriverdriver;publicBasePage(WebDriverdriver){this.driver=driver;}}classGoogleSearchPageextendsBasePage{publicGoogleSearchPage(WebDriverdriver){super(driver);// Generally do not assert within pages or components.// Effectively throws an exception if the lambda condition is not met.newWebDriverWait(driver,Duration.ofSeconds(3)).until(d->d.findElement(By.id("logo")));}publicGoogleSearchPagesetSearchString(Stringsstr){driver.findElement(By.id("gbqfq")).sendKeys(sstr);returnthis;}publicvoidclickSearchButton(){driver.findElement(By.id("gbqfb")).click();}}
packagedev.selenium.test_practices;importjava.time.Duration;importorg.junit.jupiter.api.Disabled;importorg.junit.jupiter.api.Test;importorg.openqa.selenium.By;importorg.openqa.selenium.WebDriver;importorg.openqa.selenium.chrome.ChromeDriver;importorg.openqa.selenium.support.ui.WebDriverWait;abstractclassBasePage{protectedWebDriverdriver;publicBasePage(WebDriverdriver){this.driver=driver;}}classGoogleSearchPageextendsBasePage{publicGoogleSearchPage(WebDriverdriver){super(driver);// Generally do not assert within pages or components.// Effectively throws an exception if the lambda condition is not met.newWebDriverWait(driver,Duration.ofSeconds(3)).until(d->d.findElement(By.id("logo")));}publicGoogleSearchPagesetSearchString(Stringsstr){driver.findElement(By.id("gbqfq")).sendKeys(sstr);returnthis;}publicvoidclickSearchButton(){driver.findElement(By.id("gbqfb")).click();}}publicclassFluentApiExample{@Test@Disabled("Illustrative only: targets Google's live, frequently changing search page markup")voidqueryTheGoogleSearchPageUsingAFluentApi(){WebDriverdriver=newChromeDriver();try{driver.get("http://www.google.com/webhp?hl=en&tab=ww");GoogleSearchPagegsp=newGoogleSearchPage(driver);gsp.setSearchString("cheese").clickSearchButton();}finally{driver.quit();}}}