lunes, 29 de junio de 2015

Run multiples test suites

Run multiples test suites:
If we have multiples test suites and want execute them,but we want to execute just one test suite.

Create three class with simple test methods

For example:



Create three xml file with on list the test in each test suite.



Format the test suite




Run test suite


Result of the execution, the three test suites ran.



lunes, 22 de junio de 2015

Listeners

The purpose of a results listener is to be notified of the test status by TestNG. The listener can then process or aggregate the results as needed. TestNG allows for a number of different listener types.They can be listener that are notified of any state changes as the happen or reporters that are invoked once the test run has completed and we need to generate a report for the user.

TestNG manages everything through suite, tests methods and the listeners gives us the ability to act  before and after of every suite, test, methods.

Types of listener
There are many types of listeners available in TestNg for example IAnnotation Transformer,IAnnotation Transformer2, IConfigurable, IConfigurationListener,IconfigurationListener2,IExecutionListener,IHoookable,IInovokedMethodLisner,IInovokedMethodLisner2,IMethodInterceptor, IReporter,IsuiteListener,ITestListener.

ISuiteLister: It has two method in it onStart() and onFinish(). Whenever a class implements this listener,tesng guaranteesthe end-user that it will invokethe methods on start() and onFinish()before and after running a testNg suite.


ITestListener: The working of this listener is also exactly the same as ISuiteListener but the only difference is that it makes the call before and after the test. Is has seven methods in it.


IInvokedMethodListener: The working of this listener is also exactly the same as ISuiteListener & ITestListener and the only difference is that it makes the call before and after every Method. It has only two methods in it.


For example:
Implement the interface methods: ISuiteLister ,ITestListener,IInvokedMethodListener.

Create a test case, a xml file and a class to implement the ISuiteLister ,ITestListener,IInvokedMethodListener.



Test case "ExampleListener"

The testng.xml file


Execute the testng.xml file






Another example:
We take a screenshot when the test fails.
In our listener  class we will add a method that takes a screenshot and we must call within "onTestFailure" method because when an error occurs,onTestFailure method will execute, so we take the screenshot.



We also create a new class by our webDriver

This is our test case.


our test was successfully executed, but we change the button name to fail the test. 




Refresh the project and will appear the image the screenshot














lunes, 15 de junio de 2015

Enum

Enum types: An enum type is a special data types that enables for a variable to be a set of predefined constants. The variblae must be equal to one of the values that have been predefined for it.
Because they are constants, the name of an enum type's field are in uppercase letters.
We define an enum type by using the enum keyword.
We should use enum types any time we need to represent a fixed set of constants when we know all possible values at compile time.

Create our "Enum" type



Create our class where look for elements using the enum type





Identify the elements on page 



This is our test class















lunes, 8 de junio de 2015

Screenshot and Control Exists


Screenshot:
we need to import to our class "Import org.openqa.selenium.TakesScreenshot;"
We can take screenshot when an error occurs to know where it happened the error, this help to identify the problem and fix it faster.

Control Exists
we can check if the control exist using  WebElement.IsDisplay by placing the above code snippet in a try catch block.

In this class we are using to test the control exist and the screenshot






 This class is for finding the elements our page.



lunes, 1 de junio de 2015

Using Explicit and Implicit waits

Web applications now want to appear as though they are desktop applications as more and more people move to hardware like tablets or netbooks which have very small hard drives. this is all done through AJAX to the page.

This means that when we are working with selenium WebDriver we need to have it synchronized with what is happening on the page. We don't want to use something like Thread.sleep() because that doesn't make our tests run as quickly as possible. We need to use one of the next two approaches: Implicit  or explicit waits.


Implicit wait:We can  tell selenium that we would like to wait for a certain amount of time before throwing an exception that it can not find the element on the page.We should note that implicit wait will be in place for the entire time the browser is open.This means that any search for elements on the page could take the time the implicit wait is set for.
 To make the code to wait an element to appear in DOM implicit wait timeout once set, is set for the lifetime of the WebDriver object instance.
driver.manage().timeouts().implicitlyWait(5,timeUnit.SECONDS)


Explicit wait:Unfortunately implicit waits do not fit situations and for some developers is not the right thing to do. Explicit waits is when know what we want to happen and the error needs to fit that situation. To wait for certain conditional to occur before moving forward ,this can be achieved with the combination to poll by every 500 milliseconds util it returns successfully.
(new WebDriverWait(driver,10))until(ExpectedConditions.elementToBeClickable(element));

For the other methods we need to import the followings
Import.org.openqa.selenium.support.ui.WebDriverWait;
import.org.openqa.seleniu.support.ui.ExpectedConditions;

Create a new class  in which we will have our explicit and explicit waits.






Create a new class where we will have our  page's locations

Create a new class for our test.