Skip to content

Commit

Permalink
refactor test folder structure.
Browse files Browse the repository at this point in the history
add unit tests for PageScoped.java and web component field decoration.
  • Loading branch information
orcunbalcilar committed Sep 23, 2023
1 parent 9fd19e6 commit 24925c8
Show file tree
Hide file tree
Showing 21 changed files with 164 additions and 17 deletions.
48 changes: 48 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot.version}</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
Expand All @@ -58,6 +76,12 @@
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring-boot.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
Expand All @@ -72,6 +96,12 @@
<artifactId>selenium-java</artifactId>
<version>4.12.0</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>

<!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
Expand All @@ -85,6 +115,10 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency>

Expand Down Expand Up @@ -131,6 +165,20 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.7</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.20.0</version>
<scope>test</scope>
</dependency>

</dependencies>

<distributionManagement>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package integration.test.pojo;
package integration.pojo;

import io.github.orcunbalcilar.selenium.support.pagefactory.PageScoped;
import io.github.orcunbalcilar.selenium.support.pagefactory.WebComponent;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package integration.test.pojo;
package integration.pojo;

import io.github.bonigarcia.wdm.WebDriverManager;
import io.github.orcunbalcilar.selenium.support.pagefactory.WebComponentLocatorFactory;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package integration.test.pojo;
package integration.pojo;

import io.github.orcunbalcilar.selenium.support.pagefactory.WebComponent;
import org.openqa.selenium.WebElement;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package integration.test.pojo;
package integration.pojo;

import io.github.orcunbalcilar.selenium.support.pagefactory.WebComponentFieldDecorator;
import io.github.orcunbalcilar.selenium.support.pagefactory.WebComponentLocatorFactory;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package integration.test.pojo;
package integration.pojo;

import io.github.orcunbalcilar.selenium.support.pagefactory.WebComponentLocatorFactory;
import org.openqa.selenium.WebDriver;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package integration.test.pojo;
package integration.pojo;

import io.github.orcunbalcilar.selenium.support.pagefactory.WebComponent;
import org.openqa.selenium.support.FindBy;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package integration.test.springstructure;
package integration.springstructure;

import io.cucumber.spring.ScenarioScope;
import io.github.bonigarcia.wdm.WebDriverManager;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package integration.test.springstructure;
package integration.springstructure;

import io.github.orcunbalcilar.selenium.support.pagefactory.PageScoped;
import io.github.orcunbalcilar.selenium.support.pagefactory.SpringWebComponent;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package integration.test.springstructure;
package integration.springstructure;

import io.cucumber.spring.CucumberContextConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package integration.test.springstructure;
package integration.springstructure;

import io.github.orcunbalcilar.selenium.support.pagefactory.WebComponent;
import org.openqa.selenium.WebElement;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package integration.test.springstructure;
package integration.springstructure;

import java.util.List;
import org.openqa.selenium.WebElement;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package integration.test.springstructure;
package integration.springstructure;

import io.github.orcunbalcilar.selenium.support.pagefactory.SpringBeanElementInitializer;
import io.github.orcunbalcilar.selenium.support.pagefactory.WebComponentLocatorFactory;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package integration.test.springstructure;
package integration.springstructure;

import io.cucumber.spring.ScenarioScope;
import io.github.orcunbalcilar.selenium.support.pagefactory.SpringWebComponent;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package integration.test.springstructure;
package integration.springstructure;

import io.cucumber.spring.ScenarioScope;
import io.github.orcunbalcilar.selenium.support.pagefactory.SpringWebComponent;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package integration.test.springstructure;
package integration.springstructure;

import io.github.orcunbalcilar.selenium.support.pagefactory.SpringWebComponent;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package integration.test.springstructure;
package integration.springstructure;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package integration.test.springstructure;
package integration.springstructure;

import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
Expand Down
69 changes: 69 additions & 0 deletions src/test/java/unit/SpringWebComponentFieldDecoratorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package unit;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

import io.github.orcunbalcilar.selenium.support.pagefactory.SpringWebComponentFieldDecorator;
import io.github.orcunbalcilar.selenium.support.pagefactory.WebComponentLocatorFactory;
import java.lang.reflect.Field;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.PageFactory;

public class SpringWebComponentFieldDecoratorTest {

@Test
public void testDecorate_WebComponentField() throws Exception {
WebDriver driver = mock(WebDriver.class);

WebElement mockParentElement = mock(WebElement.class);
when(driver.findElement(By.cssSelector(".test-web-component"))).thenReturn(mockParentElement);

WebElement mockTestElement = mock(WebElement.class);
when(mockParentElement.findElement(By.id("test"))).thenReturn(mockTestElement);

SpringWebComponentFieldDecorator decorator =
new SpringWebComponentFieldDecorator(driver, new WebComponentLocatorFactory(driver, 5));
TestPage testPage = new TestPage();
PageFactory.initElements(decorator, testPage);

// Reaching testElement from TestWebComponent
Field webComponent = testPage.getClass().getDeclaredField("webComponent");
webComponent.setAccessible(true);
TestWebComponent component = (TestWebComponent) webComponent.get(testPage);
Field testElementField = component.getClass().getDeclaredField("testElement");
testElementField.setAccessible(true);
WebElement testElementFromComponent = (WebElement) testElementField.get(component);

assertNotNull(component);
assertEquals(mockParentElement.toString(), component.getSearchContext().toString());
assertEquals(mockTestElement.toString(), testElementFromComponent.toString());
}

@Test
public void testDecorate_PageScopedField() throws Exception {
WebDriver driver = mock(WebDriver.class);

WebElement mockPageScopedElement = mock(WebElement.class);
when(driver.findElement(By.id("pageScopedElement"))).thenReturn(mockPageScopedElement);

SpringWebComponentFieldDecorator decorator =
new SpringWebComponentFieldDecorator(driver, new WebComponentLocatorFactory(driver, 5));
TestPage testPage = new TestPage();
PageFactory.initElements(decorator, testPage);

// Reaching pageScopedElement from TestWebComponent
Field webComponentField = testPage.getClass().getDeclaredField("webComponent");
webComponentField.setAccessible(true);
TestWebComponent webComponent = (TestWebComponent) webComponentField.get(testPage);
Field pageScopedElementField = webComponent.getClass().getDeclaredField("pageScopedElement");
pageScopedElementField.setAccessible(true);

WebElement pageScopedElement = (WebElement) pageScopedElementField.get(webComponent);

assertNotNull(pageScopedElement);
assertEquals(mockPageScopedElement.toString(), pageScopedElement.toString());
}
}
11 changes: 11 additions & 0 deletions src/test/java/unit/TestPage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package unit;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

public class TestPage {
private WebElement webElement;

@FindBy(css = ".test-web-component")
private TestWebComponent webComponent;
}
19 changes: 19 additions & 0 deletions src/test/java/unit/TestWebComponent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package unit;

import io.github.orcunbalcilar.selenium.support.pagefactory.PageScoped;
import io.github.orcunbalcilar.selenium.support.pagefactory.WebComponent;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

public class TestWebComponent extends WebComponent {
@FindBy(id = "test")
private WebElement testElement;

@PageScoped
@FindBy(id = "pageScopedElement")
private WebElement pageScopedElement;

public TestWebComponent() {
super();
}
}

0 comments on commit 24925c8

Please sign in to comment.