selenium分解出Util类

在测试拍客的过程中,发现有很多的方法都是公用的,比如登陆、发拍客,评论等,如果每测试一个模块都要写一遍,对程序来说就很繁琐了,就想到把这些方法提出来成为一个Util类,如下

 

package com.jiaoyu.paike;

 

import com.thoughtworks.selenium.*;

import org.junit.After;

import org.junit.Before;

import org.junit.Test;

import java.util.regex.Pattern;

 

public class Paikeshouye extends SeleneseTestCase {

@Before

public void setUp() throws Exception {

selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://jiaxiaoquan.staff.139.com/");

selenium.start();

}

 

@Test

public void testPaikeshouye() throws Exception {

String username = "13948895822";

String passwd = "111111";

this.login(username, passwd);

}

 

/**

* 登陆

*/

public void login(String username, String passwd){

selenium.open("webroot/apps/paike/index.php?s=/Index/index/");

selenium.type("mobile", username);

selenium.type("passwd", passwd);

selenium.click("//input[@value='登 录']");

selenium.waitForPageToLoad("30000");

}

 

 

@After

public void tearDown() throws Exception {

selenium.stop();

}

 

 

}

上面的login,sendPhotos方法是可以提出来的,刚开始的时候直接改成了下面两个类

 

public class Paikeshouye extends SeleneseTestCase {

@Before

public void setUp() throws Exception {

selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://jiaxiaoquan.staff.139.com/");

selenium.start();

}

 

@Test

public void testPaikeshouye() throws Exception {

String username = "13948895822";

String passwd = "111111";

Util u = new Util();

u.login();

 

}

 

 

 

@After

public void tearDown() throws Exception {

selenium.stop();

}

 

 

}

 

 

 

public class Util extends SeleneseTestCase{

/**

* 登陆

*/

public void login(){

        selenium.open("webroot/apps/paike/index.php?s=/Index/index/");

String username = "13948895822";

String passwd = "111111";

selenium.type("mobile", username);

selenium.type("passwd", passwd);

selenium.click("//input[@value='登 录']");

selenium.waitForPageToLoad("30000");

}

 

}

 

在运营的时候,在执行到selenium.open("webroot/apps/paike/index.php?s=/Index/index/");这一句的时候,一直会报java.lang.NullPointerException.不明白为什么,在网上找了很多资料也没有这方面的说明,之后是在网上看了一篇《selenium的私房菜》,写的很好,明白了一些selenium的运行机制,原理,但是就这个问题还是没有调试出来,今天早上灵光突现,改了一下程序,就pass了,下面贴下代码

 

public class Paikeshouye extends SeleneseTestCase {

@Before

public void setUp() throws Exception {

selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://jiaxiaoquan.staff.139.com/");

selenium.start();

}

 

@Test

public void testPaikeshouye() throws Exception {

String username = "13948895822";

String passwd = "111111";

Util u = new Util(selenium);

u.login();

 

}

 

@After

public void tearDown() throws Exception {

selenium.stop();

}

 

}

 

 

 

public class Util extends SeleneseTestCase{

Selenium selenium = null; public Util(Selenium selenium){ this.selenium = selenium; }


 

/**

* 登陆

*/

 

public void login(){

        selenium.open("webroot/apps/paike/index.php?s=/Index/index/");

String username = "13948895822";

String passwd = "111111";

selenium.type("mobile", username);

selenium.type("passwd", passwd);

selenium.click("//input[@value='登 录']");

selenium.waitForPageToLoad("30000");

}

 

}

把Paikeshouye生成的selenium传过来,有点感觉是为什么,但是说不出来,研究的还不够深入,如果有研究selenium的,欢迎一起探讨O(∩_∩)O哈!一个人研究好孤单,遇到问题不知道怎么办,欢迎加我QQ:348539525,注明一下是selenium哦~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值