Peji Chinhu Chinhu Muenzaniso Sisitimu ine Java uye WebDriver

Iyi dzidziso chikamu chechipiri cheTest Automation Sisitimu yekuvandudza.

Muchikamu chekutanga, takadzidza maitiro ekugadzira chimiro chefomu kubva pakutanga.

Muchidzidzo ichi, tichagadzira makirasi ekutanga ehurongwa.

Ongorora:Kana usati waverenga chikamu 1 chedzidziso iyi, ndokumbirawo uteedzere rairo Maitiro Ekugadzira iyo Yekuongorora Yekushandisa Sisitimu kubva kuKutanga .

Neimwe nzira, iwe unogona kuumbiridza hwaro hwaro kubva ku my GitHub repo .

Paunenge uchangoteedzera iwo iwo marongero pachikamu chekutanga cheiyi dzidziso kana kuumba repo, iwe unofanirwa kuve neaya mana Maven module:

  • domain - makirasi anotsanangura bhizinesi modhi
  • chimiro - epakati makirasi ayo anoita chimiro
  • peji-zvinhu - zvinoriumba uye zvinowanisa peji rega rega rewebhu
  • kudzoreredza-bvunzo - chaiwo makirasi ebvunzo anovimba nezvinhu zvepeji

Sisitimu yedu inoenderana nePeji Chinhu Chinhu dhizaini dhizaini. Tichava zvakare tichishandisa yeWebDriver's PejiFactory kirasi kutanga WebElements.



Selenium Peji Chinhu Chinhu Muenzaniso

Ok, ikozvino zvawakagadzira hwaro hwesisitimu, ngatitange nekuwedzera iwo emakirasi ezvigadziko.

BasePage.java

Mu chimiro module, tinogadzira kirasi inonzi BasePage.java. Ese mapeji echinhu makirasi anowedzera iyo BasePage, nekudaro kugara nhaka kwese nzira dzepasi.

Yedu BasePage kirasi ichave nemugadziri uyo anotora WebDriver chinhu kutanga iyo WebDriverWait chinhu. Mugadziri anozove nebasa rekutanga WebElements kuburikidza nePageFactory.

Uye zvakare, isu tichavawo nedzimwe nzira dzekumirira dzekushandisa kubata dzakasiyana siyana dzakamirira senge WaitForElementToAppear.

Saka, iyo BasePage kirasi ichaita senge:

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)));
} }
Ongorora:Mukirasi yeBasePage hatisi kutanga chinhu cheWebDriver. Kutanga kwacho kunoitwa muBaseTest kirasi.

BaseTest.java

BaseTest kirasi inobata nzira dzekutanga uye kumisa iyo WebDriver chinhu. Sezvo ese makirasi eTest achawedzera iyo BaseTest kirasi, saka kutanga kwacho kunoitwa zvizere. Makirasi eTest achangotora chinhu cheWebDriver nekufonera iyo getDriver() nzira.

Zvakare, zvatiri kushandisa TestNG, tinogona kushandisa @BeforeSuite uye @AfterSuite zvirevo, senge:

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;
} }

Kuwedzera izvo Zvinhu zvePeji

Zvino, mu peji-zvinhu module, tinogadzira yedu yekutanga Peji Chinhu. Yedzidziso iyi, ini ndinoshandisa peji reGoogle.

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);
} }

Sezvauri kuona, iri peji chinhu hachiite zvakawanda. Iyo chete ine muvaki iyo inotora WebDriver chinhu uye ndokuchipfuudza kune chayo chikuru superclass muvaki.

Zvekare, ziva kuti hapana yeiyo peji zvinhu kana iyo BasePage inotangisa iyo WebDriver Zvinhu.

MuChikamu chechitatu chedzidziso iyi, isu tinowedzera nzira dzekubata yekutsvaga basa reGoogle peji rekutanga.

Kuwedzera iyo Miedzo

Isu tinowedzera bvunzo mu kudzoreredza-bvunzo module.

Isu tinogadzira kirasi yekuyedza inonzi GoogleHomepageTest, iine imwe bvunzo nzira iyo inongosimbisa yedu GoogleHomepage peji chinhu. Parizvino, hazviite chero chimwe chiito.

MuChikamu 3 chedzidziso iyi, ini ndichawedzera nzira dzekutsvaga uye kuongorora mhedzisiro.

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());
} }

Paunomhanyisa bvunzo iri pamusoro mu IDE yako, iwe unofanirwa kuona iyo Google Chrome browser kuvhura uye kuvhara.

Gara wakatarisira Chikamu chechitatu cheiyi dzidziso ini pandichatora iwo muenzaniso uri pamusoro nekuwedzera nekuwedzera zvimwe zvikamu kuti uwedzere chimiro.

Unogona kuwana ese ari pamusoro sosi kodhi kubva ku GitHub repo . Ndinovimba wawana izvo zviri pamusoro WebDriver Sisitimu dzidziso inobatsira.

Kuenderera mberi kuverenga: