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") 


Monday, April 17, 2017

Different methods to scroll down a window in selenium test (code is in C#)

Sometime we used to scroll down a window as per our automation requirement and we have following methods to do it:

Method 1: Using Action Call and keydown event
Method 2: Using Java Script
Method 3: using Action class and movetoelement function

Check the below code to use all three methods:

using System;
using OpenQA.Selenium.Chrome;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SikuliSharp;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium;


namespace UnitTestProject1
{
    [TestClass]
   
    public class ScrollDown
    {

        [TestMethod]
        public void TestMethod2()
        {
            ChromeDriver driver = new ChromeDriver(@"E:\Drivers");
            driver.Navigate().GoToUrl("http://robotframework.org/");


            // Method 1 to scroll down a window with action class and Key.Down even

            Actions action = new Actions(driver);

            for (int a = 1; a < 10; a = a + 1)
            {
                action.SendKeys(Keys.Down).Perform();
            }


            // Method 2 to scroll down a window with Java Script executor

            ((IJavaScriptExecutor)driver).ExecuteScript("window.scrollTo(0,1050)");


            //Method 3 to scroll down a window with Action class. Find an element and move to that element

            IWebElement element = driver.FindElement(By.Id("chat"));
            Actions actions = new Actions(driver);
            actions.MoveToElement(element);
            actions.Perform();

        }
    }

}

Saturday, April 15, 2017

Sikuli with C# - Sikuli works when others fail


Writing this blog as I found it very beneficial. In one particular scenario, where I have to click on a tile (on a web application), I tried different solution but nothing works. I have heard about sikuli but never used it. Today I tried it and found it useful. It can be used with windows application or if something doesn’t work on web.

Step 1: Installation of sikuli – This is the main step in this whole blog as without it, this sikuli will not work.


In MS VS 2015, Tools > NuGet Package Manager > Manage NuGet Packages for Solution..
Under Browse tab, enter “Sikuli” into the search box. 















Select sikulisharp and install it. You can then validate it in the references.


Step 2: Now you want sikuli installer in your system too.
  •   Install Java on your machine

  •    Download and install sikuli

    1. Follow instructions in Sikuli page. http://www.sikulix.com/quickstart.html. Otherwise major steps are written below:
                                                           






  • Download sikuli from this location. https://launchpad.net/sikuli/sikulix/1.1.1                             
  • Create a folder in C drive and put this sikulixsetup-1.1.1.jar in that folder.
  • When you will click on highlighted/downloaded sikulixsetup-1.1.1.jar, it will download some more jars shown above and setup will start after complete download





  • Choose option ‘to run scripts from the command line – the file sikuli-scripts.jar must be installed’
b. Create an environment variable SIKULI_HOME that points to the folder where you installed Sikuli.
    a. Go to Start > Right click on my computer > Properties. Click Advanced System Settings. Click       Environment Variables






















    b. Under the system variable of the Environment variables window, click new and type in           ‘SIKULI_HOME’ and the Sikuli installation path in the variable name and value fields, respectively. Click OK



Step3: Now you are ready to write your scripts. Create a unit test file in your project and add following code in that.

using System;
using OpenQA.Selenium.Chrome;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SikuliSharp;


namespace UnitTestProject1
{
    [TestClass]


    public class UnitTest1
    {
      
        [TestMethod]
        public void TestMethod1()

        {
            ChromeDriver driver = new ChromeDriver(@"E:\Automation\Drivers");
            driver.Navigate().GoToUrl("http://www.google.com");
          
            using (var session = Sikuli.CreateSession())
            {
               var clickme = Patterns.FromFile("E:\\img.png", 0.9f);
               session.Click(clickme);
            }
        }
    }
}

In this code, I am using an image file which I placed in E directory. This image is shown below.






Step 4: Run it. This code will open www.google.com and on this web page, it will click on this image.