cancel
Showing results for 
Search instead for 
Did you mean: 

Calling .cgi file from script and read dynamically generated values

SOLVED
Highlighted
Contributor

Calling .cgi file from script and read dynamically generated values

Hi,

I am doing desktop testing where I need to call .cgi script that will dynamically generate caserun IDS for testcases. I am able to call .cgi script as below.

 

var WScriptObj = Sys.OleObject("WScript.Shell");

 

for example:  path = "http://ip address/tr_new_run_testcomplete.cgi?&environment=Lab&cases=4,5,6,7"

 

WScriptObj["Run"]("path");

 

CaseRun ID created as below.

{success: true, run_id: 35}
Test Case 25: CaseRun ID: 113

I need to get that value of CaseRunID. Need to figure out, 

1) How can I save that page?

2) Can I open it with notepad and read content as string.

After that I need to call other .cgi file to update status of each test case based on CaseRun ID.

 

Thanks,

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted

OK so if you want to go with the "paste URL into browser" approach, you need to launch the browser and get the web page contents as follows.

 

If you have the Web testing module:

 

// Open the page in a browser (replace btIExplorer with your default browser)
Browsers.Items(btIExplorer).Run(CreateCaseRunID_Path); var page = Sys.Browser().Page(CreateCaseRunID_Path);
// Get the page's source code var str = page.contentDocument.documentElement.outerHTML; // TODO: extract values from str

 

If you don't have the Web testing module, and assuming your default browser is IE:

 

// Open the page in IE
var ie = Sys.OleObject("InternetExplorer.Application");
ie.Visible = true; ie.Navigate(CreateCaseRunID_Path); while (ie.ReadyState != 4) Delay(100);
// Get the page's source code var str = ie.Document.documentElement.outerHTML; // TODO: extract values from str

 

Let us know if this works for you.

 

>>

if I copy paste url string it loads the page fine without asking login info.

<<

It could be that you're already logged into this system in your browser - that's why it doesn't prompt for login. For example, you logged in a few weeks ago, or it uses your Windows account, or something like that. I initially suggested the approach with programmatic login so that the script is portable and doesn't depend on your current computer state (such as the browser you use). But of course you are free to use whatever method suits you best and works best for you. 🙂


Helen Kosova
SmartBear Documentation Team Lead
________________________
Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️

View solution in original post

9 REPLIES 9
Highlighted
Moderator

I would suggest a different approach - access that URL via an HTTP request and read the response. Here's how you can do this:

 

function Test()
{
  var strURL = "http://ip address/tr_new_run_testcomplete.cgi?&environment=Lab&cases=4,5,6,7";

  var oHTTP = Sys.OleObject("MSXML2.XMLHTTP");
  oHTTP.open("GET", strURL, false);
  oHTTP.send();
  while((oHTTP.readyState != 4) && (oHTTP.readyState != "complete")) { 
    Delay(100);
  }

  if (oHTTP.status == 200) {
    var str = oHTTP.responseText;

    // TODO: parse the str
    // ...

  } else {
    Log.Error("Could not access the URL. HTTP status " + oHTTP.status);
  }
}

How to parse the response depends on the response type. For example, if it's JSON like this

{success: true, run_id: 35}

you can use:

// TC 11
var obj = JSON.parse(oHTTP.responseText);
// TC <= 10
// var obj = eval("(" + oHTTP.responseText + ")");

var run_id = obj.run_id;

Hope this helps!


Helen Kosova
SmartBear Documentation Team Lead
________________________
Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
Highlighted

Thanks for your reply. I have further question:

I am getting response from the ipaddress.index page and not from the page for which path is "CreateCaseRunID_Path" as below.

 

CreateCaseRunID_Path = "http://ip addres/tr_new_run_testcomplete.cgi?run_summary=" + RunSummary + "&plan_id=" + PlanID + "&product_ver=" + ProdVer + "&build=" + Build + "&environment=" + Envn + "&cases=" + TestCaseNum;

 

var oHTTP = Sys.OleObject("MSXML2.XMLHTTP");
oHTTP.open("GET", CreateCaseRunID_Path, false);
oHTTP.send();

 

how can I convert it to the text, so I can parse the string to obtain desired values.

I am using TC11.

Thanks for your time.

Highlighted

oHTTP.responseText will give you the response as text.


Helen Kosova
SmartBear Documentation Team Lead
________________________
Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
Highlighted

Thanks again for your response. But still I didn't get answer. 

1) If I manually copy this string (CreateCaseRunID_Path) and paste in browser -

"http://ip address/tr_new_run_testcomplete.cgi?run_summary=" + RunSummary + "&plan_id=" + PlanID
+ "&product_ver=" + ProdVer + "&build=" + Build + "&environment=" + Envn + "&cases=" + TestCaseNum;

 

I am able to see this page --

{success: true, run_id: 39}
Test Case 25: CaseRun ID: 121
Test Case 26: CaseRun ID: 122

 

2) When I do this from script --

var oHTTP = Sys.OleObject("MSXML2.XMLHTTP");
oHTTP.open("GET", CreateCaseRunID_Path, false);
oHTTP.send();

var str = oHTTP.responseText;
Log["Message"](str)

 

I don't get response that I am looking for. 

See attached text file I saved from TC logs. It gives information about query, but not the actual response.

 

 

Highlighted

Looks like this page requires a login and password. Try replacing this line:

 

oHTTP.open("GET", CreateCaseRunID_Path, false);

with:

 

oHTTP.open("GET", CreateCaseRunID_Path, false, "username", "password");

Does this work?


Helen Kosova
SmartBear Documentation Team Lead
________________________
Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
Highlighted

Hello Helen,

Thanks for helping out. Not sure if it required username/password, if I copy paste url string it loads the page fine without asking login info.I tried putting my login info. and it gave me same response.

Problem is oHTTP.open gets the response from just the ip address (http://ip address/) page and not from the whole query (CreateCaseRunID_Path)

 

CreateCaseRunID_Path = "http://ip address/tr_new_run_testcomplete.cgi?run_summary=This%20is%20the%20summary&plan_id=id&product_ver=product&build=build&environment=envn&cases=1,2,3";

 

So I do get response back, but not the one I am trying to get.

Is there anything I am missing?

 

Thanks.

 

Highlighted

 

If I choose the other option to call .cgi file as,

 

var WScriptObj = Sys.OleObject("WScript.Shell");

var res = WScriptObj.Run(CreateCaseRunID_Path);

 

It successfully opens the webpage with dynamically generated data.

 

Any suggestions on writing script for-

save that web page as text file? (with command - right click save as)

 

thanks,

Samixa

Highlighted

OK so if you want to go with the "paste URL into browser" approach, you need to launch the browser and get the web page contents as follows.

 

If you have the Web testing module:

 

// Open the page in a browser (replace btIExplorer with your default browser)
Browsers.Items(btIExplorer).Run(CreateCaseRunID_Path); var page = Sys.Browser().Page(CreateCaseRunID_Path);
// Get the page's source code var str = page.contentDocument.documentElement.outerHTML; // TODO: extract values from str

 

If you don't have the Web testing module, and assuming your default browser is IE:

 

// Open the page in IE
var ie = Sys.OleObject("InternetExplorer.Application");
ie.Visible = true; ie.Navigate(CreateCaseRunID_Path); while (ie.ReadyState != 4) Delay(100);
// Get the page's source code var str = ie.Document.documentElement.outerHTML; // TODO: extract values from str

 

Let us know if this works for you.

 

>>

if I copy paste url string it loads the page fine without asking login info.

<<

It could be that you're already logged into this system in your browser - that's why it doesn't prompt for login. For example, you logged in a few weeks ago, or it uses your Windows account, or something like that. I initially suggested the approach with programmatic login so that the script is portable and doesn't depend on your current computer state (such as the browser you use). But of course you are free to use whatever method suits you best and works best for you. 🙂


Helen Kosova
SmartBear Documentation Team Lead
________________________
Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️

View solution in original post

Highlighted

Hi Helen,

Thanks a lot. I don't have Web testing module. 

Well I tried passing username & password, it didn't work.

oHTTP.open("GET", CreateCaseRunID_Path, false, "username", "password"); 

 I would definately go with the first approach if I get response back. But it gives me request submitted in oHTTP.responseText and not the actual response. Still I have no clue why it isn't working. 

 

Anyway second approach, opening url in browser works fine. Finally..Thanks so much.

New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors