Jsoup做接口测试

最早用Jsoup是有一个小的爬虫应用要写,发现Jsoup较HttpClient轻便多了,API也方便易懂,上手很快,对于response的 Document解析的选择器用的是cssSelector(Jquery)选择器,觉得还不错,后来因为其它原因,没有深入的研究,最近看到一位大神用 这个,我也就再学习了一下,顺便把这个用Jsoup做接口测试的相关知识点发出来给大家参考下!

一. 接口测试的相关知识点准备:

  1.firefox, firebug安装

  2.如何查看请求方式及链接,如下图

  

  我们可以看出,请求的方式是GET, 链接是www.baidu.com

二. Jsoup示例

  1.所使用到的jar包:jsoup-*.jar

  2.示例:(不要使用百度首页来做试验!!!使用百度首页做试验出不来结果的人,不要来问我为什么!!!)  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.io.IOException;
 
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
 
public class Test {
     
     public void testJsop(){
         try {
             Document doc = Jsoup.connect( "http://www.cnblogs.com/zhangfei/p/4283930.html" ).get();
             System.out.println(doc);
         } catch (IOException e) {          
             e.printStackTrace();
         }
     }
     
     public static void main(String[] args) {
         Test t = new Test();
         t.testJsop();
     }
     
}

   输出结果就是当前页面的Document对象,然后再解析这个页面对象就可以了。

三. 参数的处理

扫描二维码关注公众号,回复: 3435085 查看本文章

  1.Get请求中的参数

  

  对于这种参数,一般有两种写法:

  第一种:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Test {
     
     public void testJsop(){
         try {
             Document doc = Jsoup.connect( "http://www.cnblogs.com/zhangfei/p/?page=3" ).get();
             System.out.println(doc);
         } catch (IOException e) {          
             e.printStackTrace();
         }
     }
     
     public static void main(String[] args) {
         Test t = new Test();
         t.testJsop();
     }
     
}

   第二种:  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Test {
     
     public void testJsop(){
         try {
             Connection conn = Jsoup.connect( "http://www.cnblogs.com/zhangfei/p/" );
             conn.data( "page" , "3" );
             Document doc = conn.get();         
             System.out.println(doc);
         } catch (IOException e) {          
             e.printStackTrace();
         }
     }
     
     public static void main(String[] args) {
         Test t = new Test();
         t.testJsop();
     }
     
}

   2.post请求中的参数

   post请求的参数,一般以表单提交居多,我们来看看如何确定参数:

  

  上图中,左边红框框是key, 右边红框框是value,一般情况下,在测试系统中,输入用户名与密码就行了,比如:  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class Test {
     
     public void testJsop(){
         try {
             Connection conn = Jsoup.connect( "https://passport.jd.com/new/login.aspx" );
             conn.data( "loginname" , "test1" );
             conn.data( "loginpwd" , "test1" );
             Document doc = conn.post();        
             System.out.println(doc);
         } catch (IOException e) {          
             e.printStackTrace();
         }
     }
     
     public static void main(String[] args) {
         Test t = new Test();
         t.testJsop();
     }
     
}

 四.响应超时请求处理  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Test {
     
     public void testJsop(){
         try {
             Connection conn = Jsoup.connect( "https://passport.jd.com/new/login.aspx" );
             conn.data( "loginname" , "test1" );
             conn.data( "loginpwd" , "test1" );
             conn.timeout( 30000 );
             Document doc = conn.post();        
             System.out.println(doc);
         } catch (IOException e) {          
             e.printStackTrace();
         }
     }
     
     public static void main(String[] args) {
         Test t = new Test();
         t.testJsop();
     }
     
}

 五.cookies的管理

  在我们登录后,保存此次登录的cookies,这样,后续的操作才可以继续进行,Jsoup提供了很简便的方式来保存cookies  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class Test {
     
     public void testJsop(){
         try {
             Connection conn = Jsoup.connect( "https://passport.jd.com/new/login.aspx" );
             conn.data( "loginname" , "test1" );
             conn.data( "loginpwd" , "test1" );
             conn.timeout( 30000 );           
             conn.method(Method.POST);
             Response response = conn.execute();
             Map<String, String> cookies = response.cookies();
             Document doc = Jsoup.connect( "http://order.jd.com/center/list.action" )
                 .cookies(cookies)
                 .timeout( 30000 )
                 .get();
         } catch (IOException e) {          
             e.printStackTrace();
         }
     }
     
     public static void main(String[] args) {
         Test t = new Test();
         t.testJsop();
     }
     
}

 上面的示例中,可以看出,要取得cookies,必须要有个Response的对象,所以,要用execute方法,如果直接用post方面,返回的则是Document对象,但在用execute方法时,要事先调用一下method方法设定好请求方式即可。

原文:https://www.cnblogs.com/zhangfei/p/4359408.html

猜你喜欢

转载自www.cnblogs.com/peachh/p/9746443.html