Monday, 25 July 2022

Types of Design Patterns.

 1) Creational:-

    These design patterns are used when a decision must be made at the time of  instantiation of a class            (i.e.creating an object of a class).

    1. Factory Method Pattern :Factory Method Pattern says that just define an interface or abstract  class for creating an object but let the subclasses decide which class to instantiate
    2. Abstract Factory Pattern :Abstract Factory Pattern says that just define an interface or abstract class for creating families of related (or dependent) objects but without     specifying their concrete sub-classes.
    3. Singleton Pattern: Singleton Pattern says that just"define a class that has only one instance and provides a global point of access to it".In other words, a class must ensure that only single instance should be created and single object can be used by all other classes.

                           There are two forms of singleton design pattern

    •   Early Instantiation: creation of instance at load time.
    •   Lazy Instantiation: creation of instance when required.

               4 . Prototype Pattern: Prototype Pattern says that cloning of an existing object instead of                    creating new one and can also be customised as per the requirement. This pattern should                    be followed, if the cost of creating a new object is expensive and resource intensive.

               5. Builder Pattern: Builder Pattern says that "construct a complex object from simple                         objects using step-by-step approach"  It is mostly used when object can't be created in                        single step like in the de-serialisation of a complex object.

              6.Object Pool Pattern:Object Pool Pattern says that " to reuse the object that                                       are expensive to create". Basically, an Object pool is a container which contains a                               specified amount of objects. When an object is taken from the pool, it is not available in                     the pool until it is put back.Objects in the pool have a lifecycle: creation, validation and                     destroy.

2)Structural :-

      Structural design patterns are concerned with how classes and objects can be composed.

    • Adapter Pattern-Adapting an interface into another according to client expectation.
    • Bridge Pattern  -Separating abstraction (interface) from implementation.
    • Composite Pattern- Allowing clients to operate on hierarchy of objects.
    • Decorator Pattern-Adding functionality to an object dynamically.
    • Facade Pattern-Providing an interface to a set of interfaces.
    • Flyweight Pattern-Reusing an object by sharing it.
    • proxy Pattern -Representing another object.

 3)Behaviour:-

In these design patterns, the interaction between the objects should be in such a way that they can easily talk to each other and still should be loosely coupled.

  1.          Chain of Responsibility Pattern
  2.          Command Pattern
  3.          Interpreter Pattern
  4.          Iterator Pattern
  5.          Mediator Pattern
  6.          Memento Pattern
  7.          Observer Pattern
  8.          State Pattern
  9.          Strategy Pattern
  10.         Template Pattern
  11.         Visitor Pattern
  12.          Null Object




Tuesday, 2 December 2014

Understanding Insight Objects in UFT 11.5!!

With Unified Functional Testing, there were a lot of features like integrating GUI testing and API testing, introducing File Content Checkpoint, and create insight object or recording in Insight mode. In this article I will try to explain what this feature of Insight recording is and how to implement same.

Insight is an image-based identification ability, to recognize objects in application based on what they look like instead of using properties that are part of design. UFT stores an image of the object with insight test objects and uses this image as main description property to identify object in the application. Together with insight property, UFT can use ordinal identifier to uniquely identify the object. Another property we can use is Similarity - Specifies how similar a control in the application has to be to the test object image for it to be considered a match. And Visual relation identifier can also be used to identify the object based on its relative position with respect to other objects in the application. 

Low points of insight object are we cannot add insight object from object spy and snapshots occupy a lot of disk space.

Adding Insight Objects :

UFT allow adding Insight object either in recording Mode or manually in object repository:
1.  Insight Recording: When we record in UFT, There is option to select recording mode. The recording mode in UFT is default, analog, Low Level recording, and Insight recording. When we select recording mode as Insight recording, UFT records object as insight object for images and identifies rest of objects like edit box as window object WinObject and perform action on them.


2.Insight Objects can be added through Object Repository. Go to Resources>Object Repository. This will open Object Repository window. In Object Repository window, Navigate to Object>Add Insight objects to Local. This can also be added by icon as shown below.
Once we click on Add Insight objects to local, UFT asks to select Learn Mode which can be manual and automatic. Manual Mode provides magnifier to select minute objects in the application.


Insight object is always added to the object repository as a child of the test object that represents its containing application, such as a Window or Browser object. 

In the Editor, test object image is displayed in the step instead of the test object name. Enlarge Image is displayed on hovering over the object.

Options available to work with Insight in UFT 11.5:

UFT 11.5 provides following options for working with insight objects. Options for Insight Object are at Tools>Options>GUI Testing>Insight. This pane enables you to define options that customize how UFT handles Insight test objects when creating test object, and during record and run sessions.
Options to customize the insight object are as follows:
 a.   Save the clicked coordinates as the test object’s Click Point when recording a test object.
 b.   When recording a test, display mouse operations.




c.When editing shows test object image in steps and displaying Select learn mode dialog box
d. Snapshots for insight object takes a lot of disk space, we can provide option to limit test object image, maximum pixels around image, and maximum number of snapshots to save when recording a test object




--Sudhakar.Mangi

Tuesday, 7 October 2014

Using Page Factories with Loadable Component!!!

Continues with my previous Post!!!

LoadableComponent is another way to approach PageObjects. LoadableComponent 
is a base class that all of the pages need to extend. The base class has the following 
methods on the interface
             
                   1) get()
                   2) isLoaded()
                   3) load()


 Instead of the usual public class PageObject, we change it:

     public class PageObject extends LoadableComponent<PageObject>

We will have to add overrides for the load() and isLoaded() method. The load method
will load the page for us and the isLoaded() method can allow us to check if the page 
has been loaded correctly.

@override
Protected void load() {
selenium.get("http://book.theautomatedtester.co.uk");
}

@protected void isLoaded() {
String url = selenium.getCurrentUrl();
If (url != "http://book.theautomatedtester.co.uk"){
throw new Exception("The wrong page has loaded");
}
}

As we can see this is just a simple bit of code, but we can make sure that we start 
on the right page when we need to.

Now that we have learnt about LoadableComponents, we should have a look at 
seeing it in action. We need to make changes to our Java Class.


1. The following is how the code should look so far:

public class Chapter2 {
WebDriver selenium;
@FindBy(how= How.NAME, using="verifybutton")
WebElement verifybutton;
public Chapter2(WebDriver selenium){
this.selenium = selenium;
if (!"Chapter 2".equalsIgnoreCase(this.selenium.getTitle())){
selenium.get("http://book.theautomatedtester.co.uk/chapter2");
}
}
public boolean isButtonPresent(String button){
return selenium.findElements(By.xpath
("//input[@id='"+button+"']")).size()>0;
}
}


2. If we have a look at our Chapter 2 Java class, we can see that we need to extend
LoadableComponent. Since this takes generics we will have to pass in our
PageObject class. It should look like:

public class Chapter2 extends LoadableComponent<Chapter2> {

3. In our constructor, we will have to initialize our page factory. We can remove the
rest of the code in there since that will be moved to load(). It should look like the
following:

public Chapter2(WebDriver selenium){
this.selenium = selenium;
PageFactory.initElements(selenium, this);
}


4. We now need to add our override methods. These will allow us to check that we are
on the right page when we load this component:

@override
Protected void load() {
selenium.get("http://book.theautomatedtester.co.uk/chapter2");
}
@protected void isLoaded() {
String url = selenium.getCurrentUrl();
If (url != "http://book.theautomatedtester.co.uk/chapter2"){
throw new Exception("The wrong page has loaded");
}
}

5. Now we need to have a look at updating our test to load everything for us.
 To do this we need to change:

@Test
public void ShouldLoadTheHomePageAndThenCheckButtonOnChapter2() {
selenium.get("http://book.theautomatedtester.co.uk");
HomePage hp = new HomePage(selenium);
Chapter2 ch2 = hp.clickChapter2();
assertTrue(ch2.isButtonPresent("but1"));
}

6. To look like this:

@Test
public void ShouldLoadTheHomePageAndThenCheckButtonOnChapter2(){
Chapter2 cht = new Chapter2(selenium).get();
ch2.isButton("but1");
}


7. Run your test. Everything should look like the following:


public class Chapter2 extends LoadableComponent<Chapter2>{
WebDriver selenium;
@FindBy(how= How.NAME, using="verifybutton")
WebElement verifybutton;
public Chapter2(WebDriver selenium){
this.selenium = selenium;
PageFactory.initElements(selenium, this);
}
@override
Protected void load() {
selenium.get("http://book.theautomatedtester.co.uk/chapter2");
}
@protected
public void isLoaded() {
String url = selenium.getCurrentUrl();
If (url != "http://book.theautomatedtester.co.uk/chapter2"){
throw new Exception("The wrong page has loaded");
}
}
public boolean isButtonDisplayed(String button){
return selenium.findElement(By.id("button")).isDisplayed();
}
}

What just happened?

We have just converted our page object to use the LoadableComponent class 
that comes with the Selenium Project. We saw how we simplified on constructors 
and then just moved this into somewhere easy to maintain. We have seen that 
we can move a lot of the boiler plate code out of our class and rely on it 
being pulled in via LoadableComponent. This means that we no 
longer need to maintain it or we add those items.

Imagine how you have to work with a flow that takes you through a number of pages.
LoadableComponent allows us to set up a workflow. To get this right we need to pass
one in like the following when doing your test setup:

@Before
public void prepareComponents() {
WebDriver selenium = new FirefoxDriver();
HomePage homePage = new HomePage(selenium);
Chapter2 chapter2 = new SecuredPage(selenium, homePage);

}