简体   繁体   English

如何在Selenium python中提交表单后检测POST?

[英]How to detect POST after form submit in Selenium python?

I'm using Selenium (python bindings, with Django) for automated web app testing. 我正在使用Selenium(python绑定,使用Django)进行自动Web应用程序测试。 My page has a form with a button, and the button has a .click() handler that captures the click (ie the form is not immediately submitted). 我的页面有一个带按钮的表单,按钮有一个.click()处理程序,用于捕获点击(即表单未立即提交)。 The handler runs function2() , and when function2() is done, it submits the form. 处理程序运行function2() ,当function2()完成时,它提交表单。

In my test with Selenium, I fill in the form, then click the Submit button. 在我使用Selenium的测试中,我填写表单,然后单击“提交”按钮。 I want to verify that the form is eventually submitted (resulting in a POST request). 我想验证表单最终是否已提交(导致POST请求)。 The form POSTs and then redirects with GET to the same url, so I can't check for a new url. 表单POST,然后使用GET重定向到相同的URL,因此我无法检查新的URL。 I think I need to check that the POST request occurs. 我想我需要检查POST请求是否发生。 How do I check for successful form submission? 如何检查表单提交是否成功?

Alternatively I could check at the database level to see that some new object is created, but that would make for a bad "unit" test. 或者,我可以在数据库级别检查是否创建了一些新对象,但这会导致错误的“单元”测试。

Ok, so your test suite needs to check something which is not visible to the user. 好的,所以你的测试套件需要检查一些用户看不到的东西。 Here's some methods that come to mind. 这里有一些想到的方法。

Make Your Server Record The Data 让您的服务器记录数据

Use a server that will record POST requests made to it, and record such POSTs so that the testing code can check that the POSTs are performed. 使用将记录对其发出的POST请求的服务器,并记录此类POST,以便测试代码可以检查POST是否已执行。

This is the approach I've used for one of my applications. 这是我用于我的一个应用程序的方法。 I've created a very small and stupid server based on the Express framework of Node.js. 我基于Node.js的Express框架创建了一个非常小而愚蠢的服务器。 It just records POST requests. 它只记录POST请求。

The small and stupid server may not work in all cases. 小而愚蠢的服务器可能无法在所有情况下工作。 If I had to do this with my actual Django codebase, here's what I'd do. 如果我必须使用我的实际Django代码库,这就是我要做的。 I already use a variable named TESTING in my global project settings, which is true when the code is being run by a test suite. 我已经在我的全局项目设置中使用了一个名为TESTING的变量,当代码由测试套件运行时就是如此。 When TESTING is true, I'd make it so that the views that are supposed to receive the POST log what they've received in a place where my test framework can check for it. TESTING为真时,我会这样做,以便应该接收POST的视图记录他们在我的测试框架可以检查它的地方收到的内容。 (It could be a file.) (它可能是一个文件。)

Make Your Client Record The Data 让您的客户记录数据

Modify your JavaScript code to keep a record of what POSTs it makes. 修改您的JavaScript代码以记录它所发布的POST。 Then you could use driver.execute_script to retrieve this information and check for what you'd expect to find if your code ran correctly. 然后,您可以使用driver.execute_script检索此信息,并检查在代码正确运行时您希望找到的内容。 Generally, I would make it so that this kind of recording is done only when the code is being tested. 一般来说,我会这样做,只有在测试代码时才会进行这种记录。 No such recording would happen in production. 在制作中不会发生这样的录音。

Make a Proxy Record The Data 制作代理记录数据

Make the browser started by Selenium connect through a proxy that can records interactions and save them to a file. 使Selenium启动浏览器通过代理连接,该代理可以记录交互并将其保存到文件中。 Then the test suite can check what was recorded. 然后测试套件可以检查记录的内容。 This has the advantage of not needing any changes to your application's codebase. 这样做的好处是不需要对应用程序的代码库进行任何更改。 The only tool I can recommend if you decide to go this way is mitmproxy . 如果您决定采用这种方式,我可以推荐的唯一工具是mitmproxy I've had good success using it to record interactions with the Zotero server so as to avoid hitting it each time I run my test suite. 我用它来记录与Zotero服务器的交互非常成功,以避免每次运行我的测试套件时都能碰到它。

I guess it depends on how deep down the rabbit hole you want to go. 我想这取决于你想要去兔洞的深度。

If you're building a test for the functional side from the user's perspective, and your GET action results in changes on the webpage, then trigger the submit with selenium, then wait for the changes to propagate to the webpage (like waiting for one/more elements to change value or waiting for an element to appear) 如果您正在从用户的角度构建功能方面的测试,并且您的GET操作导致网页上的更改,则使用selenium触发提交,然后等待更改传播到网页(如等待一个/更多元素来改变价值或等待元素出现)

If you want to build an unit test, then all you should be testing is the ability to Submit the data, not also the ability of the javascript code to do a POST request, then a GET then display the data. 如果你想构建一个单元测试,那么你应该测试的是提交数据的能力,而不是javascript代码执行POST请求的能力,然后GET然后显示数据。

If you want to build an integration test, then you will need to check that each individual action in the sequence you described is performed correctly in whatever scenario you deem appropriate and then check that the total result of those actions is as expected. 如果要构建集成测试,则需要检查所描述的序列中的每个单独操作是否在您认为合适的任何场景中正确执行,然后检查这些操作的总结果是否符合预期。 The tricky part will be chaining all those checks together. 棘手的部分将所有这些支票链接在一起。

If you want to build an end to end test, then you need to check for all of the above, plus changes to any permanent storage locations that the code you test changes (like databases or in-memory structures) plus whatever stress/security/usability/performance checks your software needs to pass in your specific context. 如果您想构建端到端测试,那么您需要检查以上所有内容,以及对您测试的代码更改的任何永久存储位置的更改(如数据库或内存中结构)以及任何压力/安全性/可用性/性能检查您的软件需要通过您的特定上下文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM