Automation Using Selenium Webdriver

TestNG Advanced Concepts With Examples



Just a small introduction about all then we will discuss TestNG in details.
1-TestNG,Junit,Nunit are a separate testing framework which is freely available in the market.
2-TestNG,Junit mainly we can integrate into Java whereas Nunit works with C# only.
3-Junit comes by default with Eclipse but TestNG you have to install an Add-ons.
4-Junit has fewer annotations as compared to TestNG.
5-Junit automatically does not create HTML report so we have to use ant build tool, but TestNG has rich HTML report generations even for single test case it creates 3 HTML reports

we have so much difference please refer their official website you will get a clear picture that how it works.
Note- TestNG is successor of Junit so I will be focusing on TestNG
Official website for Junit is-    http://junit.org/
Official website for TestNg is- http://testng.org/
Official website for Nunit is-   http://www.nunit.org/

What is TestNG and Why we have to use in Selenium

Ans- TestNG is a testing framework which is designed to cover all categories of tests:  unit, functional, end-to-end, integration, etc…
We can combine TestNG with Selenium and we can write Test Cases in Eclipse using Java.

What are the benefits of using TestNG ?
Ans:
  1. a) TestNG allows us to execute of test cases based on group.
  2. b) In TestNG Annotations are easy to understand.
  3. c) Parallel execution of Selenium test cases is possible in TestNG.
  4. d) Three kinds of report generated
  5. e) Order of execution can be changed
  6. f) Failed test cases can be executed
  7. g) Without having main function we can execute the test method.
  8. h) An xml file can be generated to execute the entire test suite. In that xml file we can   rearrange our execution order and we can also skip the execution of particular test case.

      Most of the time we have faced this question in interviews that Can we execute only failed test cases in Selenium or can we identify only failed test cases in Selenium and re-run them.



I really love this feature of TestNG that you can run only failed test cases explicitly without any code. This can be easily done by running one simple testng-failed.xml.


                                    Execute Failed test cases using Selenium
                                           Real-time Example


Take an example that you have one test suite of 100 test cases and once you start execution of test suite there  are a number of chances that some test cases will fail.Consider 15 test cases are failing out of 100 now you need to check why these test cases are failing so that you can analyze and find out the reason why they have failed.

Note- Your script can fail due to so many reasons some of them are

1- Some locator has been changed in application because the application is getting new feature- so in this case you need to modify your script in other words you have to refine your script.

You can not avoid maintenance of test script you always have to maintain your scripts
2- Either functionality has been broken- in this case, you have to raise a defect and assign to the respective person.

Execute Failed test cases using Selenium
Steps

1-If your test cases are failing then once all test suite completed then you have to refresh your project . Right click on project > Click on refresh or Select project and press f5.

2-Check test-output folder, at last, you will get testng-failed.xml

3- Now simply run testng-failed.xml.


         

How to run testng-failed.xml
We don’t have to perform any other activity once you will  get testng-failed.xml double click on this and analyze which test case are failing and why . Then modify your script and run it.

To run above xml simple right click on xml then Select run as then TestNG Suite.


or more info visit TestNG official website
Thanks for visiting my blog. Keep visiting.
Please comment below if you finding any issue. Have a nice day ðŸ™‚

Advance Selenium Reporting with Screenshots Part 2


As we all know TestNG default reports are not attractive.I have new topic Advance reporting Selenium using extent report.Recently we got the new version of Extent report that we have used earlier and we have seen how we can use it.
If you have not checked the previous version of extent test then refer below article that will help you to understand what is Extent report.
You can check the detailed description about extent report from here- Extent Report Documentation
I created a detailed youtube video on this that will help you to Achieve the same.
 But before moving forward you should know some of the features that we are going to use in this video.
 Advance reporting Selenium with Screenshots

Download Extent report

1- Open http://extentreports.relevantcodes.com/ and Download Java version 2.40.2
2- Once download completes, unzip the file.
 3- After unzip you will get lib folder and 1 additional jar.
 4- You have to add all jars mentioned below and then run the below program.

 <span style="font-family: arial, helvetica, sans-serif; font-size: 14pt;">package SmokeTest;
import library.Utility;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;
public class VerifyTitle
{
ExtentReports report;
ExtentTest logger;
WebDriver driver;
@Test
public void verifyBlogTitle()
{
report=new ExtentReports("C:\\Report\\LearnAutomation.html");
logger=report.startTest("VerifyBlogTitle");
driver=new FirefoxDriver();
driver.manage().window().maximize();
logger.log(LogStatus.INFO, "Browser started ");
driver.get("http://www.learn-automation.com");
logger.log(LogStatus.INFO, "Application is up and running");
String title=driver.getTitle();
Assert.assertTrue(title.contains("Google"));
logger.log(LogStatus.PASS, "Title verified");
}
@AfterMethod
public void tearDown(ITestResult result)
{
if(result.getStatus()==ITestResult.FAILURE)
{
String screenshot_path=Utility.captureScreenshot(driver, result.getName());
String image= logger.addScreenCapture(screenshot_path);
logger.log(LogStatus.FAIL, "Title verification", image);
}
report.endTest(logger);
report.flush();
driver.get("C:\\Report\\LearnAutomation.html");
}
}</span>


How to disable Selenium Testcases using TestNG Feature




Welcome to Selenium Tutorial, Today we are going to discuss very silent feature of TestNG which allow us to enable or disable our test case based on our requirement.

Why to Disable Selenium Test cases?

Before moving forward you should have Eclipse TestNG setup ready if still not configured then please doSelenium Eclipse setup now.
Once our test suite size will increase day by day then chances are high that you don’t want to execute some test cases.
Ok so let’s consider a scenario that you have written 10 testcases but now you want to execute only 5 testcase because other 5 are working correctly

How to Disable Selenium Test cases?

In TestNG we can achieve this by simply adding enable attribute and can set value to true/false.
If test case enable as false them while running test cases TestRunner simply will ignore this testcase and will only run testcase whose enable is set to true.
Note- By default @Test is set to enable
Scenario – I have 3 test case but I want to execute only 2 test case.
Precondition- TestNG should be installed

Program for – Disable Selenium Test cases

import org.testng.annotations.Test;
public class TestEnableTC {
@Test
public void testLoginApp(){
System.out.println("User is able to login successfully");
}
@Test(enabled=false)
public void testRegisteruser(){
System.out.println("User is able to register successfully");
}
@Test
public void testLogoutApp(){
System.out.println("User is able to logout successfully");
}
}




             
                  
             What is Cross browser testing            
Cross browser, testing refers to testing the application in multiple browsers like IE, Chrome, Firefox so that we can test our application effectively.IE, Chrome, Firefox so that we can test our application effectively.

Cross browser, testing is very important concept in Automation because here the actual automation comes into the picture.

Example- Suppose if you have 20 test cases that you have to execute manually, so it is not a big deal right we can execute in 1 day or 2 days. However, if the same test cases you have to execute in five browsers it means 100 test cases then probably you will take one week or more than one week to do the same and it will be quite boring as well.

If you automate these 20 test cases and run them then it will not take more than one or two hour depends on your test case complexity.

Cross Browser Testing using Selenium Webdriver

To achieve this we will use TestNG parameter feature, we will pass parameter from TestNG.xml file, and based on our parameter Selenium will initiate our browsers.

In this scenario, we will run the same testcase with two different browser parallel.

Step 1- Write testcase
---------------------------------
package SampleTestcases;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class TestCase1 {
@Test
// Here this parameters we will take from testng.xml
@Parameters("Browser")
public  void test1(String browser)
{
if(browser.equalsIgnoreCase("FF"))
{
WebDriver driver=new FirefoxDriver();

driver.manage().window().maximize();

driver.get("http://www.facebook.com");

driver.quit();

}
else if(browser.equalsIgnoreCase("IE")){

System.setProperty("webdriver.ie.driver", "./server/IEDriverServer.exe");

WebDriver driver=new InternetExplorerDriver();

driver.manage().window().maximize();

driver.get("http://www.facebook.com");

driver.quit();
}
}

}
 Step 2- Create testng.xml and specify
------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
Here parallel is an attribute which specify the mode of execution and thread-count specify how many browser should open
<suite name="Suite" parallel="tests" thread-count="2">

<test name="Test">

<parameter name="Browser" value="FF" />

<classes>

<class name="SampleTestcases.TestCase1"/>

</classes>

</test>

<test name="Test1">

<parameter name="Browser" value="IE" />

<classes>

<class name="SampleTestcases.TestCase1"/>

</classes>

</test>

</suite>
Step 3- Run this xml file refer the below screenshot
------------------------------------------------------

Note- To create testng.xml- Right, click on your testcase then go to TestNG then convert to TestNG> It will generate testng.xml then make changes as per above xml file and finish. You will get testng.xml file inside the project



I am sure you must be confused with Title like How to create a dependency between test cases in Selenium but in this post we will see this through example.

In real time, you will come across many situation where you have to create test cases which are dependent on the previous test case.

Take an example I have 3 test case.

First one which verify login credential and start application
Second test case check some validation  or do some activity
Third test case simply just for log-out activity.

If my first test case does not work (failed) that it does not make any sense to run second and third test case so in these type of scenarios you can use dependency between test case.

How can I do this?

TestNG already comes with this features so you can use the same with @Test annotations


Syntax- @Test(dependsOnMethods={"loginTestCase"})

public void testAccount()
{

// sample code

}
Syntax- @Test(dependsOnMethods={"loginTestCase"})

public void testAccount()
{

// sample code

}
How to create dependency between test cases in Selenium



It means this test case name testAccount depends on test case whose name is loginTestCase so until loginTestCase will not execute successfully this test case won’t execute so if loginTestCase will pass this test case will execute and if loginTestCase will fail then this test case won’t execute and runner will skip this test cases and this test case will come under skip test cases.

Let’s implement the same


package Demo;

import org.testng.Assert;
import org.testng.annotations.Test;

public class TestMultiple {

@Test
public void testLogin()
{

System.out.println("login done");

}

@Test(dependsOnMethods={"testLogin"})
public void testAccount()
{
System.out.println("Account has been created");

}

@Test(dependsOnMethods={"testLogin","testAccount"})
public void testLogout()
{
System.out.println("logout");

}

}


package Demo;

import org.testng.Assert;
import org.testng.annotations.Test;

public class TestMultiple {

@Test
public void testLogin()
{

System.out.println("login done");

}

@Test(dependsOnMethods={"testLogin"})
public void testAccount()
{
System.out.println("Account has been created");

}

@Test(dependsOnMethods={"testLogin","testAccount"})
public void testLogout()
{
System.out.println("logout");

}

}


In the above scenario if testLogin will pass then only testAccount will execute and testLogout will only execute of testLogin and testAccount will  be executed

but if testLogin will fail due to some error or exception the testAccount will not be executed and this test case will skip and testLogout is dependent on testLogin and testAccount so now this will also come into skipped category

Let’s implement again


import org.testng.Assert;
import org.testng.annotations.Test;

public class TestApplication {

@Test
public void testLogin()

{
Assert.assertEquals("Selenium", "Selnium");
System.out.println("login done");

}

@Test(dependsOnMethods={"testLogin"})
public void testAccount()

{
System.out.println("Account has been created");

}

@Test(dependsOnMethods={"testLogin","testAccount"})

public void testLogout()

{
System.out.println("logout");
}
}

import org.testng.Assert;
import org.testng.annotations.Test;

public class TestApplication {

@Test
public void testLogin()

{
Assert.assertEquals("Selenium", "Selnium");
System.out.println("login done");

}

@Test(dependsOnMethods={"testLogin"})
public void testAccount()

{
System.out.println("Account has been created");

}

@Test(dependsOnMethods={"testLogin","testAccount"})

public void testLogout()

{
System.out.println("logout");

}
}


Output

FAILED: testLogin
java.lang.AssertionError: expected [Selnium] but found [Selenium]
at org.testng.Assert.fail(Assert.java:94)
SKIPPED: testAccount
SKIPPED: testLogout

===============================================
Default test
Tests run: 3, Failures: 1, Skips: 2
===============================================

===============================================
Default suite
Total tests run: 3, Failures: 1, Skips: 2
===============================================

OUTPUT in HTML


























2 comments: