51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 3555|回复: 6
打印 上一主题 下一主题

有关Selenium中如何连续执行2个方法

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2011-8-30 11:47:42 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
想问下,有没有人知道如何通过selenium,连续运行1个class中的2个方法?

比如说:
方法1中执行了登录和添加的操作。
方法2中执行了修改的操作。

如何在执行完方法1之后,直接执行方法2,而不需要重新开启1个Selenium?
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

该用户从未签到

2#
发表于 2011-8-30 17:00:56 | 只看该作者
我也是新手,但是如果这个东西要是让我做的话,我会这么做。

单元测试框架中有一个suite概念,一个suite 加载login的登录方法,加载你的添加方法,加载你的修改方法,
是直接调用方法的,没有冲突。
回复 支持 反对

使用道具 举报

该用户从未签到

3#
发表于 2011-9-5 11:54:38 | 只看该作者
两个@Test连在一起就行啦。。
回复 支持 反对

使用道具 举报

  • TA的每日心情
    开心
    2018-9-2 15:58
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]测试小兵

    4#
    发表于 2011-11-17 11:38:45 | 只看该作者
    给个简单的例子吧:
    1)功能代码,也就是你的登录和添加操作的代码:
      //登录
    package MyScript;
    import com.thoughtworks.selenium.*;
    public class Login{
            public void myLogin(Selenium selenium){
                    selenium.open("/");
                    selenium.type("//input[@id='userId' and @value='' and @type='text']", "sgf");
                    selenium.type("id=password", "111111");
                    selenium.type("//input[@id='organization' and @value='' and @type='text']", "sgf");
                    selenium.click("name=submitForm");
            }
    }

    //添加
    package MyScript;

    import com.thoughtworks.selenium.*;

    public class CreateUC
    {
            public void myCreateUC(Selenium selenium){

                    selenium.waitForPageToLoad("30000");
                    System.out.println("CreateUC-1");
                    selenium.selectFrame("mainFrame");
                    selenium.selectFrame("commandlist");
                    selenium.click("//table[@id='urgentcast']/tbody/tr/td[2]/b");
                    selenium.selectFrame("relative=up");
                    selenium.selectFrame("detail");
                    selenium.waitForPageToLoad("30000");
                    selenium.selectFrame("ucToolbar");
                    selenium.click("id=newUrgentLink");
                    selenium.selectFrame("relative=up");
                    selenium.selectFrame("ucFrame");
                    selenium.selectFrame("ucList");
                    selenium.click("link=New Urgent Cast");
                    selenium.selectFrame("relative=up");
                    selenium.selectFrame("ucDetail");
                    selenium.waitForPageToLoad("30000");
                    selenium.type("id=name", "a");
                    //selenium.waitForPageToLoad("10000");
                    selenium.type("id=text", "aaaaaaaaaaaaaa");
                    selenium.select("id=selDurationSecond", "label=03");
                    selenium.selectFrame("relative=up");
                    selenium.selectFrame("relative=up");
                    selenium.selectFrame("ucToolbar");
                    selenium.click("id=saveLink");
                    System.out.println("CreateUC-2");
            }
    }

    //创建一个selenium对象,去调用这两个方法(登录,添加)
    package Test;

    import MyScript.CreateUC;
    import MyScript.Login;

    import com.thoughtworks.selenium.DefaultSelenium;
    import com.thoughtworks.selenium.Selenium;

    import junit.framework.TestCase;

    public class TestMyScript extends TestCase{

            static Selenium selenium = null; //关键,一定要是静态的
            public TestMyScript(String strFunctionName){
                    super(strFunctionName);       
                    if (selenium == null) {//这个判断也很关键
                            selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://192.168.2.100/");
                            selenium.start();
                    }
                   
            }
            public void testLogin(){
                    Login login = new Login();
                    login.myLogin(selenium);
            }
           
            public void testCreateUC(){
                    CreateUC createUC = new CreateUC();
                    createUC.myCreateUC(selenium);
            }       

    }

    //加入testsuite
    package Test;

    import junit.framework.TestSuite;
    import junit.textui.TestRunner;

    public class RunWithTestSuite{
           
            public TestSuite testLogin(){
                    TestSuite suite = new TestSuite();
                    suite.addTest(new TestMyScript("testLogin"));
                    suite.addTest(new TestMyScript("testCreateUC"));
                    return suite;
            }
            public static void main(String[] args){
                    RunWithTestSuite myTestSuite =  new RunWithTestSuite();
                    TestSuite suite = myTestSuite.testLogin();
                    TestRunner.run(suite);
            }
    }
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    5#
    发表于 2011-11-18 09:14:03 | 只看该作者
    suit()会把每个独立的class中的start、stop都执行一次,如果是一个场景内的对象好像有点儿冗余,其实只要反复循环start和stop之间的部分就可以了。只要后台代理启动着,怎么做,做什么,都是你说了算。
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    6#
    发表于 2011-11-18 09:14:58 | 只看该作者
    suit()会把每个独立的class中的start、stop都执行一次,如果是一个场景内的对象好像有点儿冗余,其实只要反复循环start和stop之间的部分就可以了。只要后台代理启动着,怎么做,做什么,都是你说了算。
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    7#
    发表于 2011-11-21 16:16:09 | 只看该作者
    建议参考 seleniumgrid
    回复 支持 反对

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-5-17 22:26 , Processed in 0.076402 second(s), 27 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表