Te Anga Tauira Ahanoa Wharangi me Java me WebDriver

Ko tenei akoranga te waahanga tuarua o te whanaketanga Anga Aunoa Whakamatau.

I te waahanga tuatahi, i ako maatau me pehea te hanga i te anga o te anga mai i te wahanga.

I roto i tenei akoranga, ka hangaia e maatau nga karaehe turanga mo te angamahi.

Panui:Mena kaore ano kia panuihia e koe te waahanga 1 o tenei akoranga, tena koa whaia nga tohutohu kei runga Me pehea te Hanga i tetahi Anga Whakamaatau Aunoa mai i te Ngaru .

Ano hoki, ka taea e koe te whakatauira i te anga turanga mai i taku GitHub repo .

Ka whai ana koe i nga tohutohu kei runga i te waahanga 1 o tenei akoranga, ka taatai ​​ranei i te repo, me whai koe i enei waahanga Maven e wha:

  • rohe - nga akomanga e whakaahua ana i te tauira pakihi
  • anga - nga akomanga matua e hanga ana i te angamahi
  • wharangi-ahanoa - nga waahanga me nga kairapu o ia whaarangi paetukutuku
  • whakamātautau-whakaheke - nga akomanga whakamatautau tuuturu e whakawhirinaki ana ki nga mea whaarangi

Ko ta maatau anga ka anga ki te tauira hoahoa Tauira Ahanoa Wharangi. Ka whakamahia hoki e maatau te WebDriver's Wharehau akomanga ki te arawhiti i nga PaetukutukuTukutuku.

Tauira Ahanoa Wharangi Selenium

Ok, inaianei kua hanga e koe te hanganga turanga, kia tiimata ma te taapiri i nga karaehe turanga.

PaeRaupapa.java

I roto i te anga kōwae, ka hangaia e maatau tetahi akomanga e huaina ana ko BasePage.java. Ko nga akomanga ahanoa whaarangi katoa ka toro atu i te PaeRaupapa, na reira ka riro nga tikanga taatai ​​katoa.

He kaihanga a ta maatau akomanga BasePage ka tango i tetahi taonga WebDriver hei arahi i tetahi taonga WebDriverWait. Ka riro ma te kaihanga te kawenga ki te arawhiti i nga PaetukutukuTohu ma te WharangiKaupapa.

Hei taapiri, ka whai ano hoki maatau i nga tikanga tatari whaihua hei whakahaere i nga momo tatari penei i te WaitForElementToAppear.

Na, ko te akomanga BasePage ka penei:

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)));
} }
Panui:I roto i te karaehe BasePage kaore matou e arawhiti i te ahanoa WebDriver. Ko te arawhiti ka mahia i te akomanga BaseTest.

BaseTest.java

Ko te akomanga BaseTest e mau ana i nga tikanga ki te arawhiti me te whakamutu i te mea PaetukutukuDriver. Na te mea ka whakawhānuitia e nga karaehe Whakamatau te karaehe BaseTest, katahi ka tino mahia te arawhiti. Ma nga Akomanga Whakamatau e tiki noa te ahanoa WebDriver ma te waea i te getDriver() tikanga.

Ano hoki, i a maatau e whakamahi ana i te TestNG, ka taea e maatau te whakamahi i te @BeforeSuite me @AfterSuite tauākī, pēnei i:

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

Te Taapiri i nga Ahanoa Wharangi

Na, i roto i te wharangi-ahanoa kōwae, ka hangaia e maatau a maatau Ahanoa Wharangi tuatahi. Mo tenei akoranga, ka whakamahi ahau i te whaarangi kaainga Google.

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

Kei te kite koe, kaore e nui te mahi o tenei ahanoa whaarangi. He kaihanga noa iho tana ka tango i tetahi taonga WebDriver ka tukuna ki tana kaihanga taapiri.

Ano hoki, kia maumahara kaore tetahi o nga whaarangi whaarangi me nga PaeRaupapa e whakauru i nga Ahanoa PaetukutukuDriver.

I te Wahanga 3 o tenei akoranga, ka taapirihia e maatau nga tikanga hei whakahaere i te mahi rapu a te kaainga a Google.

Te Taapiri i nga Whakamatau

Ka taapirihia e maatau nga whakamatautau i roto i te whakamātautau-whakaheke kōwae.

Ka waihangahia e maatau he akomanga whakamatautau i huaina ko GoogleHomepageTests, me te tikanga whakamatautau kotahi e whakaatu tika ana i ta maatau kaupapa wharangi GoogleHomepage. I tenei wa, kaore he mahi ke atu.

I te Wahanga 3 o tenei akoranga, ka taapirihia e au nga tikanga hei rapu me te arotake i nga hua.

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

Ka whakahaerehia e koe te whakamatautau i runga ake nei i to IDE, me kite e koe he tirotiro me te kati o te tirotiro a Google Chrome.

Me mataara tonu mo te Wahanga 3 o tenei akoranga ka tangohia e au te tauira i runga ake nei ma te taapiri i etahi atu waahanga hei whakawhānui i te anga.

Ka taea e koe te tiki i nga waehere punawai o runga ake nei mai i te GitHub repo . Ko te tumanako kua kitea e koe te pai o te Mahere Paetukutuku WebDriver.

Panui atu: