Saturday 23 June 2018

TestNG Priority and Sequencing

Hi everyone, in this post we will see how to priorities Test cases in TestNG and how to sequence it as per our requirement. If you create a test class and add multiple test cases and run it then each time test case execution sequence is randomly selected .
With TestNG annotations @Test and parameter "priority" we can define sequence and priority for executing test cases. Let us consider the below example and if you run the test class given below multiple times you can see that each time execution sequence/flow is different hence to define a fixed execution flow we user TestNG annotation priority.

Video Tutorial -



package test;
import org.testng.annotations.Test;
 
public class TesNGSequencing {
 
 
 
  @Test
 
  public void One() {
 
      System.out.println("Test Case number One");
 
  }
 
  @Test
 
  public void Two() {
 
   System.out.println("Test Case number Two");
 
  }
 
  @Test
 
  public void Three() {
 
   System.out.println("Test Case number Three");
 
  }
 
  @Test
 
  public void Four() {
 
   System.out.println("Test Case number Four");
 
  }
 
}

Given below is the example for using priority annotations. When you run the below test class then each time execution flow is constant and starts with test case having 0 priority.


package test;
import org.testng.annotations.Test;
 
public class TesNGSequencing {
 
 
 
  @Test(priority=0)
 
  public void One() {
 
      System.out.println("Test Case number One");
 
  }
 
  @Test(priority=1)
 
  public void Two() {
 
   System.out.println("Test Case number Two");
 
  }
 
  @Test(priority=2)
 
  public void Three() {
 
   System.out.println("Test Case number Three");
 
  }
 
  @Test(priority=3)
 
  public void Four() {
 
   System.out.println("Test Case number Four");
 
  }
 
}

Skipping a Test -


If you come across a situation where there is requirement to skip some tests then in this case you can do it using @Test annotations and enabled parameter.




package test;
import org.testng.annotations.Test;
 
public class TesNGSequencing {
 
 
 
  @Test(priority=0, enabled=false)
 
  public void One() {
 
      System.out.println("Test Case number One");
 
  }
 
  @Test(priority=1)
 
  public void Two() {
 
   System.out.println("Test Case number Two");
 
  }
 
  @Test(priority=2)
 
  public void Three() {
 
   System.out.println("Test Case number Three");
 
  }
 
  @Test(priority=3)
 
  public void Four() {
 
   System.out.println("Test Case number Four");
 
  }
 
}

Friday 22 June 2018

TestNG Assertions and Difference Between Hard Assert and Soft Assert

Hi everyone, In this post we are going to cover very important feature of TestNG which is TestNG Assertions. Why this is important? because no test case is considered complete unless we have assert actual values with expected values.Assertions helps us to verify weather the test case is passed or failed.
There are three types of Assertions in TestNG Hard Assert, Soft Assert . Let us learn all these in detail.

Video Tutorial


What is Assertion and How it Works ?


Consider below example where we are launching google.com and asserting that title should be equals to Google. this is the simplest example of assertion and this is how test case with assertion looks like.

@Test
 public void testCaseVerifyHomePage() {
  System.setProperty("webdriver.chrome.driver", "C:\\Softwares\\chromedriver_win32\\chromedriver.exe");
  WebDriver driver=new ChromeDriver();
  driver.navigate().to("http://google.com");
  Assert.assertEquals("Gooooogle", driver.getTitle());
 }

Now if you change your code to assert the title with Expected value as "Doodle" then it will throw Exception that Expected - Doodle but Found - Google.

@Test
 public void testCaseVerifyHomePage() {
  System.setProperty("webdriver.chrome.driver", "C:\\Softwares\\chromedriver_win32\\chromedriver.exe");
  WebDriver driver=new ChromeDriver();
  driver.navigate().to("http://google.com");
  Assert.assertEquals("Doodle", driver.getTitle());
 }
In this way assertions helps us to make sure test case is passed or failed and we can catch the bugs with these types of assertions.
Apart from assertEquals there are various types of Assertions as per given below.

Commonly Used Assertions -


assertEqual(String actual,String expected) :- It takes two string arguments and checks whether both are equal, if not it will fail the test.

assertEqual(String actual,String expected, String message) :- It takes three string arguments and checks whether both are equal, if not it will fail the test and throws the message which we provide.

assertEquals(boolean actual,boolean expected) :- It takes two boolean arguments and checks whether both are equal, if not it will fail the test.

assertEquals(java.util.Collection actual, java.util.Collection expected, java.lang.String message) :- Takes two collection objects and verifies both collections contain the same elements and with the same order. if not it will fail the test with the given message.

assertTrue(condition) :- It takes one boolean arguments and checks that a condition is true, If it isn't, an AssertionError is thrown.

assertTrue(condition, message) :- It takes one boolean argument and String message. It Asserts that a condition is true. If it isn't, an AssertionError, with the given message, is thrown.

assertFalse(condition) :- It takes one boolean arguments and checks that a condition is false, If it isn't, an AssertionError is thrown.

assertFalse(condition, message) :- It takes one boolean argument and String message. It Asserts that a condition is false. If it isn't, an AssertionError, with the given message, is thrown.

Hard Assert -  


Hard Assert is a type of TestNG assetion which throws Exception Immediately if the assert statement fails and move on to the next testcase and if there is any code in the current test case after assert statement it will not execute that statement. The google example which we saw earlier is also an example of hard assert. you can try adding code  after assert statement it will not get executed.
In the below example after first assert statement execution the test case execution stops and further statements in the test case are not executed.


@Test
 public void test1(){
   
   Assert.assertTrue(5<1);
   System.out.println(Assertion Failed);
   Assert.assertFalse(1<5);
   System.out.println(Assertion Failed);
   Assert.assertEquals(Passed, Failed);
   System.out.println(Assertion Failed);
 }

Soft Assert - 

To deal with the disadvantage of Hard Assertions, customized error handler provided by TestNG is called Soft Assertion. Soft Assertions are the type of assertions that do not throw an exception when an assertion fails and would continue with the next step after assert statement. This is usually used when our test requires multiple assertions to be executed and the user want all of the assertions/codes to be executed before failing/skipping the tests.


@Test
 public void test1(){
   SoftAssert sa= new SoftAssert();
   sa.assertTrue(5<1);
   System.out.println(Assertion Failed);
   sa.assertFalse(1<5);
   System.out.println(Assertion Failed);
   sa.assertEquals(Passed, “Failed);
   System.out.println(Assertion Failed); 
                        sa.assertAll()
 }


Thursday 14 June 2018

TestNG Grouping and Depends On

In our previous post we have learnt about TestNG annotations . In this post we will see how we can group test cases and specify dependent test cases using TestNG annotations.

Groups‘ is one annotation of TestNG which can be used in the execution of multiple tests as a group. Let’s say you have hundred tests of class Devices and in it ten methods of laptops, ten method of Notebooks and so on. You probably like to run all the Notebooks tests together in a batch. And you want all to be in a single test suite. With the help of grouping you can efficiently address this situation.

How to do it…
1) Create two methods for Laptops, two methods for Notebooks and one method in conjunction with Laptops and Notebooks.
2) Group them separately with using  (groups = { ” Group Name” })

Video Tutorial -

 

import org.testng.annotations.Test;
 
public class TestGrouping {
 
  @Test (groups = { "Laptops" })
 
  public void laptop1() {
 
   System.out.println("Batch Laptops- Test Laptops1");
 
  }
 
  @Test (groups = { "Laptops" })
 
  public void laptop2() {
 
   System.out.println("Batch Laptops- Test Laptops 2");
 
  }
 
  @Test (groups = { "Notebooks" })
 
  public void notebook1() {
 
   System.out.println("Batch Notebooks- Test Notebooks 1");
 
  }
 
  @Test (groups = { "Notebooks" })
 
  public void notebook2() {
 
   System.out.println("Batch Notebooks- Test Notebooks2");
 
  }
 
  @Test (groups = { "Laptops", "Touch Screen Laptops" })
 
  public void youchScreenlaptop() {
 
   System.out.println("Batch Touch Screen Laptops- Test Touch Screen Laptops1");
 
  }
 
}

Add below code into TestNG.xml-



<suite name="Suite">
 
    <test name="Practice Grouping">
 
        <groups>
 
     <run>
 
  <include name="Laptops" />
 
     </run>
 
 </groups>
 
 <classes>
 
     <class name="PackageName.TestGrouping" />
 
 </classes>
 
    </test>
 
</suite>

Run The TestNG,xml -> right click -> Run as -> TestNG Suit.

Dependenct Test -

you may need to invoke methods in a Test case in a particular order or you want to share some data and state between methods. This kind of dependency is supported by TestNG .
TestNG allows you to specify dependencies with two options given below.
  • Using attributes dependsOnMethods in @Test annotations
  • Using attributes dependsOnGroups in @Test annotations.

import org.testng.annotations.Test;
 
public class Dependent {
 
  @Test (dependsOnMethods = { "OpenBrowser" })
 
  public void SignIn() {
 
   System.out.println("This will execute SignIn");
 
  }
 
  @Test
 
  public void OpenBrowser() {
 
   System.out.println("This will execute Open Browser");
 
  }
 
  @Test (dependsOnMethods = { "SignIn" })
 
  public void SignOut() {
 
   System.out.println("This will execute Log Out");
 
  }

In case of any feedback and questions post in the comment section below.

What is TestNG Annotations

Hi everyone in this post we will learn basics of TestNG annotations. In our previous tutorial we have seen introduction and setup of TextNG. Now lets learn what is TestNG annotations and how to use TestNG annotations to define proper test suit execution flow.

TestNG Annotation

Lets understand the simple example given below which will help you understand how annotations helps to define the flow.In TestNG annotations @Test is the smallest annotation here. @Method will be executed first, before and after the execution of @Test. The same way @Class will be executed first, before and after the execution of @Method and so on.

Video Tutorial -

 

Do try this out and post your feedback and questions in the comment section below.


import org.testng.annotations.AfterClass;
 
import org.testng.annotations.AfterMethod;
 
import org.testng.annotations.AfterSuite;
 
import org.testng.annotations.AfterTest;
 
import org.testng.annotations.BeforeClass;
 
import org.testng.annotations.BeforeMethod;
 
import org.testng.annotations.BeforeSuite;
 
import org.testng.annotations.BeforeTest;
 
import org.testng.annotations.Test;
 
public class Sequencing { 
 
               @BeforeSuite
 
  public void beforeSuite() {
 
   System.out.println("This will execute before the Test Suite");
 
  }
  
 
                @BeforeClass
 
  public void beforeClass() {
 
   System.out.println("This will execute before the Class");
 
  } 
               @BeforeTest
 
  public void beforeTest() {
 
   System.out.println("This will execute before the Test");
 
  }
 
                @BeforeMethod
 
  public void beforeMethod() {
 
   System.out.println("This will execute before every Method");
 
  } @Test
 
  public void testCase1() {
 
   System.out.println("This is the Test Case 1");
 
  }
 
  @Test
 
  public void testCase2() {
 
   System.out.println("This is the Test Case 2");
 
  }
 
  
 
  @AfterMethod
 
  public void afterMethod() {
 
   System.out.println("This will execute after every Method");
 
  }
 
  
 
  @AfterClass
 
  public void afterClass() {
 
   System.out.println("This will execute after the Class");
 
  }
 
  
 
  @AfterTest
 
  public void afterTest() {
 
   System.out.println("This will execute after the Test");
 
  }
 
  
  @AfterSuite
 
  public void afterSuite() {
 
   System.out.println("This will execute after the Test Suite");
 
  }
 
 }

Saturday 9 June 2018

Introduction to TestNG and TestNG Setup in Eclipse

TestNG is Next Generation Testing framework developed in the lines of JUnit and NUnit, but it has some new functionalities that make it more powerful and easier to use. TestNG is designed to cover all kinds of tests: unit, functional, end-to-end, integration, etc., and it requires JDK 5 or higher.


Pros of using TestNG -
There are number of benefits but from Selenium/Appium perspective, major advantages of TestNG are :
  1. It gives the ability to produce HTML Reports of Test Suit execution.
  2. Annotations makes testers life easy and defines flow of test execution.
  3. Test cases can be Grouped & Prioritized more easily using TestNG annotations.
  4. Parallel testing is possible for example running same test on multiple browsers at the same time.
  5. Generates Logs which are useful for debugging purposes.
  6. Data Parameterization is possible.

Video Tutorial -


 How to Configure TestNG in Eclipse -

1. Open Eclipse.
2. Navigate to Eclipse Marketplace.

3. Search TestNG.


4. Click on Install.
5. Done.


Monday 4 June 2018

Easiest Way to Start Appium Server with Java Code

When we are building an automation framework it is very important to perform end to end automation including starting and stopping appium server. keeping that in mind in this post I will be sharing simplest and quickest way to install appium server and start & stop appium server using simple java code.
This is very simple and easy way to start appium server programmatically using java code. In our previous tutorial we have seen how to start appium server using java code with this   AppiumDriverLocalService class. But in this tutorial we will see very simple and quick way of installing appium server and starting it using simple java code.


Step 1 > Install Node Js from  HERE
 
Step 2 > Open Node Js Command Prompt as shown below.

Step 3 > Execute command npm install -g appium
 
Step 4 > Verify Appium is Installed Successfully by executing command  appium -v
 
Step 5 > Start appium server using command appium -a 127.0.0.1 -p 4723
 
Step 6 > Do CTRL + C to stop ther server

Below is the Java Code to Start and Stop Appium Server Programaticall. In the code we
are executing the command using java to start and stop the server.
Do try this out and post your feedback, Suggestion and questions in comment section 
below.



import java.io.IOException;

/**
 * Appium Manager - this class contains method to start and stops appium server  
 */
public class AppiumManager {

 public void startServer() {
  Runtime runtime = Runtime.getRuntime();
  try {
   runtime.exec("cmd.exe /c start cmd.exe /k \"appium -a 127.0.0.1 -p 4725 --session-override -dc \"{\"\"noReset\"\": \"\"false\"\"}\"\"");
   Thread.sleep(10000);
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 public void stopServer() {
  Runtime runtime = Runtime.getRuntime();
  try {
   runtime.exec("taskkill /F /IM node.exe");
   runtime.exec("taskkill /F /IM cmd.exe");
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}