Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<playwright.version>1.58.0</playwright.version>
<playwright.version>1.59.0</playwright.version>
<testng.version>7.12.0</testng.version>
<maven.compiler.version>3.15.0</maven.compiler.version>
<surefire.version>3.5.5</surefire.version>
Expand Down
182 changes: 182 additions & 0 deletions src/test/java/io/github/mfaisalkhatri/tests/ClickOperationTests.java
Original file line number Diff line number Diff line change
@@ -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 ();
}
}
37 changes: 22 additions & 15 deletions src/test/java/io/github/mfaisalkhatri/tests/DropdownTests.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions test-suites/testng-clickoperations.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Playwright Demo Tests ">
<test name="Click operation tests">
<classes>
<class name="io.github.mfaisalkhatri.tests.ClickOperationTests">
<methods>
<include name="testLeftClick"/>
<include name="testRightClick"/>
<include name="testDoubleClick"/>
<include name="testMouseHover"/>
<include name="testForceMouseClick"/>
<include name="testPositionBasedClick"/>
</methods>
</class>
</classes>
</test>
</suite>
Loading