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