- Jan 12, 2023
- admin
- 0
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/