Iphepha leNqaku leModeli yeSakhelo ngeJava kunye neWebDriver

Olu qeqesho yinxalenye yesibini yoPhuculo lweNkqubo yokuZenzekelayo yovavanyo.

Kwinxalenye yokuqala, safunda indlela yokwenza ubume besakhelo kwasekuqaleni.

Kule tutorial, siza kwenza iiklasi ezisisiseko zesakhelo.


Phawula:Ukuba awukhange ufunde icandelo 1 lale khokelo, nceda landela imiyalelo kwi Uyenza njani iNkqubo yokuZenzekelayo yoVavanyo ukusuka ekuqaleni .

Ngenye indlela, unokulingisa isakhelo esisiseko ukusuka ku- my IGitHub repo .

Nje ukuba uyilandele imiyalelo kwinxalenye 1 yale khokelo okanye wenze i-repo, kuya kufuneka ube neemodyuli ezine zeMaven:


  • thambeka - Iiklasi ezichaza imodeli yeshishini
  • sikhokelo -iklasi eziphambili ezenza isakhelo
  • izinto zephepha - izinto kunye nabahlali kwiphepha lewebhu ngalinye
  • Uvavanyo olwenziwe ngokutsha -iklasi zovavanyo zoqobo ezixhomekeke kwizinto zephepha

Isakhelo sethu siya kusekelwa kwimodeli yoyilo lweModeli yePhepha. Siza kuphinda sisebenzise iWebDriver Iphepha leFactory iklasi yokuqalisa ii-WebElements.





Imodeli yento yePhepha leSelenium

Kulungile, ngoku wenze ulwakhiwo lwesiseko, masiqale ngokongeza iiklasi ezisisiseko.

BasePage.java

Kwi sikhokelo imodyuli, senza iklasi ebizwa ngokuba yiBasePage.java. Onke amaphepha ezinto zephepha aya kwandisa i-BasePage, yiyo loo nto ilifa zonke iindlela ezisisiseko.

Iklasi yethu yeBasePage iya kuba nomakhi othatha into yeWebDriver yokuqalisa into yeWebDriverWait. Umakhi uya kuba noxanduva lokuqalisa ii-WebElements ngePageFactory.


Ukongeza, siza kuba neendlela zokulinda eziluncedo ekuphatheni izinto ezahlukeneyo zokulinda ezinje nge-WaitForElementToAppear.

Ke, iklasi yeBasePage iya kujongeka:

package rima.framework.core; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.support.PageFactory; import org.openqa.selenium.support.pagefactory.AjaxElementLocatorFactory; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; public class BasePage {
private static final int TIMEOUT = 5;
private static final int POLLING = 100;
protected WebDriver driver;
private WebDriverWait wait;
public BasePage(WebDriver driver) {
this.driver = driver;
wait = new WebDriverWait(driver, TIMEOUT, POLLING);
PageFactory.initElements(new AjaxElementLocatorFactory(driver, TIMEOUT), this);
}
protected void waitForElementToAppear(By locator) {
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
}
protected void waitForElementToDisappear(By locator) {
wait.until(ExpectedConditions.invisibilityOfElementLocated(locator));
}
protected void waitForTextToDisappear(By locator, String text) {
wait.until(ExpectedConditions.not(ExpectedConditions.textToBe(locator, text)));
} }
Phawula:Kwiklasi yeBasePage asiqalisi into yeWebDriver. Ukuqaliswa kwenziwa kwiklasi yeBaseTest.

BaseTest.java

Iklasi yeBaseTest ibambe iindlela zokuqalisa kunye nokuphelisa into yeWebDriver. Kuba zonke iiklasi zovavanyo ziya kulandisa iklasi yeBaseTest, emva koko ukwenziwa kwenziwe ngokungagungqiyo. Iiklasi zovavanyo ziya kufumana ngokulula into yeWebDriver ngokubiza getDriver() indlela.

Kananjalo, njengoko sisebenzisa iTestNG, singasebenzisa @BeforeSuite kunye @AfterSuite izichazi, ezinje:


package rima.framework.core; import io.github.bonigarcia.wdm.ChromeDriverManager; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.testng.annotations.AfterSuite; import org.testng.annotations.BeforeSuite; public class BaseTest {
private WebDriver driver;
@BeforeSuite
public void beforeSuite() {
System.setProperty('headless', 'false'); // You can set this property elsewhere
String headless = System.getProperty('headless');

ChromeDriverManager.chromedriver();
if('true'.equals(headless)) {

ChromeOptions chromeOptions = new ChromeOptions();

chromeOptions.addArguments('--headless');

driver = new ChromeDriver(chromeOptions);
} else {

driver = new ChromeDriver();
}
}
@AfterSuite
public void afterSuite() {
if(null != driver) {

driver.close();

driver.quit();
}
}
public WebDriver getDriver() {
return driver;
} }


Ukongeza iiNjongo zePhepha

Ngoku, kwi izinto zephepha Imodyuli, senza i-Object Page yethu yokuqala. Kule khokelo, ndiza kusebenzisa iphepha lasekhaya likaGoogle.

package rima.pageobjects.homepage; import org.openqa.selenium.WebDriver; import rima.framework.core.BasePage; public class GoogleHomepage extends BasePage {
public GoogleHomepage(WebDriver driver) {
super(driver);
} }

Njengoko ubona, le nto yephepha ayenzi lukhulu. Inomakhi kuphela othatha into yeWebDriver kwaye ayigqithisele kumakhi wayo we-superclass.

Kwakhona, qaphela ukuba akukho nanye yezinto zephepha okanye i-BasePage eqala i-WebDriver Objects.

KwiCandelo 3 lale khokelo, siza kongeza iindlela zokwenza umsebenzi wokukhangela kwiphepha lasekhaya likaGoogle.




Ukongeza iiMvavanyo

Songeza iimvavanyo kwi Uvavanyo olwenziwe ngokutsha imodyuli.

Senza iklasi yovavanyo ebizwa ngokuba yiGoogleHomepageTests, ngendlela enye yovavanyo eqinisekisa into yethu kwiphepha leGoogleHomepage. Okwangoku, ayenzi naliphi na elinye inyathelo.

KwiCandelo 3 lale khokelo, ndiza kongeza iindlela zokukhangela kunye nokuqinisekisa iziphumo.

package rima.tests.homepage; import org.testng.annotations.Test; import rima.framework.core.BaseTest; import rima.pageobjects.homepage.GoogleHomepage; public class GoogleHomepageTests extends BaseTest {
@Test
public void homepageTests() {
GoogleHomepage googleHomepage = new GoogleHomepage(getDriver());
} }

Xa uqhuba olu vavanyo lungasentla kwi-IDE yakho, kuya kufuneka ubone isikhangeli seGoogle Chrome sivulekile kwaye sivale.


Hlala ubukele iCandelo 3 lesi sifundo apho ndiza kuthatha lo mzekelo ungasentla ngokongeza ezinye izinto zokwandisa isikhokelo.

Unokufumana yonke le khowudi yomthombo ingasentla kwi IGitHub repo . Ndiyathemba ukuba uyifumene iluncedo kwisikhokelo seWebhu yeWebhu.

Ukuqhubeka kokufunda: