SELENIUM – SOAP Webservice Automation



SELENIUM – SOAP Webservice Automation

Sample code to Automate SOAP Webservice in C#..

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Xml;
using NUnit;
using NUnit.Framework;

namespace SeleniumDemo_March.TestCases
{
    public class InvokeWebService
    {
        /// Execute a Soap WebService call
        /// </summary>
         [Test]
        public void AddNumbers()
        {
            string soapResult = "";
            HttpWebRequest request = CreateWebRequest();
            XmlDocument objDoc = new XmlDocument();
            objDoc.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8""?>
<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
  <soap:Body>
    <Add xmlns=""http://tempuri.org/"">
      <intA>10</intA>
      <intB>20</intB>
    </Add>
  </soap:Body>
</soap:Envelope>");

           

            using (Stream stream = request.GetRequestStream())
            {
                objDoc.Save(stream);
            }

            using (WebResponse response = request.GetResponse())
            {
                using (StreamReader rd = new StreamReader(response.GetResponseStream()))
                {
                    soapResult = rd.ReadToEnd();
                    Console.WriteLine(soapResult);
                }
            }
           
            Assert.IsTrue(soapResult.Contains("<AddResult>30</AddResult>") == true);
            Console.WriteLine("Sum is: 30");
        }
        [Test]
        public void SubtractNumbers()
        {

            string soapResult = "";
            HttpWebRequest request = CreateWebRequest();
            XmlDocument objDoc = new XmlDocument();
            objDoc.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8""?>
<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
  <soap:Body>
    <Subtract xmlns=""http://tempuri.org/"">
      <intA>30</intA>
      <intB>20</intB>
    </Subtract>
  </soap:Body>
</soap:Envelope>");



            using (Stream stream = request.GetRequestStream())
            {
                objDoc.Save(stream);
            }

            using (WebResponse response = request.GetResponse())
            {
                using (StreamReader rd = new StreamReader(response.GetResponseStream()))
                {
                    soapResult = rd.ReadToEnd();
                    Console.WriteLine(soapResult);
                }
            }
          
            Assert.IsTrue(soapResult.Contains("<SubtractResult>10</SubtractResult>") == true);
            Console.WriteLine("Result is: 10");
        }
        /// <summary>
        /// Create a soap webrequest to [Url]
        /// </summary>
        /// <returns></returns>
        public HttpWebRequest CreateWebRequest()
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(@"http://www.dneonline.com/calculator.asmx");
            webRequest.Headers.Add(@"SOAP:Action");
            webRequest.ContentType = "text/xml;charset=\"utf-8\"";
            webRequest.Accept = "text/xml";
            webRequest.Method = "POST";
            return webRequest;
        }
    }
}



Learn about REST API Automation

https://techtutorialz.com/selenium-api-automation/

Learn about API testing.

https://techtutorialz.com/category/api-testing/

Tags: , ,
Leave a comment

Your email address will not be published. Required fields are marked *

Subscribe now

Receive weekly newsletter with educational materials, new courses, most popular posts, popular books and much more!

https://bridgejunks.com/ https://crownmakesense.com/ https://brithaniabookjudges.com/ https://hughesroyality.com/ https://rhythmholic.com/ https://bandar89.simnasfikpunhas.com/ https://www.100calshop.co.il/products/thailand/ https://myasociados.com/ https://solyser.com/ http://konfidence.cz/ https://muscadinepdx.com/ https://bandar89.parajesandinos.com.ve/ https://goremekoop.com/ https://oncoswisscenter.com/ https://www.turunclifehotel.com/bandar89/ https://www.houseofproducts.biz/ https://taimoormphotography.com/
BIJI18 BIJI18 BIJI18