-1

I am using OpenQA.Selenium.Chrome ChromeDriver for automating the browser changes.

As per the application, The URL will only send a response when is the user is login in into the browser, otherwise, it will return 400 Error

I need to identify post-login if the URL exists or not, I am unable to find any function to call a httpGet request from the IWebDriver driver object

IWebDriver driver = new ChromeDriver();

enter image description here

Thanks in advance.

Mayank
  • 1,351
  • 5
  • 23
  • 42
  • If you are trying to do it from an api call add RestSharp to your solution. – Dazed Feb 14 '21 at 18:09
  • @GregBurghardt It is not a code The Image attached is part of a library that is provided by Selenium. Chrome Webdriver that one can integrate in a C# project and do the automation. – Mayank Feb 17 '21 at 12:27
  • The image in your question is part of a library? I think you are misunderstanding me. The image in your question that has C# code in it. Remove the image from your question and then copy the code you took a screen shot of, and paste it into your question. – Greg Burghardt Feb 17 '21 at 12:45
  • @GregBurghardt Yes, It is the part of the Question, That resides under Webdriver assembly, that is visible at the top of the image. This library is from selenium that has functions that can automate a browser action and my question is related to how to raise an ajax call from the browser using this webdriver Chrome library. – Mayank Feb 17 '21 at 13:17
  • The image confused me. I've never seen anyone take a screenshot of code from "going to definition" on an assembly class before. Not sure why you would need to. What code have you tried? – Greg Burghardt Feb 17 '21 at 13:29
  • Also, type `driver.` and see what Intellisense in Visual Studio comes up with. I bet you'll find the answer there. – Greg Burghardt Feb 17 '21 at 13:29

1 Answers1

0

Got a solution using class WebDriverWait that basically can run a javascript method from the current browser instance.

So what I did is calling a nonsynchronous i.e. async = false while raising XMLHttpRequest from javascript like below

return (function () {
    {
        var result = false;
        try {
            {
                var xhttp = new XMLHttpRequest();
                xhttp.open('GET', '<YOUR GET URL HERE>', false); // last param is async = false
                xhttp.send();
                console.log(xhttp.responseText);
                result = !xhttp.responseText.includes('HTTP ERROR 404');
            }
        } catch (err) {
            {}
        }
        return result;
    }
})()

And calling this javascript method on loop till the timeout (TimeSpan is 5000 seconds) from the browser using the WebDriverWait class' config method and casting to IJavaScriptExecutor like below

IWebDriver driver = new ChromeDriver();
TimeSpan timeToWait = TimeSpan.FromSeconds(5000);
WebDriverWait wait1 = new WebDriverWait(driver, timeToWait);
wait1.Until(d =>
            {
                string url = "<Your GET request URL>";
                bool isURLReachable = (bool)((IJavaScriptExecutor)d).ExecuteScript(String.Format(@"return (function() {{ var result = false; try {{ var xhttp = new XMLHttpRequest(); xhttp.open('GET', '{0}', false); xhttp.send(); console.log(xhttp.responseText); result = !xhttp.responseText.includes('HTTP ERROR 404'); }} catch (err) {{ }} return result;}})()", url));
                    return isURLReachable;
});

This will wait until isURLReachable has true value.

Hope this will help others as well.

Mayank
  • 1,351
  • 5
  • 23
  • 42