0% found this document useful (0 votes)
276 views47 pages

Automation-Selenium Test Automation Framework

The document provides an overview of Selenium Test Automation Framework (STAF). It discusses Behavior Driven Development (BDD) fundamentals and why Cucumber is used. It describes the Cucumber testing stack including features, scenarios, and steps. It then demonstrates how to set up a sample automation project in Cucumber with a scenario, step definitions, Serenity.properties file, step libraries, and page objects.

Uploaded by

Alok Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
276 views47 pages

Automation-Selenium Test Automation Framework

The document provides an overview of Selenium Test Automation Framework (STAF). It discusses Behavior Driven Development (BDD) fundamentals and why Cucumber is used. It describes the Cucumber testing stack including features, scenarios, and steps. It then demonstrates how to set up a sample automation project in Cucumber with a scenario, step definitions, Serenity.properties file, step libraries, and page objects.

Uploaded by

Alok Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 47

SELENIUM TEST AUTOMATION

FRAMEWORK(STAF)

Prepared By: Sudip Rakshit


November 2015
LET’S START FROM THE BEGINNING…

2 Confidential / CenturyLink Employees and


Contractors Only
BDD FUNDAMENTALS
• Behavior Driven Development(BDD), is a core concept underlying many important
features of Serenity.
• Behavior Driven Development is used for conversations and collaboration of concrete
examples to understanding the features that are suppose to build.
• Conversations about concrete examples, and counter-examples, are a great way to
eliminate any hidden assumptions or misunderstandings about what a feature needs to
do.
• Automating acceptance tests provides valuable feedback to the whole team, unlike unit
and integration tests, these are expressed in business terms, and can be understood by
non-developers..
• For instance,
In order to create a commercial account in Salesforce
As a commercial user
I want to create commercial customer account containing all information

3 Confidential / CenturyLink Employees and


Contractors Only
Why Cucumber?

• In Cucumber, you express acceptance criteria in a


natural, human-readable form. 
• Cucumber is a popular BDD test automation tool. 
• Provides information about high level business
functionality and the purpose of application under test.
• It consists of scenarios that represent a particular
functionality which is under test.

4 Confidential / CenturyLink Employees and


Contractors Only
The Cucumber Testing Stack

• The tests themselves are housed in Features – a text


file with a feature extension.  Features are made up
of Scenarios – scenarios are synonymous to test
cases.  The Scenarios are made up of Individual Steps
– Steps are nothing but the component interactions (or
Test Steps) for a given Scenario or Test Cases.
• The Features, Scenarios & Steps make up the Business
Facing components of the Cucumber stack – this the
Front-End File/Document that Business Analysts-
Developers-QA Analysts use to define testing
requirements.

5 Confidential / CenturyLink Employees and


Contractors Only
6 Confidential / CenturyLink Employees and
Contractors Only
HOW DOES IT
ACTUALLY WORK

7 Confidential / CenturyLink Employees and


Contractors Only
Automation Project

8 Confidential / CenturyLink Employees and


Contractors Only
Let’s start with our first project

SfaTest

9 Confidential / CenturyLink Employees and


Contractors Only
The Scenario Runner(Runner Class/CukesTest.class)

Cucumber runs the feature files through JUnit, and needs a dedicated test
runner class to actually run the feature files. 

@TestEnvironment(Environment.E2E)
@RunWith(CucumberWithSerenity.class)
@CucumberOptions(features="src/test/resources/features",tags={"@Test"})
public class CukesTest { }

• @TestEnvironment-The test environment to run.


• @RunWith-It requires Cucumber withserenity class to run the test
• @CucumberOptions-
-features: provide the root directory of the feature files.
-tags: the tags of features/scenario to be included

   

10 Confidential / CenturyLink Employees and


Contractors Only
Writing the scenario

Let us start with the same example :

Given I am logged in as a "Commercial" user in SFA

When I create a commercial customer account as "


Commercial Customer Account"

Then account should be created successfully

11 Confidential / CenturyLink Employees and


Contractors Only
Step Definition

• In Cucumber, each line of the Gherkin scenario maps to


a method in a Java class, known as a Step Definition.

• These use annotations like 


@Given, @When and @Then match lines in the
scenario to Java methods. 

12 Confidential / CenturyLink Employees and


Contractors Only
• The @Steps annotation tells Serenity that this variable
is a Step Library.
• Layered approach makes the tests both easier to
understand and to maintain,
• Helps build up a great library of reusable business-level
steps that we can use in other tests.
• Without the layered approach, step definitions tend to
become technical very quickly, which limits reuse and
makes them harder to understand and maintain.

13 Confidential / CenturyLink Employees and


Contractors Only
public class AccountStepDefinition {
@Steps
UserSteps endUser;
@Steps
CommercialAccountSteps commercialAccountSteps;
@Given("^I am logged in as a \"([^\"]*)\" user in SFA$")
public void i_am_logged_in_as_a_user_in_SFA(String userType) {
endUser.is_in_sfa_login_page();
endUser.logs_in_as(userType);
}
@When("^I create a commercial customer account as \"([^\"]*)\"$")
public void i_create_a_commercial_customer_account(String accountName) {
commercialAccountSteps.create_commercial_account(accountName);
Serenity.setSessionVariable("account_name").to(accountName);
}
@Then("^account should be created successfully$")
public void account_should_be_created_successfully() {
assertThat(
"Account is not created",
accountSteps.is_account_created(Serenity.sessionVariableCalled(
"account_name").toString()));
}
14 Confidential / CenturyLink Employees and
Contractors Only
Serenity.properties

Serenity.properties contain the key-value pairs required to run the WebDriver instance.

# Options are firefox, chrome, iexplorer
webdriver.driver=firefox
 
# Below property is to wait for specific amount of time in milli seconds before timing
out
webdriver.timeouts.implicitlywait=10000
 
# Below property is to define when to take screenshots
# Other Screenshot options are FOR_EACH_ACTION,
BEFORE_AND_AFTER_EACH_STEP, AFTER_EACH_STEP
serenity.take.screenshots=FOR_FAILURES
 

15 Confidential / CenturyLink Employees and


Contractors Only
Let’s go through

SfaTools

16 Confidential / CenturyLink Employees and


Contractors Only
The Step Libraries (Step.class)

• These steps implement the “how" behind the “what" of the Given-
When-Then" steps.
• Steps should not be overly complex, and should focus on working
at a single level of abstraction.
• Steps typically orchestrate calls to more technical layers such as
web services, databases, or WebDriver page objects.
• For example, in automated web tests, the step library methods do
not call WebDriver directly, but rather they typically interact
with Page Objects.
• Step libraries often use Page Objects, which are automatically
instantiated
• The @Step annotation indicates a method that will appear as a
step in the test reports.
• The @Step annotation helps taking screenshots according
serenity.property value
• @Step annotation resembles business actions.
17 Confidential / CenturyLink Employees and
Contractors Only
public class AccountSteps extends SfaSteps{
LoginPage loginPage;
CommercialAccountEditPage commercialAccountEditPage;
@Step
public void logs_in_as(String userType) {
loginPage.shouldExist(loginPage);
loginPage.enterCredentials(userType);
loginPage.btn_log_in_to_salesforce.click();
loginPage.WaitForPageToLoad(100);
}
@Step
public void create_commercial_account(String accountName) {
create_commercial_account(get_data_for_page(commercialAccountEditPage)
.getMandatoryFieldsFromAllContainers());
}
@Step
public boolean is_account_created(String accountName) {
boolean isOpen = false;
isOpen = is_account_open(accountName);
return isOpen;
}
}

18 Confidential / CenturyLink Employees and


Contractors Only
The Page Objects

• Page Objects encapsulate how a test interacts with a particular


web page.
• They hide the WebDriver implementation details about how
elements on a page are accessed and manipulated behind more
business-friendly methods.
• Like steps, Page Objects are reusable components that make the
tests easier to understand and to maintain.
• Serenity automatically instantiates Page Objects for you, and
injects the current WebDriver instance.
• All you need to worry about is the WebDriver code that interacts
with the page. And Serenity provides a few shortcuts to make this
easier as well. 
• We may use a member variable annotated with
the @FindBy annotation

19 Confidential / CenturyLink Employees and


Contractors Only
public class LoginPage extends SfaPage {
@FindBy(id = "username")
public WebElementFacade tbx_username;

@FindBy(id = "password")
public WebElementFacade tbx_password;

public void enterCredentials(String userType) {


IntDataContainer dataContainer = envData.getContainer(
this.getClass().getSimpleName()).getContainer(userType);
tbx_username.type(dataContainer.getFieldValue("tbx_username"));
tbx_password.type(dataContainer.getFieldValue("tbx_password"));
}
}

20 Confidential / CenturyLink Employees and


Contractors Only
Points To Remember

21 Confidential / CenturyLink Employees and


Contractors Only
While Creating a Feature File or existing file..

o   Try to reuse the existing gherkin.


o   Pass parameter when same step can be reused with
different value in future.
o   Gherkin should be simple but definitive.

22 Confidential / CenturyLink Employees and


Contractors Only
While Creating related Step definition…

o   Create related methods to perform the action


o   Make the function name readable according to the
functionality
o   Provide proper annotation as mentioned in Gherkin.
o   Create the objects of the step and page classes as
required.

23 Confidential / CenturyLink Employees and


Contractors Only
While Creating a class related to the Step which will
contain all the logical step

o   Create objects of the Pages which are required to


execute the steps
o   Extend BaseStep Class like SfaSteps class
o   Create readable method names according to the logical
step which is being performed.
o   Call the related functions from the page to complete the
logical flow
 

24 Confidential / CenturyLink Employees and


Contractors Only
While Creating class related to the PageObject..
    
o Provide the name as per the Page name like
OppurtunityEditPage.class

..continued

25 Confidential / CenturyLink Employees and


Contractors Only
While Creating class related to the PageObject..
o Extend BasePage Class like SfaPage class
o Add all the elements of the page as required with a proper name as
provided in the page
o Include all small actions in the page in a respective method.
o Provide the container name from where the data will be fetched/extracted.
o Provide the unique element of the page
   Follow the naming convention according to the element type-
•   “tbx"-TextBox
•   "btn"-Button
•   "lbl"-Label
•   "lnk"-Link
•   "ddl"-DropdownList
•   "cbx"-CheckBox
•   "img"-image
• “rbn”-radio button

26 Confidential / CenturyLink Employees and


Contractors Only
Let’s refresh our memory

• Architecture of framework

• Project SfaTest

• Project SfaTools

27 Confidential / CenturyLink Employees and


Contractors Only
SfaTest

• Runner Class/CukesTest.class.

• Feature.

• Step Definition.

• Serenity.property.

28 Confidential / CenturyLink Employees and


Contractors Only
Sfa Tools

• Step library

• Page Objects

29 Confidential / CenturyLink Employees and


Contractors Only
Let’s get rolling…

30 Confidential / CenturyLink Employees and


Contractors Only
Data.xml

There are two base containers inside root.


 Environment
• envData variable
• Values dependent on environment
• Refresh data

 CommonData
• commonData Variable
• Values independent on environment

31 Confidential / CenturyLink Employees and


Contractors Only
AutoPopulate.class

It is the class which is responsible to decide the type


of element and put values in it.
• The method takes the object of the page and xml fields as the
parameter and populates the value in the webpage as per
prefix of the element.
• Data.xml field sequence is followed.
• The page class name should be same as the container name
for it to work
• All the elements mentioned in xml should exist in page class
• Should be properly prefixed.

32 Confidential / CenturyLink Employees and


Contractors Only
AutoPopulate methods

public void fillMandatoryFields(Page page, IntDataContainer


dataContainer)
It fills mandatory fields on the page whose type is mandatory in xml in container
mentioned
public void fillAllFields(Page page, IntDataContainer dataContainer)
It fills all fields on the page whose type is mandatory in xml in container mentioned
public void fillMandatoryFieldsFromAllContainers(Page page,
IntDataContainer dataContainer)
It fills mandatory fields on the page whose type is mandatory in xml in current
container and sub-container
public void fillAllFieldsFromAllContainers(Page page, IntDataContainer
dataContainer)
It fills all fields on the page whose type is mandatory in xml in current container and
sub-container
public void fillFields(Page page, List<IntContainerField> fields)
It fills custom fields mentioned in the list on the web page

33 Confidential / CenturyLink Employees and


Contractors Only
Other important methods

public String getFieldValue( String name )


return the value of the field
public IntDataContainer getContainer( String name )
returns the container
public void setFieldValue( String name, String value )
sets the field value of the field name passed
public List<IntContainerField> getFields()
returns list of fields
public List<IntContainerField> getMandatoryFields()
returns list of mandatory fields of the container
public Map<String,IntDataContainer> getContainers()
returns all the child list of containers
public void removeField(String fieldName)
removes the field from the container
public List<IntContainerField> getMandatoryFieldsFromAllContainers()
return list of mandatory fields from all containers

34 Confidential / CenturyLink Employees and


Contractors Only
Other important methods

 
public List<IntContainerField> getFieldsFromAllContainers()
return list of all fields from all containers
public void removeFields(String... fieldNames)
removes multiple fields from container
public void removeFieldsFromAllContainers(String... fieldNames)
removes multiple fields from current and sub container
public void setActualValues()
sets the actual values of the fields
public void setActualValuesForAllContainers()
sets the actual values in current and sub-container
 

35 Confidential / CenturyLink Employees and


Contractors Only
Annotation

• Name
 Name of the container should be same as Page Object Class
 Name of the field should have a valid prefix

• Type
 Mandatory
 Blank

• Value
 String
 String with prefix
 Keyword

36 Confidential / CenturyLink Employees and


Contractors Only
Various Approach in data.xml
The field value can be provided with various prefix to populate or verify different
elements-
Date:
• It populates the current date of the system.
• It can be suffixed with another string which populates the string prefixed by
“_currentdate”
DateTime:
• It populates the current date appended with current time of the system.
• It can be suffixed with another string which populates the string prefixed by
“_currentdatetime”
Wait-T:
• It waits for the time in seconds mentioned before populating the value(replace T
with numeric value)
Index:2
• Selects the second index from the Drop down
Container->Field
• refers the value of the element to the field in the container mentioned
• example- ServiceLocationEditPage->tbx_service_address_line1
 

37 Confidential / CenturyLink Employees and


Contractors Only
AutoVerify.class

This is class in the framework which helps in verifying


fields/values in the scenario.
• The method takes the object of the page and xml fields as the
parameter and asserts whether the value populated on the
webpage contains the expected value or not.
• Data.xml field sequence is followed.
• The page class name should be same as the container name
for it to work
• All the elements mentioned in xml should exist in page class
• Should be properly prefixed.

 
 

38 Confidential / CenturyLink Employees and


Contractors Only
AutoVerify methods

public void verifyMandatoryFields( (Page page, IntDataContainer dataContainer) {}


It fills mandatory fields on the page whose type is mandatory in xml in container
mentioned
public void verifyAllFields(Page page, IntDataContainer dataContainer) {}
It fills all fields on the page whose type is mandatory in xml in container mentioned
public void verifyMandatoryFieldsFromAllContainers(Page page, IntDataContainer
dataContainer) {}
It fills mandatory fields on the page whose type is mandatory in xml in current
container and sub-container
public void verifyAllFieldsFromAllContainers(Page page, IntDataContainer
dataContainer) {}
It fills all fields on the page whose type is mandatory in xml in current container and
sub-container
public void verifyCustomFields(Page page, List<IntContainerField> fields) {}
It fills custom fields mentioned in the list on the web page

39 Confidential / CenturyLink Employees and


Contractors Only
Other Important methods in Framework

public static String getDate(String... format) {}


returns the date in the format
public void switchToChildWindow() {}
switches the driver instance to child window
public void switchToParentWindow() {}
switches the driver instance back to parent window
public void switchToFrame(String frameName, int... waitTime) {}
switches the instance to the iFrame
public void WaitForPageToLoad(int... waitTime) {}
waits for the page to load for the time mentioned
public void WaitForFrameToLoad(final String frameName, int... waitTime) {}
waits for the mentioned frame to load for the time mentioned
public void shouldExist(Page page, int... waitTime) {}
waits for the unique element of the page to load for the time mentioned
public boolean isExist(Page page, int... waitTime) {}
verifies if the unique element of page mentioned is present for the time mentioned

40 Confidential / CenturyLink Employees and


Contractors Only
Serenity methods

 
There are various serenity methods available , however we will
extensively use the following-
serenity.setSessionVariable(Key).to(value)
It creates a session variable with the key provided as a parameter and also
assigns the value

serenity.sessionVariableCalled(Key)
It returns the value of the session variable with the name provided

  serenity.sessionVariableCalled(Key).equals(String)
verifies if the value of the string is equal to the variable value

41 Confidential / CenturyLink Employees and


Contractors Only
Dynamic Element Identification/Dynamic Xpath
Creation

There might be situation where the element changes


dynamically or there might be a need to modify XPath
as required.
 
A method should be created in Page which takes
parameter as the product/element specific values and
returns the XPath to take action.
 

42 Confidential / CenturyLink Employees and


Contractors Only
Lets look into a scenario to have a better
understanding

43 Confidential / CenturyLink Employees and


Contractors Only
Naming Convention

• To achieve a readable, consistent, optimized code, there has to be rule/convention


which needs to be followed for better maintenance.
• Let us all strictly follow the naming convention-
 A page object class name should always end with “Page”
 A step library class name should always end with “Steps”
 A step definition class name should always end with “StepDefinition”
 An Edit webpage must have “Edit” in the class name.
 A View webpage must have “View “ in the class name.
• The name of the elements should always be prefixed with the proper element type
prefix.
• The name of the element should be same as it appears in webpage in smaller case
with space replaced by ”_”.
• The name of the method should be given as per logical step performed.
For class name, use camelCase with first letter in uppercase.
Use camelCase for method names.
Name the step definition method similar to the gherkin should be used.

44 Confidential / CenturyLink Employees and


Contractors Only
Other Rules

• Session variables should be used only in Step definition.


• Pass parameter when the same gherkin can be reused
with other data.
• Avoid duplicity, check for existing Gherkin.
• ShouldExist should not be the last step of any method.
• Always start with shouldExist method.
• Small logical methods should be a part of the page so
that it can be reused in future.

45 Confidential / CenturyLink Employees and


Contractors Only
QUESTIONS??

46 Confidential / CenturyLink Employees and


Contractors Only
47 Confidential / CenturyLink Employees and
Contractors Only

You might also like