How Run Jenkin’s job using dynamic base URL using selenium C#

If you want to run the Jenkin’s job using dynamic base URL using selenium c# or want to change the base URL at the Run time of automation suite.

Here is a complete solution to change the base URL in Jenkin’s at run time of automation using selenium C# and how to change the App.config at Run time. 

Before reading this solution I’ll recommend checking the program to Read data from Cammnd line in C# and Write the same data in an XML file using node name.

As per my task. I have some custom requirements to execute an automation suite using Jenkins, which is built-in C# suing Nunit and SpecFlow. So I need to pass the Base URL of the application from the Jenkin’s parameter.

For Example, We have multiple URLs for an application and we want to run the same automation suite for all the base URLs depends on the requirement.

Step to pass Dynamic Base URL in Jenkin’s Job

  1. Create a UrlConfig.xml file.
  2. Create a (url.exe)Desktop app. Which takes input data(URL) from the CMD and writes the data in the XML file UrlConfig.xml.
  3. Read the data from “UrlConfig.xml” by node name and use it in the entire application
  4. Create a Jenkins Job as build as a parameter and call the URL.EXE from any location and execute this batch command before the build.

 Let’s check these steps in detail

 Create a UrlConfig.xml

Create a .xml File with a custom node name. It’s optional you can keep any name as per your requirement. In the below example My child node name is <URL> because I want to change the URL of the application at run time.

Now the XML file is ready in the next step will create a C# program to read the data from CMD and write the data in the XML file.

<?xml version="1.0" standalone="yes"?>
<Datas>
  <Data>
    <URL>Enter URL…………</URL>
  </Data>
</Datas>

How to write data in XML from CMD in C#.

This is a C# program to read data from the command prompt(CMD) and write the data in the XML file. It will take input from CMD and write the same data in our XML(new.xml) file.

Now we need the .exe file of this C# program so In the next step let’s see how can we find the .exe file of any C# program.

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace DemoApp
{
    class Program
    {
        static void Main(string[] args)
        {
            
            string filepath = @"C:\Users\bhupi\Desktop\folder\new.xml";
            DataSet ds = new DataSet();
            if (args.Count() > 0)
            {
                ds.ReadXml(filepath);
                ds.Tables[0].Rows[0]["URL"] = args[0];
                //In case of multiple URL
               // ds.Tables[0].Rows[0]["URL2"] = args[1];
                ds.WriteXml(filepath);
            }
        }
    }
}

How to find .exe file of a C# program

Simply, Navigate to your project location and search for bin/debug folder. you will get the .exe file of the program in the debug folder.

Now Run the .exe file from CMD  and check your program(.exe) is working properly as per the requirement.

How to Run .exe file from Command prompt

Open the command prompt and navigate to the path of .exe file and enter the below command.

Command:  url.exe “Enter URL Here…..”

Verify the XML ==> Now check your XML file. It should be updated with new data.

Now, we have a C# program or (.exe) file to read data from cmd and write the data in XML that we will pass from Jenkin’s command section in Jenkin’s job.

The Last step to read data from the XML file using node name and use this data(base URL) in the entire automation framework.

How to read data from XML by Node name.

This is a custom method to read the data from the XML file by node name or tag name. It will take input arg as node name and return the data of the node in string type.

public static string GetValueFromXML(string TagName)
       {
           string path1 = AppDomain.CurrentDomain.BaseDirectory.Replace("\\bin\\Debug", "");
           string path = path1 + "UrlConfig.xml";
           string text = System.IO.File.ReadAllText(path);
           XmlDocument xmlDoc = new XmlDocument();
           xmlDoc.LoadXml(text);
           XmlNodeList xnList = xmlDoc.SelectNodes("/Datas/Data");
           string URLdata = null;
           foreach (XmlNode xn in xnList)
           {
               URLdata = xn["URL"].InnerText;
           }
           return URLdata;
       }

All the basic requirement is ready now. run the .exe from the command prompt of Jenkin’s job and pass the input parameter as a variable name.

Run your url.exe from Jenkins

Command: “Complete path of your URL.exe” “Complete path of your URL.exe”

Check more challenging problems in automation using Specflow c#

Step by Step Specflow Tutorial

Leave a Reply

Your email address will not be published.

one × one =