Skip to content
Open
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
15 changes: 15 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<allure.version>2.35.2</allure.version>
</properties>

<dependencies>
Expand All @@ -27,6 +29,19 @@
<version>5.11.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.27.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-jupiter</artifactId>
<version>${allure.version}</version>
<scope>test</scope>
</dependency>
</dependencies>


</project>
Original file line number Diff line number Diff line change
@@ -1,15 +1,67 @@
package com.serenitydojo.playwright;

import com.microsoft.playwright.Browser;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import com.microsoft.playwright.*;
import com.microsoft.playwright.junit.UsePlaywright;
import org.junit.jupiter.api.*;

import java.util.Arrays;

//This annotation allows you to do test without creating playwright environment and browser
//@UsePlaywright
public class ASimplePlaywrightTest {

private static Playwright playwright;
private static Browser browser;
private static BrowserContext browserContext;
Page page;

@BeforeAll
public static void setUpBrowser(){
playwright = Playwright.create();
browser = playwright.chromium().launch(
new BrowserType.LaunchOptions()
.setHeadless(false)
.setArgs(Arrays.asList("--no-sandbox", "--disable-extensions","--disable-gpu"))
);
//this allows paralel execution
browserContext = browser.newContext();

}

@BeforeEach
public void setUp(){
page = browser.newPage();
}

@AfterAll
public static void tearDown(){
browser.close();
playwright.close();
}

@Test
void shouldShowThePageTitle() {
// TODO: Write your first playwright test here

page.navigate("https://practicesoftwaretesting.com/");
String title = page.title();
Assertions.assertTrue(title.contains("Practice Software Testing"));

}

@Test
void shouldSearchByKeyword(){

page.navigate("https://practicesoftwaretesting.com/");
page.locator("[placeholder=Search]").fill("Pliers");
//this is basically sort of xpath
page.locator("button:has-text('Search')").click();

//this will look for the element with card
int matchingSearchResults = page.locator(".card").count();
System.out.println(matchingSearchResults);

Assertions.assertTrue(matchingSearchResults>0);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.serenitydojo.playwright;

import com.microsoft.playwright.*;
import com.microsoft.playwright.junit.UsePlaywright;
import com.microsoft.playwright.options.AriaRole;
import org.junit.jupiter.api.*;

import java.util.Arrays;
import java.util.List;

@UsePlaywright(HeadlessChromeOptions.class)
public class AddingItemsToTheCartTest {

@DisplayName("Search for pliers")
@Test
void searchForPliers(Page page){
page.navigate("https://practicesoftwaretesting.com/");
// page.locator("[placeholder='Search']").fill("pliers");
page.getByPlaceholder("Search").fill("pliers");
page.getByRole(AriaRole.BUTTON,
new Page.GetByRoleOptions().setName("Search")).click();

Locator products = page.locator(".card").filter(
new Locator.FilterOptions()
.setHas(page.getByAltText("Pliers"))
.setHasNot(page.getByText("Out of stock"))
);

List<String> productNames = products.locator("h5").allTextContents();
System.out.println(productNames);

}














}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.serenitydojo.playwright;

import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
import com.microsoft.playwright.junit.Options;
import com.microsoft.playwright.junit.OptionsFactory;
import com.microsoft.playwright.junit.UsePlaywright;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.Arrays;

@UsePlaywright(AnAnnotatedPlaywrightTest.MyOptions.class)
public class AnAnnotatedPlaywrightTest {

public static class MyOptions implements OptionsFactory{

@Override
public Options getOptions() {
return new Options()
.setHeadless(false)
.setLaunchOptions(
new BrowserType.LaunchOptions()
.setArgs(Arrays.asList("--no-sandbox", "--disable-extensions","--disable-gpu"))
);
}
}
@Test
void shouldShowThePageTitle(Page page) {
// TODO: Write your first playwright test here

page.navigate("https://practicesoftwaretesting.com/");
String title = page.title();
Assertions.assertTrue(title.contains("Practice Software Testing"));

}

@Test
void shouldSearchByKeyword(Page page){

page.navigate("https://practicesoftwaretesting.com/");
page.locator("[placeholder=Search]").fill("Pliers");
//this is basically sort of xpath
page.locator("button:has-text('Search')").click();

//this will look for the element with card
int matchingSearchResults = page.locator(".card").count();
System.out.println(matchingSearchResults);

Assertions.assertTrue(matchingSearchResults>0);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.serenitydojo.playwright;

import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.junit.Options;
import com.microsoft.playwright.junit.OptionsFactory;

import java.util.Arrays;

public class HeadlessChromeOptions implements OptionsFactory {


@Override
public Options getOptions() {
return new Options().setLaunchOptions(
new BrowserType.LaunchOptions().setHeadless(false)
.setArgs(Arrays.asList("--no-sandbox", "--disable-extensions", "--disable-gpu"))
)
.setTestIdAttribute("data-test");
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package com.serenitydojo.playwright;

import com.microsoft.playwright.Page;
import com.microsoft.playwright.junit.UsePlaywright;
import com.microsoft.playwright.options.LoadState;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import java.util.Comparator;
import java.util.List;

import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;

@UsePlaywright(HeadlessChromeOptions.class)
public class PlaywrightAssertionsTest {

@DisplayName("Making assertions about the contents of a field")
@Nested
class LocationgElementsUsingCSS {

@BeforeEach
void openContactPage(Page page) {
page.navigate("https://practicesoftwaretesting.com/contact");
}

@DisplayName("Checking the value of a field")
@Test
void fieldValues(Page page){

var firstNameField = page.getByLabel("First Name");

firstNameField.fill("Erhan");

assertThat(firstNameField).hasValue("Erhan");

assertThat(firstNameField).not().isDisabled();
assertThat(firstNameField).isVisible();
assertThat(firstNameField).isEditable();

}



}


@DisplayName("Making assertions about data values")
@Nested
class MakingAssertionsAboutDataValues {

@BeforeEach
void openHomePage(Page page) {
page.navigate("https://practicesoftwaretesting.com");
page.waitForCondition(() -> page.getByTestId("product-name").count() > 0);
}

@Test
void allProductPricesShouldBeCorrectValues(Page page){

List<Double> prices = page.getByTestId("product-price")
.allTextContents()
.stream()
.map(price -> Double.parseDouble(price.replace("$","")))
.toList();

Assertions.assertThat(prices)
.isNotEmpty()
.allMatch(price -> price > 0)
.doesNotContain(0.0)
.allMatch(price -> price < 1000)
.allSatisfy(price ->
Assertions.assertThat(price).isGreaterThan(0.0)
.isLessThan(1000.0));

}

@Test
void shouldSortInAlphabeticalOrder(Page page){

page.getByLabel("Sort").selectOption("Name (A - Z)");
page.waitForLoadState(LoadState.NETWORKIDLE);

List<String> productNames = page.getByTestId("product-name").allTextContents();

// Assertions.assertThat(productNames).isSortedAccordingTo(Comparator.naturalOrder());
Assertions.assertThat(productNames).isSortedAccordingTo(String.CASE_INSENSITIVE_ORDER);

}

@Test
void shouldSortInReverseAlphabeticalOrder(Page page){

page.getByLabel("Sort").selectOption("Name (Z - A)");
page.waitForLoadState(LoadState.NETWORKIDLE);

List<String> productNames = page.getByTestId("product-name").allTextContents();

Assertions.assertThat(productNames).isSortedAccordingTo(Comparator.reverseOrder());

}

}






}
Loading