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