File tree Expand file tree Collapse file tree 4 files changed +98
-1
lines changed Expand file tree Collapse file tree 4 files changed +98
-1
lines changed Original file line number Diff line number Diff line change
1
+ package pages ;
2
+
3
+ import org .openqa .selenium .By ;
4
+ import org .openqa .selenium .WebDriver ;
5
+
6
+ public class AlertsPage {
7
+
8
+ private WebDriver driver ;
9
+ private By triggerAlertButton = By .xpath (".//button[text()='Click for JS Alert']" );
10
+ private By triggerConfirmButton = By .xpath (".//button[text()='Click for JS Confirm']" );
11
+ private By triggerPromptButton = By .xpath (".//button[text()='Click for JS Prompt']" );
12
+
13
+ private By results = By .id ("result" );
14
+
15
+ public AlertsPage (WebDriver driver ){
16
+ this .driver = driver ;
17
+ }
18
+
19
+ public void triggerAlert (){
20
+ driver .findElement (triggerAlertButton ).click ();
21
+ }
22
+
23
+ public void triggerConfirm (){
24
+ driver .findElement (triggerConfirmButton ).click ();
25
+ }
26
+
27
+ public void triggerPrompt (){
28
+ driver .findElement (triggerPromptButton ).click ();
29
+ }
30
+
31
+ public void alert_clickToAccept (){
32
+ driver .switchTo ().alert ().accept ();
33
+ }
34
+
35
+ public void alert_clickToDismiss (){
36
+ driver .switchTo ().alert ().dismiss ();
37
+ }
38
+
39
+ public String alert_getText (){
40
+ return driver .switchTo ().alert ().getText ();
41
+ }
42
+
43
+ public void alert_setInput (String text ){
44
+ driver .switchTo ().alert ().sendKeys (text );
45
+ }
46
+
47
+ public String getResult (){
48
+ return driver .findElement (results ).getText ();
49
+ }
50
+ }
Original file line number Diff line number Diff line change @@ -31,6 +31,11 @@ public KeyPressesPage clickKeyPresses(){
31
31
return new KeyPressesPage (driver );
32
32
}
33
33
34
+ public AlertsPage clickJavaScriptAlerts (){
35
+ clickLink ("JavaScript Alerts" );
36
+ return new AlertsPage (driver );
37
+ }
38
+
34
39
private void clickLink (String linkText ){
35
40
driver .findElement (By .linkText (linkText )).click ();
36
41
}
Original file line number Diff line number Diff line change
1
+ package alerts ;
2
+
3
+ import base .BaseTests ;
4
+ import org .testng .annotations .Test ;
5
+
6
+ import static org .testng .Assert .assertEquals ;
7
+
8
+ public class AlertTests extends BaseTests {
9
+
10
+ @ Test
11
+ public void testAcceptAlert (){
12
+ var alertsPage = homePage .clickJavaScriptAlerts ();
13
+ alertsPage .triggerAlert ();
14
+ alertsPage .alert_clickToAccept ();
15
+ assertEquals (alertsPage .getResult (), "You successfuly clicked an alert" , "Results text incorrect" );
16
+ }
17
+
18
+ @ Test
19
+ public void testGetTextFromAlert (){
20
+ var alertsPage = homePage .clickJavaScriptAlerts ();
21
+ alertsPage .triggerConfirm ();
22
+ String text = alertsPage .alert_getText ();
23
+ alertsPage .alert_clickToDismiss ();
24
+ assertEquals (text , "I am a JS Confirm" , "Alert text incorrect" );
25
+ }
26
+
27
+ @ Test
28
+ public void testSetInputInAlert (){
29
+ var alertsPage = homePage .clickJavaScriptAlerts ();
30
+ alertsPage .triggerPrompt ();
31
+
32
+ String text = "TAU rocks!" ;
33
+ alertsPage .alert_setInput (text );
34
+ alertsPage .alert_clickToAccept ();
35
+ assertEquals (alertsPage .getResult (), "You entered: " + text , "Results text incorrect" );
36
+ }
37
+ }
Original file line number Diff line number Diff line change 4
4
import org .openqa .selenium .chrome .ChromeDriver ;
5
5
import org .testng .annotations .AfterClass ;
6
6
import org .testng .annotations .BeforeClass ;
7
+ import org .testng .annotations .BeforeMethod ;
7
8
import pages .HomePage ;
8
9
9
10
public class BaseTests {
@@ -15,8 +16,12 @@ public class BaseTests {
15
16
public void setUp (){
16
17
System .setProperty ("webdriver.chrome.driver" , "resources/chromedriver" );
17
18
driver = new ChromeDriver ();
18
- driver .get ("https://the-internet.herokuapp.com/" );
19
+ goHome ();
20
+ }
19
21
22
+ @ BeforeMethod
23
+ public void goHome (){
24
+ driver .get ("https://the-internet.herokuapp.com/" );
20
25
homePage = new HomePage (driver );
21
26
}
22
27
You can’t perform that action at this time.
0 commit comments