Using python/selenium and Chrome Devtools Protocol, I am going to my webpage and looking at all the network events that happen in chrome devtools. There are quite a few events that fire when the page loads. I want to assert that the JSON results contain a certain key:value pair and POST method.
Currently with the script I have, it gives me back a few JSON responses. My Python skills are somewhat lacking as I am coming from Ruby. All I really need to do is assert that the response contains something. Maybe I need to convert the response to a string and then do an assertion with selenium, I'm not sure.
Most of the code below came from a tutorial. I know the output function is what is printing the results to my terminal. The tab.set_listener("Network.requestWillBeSent", output)
is coming from Chrome Devtools Protocol. Any help at all would be great on how to assert using selenium that the response body contains a certain string would be great.
This probably isn't too tough, but I just can't seem to figure it out. Many thanks in advance.
import pychrome
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
def output(**kwargs):
print(kwargs)
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()
tab.call_method("Network.enable", _timeout=20)
tab.set_listener("Network.requestWillBeSent", output)
driver.get("https://mywebsite.com")
driver.quit()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…