Jmeter可以处理验证码的测试用例



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

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

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

我们找不到任何插件可以做同样的?除了编写我们自己的插件,还有其他选择吗?

我自己解决了这个问题。解决方案如下:

  • 创建JSR223 PostProcessor(使用Groovy)
  • 使用JSESSIONID处理和代理设置的更实用的CAPTCHA示例
  • 在对话框中使用image.flush()来防止过期的CAPTCHA图像

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("rn", "");
                log.info(captcha);                 
                vars.put("captcha", captcha);
                myGetContent.disconnect();

的var。Put方法,我们可以以任何我们想要的方式使用captcha变量。谢谢大家的帮助。

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

你必须在你的软件中做一个解决方案:要么禁用captcha请求,要么在页面上正确打印captcha。当然,仅适用于JMeter测试。

肮脏的变通方法?在测试的所有图像中打印验证码值。然后你可以检索值然后继续

相关内容

最新更新