11package io .github .mfaisalkhatri .tests ;
22
3- import static com .microsoft .playwright .assertions .PlaywrightAssertions .assertThat ;
4-
5- import com .microsoft .playwright .Browser ;
6- import com .microsoft .playwright .BrowserType ;
7- import com .microsoft .playwright .Locator ;
8- import com .microsoft .playwright .Page ;
9- import com .microsoft .playwright .Playwright ;
3+ import com .microsoft .playwright .*;
104import com .microsoft .playwright .options .AriaRole ;
5+ import com .microsoft .playwright .options .BoundingBox ;
116import com .microsoft .playwright .options .MouseButton ;
12- import org .testng .annotations .AfterClass ;
13- import org .testng .annotations .BeforeClass ;
7+ import org .testng .annotations .AfterMethod ;
8+ import org .testng .annotations .BeforeMethod ;
149import org .testng .annotations .Test ;
1510
11+ import static com .microsoft .playwright .assertions .PlaywrightAssertions .assertThat ;
12+ import static org .testng .Assert .assertEquals ;
13+
1614public class ClickOperationTests {
1715
1816 private Playwright playwright ;
19- private Page page ;
17+ private Page page ;
2018
21- @ BeforeClass
19+ @ BeforeMethod
2220 public void setup () {
23- this .playwright = Playwright .create ();
21+ this .playwright = Playwright .create ();
2422 Browser browser = playwright .chromium ().launch (new BrowserType .LaunchOptions ().setHeadless (false )
25- .setChannel ("chrome" ));
26- this .page = browser .newPage ();
23+ .setChannel ("chrome" ));
24+ this .page = browser .newPage ();
2725 }
2826
2927 @ Test
30- public void testLeftClick () {
31- this .page .navigate ("https://the-internet.herokuapp.com/" );
32- this .page .getByRole (AriaRole .LINK , new Page .GetByRoleOptions ().setName ("Challenging DOM" ))
33- .click ();
28+ public void testLeftClick () {
29+ this .page .navigate ("https://the-internet.herokuapp.com/" );
30+ this .page .getByRole (AriaRole .LINK , new Page .GetByRoleOptions ().setName ("Challenging DOM" ))
31+ .click ();
3432 //Locator challenginDomLink = page.getByRole (AriaRole.LINK,
3533 // new Page.GetByRoleOptions ().setName ("Challenging DOM"));
3634 //challenginDomLink.click ();
3735
38- assertThat (
39- this .page .getByRole (AriaRole .HEADING , new Page .GetByRoleOptions ().setName ("Challenging DOM" ))).isVisible ();
36+ assertThat (
37+ this .page .getByRole (AriaRole .HEADING , new Page .GetByRoleOptions ().setName ("Challenging DOM" ))).isVisible ();
4038 }
4139
4240 @ Test
43- public void testRightClick () {
44- this .page .navigate ("https://the-internet.herokuapp.com/context_menu" );
45- this .page .locator ("#hot-spot" )
46- .click (new Locator .ClickOptions ().setButton (MouseButton .RIGHT ));
47- this .page .onDialog (dialog -> {
48- String alertText = dialog .message ();
49-
50- assert alertText .equals ("You selected a context menu" );
51- dialog .accept ();
41+ public void testRightClick () {
42+ this .page .navigate ("https://the-internet.herokuapp.com/context_menu" );
43+ this .page .locator ("#hot-spot" )
44+ .click (new Locator .ClickOptions ().setButton (MouseButton .RIGHT ));
45+ this .page .onDialog (dialog -> {
46+ String alertText = dialog .message ();
47+ assert alertText .equals ("You selected a context menu" );
48+ dialog .accept ();
5249 });
5350 }
5451
55- @ AfterClass
52+ @ Test
53+ public void testDoubleClick () {
54+ this .page .navigate ("https://demo.guru99.com/test/simple_context_menu.html" );
55+
56+ Locator alertBtn = this .page .locator ("#authentication > button" );
57+ this .page .onDialog (dialog -> {
58+ String text = "You double clicked me.. Thank You.." ;
59+ assertEquals (dialog .message (), text );
60+ dialog .accept ();
61+ });
62+ alertBtn .dblclick ();
63+ }
64+
65+ @ Test
66+ public void testMouseHover () {
67+ this .page .navigate ("https://automationteststore.com/" );
68+ this .page .getByRole (AriaRole .LINK , new Page .GetByRoleOptions ()
69+ .setName (" Apparel & accessories" )).hover ();
70+ this .page .getByRole (AriaRole .LINK , new Page .GetByRoleOptions ()
71+ .setName ("Shoes" ).setExact (true )).click ();
72+ assertThat (this .page .getByRole (AriaRole .HEADING ,
73+ new Page .GetByRoleOptions ().setName ("Shoes" ))).isVisible ();
74+ }
75+
76+ @ Test
77+ public void testForceMouseClick () {
78+ this .page .navigate ("https://automationteststore.com/" );
79+ this .page .getByRole (AriaRole .LINK , new Page .GetByRoleOptions ()
80+ .setName ("Specials" )).click (new Locator .ClickOptions ().setForce (true ));
81+ assertThat (this .page .getByRole (AriaRole .HEADING , new Page .GetByRoleOptions ()
82+ .setName ("Special Offers" ))).isVisible ();
83+ }
84+
85+ @ Test
86+ public void testPositionBasedClick () {
87+ this .page .navigate ("https://the-internet.herokuapp.com/horizontal_slider" );
88+ Locator slider = this .page .getByRole (AriaRole .SLIDER );
89+ BoundingBox box = slider .boundingBox ();
90+ double width = box .width ;
91+ double height = box .height ;
92+
93+ double x = width * 0.5 ;
94+ double y = height * 0.5 ;
95+
96+ slider .click (new Locator .ClickOptions ().setPosition (x , y ));
97+ this .page .waitForTimeout (3000 );
98+
99+ }
100+
101+ @ AfterMethod
56102 public void tearDown () {
57- this .page .close ();
58- this .playwright .close ();
103+ this .page .close ();
104+ this .playwright .close ();
59105 }
60- }
106+ }
0 commit comments