jmeter测试用例可以处理验证码吗?

发布于 2024-11-28 08:19:27 字数 205 浏览 5 评论 0原文

我们正在尝试构建一个 jmeter 测试用例,它执行以下操作:

  • 登录系统
  • 获取一些信息并检查是否正确。

我们面临的问题是因为登录系统时有验证码。我们计划做的是下载验证码链接并显示,然后等待用户输入值。一旦完成,一切都会如常进行。

我们找不到任何可以执行相同操作的插件?除了自己写插件之外,还有什么选择吗?

We are trying to build a jmeter testcase which does the following:

  • login to a system
  • obtain some information and check whether correct.

Where we are facing issues is because there is a captcha while logging into the system. What we had planned to do was to download the captcha link and display, and wait for user to type in the value. Once done, everything goes as usual.

We couldnt find any plugin that can do the same? Other than writing our own plugin, is there any option here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

五里雾 2024-12-05 08:19:27

我自己解决了。解决方案如下:

  • 创建一个 JSR223 后处理器(使用 Groovy)
  • 更实用的验证码示例,其中包含 JSESSIONID 处理和代理设置,
  • 使用 image.flush() 防止对话框中的验证码图像陈旧

JSR223 代理连接设置参数:

Parameters: proxy 10.0.0.1 8080

其中,以下内容代码显示验证码并等待用户输入

                import  java.awt.Image;
                import  java.awt.Toolkit;
                import  javax.swing.Icon;
                import  javax.swing.JOptionPane;
                
                import org.apache.jmeter.threads.JMeterContextService;
                import org.apache.jmeter.threads.JMeterContext;
                import org.apache.jmeter.protocol.http.control.CookieManager;  
                import org.apache.jmeter.protocol.http.control.Cookie;

                URL urlTemp ;
                urlTemp = new URL( "https://your.domainname.com/endpoint/CAPTCHACode"); 
                HttpURLConnection myGetContent = null;
                if(args[0]=="proxy" ){
                   Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(args[1], Integer.parseInt(args[2])));
                   myGetContent = (HttpURLConnection) urlTemp.openConnection(proxy);
                }else{
                       myGetContent = (HttpURLConnection) urlTemp.openConnection();
                } 
                // false for http GET
                myGetContent.setDoOutput(false);
                myGetContent.connect();
                int status = myGetContent.getResponseCode();
                log.info("HTTP Status Code: "+Integer.toString(status));
                if (status == HttpURLConnection.HTTP_OK) {
                    //We have 2 Set-Cookie headers in response message but 1 Set-Cookie entry in Map
                    String[] parts2;        
                    for (Map.Entry<String, List<String>> entries : myGetContent.getHeaderFields().entrySet()) {
                           if( entries.getKey()  == "Set-Cookie"   ){
                            for (String value : entries.getValue()) {
                               if ( value.contains("JSESSIONID") == true   ){
                                     String[] parts = value.split(";",2);
                                     log.info("Response header: "+ entries.getKey() + " - " +  parts[0] );
                                     JMeterContext context = JMeterContextService.getContext();
                                     CookieManager manager = context.getCurrentSampler().getCookieManager();
                                     parts2 = parts[0].split("=",2)
                                     Cookie cookie = new Cookie("JSESSIONID",parts2[1],"your.domainname.com","/endpoint",true,0, true, true, 0);
                                     manager.add(cookie);
                                     log.info( cookie.toString() );
                                     log.info("CookieCount "+ manager.getCookieCount().toString() );
                                }
                            }                                            
                            }
                       }//end of outer for loop
                           if ( parts2.find() == null ) {
                              throw new Exception("The Response Header not contain Set-Cookie:JSESSIONID=  .");
                          }         
                }else{
                        throw new Exception("The Http Status Code  was ${status} , not expected 200 OK.");
                }
                BufferedInputStream bins = new BufferedInputStream(myGetContent.getInputStream());
                String destFile = "number.png";
                File f = new File(destFile);
                if(f.exists() ) {                         
                    boolean fileDeleted =  f.delete();
                    log.info("delete file ... ");  
                    log.info(String.valueOf(fileDeleted));
                }
                FileOutputStream fout =new FileOutputStream(destFile);
                int m = 0;
                byte[] bytesIn = new byte[1024];
                while ((m = bins.read(bytesIn)) != -1) {
                    fout.write(bytesIn, 0, m);
                }
                fout.close();
                bins.close();
                log.info("File " +destFile +" downloaded successfully");                               
                Image   image = Toolkit.getDefaultToolkit().getImage(destFile);
                image.flush(); // release the prior cache of Captcha image
                Icon icon = new javax.swing.ImageIcon(image);
                JOptionPane pane = new JOptionPane("Enter Captcha", 0, 0, null);
                String captcha = pane.showInputDialog(null, "Captcha", "Captcha", 0, icon, null, null);
                captcha = captcha.trim();
                captcha = captcha.replaceAll("\r\n", "");
                log.info(captcha);                 
                vars.put("captcha", captcha);
                myGetContent.disconnect();

通过 vars.put 方法,我们可以以任何我们想要的方式使用验证码变量。感谢所有试图提供帮助的人。

I was able to solve it myself. The solution is as follows:

  • Create a JSR223 PostProcessor (using Groovy)
  • more practical CAPTCHA example with JSESSIONID handling and proxy setting
  • using image.flush() to prevent stale CAPTCHA image in dialog box

JSR223 Parameters for proxy connection setting:

Parameters: proxy 10.0.0.1 8080

In it, the following code displays the captcha and waits for user input

                import  java.awt.Image;
                import  java.awt.Toolkit;
                import  javax.swing.Icon;
                import  javax.swing.JOptionPane;
                
                import org.apache.jmeter.threads.JMeterContextService;
                import org.apache.jmeter.threads.JMeterContext;
                import org.apache.jmeter.protocol.http.control.CookieManager;  
                import org.apache.jmeter.protocol.http.control.Cookie;

                URL urlTemp ;
                urlTemp = new URL( "https://your.domainname.com/endpoint/CAPTCHACode"); 
                HttpURLConnection myGetContent = null;
                if(args[0]=="proxy" ){
                   Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(args[1], Integer.parseInt(args[2])));
                   myGetContent = (HttpURLConnection) urlTemp.openConnection(proxy);
                }else{
                       myGetContent = (HttpURLConnection) urlTemp.openConnection();
                } 
                // false for http GET
                myGetContent.setDoOutput(false);
                myGetContent.connect();
                int status = myGetContent.getResponseCode();
                log.info("HTTP Status Code: "+Integer.toString(status));
                if (status == HttpURLConnection.HTTP_OK) {
                    //We have 2 Set-Cookie headers in response message but 1 Set-Cookie entry in Map
                    String[] parts2;        
                    for (Map.Entry<String, List<String>> entries : myGetContent.getHeaderFields().entrySet()) {
                           if( entries.getKey()  == "Set-Cookie"   ){
                            for (String value : entries.getValue()) {
                               if ( value.contains("JSESSIONID") == true   ){
                                     String[] parts = value.split(";",2);
                                     log.info("Response header: "+ entries.getKey() + " - " +  parts[0] );
                                     JMeterContext context = JMeterContextService.getContext();
                                     CookieManager manager = context.getCurrentSampler().getCookieManager();
                                     parts2 = parts[0].split("=",2)
                                     Cookie cookie = new Cookie("JSESSIONID",parts2[1],"your.domainname.com","/endpoint",true,0, true, true, 0);
                                     manager.add(cookie);
                                     log.info( cookie.toString() );
                                     log.info("CookieCount "+ manager.getCookieCount().toString() );
                                }
                            }                                            
                            }
                       }//end of outer for loop
                           if ( parts2.find() == null ) {
                              throw new Exception("The Response Header not contain Set-Cookie:JSESSIONID=  .");
                          }         
                }else{
                        throw new Exception("The Http Status Code  was ${status} , not expected 200 OK.");
                }
                BufferedInputStream bins = new BufferedInputStream(myGetContent.getInputStream());
                String destFile = "number.png";
                File f = new File(destFile);
                if(f.exists() ) {                         
                    boolean fileDeleted =  f.delete();
                    log.info("delete file ... ");  
                    log.info(String.valueOf(fileDeleted));
                }
                FileOutputStream fout =new FileOutputStream(destFile);
                int m = 0;
                byte[] bytesIn = new byte[1024];
                while ((m = bins.read(bytesIn)) != -1) {
                    fout.write(bytesIn, 0, m);
                }
                fout.close();
                bins.close();
                log.info("File " +destFile +" downloaded successfully");                               
                Image   image = Toolkit.getDefaultToolkit().getImage(destFile);
                image.flush(); // release the prior cache of Captcha image
                Icon icon = new javax.swing.ImageIcon(image);
                JOptionPane pane = new JOptionPane("Enter Captcha", 0, 0, null);
                String captcha = pane.showInputDialog(null, "Captcha", "Captcha", 0, icon, null, null);
                captcha = captcha.trim();
                captcha = captcha.replaceAll("\r\n", "");
                log.info(captcha);                 
                vars.put("captcha", captcha);
                myGetContent.disconnect();

By vars.put method we can use the captcha variable in any way we want. Thank you everyone who tried to help.

落墨 2024-12-05 08:19:27

由于 CAPTHA 用于检测非人类,JMeter 总是会失败。

您必须在软件中采取解决方法:禁用验证码请求或在页面上的某个位置打印正确的验证码。当然,仅适用于 JMeter 测试。

Since CAPTHA used to detect non-humans, JMeter will always fail it.

You have to make a workaround in your software: either disable captcha requesting or print somewhere on page correct captcha. Of course, only for JMeter tests.

梦明 2024-12-05 08:19:27

肮脏的解决方法?在替代图像中打印验证码值以进行测试。然后您可以检索该值并继续。

Dirty workaround? Print the captcha value in alt image for the tests. And then you can retrieve the value and go on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文