- Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathSample5.java
More file actions
Latest commit
93 lines (74 loc) · 3.09 KB
/
Copy pathSample5.java
File metadata and controls
93 lines (74 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
packageselenium.sample;
importorg.junit.After;
importorg.junit.Before;
importorg.junit.Test;
importorg.openqa.selenium.Alert;
importorg.openqa.selenium.By;
importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.chrome.ChromeDriver;
importjava.io.File;
importstaticorg.junit.Assert.assertEquals;
importstaticorg.junit.Assert.assertTrue;
publicclassSample5 {
WebDriverdriver;
// method which is being run before each test
@Before
publicvoidstartingTests() throwsException {
// from Sample 1:
StringlibWithDriversLocation = System.getProperty("user.dir") + File.separator + "lib" + File.separator;
System.setProperty("webdriver.chrome.driver", libWithDriversLocation + "chromedriver" + newselenium.ChangeToFileExtension().extension());
// declaration above:
driver = newChromeDriver();
//open page:
driver.get("https://kristinek.github.io/site/examples/alerts_popups");
}
// method which is being run after each test
@After
publicvoidendingTests() throwsException {
driver.close();
}
@Test
publicvoidalertOnOpeningPage() throwsException {
driver.get("https://kristinek.github.io/site/examples/al_p");
// org.openqa.selenium.UnhandledAlertException: unexpected alert open:
// driver.findElement(By.id("heading")).getText();
Alertalert = driver.switchTo().alert();
assertEquals("Booooooooo!", alert.getText());
alert.accept();
assertEquals("This page is alerted", driver.findElement(By.id("heading")).getText());
}
@Test
publicvoidalertOnclickingButton() throwsException {
driver.findElement(By.className("w3-red")).click();
Alertalert = driver.switchTo().alert();
assertEquals("I am an alert box!", alert.getText());
alert.accept();
assertEquals("", driver.findElement(By.id("textForAlerts")).getText());
}
@Test
publicvoidpopUpConform() throwsException {
driver.findElement(By.className("w3-teal")).click();
Alertalert = driver.switchTo().alert();
assertEquals("Press a button!", alert.getText());
alert.accept();
assertEquals("Why on earth have you agreed to it?!", driver.findElement(By.id("textForAlerts")).getText());
}
@Test
publicvoidpopUpDeny() throwsException {
driver.findElement(By.className("w3-teal")).click();
Alertalert = driver.switchTo().alert();
assertEquals("Press a button!", alert.getText());
alert.dismiss();
assertEquals("You have dared to deny me!!!", driver.findElement(By.id("textForAlerts")).getText());
}
@Test
publicvoidpopUpEnterNumber() throwsException {
StringenterKeys = "5";
driver.findElement(By.className("w3-khaki")).click();
Alertalert = driver.switchTo().alert();
assertEquals("Please enter a number", alert.getText());
alert.sendKeys(enterKeys);
alert.accept();
assertTrue(driver.findElement(By.id("textForAlerts")).getText().contains("instead of " + enterKeys));
}
}