Wednesday, 30 May 2018


How to run Cucumber Feature file using Junit or TestNg



Run through Junit


@RunWith(Cucumber.class)
@CucumberOptions(
                  features = “src/test/Your features file“,
                  glues="src/test/Your Step Defnitio File"
                  tags ={“@SoapUI,@Functional"},...  )

public class TestRunnerClass
{
// You should keep empty

}

Run through TestNG

@CucumberOptions(
        format={"pretty","json:path/to/json_report.json"},
        features = "Path_to_Your_features_file",
        glue="Path_to_Your_stepDefinition file",
        tags={"@smoke,@regression")
        )

public class TestRunner extends AbstractTestNGCucumberTests

{
  //keep empty
}

Difference between Nunit and Xunit and MSTest

Attributes ( like Annotations in junit or TestNG)
NUnit 3.xMSTest 15.xxUnit.net 2.xComments
[Test][TestMethod][Fact]Marks a test method.
[TestFixture][TestClass]n/axUnit.net does not require an attribute for a test class; it looks for all test methods in all public (exported) classes in the assembly.
Assert.That
Record.Exception
[ExpectedException]Assert.Throws
Record.Exception
xUnit.net has done away with the ExpectedException attribute in favor of Assert.Throws. See Note 1
[SetUp][TestInitialize]ConstructorWe believe that use of [SetUp] is generally bad. However, you can implement a parameterless constructor as a direct replacement.
[TearDown][TestCleanup]IDisposable.DisposeWe believe that use of [TearDown] is generally bad. However, you can implement IDisposable.Disposeas a direct replacement.
[OneTimeSetUp][ClassInitialize]IClassFixture<T>To get per-class fixture setup, implement IClassFixture<T> on your test class.
[OneTimeTearDown][ClassCleanup]IClassFixture<T>To get per-class fixture teardown, implement IClassFixture<T> on your test class.
n/an/aICollectionFixture<T>To get per-collection fixture setup and teardown, implement ICollectionFixture<T> on your test collection.
[Ignore("reason")][Ignore][Fact(Skip="reason")]Set the Skip parameter on the [Fact] attribute to temporarily skip a test.
[Property][TestProperty][Trait]Set arbitrary metadata on a test
[Theory][DataSource][Theory]
[XxxData]
Theory (data-driven test).

Difference between Junit and TestNG

FeatureJUnit 4TestNG
test annotation@Test@Test
run before all tests in this suite have run@BeforeSuite
run after all tests in this suite have run@AfterSuite
run before the test@BeforeTest
run after the test@AfterTest
run before the first test method that belongs to any of these groups is invoked@BeforeGroups
run after the last test method that belongs to any of these groups is invoked@AfterGroups
run before the first test method in the current class is invoked@BeforeClass@BeforeClass
run after all the test methods in the current class have been run@AfterClass@AfterClass
run before each test method@Before@BeforeMethod
run after each test method@After@AfterMethod
ignore test@ignore@Test(enbale=false)
expected exception@Test(expected = ArithmeticException.class)@Test(expectedExceptions = ArithmeticException.class)
timeout@Test(timeout = 1000)@Test(timeout = 1000)

Selenium, C# with SpecFlow Setup requirement

1. Selenium WebDriver

Site : http://selenium-release.storage.googleapis.com/index.html?path=3.12/

File Name : selenium-dotnet-3.12.1.zip


2. Visual Studio

3. Nunit
       Go with Nunit 3 as it supports Parallel execution

      Site: http://nunit.org/download/

4. C# ( will be supported by Visual Studio by default)
5. Reporting ( NuGet Gallery | NUnitOrange 2.1.0)

It will covert TestResults.xml to Test Results.html

6. Building Tool ( MSbuild) - it is coming by default with OS
7. Jenkins ( https://jenkins.io)
8. Object Identification ( You can use Xpath and CSS, Please refer my previous post)
9. SpecFlow (BDD)
               Site : https://github.com/techtalk/SpecFlow/wiki/Advanced-Project-Setup










Selenium C# Automation Setup requirement

1. Selenium WebDriver

Site : http://selenium-release.storage.googleapis.com/index.html?path=3.12/

File Name : selenium-dotnet-3.12.1.zip


2. Visual Studio

3. Nunit
       Go with Nunit 3 as it supports Parallel execution

      Site: http://nunit.org/download/

4. C# ( will be supported by Visual Studio by default)
5. Reporting ( NuGet Gallery | NUnitOrange 2.1.0)

It will covert TestResults.xml to Test Results.html

6. Building Tool ( MSbuild) - it is coming by default with OS
7. Jenkins ( https://jenkins.io)
8. Object Identification ( You can use Xpath and CSS, Please refer my previous post)











Complete Xpath and CSS Guidance sheet:

www.cheat-sheets.org/saved-copy/Locators_table_1_0_2.pdf

Xpath and CSS Explanation

http://qavalidation.com/2015/05/xpath-css-patterns.html/

http://makeseleniumeasy.com/2018/04/09/writing-conditional-xpath-in-selenium-webdriver/



How to Launch Winium Driver using Java? (Winium.Desktop tool is useful to handle desktop based Application)

public WiniumDriver driver=null;
public DesktopOptions options;
public String DesktopAppPath="C:\\Windows\\System32\\Calc.exe";

@BeforeMethod
public void OpenCalculator()
{
// Please restart Winium.Desktop Driver before you proceed. There is some security reason, not able to start in Cognizant machine
options= new DesktopOptions();
options.setApplicationPath(DesktopAppPath); // like driver.get method
try{
driver=new WiniumDriver(new URL("http://localhost:9999"),options);
Thread.sleep(4000);

}
catch(Exception e){
System.out.println(e.getMessage());
}


Facing trouble while sending value for WebElement then use the below

JavascriptExecutor jse= (JavascriptExecutor)driver;
String script= "arguments[0].setAttribute('value','"+Yourvalue+"');";
jse.executeScript(script, YourWebElement);

Tuesday, 29 May 2018



Difference between Page Object Model and Page Factory

POM:
_____
By username=By.name("userName");


PageFactory:
____________
// Defining Locators
@FindBy(name="userName")
WebElement userName;



POM:
____
Signon(WebDriver driver)
{
this.driver=driver;
}





PageFactory:
_____________

SignonPF(WebDriver driver)
{
this.driver=driver;
PageFactory.initElements(driver, this);
}


POM:
____

public void enterUsername(String name)
{
driver.findElement(username).sendKeys(name);
}


PageFactory:
____________

public void enterUser(String name)
{
userName.sendKeys(name);
}


Automation Testing Framework




You can have any of these combination like

Selenium WebDriver  --  UI

Java (Core / J2EE) / C#   -- To write Utility Functions like reading file, reusable functions

TestNG or Junit / Nunit or Xunit  -- To change your Automation work flow and Verify your actual and expected results, suite creation, Cross Browser testing
Cucumber / SpecFlow   - Helpful for BDD framework

Third Party supported Tool / jar - Apache POI Excel jar (read Excel), Sikuli, AutoIt, Winium.Desktop are helpful to handle Desktop based components

Xpath / CSS  -  To locate Web Component using Firebug, firepath
UIAVerify . AutoIt / UFT - To locate window component like Authentication required dialog, save as dialog

Maven / MSbuild -- Maven (Project Management tool)  where most of the part used in Development side and but in Testing side, we are using Dependency section for downloading jar file, placing Test suite file, Reporting our test results, compiling our unit test like junit using Sure fire plugin etc.

Framework -  Hybrid / BDD

Design Pattern  - Page Object Model / Page Factory

Jenkins - Continuous integration and Continuous delivery tool

Flow

POM.xml--> TestNG.xml--> Test class--> Test Methods ( Your Test case)



Nunit 2 Attributes


[TestFixture]
public class TestFixtureLifetime
{

    [TestFixtureSetUp] 
    public void Init()
    { Console.WriteLine("BeforeTest Fixture"); }

    [TestFixtureTearDown] 
    public void Cleanup()
    { Console.WriteLine("After Test Fixture");}


    [SetUp]
    public void BeforeTest()
    { Console.WriteLine("BeforeTest"); }

    [TearDown]
    public void AfterTest()
    { Console.WriteLine("AfterTest"); }

    [Test]
    public void Test1()
    { Console.WriteLine("Test1"); }

    [Test]
    public void Test2()
    { Console.WriteLine("Test2"); }
}

When I run this test  I get the following output:

BeforeTest Fixture
BeforeTest
Test1
AfterTest
BeforeTest
Test2
AfterTest
After Test Fixture


TestFixtureSetUp  - BeforeClass
TestFixture --  Class
Setup - BeforeMethod
Test
TearDown - AfterMethod
TestFixtureTearDown - AfterClass


Collections in C#



There are 2 types of collections in C#
1. Non Generic
2. Generic


Non Generic:
1. ArrayList
2. SortedList ( it is like map in Java)
3. Stack ( LIFO)
4. Queue ( FIFO)
5. HashTabe ( it is like map in java)


Generic:
1. List<T>
2. Dictionary<TKey,TValue> ( it is like map in java)
3. SortedList<TKey,TValue>
4. Stack<T> ( LIFO)
5. Queue<T> ( FIFO)
6. HashSet<T> ( it is like map in java)
Difference between  Cucumber Hooks (Java) and SpecFlow Hooks( C#)

Cucumber Hooks

[BeforeTestRun] - Before you ran the entire suite
[AfterTestRun] - After you ran the entire suite


[BeforeFeature]  - Before you ran the Feature
[AfterFeature] - After you ran the Feature

[BeforeScenario]  - Before you ran the Scenario
[AfterScenario]  - After you ran the Scenario

[BeforeStep]  - Beforethe every scenario step
[AfterStep]- after completing the every scenario step


SpecFlow Hooks

@Before
@After