Created
May 22, 2025 11:04
-
-
Save solanoize/860e0858e20e72ff37915d6f1c7ebb42 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// implementasi BasePage pada Page Object Model untuk skip error jika tidak ditemukan elemen | |
public class BasePage { | |
protected WebDriver driver; | |
protected WebDriverWait wait; | |
protected List<String> missingElements = new ArrayList<>(); | |
public BasePage(WebDriver driver) { | |
this.driver = driver; | |
this.wait = new WebDriverWait(driver, Duration.ofSeconds(5)); | |
} | |
protected WebElement checkAndGet(String name, By locator) { | |
try { | |
return wait.until(ExpectedConditions.presenceOfElementLocated(locator)); | |
} catch (TimeoutException e) { | |
missingElements.add(name); | |
return null; | |
} | |
} | |
public List<String> getMissingElements() { | |
return missingElements; | |
} | |
public boolean hasMissingElements() { | |
return !missingElements.isEmpty(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LoginPage extends BasePage { | |
public WebElement buttonLogin; | |
public WebElement inputUsername; | |
public WebElement inputPassword; | |
public LoginPage(WebDriver driver) { | |
super(driver); | |
this.buttonLogin = checkAndGet("buttonLogin", By.id("login-button")); | |
this.inputUsername = checkAndGet("username", By.id("username")); | |
this.inputPassword = checkAndGet("password", By.id("password")); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LoginPageTest { | |
WebDriver driver; | |
LoginPage loginPage; | |
@BeforeClass | |
public void setUp() { | |
driver = new ChromeDriver(); // atau inisialisasi sesuai kebutuhan | |
loginPage = new LoginPage(driver); | |
if (loginPage.hasMissingElements()) { | |
throw new SkipException("Elemen tidak ditemukan: " + String.join(", ", loginPage.getMissingElements())); | |
} | |
} | |
@Test | |
public void testLoginButtonVisible() { | |
Assert.assertTrue(loginPage.buttonLogin.isDisplayed()); | |
} | |
@Test | |
public void testUsernameInputEditable() { | |
loginPage.inputUsername.sendKeys("admin"); | |
Assert.assertEquals(loginPage.inputUsername.getAttribute("value"), "admin"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.juaracoding.test.authentications; | |
import java.time.Duration; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.openqa.selenium.By; | |
import org.openqa.selenium.TimeoutException; | |
import org.openqa.selenium.WebDriver; | |
import org.openqa.selenium.WebElement; | |
import org.openqa.selenium.support.ui.ExpectedConditions; | |
import org.openqa.selenium.support.ui.WebDriverWait; | |
import org.testng.Assert; | |
import org.testng.SkipException; | |
import org.testng.annotations.BeforeClass; | |
import org.testng.annotations.BeforeMethod; | |
import org.testng.annotations.Test; | |
import com.juaracoding.test.DriverSingleton; | |
public class SignInWithoutCredential { | |
WebElement buttonLogin; | |
WebElement usernameField; | |
WebDriver driver; | |
List<String> missingElements = new ArrayList<>(); | |
boolean skipClass = false; | |
// Helper untuk cek dan catat elemen | |
private WebElement checkAndGet(WebDriverWait wait, String name, By locator) { | |
try { | |
return wait.until(ExpectedConditions.presenceOfElementLocated(locator)); | |
} catch (TimeoutException e) { | |
missingElements.add(name); | |
return null; | |
} | |
} | |
@BeforeClass | |
public void setup() { | |
driver = DriverSingleton.createOrGetDriver(); | |
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5)); | |
buttonLogin = checkAndGet(wait, "buttonLogin", By.xpath("/html/body/div[2]/div[1]/div/div/form/input[3]")); | |
usernameField = checkAndGet(wait, "usernameField", By.xpath("//*[@data-test=\"usernames\"]")); | |
if (!missingElements.isEmpty()) { | |
skipClass = true; | |
} | |
} | |
@BeforeMethod | |
public void checkElementsExist() { | |
if (skipClass) { | |
throw new SkipException("No element found: " + String.join(", ", missingElements)); | |
} | |
} | |
@Test | |
public void testStep03() { | |
usernameField.sendKeys("hello"); | |
buttonLogin.click(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment