Skip to content

Commit 0d6cba7

Browse files
AssertJ Web Test
1 parent d01ed5c commit 0d6cba7

File tree

9 files changed

+222
-0
lines changed

9 files changed

+222
-0
lines changed

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<assertJ.version>3.24.2</assertJ.version>
2020
<joda.time.version>2.12.5</joda.time.version>
2121
<assertJ.joda.verson>2.2.0</assertJ.joda.verson>
22+
<lombok.version>1.18.30</lombok.version>
2223
</properties>
2324

2425
<dependencies>
@@ -68,5 +69,11 @@
6869
<version>${assertJ.joda.verson}</version>
6970
<scope>test</scope>
7071
</dependency>
72+
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
73+
<dependency>
74+
<groupId>org.projectlombok</groupId>
75+
<artifactId>lombok</artifactId>
76+
<version>${lombok.version}</version>
77+
</dependency>
7178
</dependencies>
7279
</project>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package Drivers;
2+
3+
public enum BrowserType {
4+
5+
CHROME,
6+
FIREFOX,
7+
EDGE;
8+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package Drivers;
2+
3+
import org.openqa.selenium.WebDriver;
4+
import org.openqa.selenium.chrome.ChromeDriver;
5+
import org.openqa.selenium.edge.EdgeDriver;
6+
import org.openqa.selenium.firefox.FirefoxDriver;
7+
8+
public class DriverFactory {
9+
10+
private DriverFactory(){
11+
12+
};
13+
14+
static WebDriver driver;
15+
16+
public static void initDriver(BrowserType browser){
17+
switch(browser){
18+
case FIREFOX:
19+
driver=new FirefoxDriver();
20+
break;
21+
case EDGE:
22+
driver=new EdgeDriver();
23+
break;
24+
default:
25+
driver=new ChromeDriver();
26+
break;
27+
}
28+
if(driver!=null)
29+
DriverManager.setDriver(driver);
30+
else
31+
throw new RuntimeException("WebDriver object is not initialized");
32+
}
33+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package Drivers;
2+
3+
import org.openqa.selenium.WebDriver;
4+
5+
public class DriverManager {
6+
7+
private static ThreadLocal<WebDriver> driver=new ThreadLocal<WebDriver>();
8+
9+
public static void setDriver(WebDriver driverObj){
10+
driver.set(driverObj);
11+
}
12+
13+
public static WebDriver getDriver(){
14+
return driver.get();
15+
}
16+
17+
public static void dropDriverObj(){
18+
driver.remove();
19+
}
20+
21+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package demo.customassert;
2+
3+
import Drivers.BrowserType;
4+
import Drivers.DriverFactory;
5+
import Drivers.DriverManager;
6+
import org.testng.annotations.AfterMethod;
7+
import org.testng.annotations.BeforeMethod;
8+
9+
public class BaseTest {
10+
11+
@BeforeMethod
12+
public void launchTest(){
13+
DriverFactory.initDriver(BrowserType.CHROME);
14+
DriverManager.getDriver().manage().window().maximize();
15+
}
16+
17+
@AfterMethod
18+
public void quitTest(){
19+
DriverManager.getDriver().quit();
20+
DriverManager.dropDriverObj();
21+
}
22+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package demo.customassert;
2+
3+
import Drivers.DriverManager;
4+
import org.openqa.selenium.By;
5+
6+
public class ProductPage {
7+
8+
private static final By PRODUCT_NAME = By.xpath("//div[contains(@class,'entry-content content-title')]/h1");
9+
private static final By PRODUCT_CODE = By.xpath("//span[text()='Product Code:']/following-sibling::span");
10+
private static final By PRODUCT_BRAND = By.xpath("//span[text()='Brand:']/following-sibling::a");
11+
private static final By PRODUCT_AVAILABILITY = By.xpath("//span[text()='Availability:']/following-sibling::span");
12+
private static final By CART_COUNT = By.xpath("//div[contains(@class,'flex-wrap')]//span[contains(@class,'cart-item-total')]");
13+
14+
public static ProductPage getProductPage() {
15+
return new ProductPage();
16+
}
17+
18+
public void loadProductPage(){
19+
DriverManager.getDriver().get("https://ecommerce-playground.lambdatest.io/index.php?route=product/product&product_id=40&search=iphone");
20+
}
21+
22+
public ProductPageValidator getValidator() {
23+
return ProductPageValidator.builder()
24+
.productBrand(getProductBrand())
25+
.productName(getProductName())
26+
.productCode(getProductCode())
27+
.productAvailability(getProductAvailability())
28+
.cartCount(Integer.valueOf(getCartCount())).build();
29+
}
30+
31+
public String getProductName() {
32+
return DriverManager.getDriver().findElement(PRODUCT_NAME).getText();
33+
}
34+
35+
public String getProductCode() {
36+
return DriverManager.getDriver().findElement(PRODUCT_CODE).getText();
37+
}
38+
39+
public String getProductBrand() {
40+
return DriverManager.getDriver().findElement(PRODUCT_BRAND).getText();
41+
}
42+
43+
public String getProductAvailability() {
44+
return DriverManager.getDriver().findElement(PRODUCT_AVAILABILITY).getText();
45+
}
46+
47+
public String getCartCount() {
48+
return DriverManager.getDriver().findElement(CART_COUNT).getText();
49+
}
50+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package demo.customassert;
2+
3+
import org.assertj.core.api.AbstractAssert;
4+
import org.assertj.core.api.SoftAssertions;
5+
6+
public class ProductPageAssert extends AbstractAssert<ProductPageAssert, ProductPageValidator> {
7+
8+
SoftAssertions assertions;
9+
protected ProductPageAssert(ProductPageValidator productPageAssert) {
10+
super(productPageAssert, ProductPageAssert.class);
11+
assertions=new SoftAssertions();
12+
}
13+
14+
public static ProductPageValidator getValidator(){
15+
return null;
16+
}
17+
18+
public ProductPageAssert productNameIs(String productName){
19+
assertions.assertThat(actual.getProductName()).isEqualTo(productName);
20+
return this;
21+
}
22+
public ProductPageAssert productCodeIs(String productCode){
23+
assertions.assertThat(actual.getProductCode()).isEqualTo(productCode);
24+
return this;
25+
}
26+
public ProductPageAssert productBrandIs(String productBrand){
27+
assertions.assertThat(actual.getProductBrand()).isEqualTo(productBrand);
28+
return this;
29+
}
30+
public ProductPageAssert productAvailabilityIs(String productAvailability){
31+
assertions.assertThat(actual.getProductAvailability()).isEqualTo(productAvailability);
32+
return this;
33+
}
34+
public ProductPageAssert matchesDefaultCartCount(){
35+
assertions.assertThat(actual.getCartCount()).isEqualTo(0);
36+
return this;
37+
}
38+
public void assertAll(){
39+
assertions.assertAll();
40+
}
41+
42+
43+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package demo.customassert;
2+
3+
import lombok.Builder;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
7+
@Builder
8+
@Getter
9+
public class ProductPageValidator {
10+
11+
private String productName;
12+
private String productCode;
13+
private String productAvailability;
14+
private String productBrand;
15+
private Integer cartCount;
16+
17+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package demo.customassert;
2+
3+
import org.testng.Assert;
4+
import org.testng.annotations.Test;
5+
6+
public class ProductTest extends BaseTest{
7+
8+
@Test(description = "Test Assertions using AssertJ")
9+
public void verifyLoginPageSpec(){
10+
ProductPage productPage=ProductPage.getProductPage();
11+
productPage.loadProductPage();
12+
ProductPageAssert productAssert=new ProductPageAssert(productPage.getValidator());
13+
productAssert
14+
.productNameIs("iPhone")
15+
.productCodeIs("product 11")
16+
.productAvailabilityIs("In Stock")
17+
.productBrandIs("Apple")
18+
.matchesDefaultCartCount()
19+
.assertAll();
20+
}
21+
}

0 commit comments

Comments
 (0)