Thursday, April 26, 2018

Headless Browser : Run test in headless browser


Problem Statement: Your scripts execution takes a lot of time and you want to reduce this execution time.

How: Headless browser is a program with no UI but works as any other normal browser in the background.

Overall Benefits:
1. To improve the overall speed of script execution.
2. Opening multiple browsers on one single machine. Since there is no UI so less memory is used. can be used for load testing.
3. If you cant install a browser on a machine, a headless browser is the best option to choose.
4. Your machine is free for other tasks to do.

Options:
Few headless drivers are:
1. Chrome
2. HTMLUnit
3. Ghost
4. PhantomJS
5. Watir
6. Slimer
7. Trifle

How to do it with chrome:

Initialise your browser withthe following setup:

System.setProperty("webdriver.chrome.driver", "ChromeDriverPath");
ChromeOptions options = new ChromeOptions();
options.addArguments("headless");
WebDriver driver = new ChromeDriver(options);

And run your scripts

Notes: Google support headless browser with version 59 and above.

Wednesday, April 4, 2018

Soft Assert or Multiple Assert in C# using Nunit

Soft Assert or Multiple asserts is required in few cases where a user wants his test to continue if an assert fails. Usually, a tester has to test multiple validation points in a single test case then he needs this feature.

Using nunit, it is possible. see the code below.

Step 1: Install nunit using NuGet Manager

Step 2: Create a class "TestMultipleAssert" and add following code in it.

using NUnit.Framework;

namespace WordPressAutomation.DifferentTests
{
  
    [TestFixture]
    public class TestMultipleAssert
    {
      

         [Test]
        public void ComplexNumberTest()
        {
         
            Assert.Multiple(() =>
            {
                Assert.AreEqual(5, 5, "First Assert");
                Assert.AreEqual("Testing 2", "Testing 2", "Second Assert");
                Assert.AreNotEqual("123", "ABC", "Asserts are not equal");
             });
        }
    }
}

Step 3: Run this code and everything will pass.

Step 4: Change the first assert expected value

 Assert.AreEqual(6, 5, "First Assert");

Step 5: Now execute it. Test case will fail in this case

Wednesday, February 21, 2018

Integrate Jenkins with Selenium, Java and Maven

We all know the purpose of Jenkins but how to integrate it with a selenium project which uses Java and Maven.

Prerequisite: Install Java, Maven, and Jenkins in the system where test case execution needs to be performed.

Step 1: Install few plugin in Jenkins shown in the pic below.



Step 2: Goto "Manage Jenkins" -> "Global Tool Configuration" and configure Java and Maven. setup the path of JAVA_HOME and MAVEN_HOME as per your system configuration. 

Step 3: Goto Jenkins -> New Item -> Give a name to this project like "DummyAutomation" -> Select "Freestyle project" and click OK.

Step 4: In this new DummyAutomation Project, Goto "Build" Section and click "Add Build Step" and select "Invoke top-level Maven targets".

Step 5: Select the "Maven Version", given in step 2. Set the goal "clean install" and set the path of POM file as shown in the pic below. 


We are done with the basic set up of Jenkins.

Step 6:  Save the project and click on "Build Now". Project will execute with a success


There are certain other advance areas that you can touch and make your Jenkins setup more reliable

Step 7: you can setup the email notification by selection "Editable Email Notification" in "Post-build Actions".




Grouping the tests and setting the priorities using TestNG annotation

Usually, a tester needs to execute the tests in a group as per requirement and in that group, tests are required to execute in a predefined order because of their dependency on each other.

TestNG gives us the facility to perform these actions with very easy to use annotation. Let's try to learn with an easy example-

Step 1 : Create a java test file with following code:

import org.testng.annotations.Test;

public class TestDummy1 extends TestBase{

@Test (groups = { "Sanity" })
public void Test1() throws Exception
{
System.out.println("Dummy Test 1 is called in Sanity group");
}
@Test (groups = { "Sanity" })
public void Test2() throws Exception
{
System.out.println("Dummy Test 2 is called in Sanity group");
}
@Test (groups = { "Regression" }, priority=2)
public void Test3() throws Exception
{
System.out.println("Dummy Test 3 is called in Regression group");
}
@Test (groups = { "Regression" }, priority=1)
public void Test4() throws Exception
{
System.out.println("Dummy Test 4 is called in Regression group");
}
@Test (groups = { "Regression" }, priority=3)
public void Test5() throws Exception
{
System.out.println("Dummy Test 5 is called in Regression group");
}
}

Step 2 : Create a testNG file with following code:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<!-- <suite name="Suite" > -->
<suite name="Suite">
<listeners>
<listener class-name="com.listeners.TestNGListeners" />
</listeners>

<!-- <test name="DummyTest" parallel="tests">
<groups>
<run>
<include name="Sanity" />
</run>
</groups>
<classes>
<class name="com.tests.Dummy.TestDummy1" />
</classes>
</test> -->

<test name="DummyTest1" parallel="tests">
<groups>
<run>
<include name="Regression" />
</run>
</groups>
<classes>
<class name="com.tests.Dummy.TestDummy1" />
</classes>
</test>
</suite

Step 3: Execute the testNG file and you will get the result. Regression group will be executed as per the priority given in test methods.

Dummy Test 4 is called in Regression group
Dummy Test 3 is called in Regression group
Dummy Test 5 is called in Regression group

Step 4: Uncomment the other group(Sanity) of tests in testNG file and execute it again.

Dummy Test 1 is called in Sanity group
Dummy Test 2 is called in Sanity group

Tuesday, January 16, 2018

Powershell command to change properties file



1.   To update App.properties file with Key=Value pair



$data =[io.file]::ReadAllText("C:\Resources\ConfigFiles\app.properties")
$data = $data -replace "Environment=(.+)","Environment=RC"
[IO.File]::WriteAllText("C:\Resources\ConfigFiles\app.properties", $data.TrimEnd())




2. To update XML file with different section and Key=Value pair



[xml] $xml = Get-Content "C:\Resources\ConfigFiles\app.properties"
$($xml.configuration.appSettings.add | Where-Object { $_.key -eq "Environment" }).value = "RC"

$xml.Save("C:\Resources\ConfigFiles\app.properties")