Skip to content

Commit

Permalink
define common methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ch3yne committed Mar 18, 2024
1 parent 2ec6968 commit cefcdc2
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class MetalakePageTest extends AbstractWebIT {
@BeforeEach
public void beforeEachTest() {
try {
Thread.sleep(metalakePage.sleepTimeMillis);
Thread.sleep(AbstractWebIT.SLEEP_MILLIS);
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
import org.openqa.selenium.support.ui.WebDriverWait;

public class MetalakePage extends AbstractWebIT {
public int sleepTimeMillis = 1_000;

@FindBy(
xpath =
"//div[contains(@class, 'MuiDataGrid-main')]//div[contains(@class, 'MuiDataGrid-virtualScroller')]//div[@role='rowgroup']")
Expand Down Expand Up @@ -57,10 +55,11 @@ public class MetalakePage extends AbstractWebIT {
@FindBy(xpath = "//button[@data-refer='add-metalake-props']")
public WebElement addMetalakePropsBtn;

@FindBy(xpath = "//button[@aria-label='Go to next page']")
@FindBy(xpath = "//div[@data-refer='metalake-table-grid']//button[@aria-label='Go to next page']")
public WebElement nextPageBtn;

@FindBy(xpath = "//button[@aria-label='Go to previous page']")
@FindBy(
xpath = "//div[@data-refer='metalake-table-grid']//button[@aria-label='Go to previous page']")
public WebElement prevPageBtn;

public MetalakePage() {
Expand Down Expand Up @@ -94,8 +93,14 @@ public void setMetalakeCommentField(String commentField) {
}

public void setQueryInput(String queryInput) {
clearQueryInput();
queryMetalakeInput.sendKeys(queryInput);
try {
Thread.sleep(AbstractWebIT.SLEEP_MILLIS);
clearQueryInput();
queryMetalakeInput.sendKeys(queryInput);
Thread.sleep(AbstractWebIT.SLEEP_MILLIS);
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
}

public void clearQueryInput() {
Expand All @@ -107,26 +112,31 @@ public void clearQueryInput() {
public void clickDeleteMetalakeBtn(String name) {
try {
String xpath = "//button[@data-refer='delete-metalake-" + name + "']";
WebElement deleteMetalakeBtn = driver.findElement(By.xpath(xpath));
WebDriverWait wait = new WebDriverWait(driver, AbstractWebIT.MAX_TIMEOUT);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(xpath)));
deleteMetalakeBtn.click();
Thread.sleep(sleepTimeMillis);
waitClickable(By.xpath(xpath), AbstractWebIT.MAX_TIMEOUT);
clickAndWait(By.xpath(xpath));
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
}

public void clickViewMetalakeBtn(String name) {
String xpath = "//button[@data-refer='view-metalake-" + name + "']";
WebElement viewMetalakeBtn = driver.findElement(By.xpath(xpath));
viewMetalakeBtn.click();
try {
String xpath = "//button[@data-refer='view-metalake-" + name + "']";
waitClickable(By.xpath(xpath), AbstractWebIT.MAX_TIMEOUT);
clickAndWait(By.xpath(xpath));
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
}

public void clickEditMetalakeBtn(String name) {
String xpath = "//button[@data-refer='edit-metalake-" + name + "']";
WebElement editMetalakeBtn = driver.findElement(By.xpath(xpath));
editMetalakeBtn.click();
try {
String xpath = "//button[@data-refer='edit-metalake-" + name + "']";
waitClickable(By.xpath(xpath), AbstractWebIT.MAX_TIMEOUT);
clickAndWait(By.xpath(xpath));
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
}

public void clickMetalakeLink(String name) {
Expand Down Expand Up @@ -237,9 +247,6 @@ public boolean verifyShowMetalakeDetails(String name) {

public boolean verifyEmptyMetalake() {
try {
// To prevent errors in actions, it is necessary to wait for the completion of frontend delay
// animations before proceeding with the next step of operation verification.
Thread.sleep(sleepTimeMillis);
String xpath =
"//div[@data-refer='metalake-table-grid']//div[contains(@class, 'MuiDataGrid-overlay')]";

Expand All @@ -260,12 +267,8 @@ public boolean verifyEmptyMetalake() {

public boolean verifyChangePagination() {
try {
Thread.sleep(500);
if (!nextPageBtn.isEnabled()) {
return false;
}
nextPageBtn.click();

waitElementClickable(nextPageBtn, AbstractWebIT.MAX_TIMEOUT);
clickElementAndWait(nextPageBtn);
// Check if the previous page button is available
return prevPageBtn.isEnabled() && performPrevPageAction();
} catch (Exception e) {
Expand All @@ -275,8 +278,8 @@ public boolean verifyChangePagination() {

private boolean performPrevPageAction() {
try {
Thread.sleep(500);
prevPageBtn.click();
waitElementClickable(prevPageBtn, AbstractWebIT.MAX_TIMEOUT);
clickElementAndWait(prevPageBtn);
return true;
} catch (Exception e) {
return false;
Expand All @@ -285,9 +288,7 @@ private boolean performPrevPageAction() {

public boolean verifyQueryMetalake(String name) {
try {
Thread.sleep(sleepTimeMillis);
setQueryInput(name);
Thread.sleep(sleepTimeMillis);
List<WebElement> dataList = dataViewer.findElements(By.xpath(".//div[@data-field='name']"));
// Check if the text in the first row matches the search input
boolean isQueried = Objects.equals(dataList.get(0).getText(), name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.openqa.selenium.By;
import org.openqa.selenium.ElementClickInterceptedException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -27,28 +31,66 @@ public class AbstractWebIT extends AbstractIT {

// https://www.selenium.dev/documentation/webdriver/waits/#implicit-waits
protected static final long MAX_IMPLICIT_WAIT = 30;

protected static final long MAX_TIMEOUT = 20;
protected static final long SLEEP_MILLIS = 1_000;

protected boolean waitShowText(final String text, final By locator) {
try {
WebElement element = pollingWait(locator);
WebElement element = pollingWait(locator, MAX_TIMEOUT);
return text.equals(element.getText());
} catch (TimeoutException e) {
return false;
}
}

protected WebElement pollingWait(final By locator) {
protected WebElement pollingWait(final By locator, final long maxTimeout) {
Wait<WebDriver> wait =
new FluentWait<>(driver)
.withTimeout(Duration.of(MAX_TIMEOUT, ChronoUnit.SECONDS))
.withTimeout(Duration.of(maxTimeout, ChronoUnit.SECONDS))
.pollingEvery(Duration.of(1, ChronoUnit.SECONDS))
.ignoring(NoSuchElementException.class);

return wait.until((Function<WebDriver, WebElement>) driver -> driver.findElement(locator));
}

protected void clickAndWait(final By locator) throws InterruptedException {
WebElement element = pollingWait(locator, MAX_IMPLICIT_WAIT);
try {
element.click();
Thread.sleep(SLEEP_MILLIS);
} catch (ElementClickInterceptedException e) {
Actions action = new Actions(driver);
action.moveToElement(element).click().build().perform();
Thread.sleep(SLEEP_MILLIS);
LOG.error(e.getMessage(), e);
}
}

protected void clickElementAndWait(final WebElement element) throws InterruptedException {
try {
element.click();
Thread.sleep(SLEEP_MILLIS);
} catch (ElementClickInterceptedException e) {
Actions action = new Actions(driver);
action.moveToElement(element).click().build().perform();
Thread.sleep(SLEEP_MILLIS);
LOG.error(e.getMessage(), e);
}
}

protected void waitClickable(final By locator, final long timeout) {
WebElement element = pollingWait(locator, MAX_IMPLICIT_WAIT);
WebDriverWait wait = new WebDriverWait(driver, timeout);
wait.until(ExpectedConditions.visibilityOf(element));
wait.until(ExpectedConditions.elementToBeClickable(element));
}

protected void waitElementClickable(final WebElement element, final long timeout) {
WebDriverWait wait = new WebDriverWait(driver, timeout);
wait.until(ExpectedConditions.visibilityOf(element));
wait.until(ExpectedConditions.elementToBeClickable(element));
}

@BeforeAll
public static void startUp() {
driver = WebDriverManager.getWebDriver(getGravitinoServerPort());
Expand Down

0 comments on commit cefcdc2

Please sign in to comment.