Created
December 1, 2023 05:02
-
-
Save dmitriyvolk/4fc830f30a71e8786bd93677efa47296 to your computer and use it in GitHub Desktop.
Alerts, Prompts, Frames
This file contains 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 lecture; | |
import org.openqa.selenium.Alert; | |
import org.openqa.selenium.By; | |
import org.openqa.selenium.NoAlertPresentException; | |
import org.openqa.selenium.support.ui.ExpectedConditions; | |
import org.testng.Assert; | |
import org.testng.annotations.Test; | |
import school.redrover.runner.BaseTest; | |
import static org.testng.Assert.fail; | |
public class AlertTest extends BaseTest { | |
void goToAlerts() { | |
getDriver().findElement(By.linkText("alerts.html")).click(); | |
} | |
@Test | |
void testReadAlertText() throws Exception { | |
goToAlerts(); | |
getDriver().findElement(By.id("alert")).click(); | |
final Alert alert = getDriver().switchTo().alert(); | |
String alertText = alert.getText(); | |
Assert.assertEquals("cheese", alertText); | |
} | |
@Test | |
void testSlowAlert() throws Exception { | |
goToAlerts(); | |
getDriver().findElement(By.id("slow-alert")).click(); | |
getWait10().until(ExpectedConditions.alertIsPresent()); | |
String alertText = getDriver().switchTo().alert().getText(); | |
Assert.assertEquals("Slow", alertText); | |
Thread.sleep(4000); | |
} | |
@Test | |
void testClickOkOnAlert() throws Exception { | |
goToAlerts(); | |
getDriver().findElement(By.id("alert")).click(); | |
Thread.sleep(2000); | |
getDriver().switchTo().alert().dismiss(); | |
try { | |
getDriver().switchTo().alert(); | |
fail("Expected NoAlertPresentException to be thrown, but it wasn't"); | |
} catch (NoAlertPresentException e) { | |
//pass | |
} | |
Thread.sleep(3000); | |
} | |
@Test | |
void testAcceptAlert() throws InterruptedException { | |
goToAlerts(); | |
getDriver().findElement(By.id("confirm")).click(); | |
getDriver().switchTo().alert().accept(); | |
String text = getDriver().findElement(By.tagName("h1")).getText(); | |
Assert.assertEquals("Heading", text); | |
Thread.sleep(2000); | |
} | |
@Test | |
void testDismissAlert() throws InterruptedException { | |
goToAlerts(); | |
getDriver().findElement(By.id("confirm")).click(); | |
getDriver().switchTo().alert().dismiss(); | |
String text = getDriver().findElement(By.tagName("h1")).getText(); | |
Assert.assertEquals("Testing Alerts and Stuff", text); | |
Thread.sleep(2000); | |
} | |
@Test | |
void testAcceptPrompt() throws Exception { | |
goToAlerts(); | |
getDriver().findElement(By.id("prompt")).click(); | |
Alert alert = getWait10().until(ExpectedConditions.alertIsPresent()); | |
final String enteredText = "Something something"; | |
alert.sendKeys(enteredText); | |
//NOT DISPLAYING | |
alert.accept(); | |
String actualText = getDriver().findElement(By.xpath("//div[@id='text']/p")).getText(); | |
Assert.assertEquals(enteredText, actualText); | |
} | |
} |
This file contains 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 lecture; | |
import org.openqa.selenium.By; | |
import org.openqa.selenium.NoSuchElementException; | |
import org.openqa.selenium.WebElement; | |
import org.testng.Assert; | |
import org.testng.annotations.Test; | |
import school.redrover.runner.BaseTest; | |
import java.util.List; | |
public class IFrameTest extends BaseTest { | |
void goToIFrames() { | |
getDriver().findElement(By.linkText("iframes.html")).click(); | |
} | |
void goToOtherIFrames() { | |
getDriver().findElement(By.linkText("slow_loading_iframes.html")).click(); | |
} | |
@Test | |
void testElementInIFrame() { | |
goToIFrames(); | |
try { | |
System.out.println(getDriver().findElement(By.id("email"))); | |
Assert.fail(); //there shouldn't be one | |
} catch (NoSuchElementException e) { | |
System.out.println("Element can't be found, and that's correct!"); | |
} | |
WebElement frame = getDriver().findElement(By.id("iframe1")); | |
// getDriver().switchTo().frame("iframe1"); | |
getDriver().switchTo().frame(frame); | |
WebElement email = getDriver().findElement(By.id("email")); | |
Assert.assertEquals("input", email.getTagName()); | |
} | |
@Test | |
void testElementOutsideIFrame() { | |
goToIFrames(); | |
Assert.assertEquals( | |
"h1", | |
getDriver().findElement(By.id("iframe_page_heading")).getTagName() | |
); | |
getDriver().switchTo().frame("iframe1"); | |
try { | |
getDriver().findElement(By.id("iframe_page_heading")); | |
Assert.fail("Expected the element iframe_page_heading to be missing"); //shouldn't be accessible | |
} catch (NoSuchElementException e) { | |
System.out.println("Element can't be found, and that's correct!"); | |
} | |
getDriver().switchTo().defaultContent(); | |
Assert.assertEquals( | |
"h1", | |
getDriver().findElement(By.id("iframe_page_heading")).getTagName() | |
); | |
} | |
@Test | |
void testSwitchBetweenIFrames() { | |
goToOtherIFrames(); | |
getDriver().switchTo().frame("noSrc"); | |
getDriver().switchTo().defaultContent(); | |
getDriver().switchTo().frame(0); | |
Assert.assertEquals("section", getDriver().findElement(By.id("announcement-banner")).getTagName()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment