Skip to content

Instantly share code, notes, and snippets.

@solanoize
Created March 17, 2025 11:55
Show Gist options
  • Save solanoize/2f0951aaf855f8197c4d5d8c67bd74c5 to your computer and use it in GitHub Desktop.
Save solanoize/2f0951aaf855f8197c4d5d8c67bd74c5 to your computer and use it in GitHub Desktop.
package com.ptintercompoland.learn;
import java.time.Duration;
import java.util.ArrayList;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class TestWaiting {
private WebDriver driver;
@BeforeClass
public void init() {
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
options.setBinary("C:\\chrome-win64\\chrome.exe");
driver = new ChromeDriver(options);
}
@Test(enabled = false)
public void test() throws InterruptedException {
driver.get("http://127.0.0.1:5500/waitingelement.html");
// driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(1000));
// driver.navigate().refresh();
// Thread.sleep(500);
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(15));
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".container > h1")));
Assert.assertEquals(element.getText(), "Hai Ini Data");
}
@Test(enabled = false)
public void testB() {
/**
* Handling alert
*/
driver.get("http://127.0.0.1:5500/myalert.html");
WebElement buttonAlert = driver.findElement(By.className("btn-1"));
buttonAlert.click();
Alert alert = driver.switchTo().alert();
String actual = alert.getText();
alert.accept();
String expected = "Test alert in here";
Assert.assertEquals(actual, expected);
}
@Test(enabled = false)
public void testC() throws InterruptedException {
/**
* Handling alert
*/
driver.get("http://127.0.0.1:5500/myalert.html");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(1400));
WebElement buttonPrompt = driver.findElement(By.className("btn-2"));
buttonPrompt.click();
Alert alert = driver.switchTo().alert();
alert.sendKeys("Budi Laksono");
alert.accept();
WebElement message = driver.findElement(By.className("message"));
Assert.assertEquals(message.getText(), "Hai Budi Laksono");
// driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(1400));
// Thread.sleep(1200);
// WebDriverWait wt = new WebDriverWait(driver, Duration.ofSeconds(10));
// alert = wt.until(ExpectedConditions.alertIsPresent());
// alert.accept();
// String actual = alert.getText();
// alert.accept();
// Assert.assertEquals(actual, "Hai Budi Laksono");
}
@Test(enabled = false)
public void testD() {
driver.get("http://127.0.0.1:5500/myframe.html");
WebElement frameOne = driver.findElement(By.tagName("iframe"));
driver.switchTo().frame(frameOne);
WebElement buttonAlert = driver.findElement(By.className("btn-1"));
buttonAlert.click();
Alert alert = driver.switchTo().alert();
String actual = alert.getText();
alert.accept();
String expected = "Test alert in here";
driver.switchTo().defaultContent();
driver.findElement(By.className("btn-1")).click();
Assert.assertEquals(actual, expected);
}
@Test(enabled = false)
public void testE() {
driver.get("http://127.0.0.1:5500/tabwindows.html");
WebElement link1 = driver.findElement(By.className("btn-1"));
// WebElement link2 = driver.findElement(By.className("btn-2"));
// WebElement link3 = driver.findElement(By.className("btn-3"));
link1.click();
// link2.click();
// link3.click();
ArrayList<String> newTb = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(newTb.get(1));
System.out.println("Page title of new tab: " + driver.getTitle());
// switch to parent window
driver.switchTo().window(newTb.get(0));
System.out.println("Page title of parent window: " + driver.getTitle());
}
@Test(enabled = true)
public void testF() throws InterruptedException {
driver.get("http://127.0.0.1:5500/navigationcommand.html");
driver.findElement(By.tagName("a")).click();
driver.navigate().refresh();
Thread.sleep(1300);
driver.navigate().back();
Thread.sleep(1300);
driver.navigate().forward();
}
@AfterClass
public void tearDown() throws InterruptedException {
Thread.sleep(1500);
driver.quit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment