POM for UI automation testing

We are now doing an UI automation testing in Selenium WebDriver. It is not a tough task because we are following POM, just need to find elements and perform operations on them. So, what is POM?
POM stands for Page Object Model. 

As be seen in above photo, under this model, for each web page in the application, we create a corresponding page class for it. This page class will find the WebElements of that web page and also we dedine page methods to perform operations on those WebElements. We put all the page classes in a Pages folder.

And then we create a separate class to do all the tests, puting all test methods in that class file. In the future, if any element is changed, we just need to change element in the page class instead of in every test related to that element. 

Code becomes less and optimized because of the reusable page methods in the POM classes!




What is the difference between driver.close() and driver.quit() command in Selenium WebDriver

Driver.close(): closes the web browser window that the user is currently working on, or we can also say the window that is being currently accessed by the WebDriver. 
Driver.quit(): closes down all the windows that the driver has opened. 
Note that both commands neither require any parameter nor return any value.

What are the different types of waits available in WebDriver?

1. Implicit Waits 2. Explicit Waits 3. Fluent Wait

The Implicit Wait is specific to the entire Browser or all the elements in the browser. When you define certain period (say 10 Secs) it suspends the further action for 10 secs. It doesn't bother whether the particular/all element(s) loaded or not on the browser.
Different elements on the page takes different times to load on the page, if we use implicit wait for 10 Sec there is no guarantee that all the elements loaded within 10 sec, some elements may take more than 1 minute, in such conditions Explicit waits can be used to wait for that particular element.

The explicit wait is used to tell the Web Driver to wait for certain conditions (Expected Conditions) or the maximum time exceeded before throwing an "ElementNotVisibleException" exception.

The fluent wait is used to tell the web driver to wait for a condition, as well as the frequency with which we want to check the condition before throwing an "ElementNotVisibleException" exception.

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!