发布新日志

  • selenium 使用junit4

    2010-08-20 15:46:35

    Selenium的开发提供的SeleneseTestCaseJunit3风格的,放在JUnit4底下跑,JUnit4Annotation功能就用不起来了。Selenium要启动浏览器,如果用不上@BeforeClass的话,每次启动都初始化一下Selenium,开个IE或者Firefox,这个测试的效率可吃不消(也有比较麻烦的Workaround,但总觉得不是很好)。而甩开SeleneseTestCase的话,又舍不得那个在测试没有通过的时候自动截屏的功能。网络上有人已经有解决方法,我整理如下:、

    编写MyRunListener,继承RunListener

    import org.junit.runner.notification.Failure;

    import org.junit.runner.notification.RunListener;

     

    public class MyRunListener extends RunListener {

        @Override

        public void testFailure(Failure failure) throws Exception {

          

        }

        }

    编写MyRunner

    import org.junit.runner.notification.RunNotifier;

    import org.junit.runners.BlockJUnit4ClassRunner;

    import org.junit.runners.model.InitializationError;

     

    public class MyRunner extends BlockJUnit4ClassRunner {

        private MyRunListener myRunListener;

     

           public MyRunner(Class<?> c) throws InitializationError {

              super(c);

              myRunListener = new MyRunListener();

           }

     

       @Override

        public void run(RunNotifier rn) {

               rn.addListener(myRunListener);

              super.run(rn);

     

        }

    }

    在测试代码中引入

    @RunWith(MyRunner.class)

    public class MyhomeTest extends SeleneseTestCase {

      具体的参与地址是: http://rockhoppertech.com/blogs/archives/45

  • selenium 对表格的验证

    2010-08-18 15:29:12

    通常页面上需要验证一个table里头的值对不对,这个table一般都没有id,所以可以用xpath来解决。

        第一步:识别该table 的某个cell。这里要注意通常一个table的第一行是表头,第二行才是具体的值,selenium识别行列从1开始数的,不是0.所以一个table的第1行第1列的值应该是:

    //table[@class='myTable']/tbody/tr[2]/td[1]

       第二步:加入验证点。

        首先要考虑这个table是不是存在,不存在就直接assertExist返回.

        然后考虑这个table是不是会返回很多行,如果需要验证每一个表格的内容,则用循环来控制:

      String table = "//div/table[@class='main-table']/tbody/tr";
      String tr = "//div/table[@class='main-table']/thead/tr/th";


      int rowNumber = selenium.getXpathCount(table).intValue();
      int lineNumber = selenium.getXpathCount(tr).intValue();


      for (int i = 1; i <= rowNumber; i++) {
        for (int j = 1; j <= lineNumber; j++) {
        String path = table + "[" + i + "]/td[" + j + "]";
        if (selenium.isElementPresent(path)) {
         String data = selenium.getText(path);
         assertNotNull(data);
        }
       }
      }

       第三步:如果是验证table的某列含有某个值:

      selenium.isElementPresent("//td[contains(text(),'my required text for verifying')]")

      也可以用:selenium.getText(path)得到那一列的值再判断:

        boolean exist = data.contains(myexpectData);
        assertEquals(exist, true);

Open Toolbar