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

Sunday, February 4, 2018

Adding dependencies to maven for selenium

To add dependencies in maven project for running selenium tests, we can add dependencies under dependencies section as shown below:





1. Go to https://mvnrepository.com

2. Add Below dependencies:

a. TestNG
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.13.1</version>
    <scope>test</scope>
</dependency>

b. Selenium-java
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.8.1</version>
</dependency>

c. Selenium chrome driver
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-chrome-driver</artifactId>
    <version>3.8.1</version>
</dependency>

3. All the other dependencies related to selenium can be found at:
http://www.mvnrepository.com/artifact/org.seleniumhq.selenium

4. The dependencies are associated with the tests and downloaded at m2 folder in users folder

Saturday, June 17, 2017

How to execute only failed TestNG tests in Eclipse

While running testNG suite, once we are done with one round of execution of tests in the test suite, It can be helpful to execute only the failed tests during the first round of execution to reduce test failures due to flakiness/synchronization issues.

Below steps shows how to achieve this in eclipse.

  • Run the required test suite through eclipse as shown below.
testng eclipse run
  • Once the execution is completed and there are failures in the execution, we may require to rerun the tests.
  • Refresh the project. folder test-output should be displayed in the project explorer.
  • File testng-failed.xml should be available in the folder.
  • Run the test suite in similar manner as shown above, i.e Run As TestNG suite.

Tuesday, December 6, 2016

Tutorials to understand TestNG for Selenium Webdriver

Installing TestNG in eclipse - This post explains how to install TestNG in eclipse.


MindMap for TestNG - Understanding the concept in TestNG using MindMap.


Annotations in TestNG - Different annotations used in TestNG 


Assertion and Soft Assertions in Selenium Webdriver using TestNG - What are different assertions and soft assertions in selenium webdriver. What is the difference between assertions and soft assertion. How to use assertion in the test.


Maven POM.xml: Dependencies for Selenium, TestNG and junit - How to add TestNG dependenies in maven project.


Using dependencies and priority in TestNG tests: Ordering tests execution - how to define dependencies between tests and priotising order of tests execution in TestNG.


Creating TestNG.xml to run Selenium test suites - How to create testNG xml file for executing testsuite in TestNG. This article explains, how to create xml file for execution in  different scenarios.


How to run multiple test suites in TestNG? - Suppose we have individual suite for different module. This article explains how to create testNG xml file to run multiple test suites.


Maven POM.xml: Dependencies for Selenium, TestNG and junit - How to add dependencies for TestNG in Maven POM.xml

Friday, November 11, 2016

Exporting selenium IDE tests to WebDriver

Script created in Selenium IDE can be exported to WebDriver/Remote Control in the languages java/c#/python/ruby that can be run using junit(java), RSpec(ruby), nUnit and testNG. 


To export test in TestNG with selenium webdriver, follow below steps:

  • Record the required operations on the application.
  • Go to File>Export Test Case as Java / TestNG / WebDriver (can export test script in different formats)
Exporting test in Selenium IDE

  • Export the test.
  • A java file will be created with content as shown below


package com.example.tests;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.testng.annotations.*;
import static org.testng.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class TESTINGAA {
  private WebDriver driver;
  private String baseUrl;
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  @BeforeClass(alwaysRun = true)
  public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "https://www.google.co.in/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void testINGAA() throws Exception {
    driver.get(baseUrl + "/?gfe_rd=cr&ei=_OMdWJKzEO3I8AeAqrBg&gws_rd=ssl");
    driver.findElement(By.id("lst-ib")).clear();
    driver.findElement(By.id("lst-ib")).sendKeys("TESTING AUTOMATION CONEPTS");
    assertTrue(isElementPresent(By.linkText("TESTING AUTOMATION CONCEPTS")));
    driver.findElement(By.linkText("TESTING AUTOMATION CONCEPTS")).click();
    driver.findElement(By.linkText("Test automation - Wikipedia")).click();
    assertTrue(isElementPresent(By.linkText("Test automation - Wikipedia")));
  }

  @AfterClass(alwaysRun = true)
  public void tearDown() throws Exception {
    driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);
    }
  }

  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private boolean isAlertPresent() {
    try {
      driver.switchTo().alert();
      return true;
    } catch (NoAlertPresentException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
}

Saturday, November 5, 2016

Using dependencies and priority in TestNG tests: Ordering tests execution

Suppose there are three tests in a test class which needs to be executed, we can define the tests in the class as shown below:


public class TestOrder {
@Test
public void TestA() {
System.out.println("test a");
}

@Test
public void TestB() {
System.out.println("test b");
}

@Test
public void TestC() {
System.out.println("test c");
}
}


When we execute the test class, all the three tests are executed, but order of the test is not sure. We can prioritize the test using priority.


public class TestOrder {
@Test(priority=1)
public void TestA() {
System.out.println("test a");
}

@Test(priority=2)
public void TestB() {
System.out.println("test b");
}

@Test(priority=3)
public void TestC() {
System.out.println("test c");
}
}


Since there can be many tests in a test suite, and it can be difficult to prioritize tests based on numbering tests. In such cases,we can prioritize the tests based on dependsongroup or dependsonmethod as shown below.  Using groups, we can group the tests based on functionality and scope of the tests




public class TestOrder {
@Test(groups = { "smoke" })
public void TestA() {
System.out.println("test a");
}

@Test(dependsOnMethods = { "TestD" }, groups = { "regression" })
public void TestB() {
System.out.println("test b");
}

@Test(dependsOnGroups = { "smoke" })
public void TestC() {
System.out.println("test c");
}

@Test
public void TestD() {
System.out.println("test d");
}
}

How to run multiple test suites in TestNG?

Suppose we have individual test suite for different features/modules in TestNG, we can run multiple testNG test from a master suite xml file by calling the individual tests as shown below:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="MainTestSuite">
    <suite-files>
        <suite-file path="suiteFeatureA.xml" />  
        <suite-file path="suiteFeatureB.xml" /> 
        <suite-file path="suiteFeatureC.xml" />  
        <suite-file path="suiteFeatureD.xml" />      
    </suite-files>
</suite>


This way, we can run multiple test suites from a master suite file in TestNG.

Tuesday, November 1, 2016

Creating TestNG.xml to run Selenium test suites.

TestNG is a testing framework used to design automated test suites for functional/unit tests in Java. Below are few reasons which makes TestNG, a strong framework for automation. 

  • TestNG provides annotations to distinguish tests from methods to be called before the test and after the tests.
  • TestNG provides assertions to validate test conditions.
  • TestNG provides data Provider annotation to create data driven automated tests.
  • TestNG test information is added in an xml file. We can easily create a suite of testNG tests in a master suite xml file and run the tests from eclipse or from Jenkins using ant files. 
  • XML file denotes a test or suite of tests to be executed.


Pre-condition: Below article are useful to setup and create first TestNG test in selenium.



Focus of this article:

  • Creating TestNG xml file to run test suite.
  • How to run TestNG xml file in eclipse.

Let us take an example, suppose there are two classes in a Java Project.  we can create a test suite using testng xml file and run the test suite from eclipse as shown in the below image.


TestNG parallel test

Creating TestNG.xml file for different scenarios:

A. Run all the tests in both classes NewTest and NewTest1. This will execute all the methods with beforetest annotation in both the classes, followed by methods with test annotation, and then methods with afterTest annotation.

 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">  
 <suite name="Suite1">  
  <test name="Test">  
   <classes>  
    <class name="testNG.NewTest"/>  
       <class name="testNG.NewTest1"/>  
   </classes>  
  </test>  
 </suite>  


B. Run test in class NewTest and then the methods in NewTest1 class:

 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">  
 <suite name="Suite1">  
  <test name="Test1">  
   <classes>  
    <class name="testNG.NewTest"/>  
         </classes>  
  </test>  
  <test name="Test2">  
   <classes>  
    <class name="testNG.NewTest1"/>  
   </classes>  
  </test>  
 </suite>  

C. Executing all tests in a package with name as TestNG(Note: TestNG is the name of package in our example, it can be any name other than TestNG also)

 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">  
 <suite name="Suite1">  
 <test name="Test1">  
   <packages>  
    <package name="testNG" />  
   </packages>  
  </test>  
 </suite>  

D. Executing all tests based on the group Name including and excluding specific tests

 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">  
 <suite name="Suite1">  
 <test name="Test1">  
  <groups>  
   <run>  
    <include name="testemail"/>  
    <exclude name="testnoemail"/>  
   </run>  
  </groups>  
   <classes>  
    <class name="testNG.NewTest"/>  
    <class name="testNG.NewTest1"/>  
   </classes>  
 </test>  
 </suite>  

E. Executing specific methods by including methods in the class:

 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">  
 <suite name="Suite1">  
 <test name="Test1">  
 <classes>  
   <class name="testNG.NewTest">  
    <methods>  
     <include name="emailval" />  
    </methods>  
   </class>  
   <class name="testNG.NewTest1"/>  
 </classes>  
 </test>  
 </suite>  

Saturday, July 30, 2016

Quick Codes- Validating element existence in Selenium Wedriver


In this article, one of the way to validate existence of multiple objects of same object type based on element text in the application is explained using Selenium Webdriver with java. Please share other better way to verify element existence using Selenium Webdriver.


public boolean IsElementExists(String ObjectType, String strElemText)
{
 boolean boolElemExists = false;
//In case of multiple objects which needs to be validated, Provide multiple objects
// text seperated by  | in strElemText. e.g: strElemText as "mail|inbox"
// Create an array by splitting array based on delimiter "|"
 String[] elemTextArray = strElemText.split("|");
// Loop through array elements
 for ( int j = 0;j<elemTextArray.length;j++)
 {
  List<WebElement> lstElement = null;
  try
// Better approach will be to pass values from enum for Object type
   switch (ObjectType)
   {
   // in case of link object storing all the elements of type link in lstElement
   case "Link":
     lstElement = driver.findElements(By.tagName("a")); 
    break;
    // in case of link object storing all the elements of type link in label
   case "Label":
    lstElement = driver.findElements(By.tagName("label")); 
    break;
   }
   for (int i=0;i<lstElement.size();i++)
   {
    if (lstElement.get(i).getText().contentEquals(strElemText.trim()))
      {
       boolElemExists = true;
       i= lstElement.size()-1;
      }
   }
   if (boolElemExists = false)
   {
    return false;
   }
   }
  catch(Exception e)
  {
   return false;
  }
 }
 return boolElemExists;
}

Saturday, July 23, 2016

Installing TestNG in eclipse


TestNG is a testing framework used to design automated functional/unit tests in Java. Eclipse is one of the most popular IDE for developing tests in Java. In this article, we will understand how to set up/install testNG with Eclipse.


A. Launch Eclipse IDE and Navigate to Help>Install new Software. A pop-up page with title as Install will be displayed.


B. Set http://beust.com/eclipse in Work With Input box and click on Add Button.


C. Checkbox for TestNG is displayed in the Page. Select checkbox and click on Next.


D. Agree to the license agreement.


E. Software download will begin and TestNG will be installed.


F. Once TestNG installation is done, a pop-up window will be displayed asking to restart eclipse for changes to take place. Click on Yes to complete TestNG installation



Installing testNG in eclipse


Another way to install TestNG in eclipse is to navigate to Help>Eclipse Marketplace and search for TestNG. Install TestNG from the search results.


Installing testNG in eclipse marketplace

Wednesday, May 18, 2016

MindMap for TestNG

In this post, basics of TestNG are described with mindmap. TestNG is an open source automation framework inspired from jUnit but having additional features making it more robust compared to jUnit. It is also used extensively with selenium automation. TestNG.xml is not explained in detail in this post, as it is an important concept and will be covered in a separate post. Hope you like this mind map.


MindMap for TestNG


Sunday, February 21, 2016

Maven POM.xml: Dependencies for Selenium, TestNG and junit

While working with Selenium, TestNG and junit with maven, we need to add in the Project Object Model (POM.xml), dependencies for Selenium, TestNG and junit. In this article, we have explained how to add the required dependency in the dependencies section in pom.xmlfile.

Add the dependency in the dependencies section for the required artifacts. Also please update the version as per latest version of the artifact.


Selenium Dependency:

<dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.52.0</version>
</dependency>

junit Dependency:

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.12</version>
</dependency>

TestNG Dependency:

<dependency>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  <version>6.8</version>
  <scope>test</scope>
</dependency>

Saturday, February 6, 2016

Assertion and Soft Assertions in Selenium Webdriver using TestNG

We create automated regression test to validate the application is working correctly. We need to add checkpoints/validation at different steps in the workflow to validate the application is correctly behaving or not.

For e.g.: Let us consider login into an e-mail website. We need to go to application URL and assert/validate username and password input box and Login button are displayed correctly. Then next step can be to assert login is successful for a positive test with valid username and password or assert proper error message is displayed for a negative test with invalid username and password.

Testing framework for automation like TestNG and jUnit for Java or nUnit for C# provides feature of assertion to assert/validate the application is working properly and scores 10/10 in the test results.

In this article, we will discuss how to add assertions using TestNG framework for selenium tests.


Problem statements of this article:

  • What is meant by Assertions in TestNG?
  • What are different types of assertion in TestNG?
  • What is the difference between assert and softassertions?
  • Code explaining how to implement assert and softassert in TestNG?


What is meant by Assertions?

With TestNG framework, We can add assertions within tests to validate whether the expected condition is true or false. In most of the assertions, the expected condition is compared with the actual results and test fails in case an assertion fails. The failure is reported in test results.

For example in a selenium test for login functionality, When we provide valid username and password and login into application, we expect the title to be displayed correctly in the page. So in this scenario, we should add an assertion for comparing the expected title of the page with the actual title displayed in the page. In case, the two values does not match, the assertion fails and test execution fails 

What are different types of assertions?

Some of the assertions that can be implemented in TestNG are as follows:
Types of assertion
Description
Assert.AssertEquals(Expected condition, Actual condition)
This compares the expected condition with actual condition and fails the test in case the assertion fails.Different object types can be compared using Assert.AssertEquals
Assert.AssertEquals(Expected condition, Actual condition, Message)
This compares the expected condition with actual condition and fails the test in case the assertion fails displaying the message as defined while calling the assertion.
Assert.assertFalse(Boolean condition)
Asserts that a condition is false
Assert.assertFalse(Boolean condition, String Message )
Asserts that a condition is false. If it isn't, an Assertion Error, with the given message, is thrown.
Assert.assertTrue(Boolean condition, String Message )
Asserts that a condition is true. Assertion fails if condition is not true and string message is displayed.
Assert.AssertNull(object)
Validates if a assertion is null.
Assert.Fail(String strMessage)
Fails an assertion without validating any conditions. This is very useful for outcome of conditional statements. e.g: if title is incorrect, fail the test.



Sample Code to explain how to create assertion in TestNG

package testNG;  
 import java.io.File;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.ie.InternetExplorerDriver;  
 import org.testng.Assert;  
 import org.testng.annotations.BeforeTest;  
 import org.testng.annotations.Test;  
 public class assertionVal {  
      WebDriver driver;  
      @BeforeTest  
      public void setup()   
      {  
           File file = new File("D:\\selenium\\IEDriverServer.exe");  
           System.setProperty("webdriver.ie.driver", file.getAbsolutePath());       
           driver = new InternetExplorerDriver();  
      }  
      @Test  
      public void assertVal()   
      {  
           driver.navigate().to("http://qaautomationqtp.blogspot.com");  
           Assert.assertEquals("Learning Automation Concepts in QTP", driver.getTitle());  
           //Assert to compare two values and reporting a message in case validation fails  
           Assert.assertEquals("aaa", "aaa", "this is the first test and value does not match");  
           //Assert to validate a boolean condition as false   
           Assert.assertFalse(driver.getTitle().contentEquals("Learning Automation Concepts in QTP"));  
           //Assert to validate a boolean condition as true   
           Assert.assertFalse(driver.getTitle().contentEquals("Learning Automation Concepts in QTP"));  
        //Assertion to validate an object is not null.  
        Assert.assertNotNull("driver");  
      }  
 }  


What is the difference between assert and soft assertions (Verify statements)?

The issue with assertion is, test execution stops in case an assertion fails. So if we do not want to stop a test on failure and continue with test execution, using assert statement will not work, 
In such sccnarios, we will use soft assertions, using  import org.testng.asserts.SoftAssert; class.
In this case, verification step will fail but test execution will continue. 

Sample Code to explain how to implement soft assertion in TestNG



 package testNG;  
 import org.testng.Assert;  
 import org.testng.annotations.Test;  
 //soft assertion will use the below softAssert class  
 import org.testng.asserts.SoftAssert;  
 public class assertionVal {  
      // Create an soft assertion object softas   
      SoftAssert softas = new SoftAssert();  
      @Test  
      public void assertVal()   
      {  
           //Create an assertion similar to hard assertion as shown below   
           softas.assertEquals("aaaa", "bb");  
           softas.assertEquals("aa","aaccc","asserts exists");  
           // this is an example of hard assertion, test will stop execution   
           //at this step in case error is encountered.  
           Assert.assertEquals("aa", "aa");  
           System.out.println("nitin");  
           //This step is very important as without this step,  
           //no soft assertion failure will be reported in test result.  
           softas.assertAll();  
      }  
 }