hard assertions and soft assertions in c#

Hard assertions:
When a hard assertion fails, it throws out an exception immediately and the code after the assertion shouldn't be excuted,the test case is failed.

Soft assertions:
When a soft assertion fails, it does not throw out an exception, and would continue with the next steps after the soft assertion.

When to use hard assertions?
When a step in the test is very important, if this step fails, the following steps shouldn't go on, you may put a particular hard assertion to verify this step, if the hard assertion fails, the testing stops and the test case is failed. For example, when you test "login in a page and then do some operations after login", if login fails, the next operations can't be done, in that situation, you can put a hard assertion to verify the login operation. If the assert fails, testing excution stops, test case is failed.

When to use soft assertions?
In some situations, even the current test step fails, you may still want to do the next steps, then you can put a soft assert to verify the current step. If it fails, it only gets a fail log but the testing excution keeps going on. The next test steps should be run. For example, you want to print 4 items, if one of them fails, you still hope to print the next one. In that situation, you may put soft assertion to verify the test step "print one item", even it fails, the testing excution will goes on to print next items, and the test case will be passed.

SOAP and REST

Simple Object Access Protocol (SOAP) and Representational State Transfer (REST) are two answers to the same question: how to access Web services.

SOAP is a standards-based Web services access protocol that has been around for a while and enjoys all of the benefits of long-term use. Originally developed by Microsoft, SOAP really isn’t as simple as the acronym would suggest.

REST is the newcomer to the block. It seeks to fix the problems with SOAP and provide a truly simple method of accessing Web services. However, sometimes SOAP is actually easier to use; sometimes REST has problems of its own. Both techniques have issues to consider when deciding which protocol to use.

SOAP is described by WSDL, and communicate using XML.

REST APIs are visited by URIs, and communicate using XML, HTML, or JSON, mostly JSON. REST APIs use POST, GET,PUT,DELETE methods of HTTP to manipulate the resource.


SOAP example:

REST example:


Try Catch in c#

When I was automating my test case today, I found an interesting thing: The case was failed, but it was reported "passed" in my test report!

My lead and I checked it carefully, then we found that as I created a test log in for the case, the LogStatus.Info is true, so in test report, the case's status was reported as "Pass" . But Actually, because an error occured, the case didn't go to the validation part.

The previous codes were as following:

                OwnersPropertiesPage();
                EnterPropertyDetails();
                EnterFinancialDetails();
                EnterTenantDetails();

                OwnersPropertiesPage(); //the case failed here

                // Screenshot
                String img = SaveScreenShotClass.SaveScreenshot(Driver.driver, "FinalScreenshot");
                Base.test.Log(LogStatus.Info, "FinalScreenshot: " + img);


                //open the "PropertyDetails" sheet
                ExcelLib.PopulateInCollection(Base.ExcelPath, "PropertyDetails");

                //name1=name of the property which has just been added.
                var name1 = ExcelLib.ReadData(2, "propertyName");
                Thread.Sleep(2000);

                //name2=name of the property which is the first property displayed on the Owners>Properties page
                var name2 = Driver.driver.FindElement(By.XPath(".//*[@id='main-content']/section/div[1]/div[1]/div[4]/div[1]/div/div/div[2]/div[2]/div[1]/div[1]/div[1]")).Text;

                if (name1 == name2)
                         Base.test.Log(LogStatus.Pass, "ValidateAddNewProperty is Passed");
                 else
                         Base.test.Log(LogStatus.Fail, "ValidateAddNewProperty is Failed");

Here is the TearDown:
[TearDown]
        public void TearDown()
        {
            // Screenshot
            String img = SaveScreenShotClass.SaveScreenshot(Driver.driver, "Report");//AddScreenCapture(@"E:\Dropbox\VisualStudio\Projects\Beehive\TestReports\ScreenShots\");
            test.Log(LogStatus.Info, "Image example: " + img);
            // end test. (Reports)
            extent.EndTest(test);
            // calling Flush writes everything to the log file (Reports)
            extent.Flush();
        }

Later, I used a Try Catch to fix the problem. I changed the codes as following:

 try
            {
                OwnersPropertiesPage();
                EnterPropertyDetails();
                EnterFinancialDetails();
                EnterTenantDetails();

               OwnersPropertiesPage(); //the case still failed here

                // Screenshot
                String img = SaveScreenShotClass.SaveScreenshot(Driver.driver, "FinalScreenshot");
                Base.test.Log(LogStatus.Info, "FinalScreenshot: " + img);


                //open the "PropertyDetails" sheet
                ExcelLib.PopulateInCollection(Base.ExcelPath, "PropertyDetails");

                //name1=name of the property which has just been added.
                var name1 = ExcelLib.ReadData(2, "propertyName");
                Thread.Sleep(2000);

                //name2=name of the property which is the first property displayed on the Owners>Properties page
                var name2 = Driver.driver.FindElement(By.XPath(".//*[@id='main-content']/section/div[1]/div[1]/div[4]/div[1]/div/div/div[2]/div[2]/div[1]/div[1]/div[1]")).Text;

                //Console.WriteLine(name1);
                //Console.WriteLine(name2);

                if (name1 == name2)

                    //Console.WriteLine("test pass");
                    Base.test.Log(LogStatus.Pass, "ValidateAddNewProperty is Passed");

            }

            catch(Exception e)
            {
                Base.test.Log(LogStatus.Fail, "ValidateAddNewProperty is Failed",e.Message);
            }

Then I got the correct report report: Failed, and the fail details was recorded in the report, like "Could not find element by: By.XPath: /html/body/div[5]/div/div[5]/a[1]".
         

Examples of a same message written in XML & JSON




 XML is short for Extensible Markup Language. 
JSON is short for JavaScript Object Notation.

Message can be written in both XML and JSON . There're examples of the same message("menu") in XML and JSON:

The text expressed as XML:

<menu id="file" value="File">
  <popup>
    <menuitem value="New" onclick="CreateNewDoc()" />
    <menuitem value="Open" onclick="OpenDoc()" />
    <menuitem value="Close" onclick="CloseDoc()" />
  </popup>
</menu>


The same text expressed as JSON:
{"menu": {
  "id": "file",
  "value": "File",
  "popup": {
    "menuitem": [
      {"value": "New", "onclick": "CreateNewDoc()"},
      {"value": "Open", "onclick": "OpenDoc()"},
      {"value": "Close", "onclick": "CloseDoc()"}
    ]
  }
}}

After learnt about XML, reading the above examples helped me understand JSON!