0

I have divided the system modules into several packages in eclipse. In there one java class is for one particular page and the other consists of the test cases.

Apart from this, I have used the default package for common methods that are using throughout the whole system.

systemDefaultCommands(package name) > TestCommands.java

public class TestCommands {
public void click(By locator , WebDriver driver) {
    try {
        WebElement element = driver.findElement(locator);
        element.click();            
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }       
}   
public void type(By locator , WebDriver driver , String key) {
    try {
        WebElement element = driver.findElement(locator);
        element.clear();
        element.sendKeys(key);          
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }       
}

}

systemAdministratorfunctionalities > AdministratorPage

can we capture web elements as this?

public By userName() { return By.xpath("//input[@id='mat-input-0']"); } Is this correct ?

public class AdministratorPage extends TestCommands{

//web elements
public By userName() {
    return By.xpath("//input[@id='mat-input-0']");
}
public By password() {
    return By.xpath("//input[@id='mat-input-1']");
}
public By loginButton() {
    return By.xpath("//span[@class='mat-button-wrapper']");
}

//Business Functions
public void openApplication(String url,WebDriver driver) {
    open(url, driver);      
}
public void loginToApplication(WebDriver driver,String userName,String password){
    type(userName(),driver,userName);
    type(password(),driver,password);
    click(loginButton(),driver);
}

}

systemAdministratorfunctionalities > TC_Administrator This is where the actions performing and print the relevant results by TestCommands methods. I have used xml file to send browser name and the data container as data provider.

public class TC_Administrator {
     WebDriver driver;
     String baseUrl;
     AdministratorPage admin = new AdministratorPage();

@Parameters("browser")
@BeforeMethod
    public void beforeMethod(String browser) {
      if(browser.equals("firefox")) {
          System.setProperty("webdriver.gecko.driver", "drivers\\\\geckodriver.exe");
          driver = new FirefoxDriver();
          driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
          driver.manage().window().maximize();
      }else if(browser.equals("chrome")){
          System.setProperty("webdriver.chrome.driver", "drivers\\chromedriver.exe");
          driver = new ChromeDriver();
          driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
          driver.manage().window().maximize();
        }            
    }
    @Test(priority = 1,dataProvider = "DataContainer")
    public void openAdministrator(String userName, String password,String searchCustomer) throws InterruptedException {
       admin.openApplication("https://test.com/", driver);
       admin.loginToApplication(driver, userName, password);            
    }

    @AfterMethod
    public void afterMethod() {
       driver.quit();
    }
2
  • What exactly is your question? – João Farias Jan 23 at 10:10
  • 1.Am I doing POM properly? 2.Can I capture the web elements as above?(AdministratorPage) – user3806999 Jan 23 at 10:15
0

There is no write way of writing page object class:

This is from selenium : https://www.selenium.dev/documentation/en/guidelines_and_recommendations/page_object_models/

There is a lot of flexibility in how the page objects may be designed, but there are a few basic rules for getting the desired maintainability of your test code.

Page objects themselves should never make verifications or assertions. This is part of your test and should always be within the test’s code, never in an page object. The page object will contain the representation of the page, and the services the page provides via methods but no code related to what is being tested should be within the page object.

There is one, single, verification which can, and should, be within the page object and that is to verify that the page, and possibly critical elements on the page, were loaded correctly. This verification should be done while instantiating the page object. In the examples above, both the SignInPage and HomePage constructors check that the expected page is available and ready for requests from the test.

A page object does not necessarily need to represent all the parts of a page itself. The same principles used for page objects can be used to create “Page Component Objects” that represent discrete chunks of the page and can be included in page objects. These component objects can provide references the elements inside those discrete chunks, and methods to leverage the functionality provided by them. You can even nest component objects inside other component objects for more complex pages. If a page in the AUT has multiple components, or common components used throughout the site (e.g. a navigation bar), then it may improve maintainability and reduce code duplication.

There are other design patterns that also may be used in testing. Some use a Page Factory for instantiating their page objects. Discussing all of these is beyond the scope of this user guide. Here, we merely want to introduce the concepts to make the reader aware of some of the things that can be done. As was mentioned earlier, many have blogged on this topic and we encourage the reader to search for blogs on these topics.

So as you can see Page object model is just a design pattern recommendation that helps to reduce the complexities in test maintenance. There is no strict coding standards or recommendations than these few that is suggested above.

When ever you code something make sure you avoid proliferation of classes

https://flylib.com/books/en/4.425.1.31/1/

Meaning remove unnessary classes and codes which you kept just to make it look like a good practice.

Summary:

  1. Is it helping in your project then its a good approach
  2. Do you have unnessary abstraction like one class just for locators , one class for calling that locator and finding the actual element and so on. In this case if an element is removed you have to updated two classes . So this is a bad practice try to minimize required classes.
1
  1. I don't think it is the best practice to pass your web-Driver as a parameter and pass it to every single method.
  2. Don't create method for returning the web-Elements , Instead Use PageFactory or your By class at the top of your page class .
  3. If possible separate your webElement from your page , Keep them in Some separate file. SO in Future if your WebElement changes you dont need to do any change in your code class. ...Happy Testing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.