Showing posts with label TestNG with WebDriver Introduction. Show all posts
Showing posts with label TestNG with WebDriver Introduction. Show all posts

Wednesday, October 5, 2016

TestNG Prioritizing/Sequencing and Skipping a Test Case

In TestNG "Priority" is used to schedule the test cases. When there are multiple test cases, we want to execute test cases in order. Like First we need to execute a test case "Registration" before login.
In order to achive, we use need to add annotation as @Test(priority=??).

If you don't mention the priority, it will take all the test cases as "priority=0" and execute.
If we define priority as "priority=", these test cases will get executed only when all the test cases which don't have any priority as the default priority will be set to "priority=0"

Let's create a new TestNG class 'MultipleTestCases' without setting Priority and execute it without setting.
Note - By default, methods annotated by @Test are executed alphabetically. Take a look over the next topic to see how to prioritize @Test.

package automationFramework;

import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;

public class MultipleTestCases {
    public WebDriver sDriver;
  @Test
  public void testOne() {
      System.out.println("This is test One");
  }
  @Test
  public void testTwo() {
      System.out.println("This is test Two");
  }
  @Test
  public void testThree(){
      System.out.println("This is test Three");
  }
  @Test
  public void testFour(){
      System.out.println("This is test four");
  }
}


Update TestNG.xml and execute the test.

<suite name = "Test - Suite">
    <test name="website QA">
        <classes>
            <class name="automationFramework.MultipleTestCases" />
        </classes>
    </test>
</suite>


The output will be like this -


So like I mentioned above, Without setting priority Test cases are executed in alphabetical order.
Now let's set Priority in the above example and run the test again.

 package automationFramework;

import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;

public class MultipleTestCases {
    public WebDriver sDriver;
  
  @Test(priority = 0)
  public void testOne() {
      System.out.println("This is test One");
  }
  @Test(priority = 1)
  public void testTwo() {
      System.out.println("This is test Two");
  }
  @Test(priority=2)
  public void testThree(){
      System.out.println("This is test Three");
  }
  @Test(priority = 3)
  public void testFour(){
      System.out.println("This is test four");
  }
}





Skipping Test Case

Sometimes, it happens that our code is not ready and the test case written to test that method/code fails. In such cases, annotation @Test(enabled = false) helps to disable this test case.
If a test method is annotated with @Test(enabled = false), then the test case that is not ready to test is bypassed.
Now, let's see @Test(enabled = false) in action.

To use two or more parameters in a single annotation, separate them with a comma:
@Test(priority = 3, enabled = false)

Let's continue to use 'MutipleTestCases' class and SKIP testcase 1 and testcase 2 and see the output.

package automationFramework;

import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;

public class MultipleTestCases {
    public WebDriver sDriver;
   
  @Test(priority = 0)
  public void testOne() {
      System.out.println("This is test One");
  }
  @Test(priority = 1, enabled = false)
  public void testTwo() {
      System.out.println("This is test Two");
  }
  @Test(priority=2, enabled = false)
  public void testThree(){
      System.out.println("This is test Three");
  }
  @Test(priority = 3)
  public void testFour(){
      System.out.println("This is test four");
  }
}


 And the output is -


Tuesday, October 4, 2016

TestNG with WebDriver Introduction and setup

TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionalities that make it more powerful and easier to use

Features of TestNG

  • Support for annotations
  • Support for parameterization
  • Advance execution methodology that do not require test suites to be created
  • Support for Data Driven Testing using Dataproviders
  • Enables user to set execution priorities for the test methods
  • Supports threat safe environment when executing multiple threads
  • Readily supports integration with various tools and plug-ins like build tools (Ant, Maven etc.), Integrated Development Environment (Eclipse).
  • Facilitates user with effective means of Report Generation using ReportNG.

TestNG is designed to cover all categories of tests:  unit, functional, end-to-end, integration, etc.

TestNG Plugin Installation in Eclipse
  1. Launch eclipse IDE -> Click on the Help option within the menu -> Select “Eclipse Marketplace” option within the dropdown.


  2. Enter the keyword “TestNG” in the search textbox and click on “Go” button as shown below. 
  3. Click on the “Go” button and click on the Install button to install TestNG.
  4. Click on “Confirm” button. Next, the application would prompt you to accept the license and then click on the “Finish” button.
  5. The installation is initiated now and the progress can be seen as following:
 Restart Eclipse. Next, Verify that TestNG appears in eclipse Preference. Click on Eclipse menu tab -> Preferences. TestNG should be visible in the Preference.




Annotations in TestNG

@BeforeSuite: The annotated method will be run before all tests in this suite have run.
@AfterSuite: The annotated method will be run after all tests in this suite have run.
@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the tag is run.
@AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the tag have run.
@BeforeGroups: The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked.
@AfterGroups: The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked.
@BeforeClass: The annotated method will be run before the first test method in the current class is invoked.
@AfterClass: The annotated method will be run after all the test methods in the current class have been run.
@BeforeMethod: The annotated method will be run before each test method.
@AfterMethod: The annotated method will be run after each test method.
@Test: The annotated method is a part of a test case.

Now, Let's create first TestNG class. 


Right click on src and click on TestNG -> Create TestNG class


Fill out Package name and Class name along with select couple of annotations. In this example i am using @BeforeMethod and @AfterMethod and rewriting out FirstTestCase.





Let’s take an example of FirstTestCase and divide the test case in to three parts .
@BeforeMethod : Launch Safari and direct it to the Base URL
@Test : Find Page title and print console message
@AfterMethod : Close Safari browser

package automationFramework;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.safari.SafariDriver;
import org.testng.annotations.AfterMethod;

public class TestNG {
    public WebDriver sDriver;
   
  @Test
  public void main() {
      String pageTitle = sDriver.getTitle().toString();
      System.out.println(pageTitle);
  }
  @BeforeMethod
  public void beforeMethod() {

      // Create a new instance of the Safari driver
      sDriver = new SafariDriver();
     
      //Put a Implicit wait, this means that any search for elements on the page could take the time the implicit wait is set for before throwing exception
      sDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
     
      String webSiteURL = "https://www.amazon.com/";
      //Launch the website
      sDriver.get(webSiteURL);
  }

  @AfterMethod
  public void afterMethod() {
      // Close the driver
      sDriver.close();
  }
}


Run the test by right click on the test case script and select Run As > TestNG Test.

TestNG produces reports in various formats.


  



Open project folder -> test-output and view index.html, emailable-report.html. Also view Default test.html inside Default suite folder.






Setup Selenium Java Project in Eclipse

Install Eclipse
If you don’t already have Eclipse installed on your system, then go ahead and install latest version of Eclipse from



Click on the Download button and complete the Installation.

After the installation is complete, Open Eclipse. 

Next, Create a new project in Eclipse by going to File -> New -> Project


 Select Java Project. Next, Enter the name of the project


Click Finish

Now that the Java Project is created, Let's import Selenium files. Right click on the project folder from the left navigation of Eclipse and select Properties.

 

Then select 'Java Build Path' and 'Libraries' tab. This is where all the selenium and lib files are added.

Next, Make sure all the Selenium files are downloaded and ready to upload. If not then download latest Selenium files from the selenium website (http://www.seleniumhq.org/download/)


Go Back to Eclipse and click 'Add External Jars' and navigate to Selenium files folder. Add all the Jar files.
Note - Add all the jar files inside Selenium folder as well as libs folder.



Now that all the Selenium files are added, Eclipse left nav would like this -



Next, Lets write some code to test and make sure selenium works fine. Continue to the next blog here 


Friday, September 30, 2016

Setup Java JDK on Mac OS for Selenium webdriver automation


Install Java on Mac OS Sierra
If you don’t have Java already install then first step is to install Java. Go to the Java download link and install latest Java SDK version.



Next select the JDK file that is compatible with your operating system. Since I am using mac OS, I will download macOSX dmg file.


Follow the steps and complete the installation.