# Page Object Model

Page Object Model is a design pattern in Selenium that defines page classes for each web page. It has the following benefits:

1. Maintainability: If the UI of the web page changes, only the page object class needs to be updated, not the test scripts.
2. Readability: The page object classes act as an abstraction layer, making the test scripts more readable.
3. Reusability: The page object classes can be reused across multiple test scripts.

How it works:

1. Create a page class for each web page.&#x20;

This class will contain:

* WebElements as fields
* Methods to represent actions on that page

For example: LoginPage class for the login page will have:

```java
public class LoginPage {
    
private WebElement usernameField;

private WebElement passwordField;

private WebElement loginButton;

public void enterUsername(String username) {
     usernameField.sendKeys(username);
}

public void enterPassword(String password) {
     passwordField.sendKeys(password);  
}

public void clickLogin() {
     loginButton.click();
}

}
```

2. Initialize the page object in your test script:

```java
LoginPage loginPage = new LoginPage(driver);
```

3. Call the methods of the page object:

```java
loginPage.enterUsername("john");
loginPage.enterPassword("pass123");
loginPage.clickLogin();
```

4. If the UI of the login page changes, only the LoginPage class needs to be updated, not the test script.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://qatesting.gitbook.io/qa/automation-testing/flow/selenium/frameworks/page-object-model.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
