binzunu1909 / ui-test-selenium

Testing on Web API server about School Management
1 stars 0 forks source link

Page - Actions - UI - Entity // Also Builder #9

Open Bisllly opened 1 year ago

Bisllly commented 1 year ago

Here is an example of how you could structure your testing files using the POM:

src/
└── test/
    ├── java/
    │   └── com/
    │       └── myapp/
    │           ├── entity/
    │           │   └── Student.java
    │           ├── page/
    │           │   ├── BasePage.java
    │           │   ├── LoginPage.java
    │           │   └── SearchPage.java
    │           ├── repository/
    │           │   ├── StudentRepository.java
    │           │   └── UserRepository.java
    │           ├── service/
    │           │   ├── StudentService.java
    │           │   └── UserService.java
    │           └── webdriver/
    │               └── WebDriverFactory.java
    └── resources/
        └── testdata/
            ├── students.csv
            └── users.csv
Bisllly commented 1 year ago

You need builder to actualize prototype in test file. Supposed you need to test the add button, just come up with the prototype first

Find.element(StudentUI.ADD_STUDENT_BUTTON).click();

then create Find class, along with FindBuilder

public class Find extends BaseActions {
    private String action;
    public Find(WebDriver driver) { super(driver); }

    public static FindBuilder element(String addStudentButton) {
        return new FindBuilder(addStudentButton);
    }
    public static class FindBuilder {
        private String addStudentButton;
        public FindBuilder(String addStudentButton) { this.addStudentButton = addStudentButton; }

        public void click() {
            findElementByXpath(addStudentButton).click();
        }
    }
}

Then you can extend it with Actions. Along with using Page, UI, Entity, etc.

Bisllly commented 1 year ago

https://www.youtube.com/watch?v=0Ptcaxyne3s (Indian guy) https://youtu.be/9KdqObSYzy8 (about builder)

https://github.com/doctor-blue/design-patterns/tree/master/Creational/Builder https://www.geeksforgeeks.org/builder-pattern-in-java/

Bisllly commented 1 year ago
package actions;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class Find {
    private WebDriver driver;
    private String locator;

    private Find(WebDriver driver) {
        this.driver = driver;
    }

    public static Find using(WebDriver driver) {
        return new Find(driver);
    }

    public WebElement element(String locator) {
        return driver.findElement(By.xpath(locator));
    }

    public Find click() {
        WebElement element = element(locator);
        element.click();
        return this;
    }
}
  1. Identify the desired usage:
    Find.using(driver).element(LogInPage.NON_SSO_BTN).click();
  2. Determine the necessary components:
  1. Start by creating the Find class.

  2. Identify the required fields:

  1. Create a private constructor to accept a WebDriver instance and initialize the driver field.

  2. Implement a static using() method that takes a WebDriver instance and returns a new Find instance with the provided WebDriver set.

  3. Implement the element() method that takes a locator as a parameter and uses the driver.findElement() method with the XPath locator to locate and return the corresponding WebElement.

  4. Implement the click() method:

  1. Test the code by using the desired usage:
Find.using(driver)
    .element(LogInPage.NON_SSO_BTN)
    .click();

That's the step-by-step process to devise the code. It involves identifying the desired usage, determining the necessary components, and implementing the methods to support the desired functionality and method chaining.