TransWikia.com

Which is preferred in selenium automation using Java, writing tests by pages or functionality?

Software Quality Assurance & Testing Asked by Na001 on January 16, 2021

I am new to Selenium automation, in automating a web application which is preferred. Can we write scripts page by page or functionalities. How to identify that?

  • if it’s page by page how we verify the success criteria for any element?
  • if it’s functionality how to verify other generic validations?

Appreciate your help in detail

Thank you

4 Answers

I agree with the other commenters that the Page Object Model is a great way to structure test code for interacting with the page, however, for the tests themselves, I recommend writing tests for functionality instead of page-by-page.

Tests aren't about validating pages, after all. All of your users and most of your staff don't care which page something is on, they care whether it works or not.

There's a bit of nuance. Unit tests will often run against a single component of a page, whereas functional tests run across multiple pages. For a functional test, grouping by functionality reflects how your developers will make changes, and allows for sensible multi-page tests. For instance, imagine you're writing a mortgage application. There are five steps for making a new application:

  1. Create an account or log in on the homepage
  2. Provide your ID
  3. Provide the property details
  4. Provide your financial details
  5. Submit the application

Your users will never visit the 'Property Details' page directly. They must take the other steps first.

If you wrote 5 separate page tests, you'd have to make extra, crappy tests for each page to make sure it was receiving and providing the right data. If you add a new page between 4&5, you now need to change the test code for page 4, the new page, and page 5.

If your application is composed of re-usable components, page-based tests make even less sense. Say users can provide financial information without logging in, on order to estimate how much they can borrow. If you're using page-based tests, you have to write and update both tests when that component changes.

If instead you're doing functionality grouped tests, you can instead create a page object for that component and write a unit tests for it. Then, during your functional tests, just pass the page object the relevant data for either page, and assume that

Answered by Dylan Lacey on January 16, 2021

Option1: If you want to use selenium for page by page verifications, you can use page object model in following way:

/***
 * Tests login feature
 */
public class Login {

  public void testLogin() {
    // fill login data on sign-in page
    driver.findElement(By.name("user_name")).sendKeys("testUser");
    driver.findElement(By.name("password")).sendKeys("my supersecret password");
    driver.findElement(By.name("sign-in")).click();

    // verify h1 tag is "Hello userName" after login
    driver.findElement(By.tagName("h1")).isDisplayed();
    assertThat(driver.findElement(By.tagName("h1")).getText(), is("Hello userName"));
  }
}

Option 2: If you are using selenium framework to test web UI, accessibility and API services, then preferable to use cucumber boilerplate:

Reference: webdriverio/cucumber-boilerplate Sample Cucumber Feature File with maximum reusability:

Feature:
    In order to keep my product stable
    As a developer or product manager
    I want to make sure that everything works as expected

Scenario: Check title of website after search
    Given I open the url "http://google.com"
    When I set "WebdriverIO" to the inputfield "#lst-ib"
    And I press "Enter"
    Then I expect that the title is "WebdriverIO - Google Search"

Option3: If you want to use an automation framework for locally built app for web UI, preferable to use Cypress instead Selenium:

Feature: Home Page Test on OrangeHRM website

   Scenario: Check valid home page tabs display
      Given I open OrangeHRM homepage
      When I SignIn as user
      Then the user name should be displayed
      And the Admin tab should be displayed
   
   Scenario: Click on Admin Tab of home page
      Given I open OrangeHRM homepage
      When I SignIn as user
      And I click on admin tab of home page
      Then the admin sub tab section displayed

Reference: narayananpalani/cypress-test-techniques.

Answered by Karunakaran on January 16, 2021

I would recommend using page object model where you create a specific class where you put all the elements/objects and actions can be done within a page that makes them reusable to test cases that you'll automate.

Answered by Oelyu09 on January 16, 2021

Try using Page Object model, to verify the elements let the function return boolean value and check if it's not false. For example, if you got click on login button create a function with try catch. If the element is available then click on it and return true else return false.

Answered by Nael Marwan on January 16, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP