Using Selenium WebDriver, Pester, and PowerShell to get and verify the network response after clicking a button
Asked Answered
P

0

1

I managed to use PowerShell 7.1 and Selenium WebDriver and Module to control the Chrome Browser. I need now to access the network response of a post request which will be invoked after the button is clicked from Selenium.

I found some good info here, here, here, and here , however, it is for Python and Java so I need to convert some code to PowerShell. I hope you can help me out.

The code snippet below from one of the above-mentioned sources is where I am having difficulties with:

...
options = webdriver.ChromeOptions()
options.add_argument("--remote-debugging-port=8000")
driver = webdriver.Chrome(ChromeDriverManager().install(), chrome_options=options)

dev_tools = pychrome.Browser(url="http://localhost:8000")
tab = dev_tools.list_tab()[0]
tab.start()
...

Specifically, this part dev_tools = pychrome.Browser(url="http://localhost:8000")

Below is another code snippet I got from one of the above-mentioned sources:

ChromeDriver driver = new ChromeDriver();
DevTools devTool = driver.getDevTools()
devTool.createSession();
devTool.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));
devTool.addListener(Network.responseReceived(), <lamda-function>)

So it is clear the Selenium 4 has support for DevTools, but I am not finding the main C# documentation so that I can use it with PowerShell.

Finally, after accessing the response, I need to verify it using PowerShell Pester.

I appreciate your help.

Preparative answered 7/2, 2022 at 15:46 Comment(12)
What do you have so far? Do you already have a replacement for pychrome? Are you already using something like the selenium-powershell module or just straight C#? This might be an example of what you're looking for: https://mcmap.net/q/162467/-how-to-call-this-function-devtools-selenium-c/7411885Forestation
And see DevTools documentation and examples here - select the csharp tab: selenium.dev/documentation/webdriver/bidirectional/…Forestation
Thanks, I am checking the stack overflow link. For the 2nd link, this is my point. The documentation seems to be WIP. I am unable to find a complete page with up-to-date info. Following that link, I was able to land here github.com/SeleniumHQ/seleniumhq.github.io/tree/dev/…, but not able to find an example from the source about intercepting network post requests.Preparative
I found this link from the dev branch: github.com/SeleniumHQ/seleniumhq.github.io/blob/dev/… which seems it has an example. But why the page is not rendered properly around the code snippet?Preparative
I looked everywhere here, and didn't find that support for network interception was implemented in C#: github.com/SeleniumHQ/selenium/tree/trunk/dotnet/src. I do see that it was implemented in Java only. I think I need to look for the Web Sockets solution instead.Preparative
Actually, after additional round of struggle, I found it. The tests are here: github.com/trabasbaeslur/SeleniumHQ/blob/AutomatedTester-codeql/…. The source code is somewhere here: github.com/trabasbaeslur/SeleniumHQ/tree/AutomatedTester-codeql/…Preparative
Ops, I just noticed that this project github.com/trabasbaeslur/SeleniumHQ is not under the official selenium project. I am lost!Preparative
Are you just having trouble creating the DevTools object? It looks like you can use the logging to check the web requests in a more straightforward way like $logs = $driver.manage().logs().get("performance") when you have network logging enabled as detailed here: https://mcmap.net/q/159642/-how-to-get-http-response-code-using-selenium-webdriverForestation
Will this ...logs().get(...) give me the response of a post request?Preparative
judging from the example in the answer there, the logs should contain detail for each request from chrome to the server (headers, POST data, etc.), and the response code. It's not clear to me if it dumps the entire response though.Forestation
To view the full data sent back from the server (like a web page), the comment says to use $driver.getPageSource(), or $driver.switchTo().frame(), though this would not be specific to an individual request. It probably depends on what you're looking for.Forestation
I need to intercept a network post request and get the response.Preparative

© 2022 - 2024 — McMap. All rights reserved.