diff --git a/pom.xml b/pom.xml index 7e56cb8..ac93114 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ UTF-8 - 1.58.0 + 1.59.0 7.12.0 3.15.0 3.5.5 diff --git a/src/test/java/io/github/mfaisalkhatri/tests/ClickOperationTests.java b/src/test/java/io/github/mfaisalkhatri/tests/ClickOperationTests.java new file mode 100644 index 0000000..1b6442f --- /dev/null +++ b/src/test/java/io/github/mfaisalkhatri/tests/ClickOperationTests.java @@ -0,0 +1,182 @@ +package io.github.mfaisalkhatri.tests; + +import com.microsoft.playwright.*; +import com.microsoft.playwright.options.AriaRole; +import com.microsoft.playwright.options.BoundingBox; +import com.microsoft.playwright.options.KeyboardModifier; +import com.microsoft.playwright.options.MouseButton; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.util.Arrays; +import java.util.List; + +import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat; +import static org.testng.Assert.assertEquals; + +public class ClickOperationTests { + + private Playwright playwright; + private Page page; + + @BeforeMethod + public void setup () { + this.playwright = Playwright.create (); + Browser browser = this.playwright.chromium () + .launch (new BrowserType.LaunchOptions ().setHeadless (false) + .setChannel ("chrome")); + this.page = browser.newPage (); + } + + @Test + public void testLeftClick () { + this.page.navigate ("https://the-internet.herokuapp.com/"); + this.page.getByRole (AriaRole.LINK, new Page.GetByRoleOptions ().setName ("Challenging DOM")) + .click (); + //Locator challenginDomLink = page.getByRole (AriaRole.LINK, + // new Page.GetByRoleOptions ().setName ("Challenging DOM")); + //challenginDomLink.click (); + + assertThat (this.page.getByRole (AriaRole.HEADING, + new Page.GetByRoleOptions ().setName ("Challenging DOM"))).isVisible (); + } + + @Test + public void testRightClick () { + this.page.navigate ("https://the-internet.herokuapp.com/context_menu"); + this.page.locator ("#hot-spot") + .click (new Locator.ClickOptions ().setButton (MouseButton.RIGHT)); + this.page.onDialog (dialog -> { + String alertText = dialog.message (); + assert alertText.equals ("You selected a context menu"); + dialog.accept (); + }); + } + + @Test + public void testDoubleClick () { + this.page.navigate ("https://demo.guru99.com/test/simple_context_menu.html"); + + Locator alertBtn = this.page.locator ("#authentication > button"); + this.page.onDialog (dialog -> { + String text = "You double clicked me.. Thank You.."; + assertEquals (dialog.message (), text); + dialog.accept (); + }); + alertBtn.dblclick (); + } + + @Test + public void testMouseHover () { + this.page.navigate ("https://automationteststore.com/"); + this.page.getByRole (AriaRole.LINK, new Page.GetByRoleOptions ().setName (" Apparel & accessories")) + .hover (); + this.page.getByRole (AriaRole.LINK, new Page.GetByRoleOptions ().setName ("Shoes") + .setExact (true)) + .click (); + assertThat ( + this.page.getByRole (AriaRole.HEADING, new Page.GetByRoleOptions ().setName ("Shoes"))).isVisible (); + } + + @Test + public void testForceMouseClick () { + this.page.navigate ("https://automationteststore.com/"); + this.page.getByRole (AriaRole.LINK, new Page.GetByRoleOptions ().setName ("Specials")) + .click (new Locator.ClickOptions ().setForce (true)); + assertThat (this.page.getByRole (AriaRole.HEADING, + new Page.GetByRoleOptions ().setName ("Special Offers"))).isVisible (); + } + + @Test + public void testPositionBasedClick () { + this.page.navigate ("https://the-internet.herokuapp.com/horizontal_slider"); + Locator slider = this.page.getByRole (AriaRole.SLIDER); + BoundingBox box = slider.boundingBox (); + double width = box.width; + double height = box.height; + + double x = width * 0.5; + double y = height * 0.5; + + slider.click (new Locator.ClickOptions ().setPosition (x, y)); + this.page.waitForTimeout (3000); + + } + + @Test + public void testClickWithModifiers () { + this.page.navigate ("https://jqueryui.com/selectable/"); + FrameLocator frame = this.page.frameLocator (".demo-frame"); + + Locator items = frame.locator ("#selectable li"); + + items.nth (0) + .click (new Locator.ClickOptions ().setModifiers (List.of (KeyboardModifier.CONTROL))); + + items.nth (3) + .click (new Locator.ClickOptions ().setModifiers (List.of (KeyboardModifier.CONTROL))); + + this.page.waitForTimeout (2000); + + this.page.navigate ("https://the-internet.herokuapp.com/"); + this.page.getByRole (AriaRole.LINK, new Page.GetByRoleOptions ().setName ("File Download") + .setExact (true)) + .click (new Locator.ClickOptions ().setModifiers (List.of (KeyboardModifier.SHIFT))); + + this.page.waitForTimeout (2000); + + } + + @Test + public void testDelayClick () { + this.page.navigate ("https://the-internet.herokuapp.com/"); + this.page.getByRole (AriaRole.LINK, new Page.GetByRoleOptions ().setName ("Challenging DOM")) + .click (new Locator.ClickOptions ().setDelay (2000)); + assertThat (this.page.getByRole (AriaRole.HEADING, + new Page.GetByRoleOptions ().setName ("Challenging DOM"))).isVisible (); + } + + @Test + public void testDragAndDropActions () { + this.page.navigate ("https://the-internet.herokuapp.com/"); + this.page.getByRole (AriaRole.LINK, new Page.GetByRoleOptions ().setName ("Drag and Drop")) + .click (); + this.page.locator ("#column-a") + .dragTo (this.page.locator ("#column-b")); + assertThat (this.page.locator ("#column-a")).containsText ("B"); + + } + + @Test + public void testDragAndDropManually () { + this.page.navigate ("https://the-internet.herokuapp.com/"); + this.page.getByRole (AriaRole.LINK, new Page.GetByRoleOptions ().setName ("Drag and Drop")) + .click (); + this.page.locator ("#column-a") + .hover (); + this.page.mouse () + .down (); + this.page.locator ("#column-b") + .hover (); + this.page.mouse () + .up (); + assertThat (this.page.locator ("#column-a")).containsText ("B"); + } + + @Test + public void testProgrammaticClick () { + this.page.navigate ("https://the-internet.herokuapp.com/"); + this.page.getByRole (AriaRole.LINK, new Page.GetByRoleOptions ().setName ("Checkboxes")) + .dispatchEvent ("click"); + + assertThat (this.page.getByRole (AriaRole.HEADING)).containsText ("Checkboxes"); + + } + + @AfterMethod + public void tearDown () { + this.page.close (); + this.playwright.close (); + } +} \ No newline at end of file diff --git a/src/test/java/io/github/mfaisalkhatri/tests/DropdownTests.java b/src/test/java/io/github/mfaisalkhatri/tests/DropdownTests.java index dc5bce6..66ba43d 100644 --- a/src/test/java/io/github/mfaisalkhatri/tests/DropdownTests.java +++ b/src/test/java/io/github/mfaisalkhatri/tests/DropdownTests.java @@ -1,16 +1,21 @@ package io.github.mfaisalkhatri.tests; -import com.microsoft.playwright.*; +import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat; +import static org.testng.Assert.assertEquals; + +import java.util.regex.Pattern; + +import com.microsoft.playwright.Browser; +import com.microsoft.playwright.BrowserType; +import com.microsoft.playwright.Locator; +import com.microsoft.playwright.Page; +import com.microsoft.playwright.Playwright; +import com.microsoft.playwright.options.AriaRole; import com.microsoft.playwright.options.SelectOption; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; -import java.util.regex.Pattern; - -import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat; -import static org.testng.Assert.assertEquals; - public class DropdownTests { private Playwright playwright; @@ -65,23 +70,25 @@ public void testSelectByIndex() { @Test public void testSelectByValue() { - this.page.navigate("https://www.lambdatest.com/selenium-playground/select-dropdown-demo"); - final Locator dropdownField = this.page.locator("#select-demo"); + this.page.navigate ("https://the-internet.herokuapp.com/dropdown"); - dropdownField.selectOption(new SelectOption().setValue("Wednesday")); + this.page.getByRole (AriaRole.COMBOBOX) + .selectOption ("Option 2"); - final Locator daySelected = this.page.locator(".pt-10 p"); - assertThat(daySelected).hasText("Day selected :- Wednesday"); + assertThat (page.getByRole (AriaRole.COMBOBOX)).containsText ("Option 2"); } @Test public void testMultiSelectOptions() { - this.page.navigate("https://www.lambdatest.com/selenium-playground/select-dropdown-demo"); + this.page.navigate("https://testautomationpractice.blogspot.com/"); - final Locator dropdownField = this.page.locator("#multi-select"); - dropdownField.selectOption(new SelectOption[]{new SelectOption().setLabel("New York"), new SelectOption().setLabel("Texas"), new SelectOption().setValue("California"), new SelectOption().setIndex(7)}); + final Locator dropdownField = this.page.getByRole (AriaRole.LISTBOX, new Page.GetByRoleOptions ().setName ( + "Sorted List:")); + dropdownField.selectOption (new SelectOption [] {new SelectOption ().setLabel ("Cat"), + new SelectOption ().setLabel ("Cheetah"), new SelectOption ().setIndex (2)}); - assertThat(dropdownField).hasValues(new Pattern[]{Pattern.compile("California"), Pattern.compile("New York"), Pattern.compile("Texas"), Pattern.compile("Washington")}); + assertThat(dropdownField).hasValues (new Pattern[]{Pattern.compile ("cat"), + Pattern.compile ("cheetah"), Pattern.compile ("deer")}); } @AfterClass diff --git a/test-suites/testng-clickoperations.xml b/test-suites/testng-clickoperations.xml new file mode 100644 index 0000000..2409d2a --- /dev/null +++ b/test-suites/testng-clickoperations.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file