zoukankan      html  css  js  c++  java
  • 【Selenium】Selenium1

    一、Selenium1组件

      (1)Selenium服务器,负责启动关闭浏览器;解释和运行从测试程序中传来的Selenium命令;HTTP代理;获取和验证在浏览器和被测试的应用程序之间的传递的HTTP消息

      (2)客户端库文件,各种编程语言和SeleniumRC服务器之间的接口

    二、Selemium文件

      (1)Libs文件夹:Java相关的基础框架

      (2)CHANGELOG:记录Seleniu的变更情况

      (3)Selenium-java-2.25.0.jar:Selenium1和Selenium2的主要API文件

      (4)Selenium-java-2.25.0-srcs.jar:Selenium源码

    三、添加引用

      File→Project Structure→Dependencies→加号→JARS or

    四、驱动Selenium服务器

      java -jar D:workSpacedome1src estselenium-server-standalone-2.49.0.jar

    五、选择浏览器

    public class dome2
    {
    public static void main(String[] args){
    DefaultSelenium selenium=new DefaultSelenium("localhost" ,4444,"*firefox","" +
    "https://www.baidu.com/");
    selenium.start();
    }
    }

     serverHost:服务器的主机名

     serverPort:服务器端口,默认:4444

    browerString:“*浏览器名”,“*ieplore”

    六、Selenium

    (1)浏览器导航操作

    ①open(url)

     public static void main(String[] args){
    DefaultSelenium selenium=new DefaultSelenium("localhost" ,4444,"*firefox","" +
    "https://www.baidu.com/");
    selenium.start();     selenium.open("https://www.baidu.com") }

    ②GoBack(url)

       selenium.goBack();

    ③Refresh(),WindowFocus(),WindowMaximize();Close();

      selenium.WindowFocus();

      selemium.WindowMaixmize();

      selenium.Refresh();

      selemiun.Close();

    (2)操作页面元素

    ①type(locator,value):input类型元素中输入值,用键盘输入,下拉框,列表框,复选框

            参数:locator:元素定位表达式

                   Value:输入值

    selenium.type("id=kw","selenium");

    ②typeKeys(locator,value):模拟键盘一个一个输入,相当调用keyDown、keyUp、KeyPress,汉字,先type设置字段的值,typeKey触发键盘事件

            参数:locator:元素定位表达式

                   Value:输入值

    ③click(locator):单击链接、复选单选框,如有加载+waitForPageToLoad

            参数:locator:元素定位表达式

    selenium.click("name=");

    ④clickAt(locator,coordString)相对坐标

            参数:locator:元素定位表达式

                   coordString:(x,y)(1,1)

    selenium.clickAt("name=","1,1");

    ⑤doubleClick(locator):单击链接、复选单选框,如有加载+waitForPageToLoad或clickAndWait

            参数:locator:元素定位表达式

    ⑥doubleClickAt(locator,coordString)相对坐标

            参数:locator:元素定位表达式

                   coordString:(x,y)(1,1)

    ⑦select(selectLocator,optionLocator):下拉框

            参数:locator:下拉框定位表达式

              optionLocator:下拉框选项的定位表达式 

                    label=文本值,label=文本值(默认)

                    value=真实值,label=3

                    id=id,id=option3

                    index=索引值,从0开始,index=2

    selenium.select("name=rn","每页显示50条");

    ⑧check(locator):复选框、单选框,一般不用

            参数:locator:元素定位表达式

    ⑨uncheck(locator):取消复选框、单选框勾选

            参数:locator:元素定位表达式

    ⑩focus(locator):焦点转移到指定元素

            参数:locator:元素定位表达式

    (3)键盘鼠标模拟操作

    键盘

        altKeyDown():Alt键不放,直到调用altKeyUp()或重新加载新的页面,没有参数

        altKeyUp():松开alt键,没有参数

        controlKeyDown():Ctrl键不放,直到调用ctrlKeyUp()或重新加载新的页面,没有参数

        controlKeyUp():松开Ctrl,没有参数

        shiftlKeyDown():Shift键不放,直到调用stiftKeyUp()或重新加载新的页面,没有参数

          shiftKeyUp():松开Shift,没有参数

        keyDown(locator,keySequence):按下某个键不放,直到调用keyUp()

          locator:元素的定位表达式

          keySequence:要输入字符串,按键的ASCLL码

        keyPress(locator,keySequence):敲击了某个按键

          locator:元素的定位表达式

          keySequence:要输入字符串,按键的ASCLL码

          keyUp(locator,keySequence):松开某个按键

          locator:元素的定位表达式

          keySequence:要输入字符串,按键的ASCLL码

        鼠标

        mouseDown(locator):指定元素上按下鼠标左键不放

          locator:元素的定位表达式

        mouseDownAt(locator,coordString):指定元素上按下鼠标左键不放,相对坐标

          locator:元素的定位表达式

          coordString:(x,y)

        mouseDownRight(locator):指定元素上按下鼠标右键不放

          locator:元素的定位表达式

        mouseDownAt(locator,coordString):指定元素上按下鼠标右键不放,相对坐标

          locator:元素的定位表达式

          coordString:(x,y)

        mouseUp(locator):松开指定元素上按下的鼠标左键

          locator:元素的定位表达式

        mouseUpAt(locator,coordString):松开指定元素上按下的鼠标左键,相对坐标

          locator:元素的定位表达式

          coordString:(x,y)

        mouseUpRight(locator):松开指定元素上按下的鼠标右键

          locator:元素的定位表达式

        mouseUpAt(locator,coordString):松开指定元素上按下的鼠标右键,相对坐标

          locator:元素的定位表达式

          coordString:(x,y)

        mouseOver(locator):鼠标光标移动到指定元素内

          locator:元素的定位表达式

        mouseOut(locator):鼠标光标移动到指定元素外

          locator:元素的定位表达式

                  Ctrl+Alt+C:CtrlKeyDown、altKeyDown、keyDown,然后一个一个keyup

    (4)获取页面元素的内容

    ①getTitel():返回当前网页的标题

    String title = selenium.getTitle();2、

    ②getLocation():获取当前网页的url

    String title2 = selenium.getLocation();

    ③getValue(locator):input元素所存放的值,文本框、复选框、单选框中的值

    ④isEditable(locator):input元素可编辑状态,返回ture、false

    boolean editable = selenium.isEditable("");

    ⑤getTest(locator):元素文本

    ⑥isChecked(locator):input元素可编辑状态,返回ture、false

    boolean editable = selenium.isChecked("id=");

    ⑦getSelectedIndex(selectLocator):索引,0开始

    String selectedIndex = selenium.getSelectedIndex("name=");

    ⑧getSelectedLabel(selectLocator):文本值

    ⑨getSelectedValue(selectLocator):真实值

    ⑩storeSelectedOptions(selectLocator):

          selectLocator:元素的定位表达式   

    String[] selectOptions = selenium.getSelectOptions("name=rn");
    for ( int i=0;i<selectOptions.length;i++){
    System.out.println(selectOptions[i].toString());
    }  

    ⑾getTable(tableCellAddress):表格中某个单元格(td元素)的值,行号、列号从0开始

          tableCellAddress:表格的定位表达式.行号.列号,foo.1.4

    String table = selenium.getTable("xpath/table[1].0.2");

    ⑿getAttribute(attributeLocator):获取属性的值

          attributeLocator:属性的定位表达式+@属性名称,foo@bar

          <input type="submit" onclick="this.checked=1" name="btnK" value="Google 搜索">

    String submit = selenium.getAttribute("name=btnK@type");

    ⒀isTextPresent(pattern):文本是否在存在,返回true或false

          pattern:查找的文本

    boolean present = selenium.isTextPresent("我是文本");

    ⒁isElementPresent(locator):指定元素是否存在,返回true或false

          locator:元素的定位表达式

    boolean exist = selenium.isElementPresent("name=");

    ⒂isVisible(locator):指定元素是否在页面出现,返回true或false

          locator:元素的定位表达式

    boolean present = selenium.isVisible("name=");

    ⒃getXpathCount(locator):符合Xpath表达式的元素的数量

          locator:元素的定位表达式

    Number count = selenium.getXpathCount("xpath");

    (5)设置等待

    ①WaitForPageToLoad(timeout):等待页面加载完毕

          timeout:超时时间,毫秒

    selenium.waitForPageToLoad("2000");

    ②setTimeOut(timeout):设置默认超时时间,对Open()方法有效,默认30s

          timeout:超时时间,毫秒

    ③setSpeed(value):设置测试执行速度,默认0s

          value:各个步骤之间的时间间隔

    (6)测试控制和调试类操作

    ①captureEntirePageScreenshot(filename,kwargs):当前浏览器窗口截图保存为PNG文件,只能firefox、chrome、iexploreproxy使用

          filename:保存路径

          kwargs:保存方式

    ②captureScreenshot(filename):截取整个屏幕内容,不限浏览器

          filename:保存路径

    selenium.captureEntirePageScreenshot("D:\123.png","");

    ③highlight(locator):暂时将元素北京改为黄色,稍后取消

          locator:元素的定位表达式

    selenium.highlight("name=");

    (7)JavaScript弹出对话框的处理

    分为3种

    ①Alert:警告对话框,只有一个确定按钮

    ②Confirmation:确认对话框,确定和取消

    ③Prompt:输入对话框,需要输入内容

    node.js环境配置

    ①http://nodejs.cn/download/下载→安装→cmd:node -v 安装成功

    ②下载插件:setting→plugins→查找node下载→重启→在new project中就多出一项NodeJS 

    ③点击右上角的绿色箭头,启动服务。 出现如下提示”Listening on port 3000”,说明服务启动成功→在浏览器中输入”http://localhost:3000/“,出现下图说明成功 

    ④index.ejs

    <body>
    <input type="button" onclick="alert('这是Alert');"value="Alert"/>
    <br/>
    <input type="button" onclick="confirm('这是Confirmation');"value="Confirmation"/>
    <br/>
    <input type="button" onclick="prompt('这是prompt','');"value="prompt"/>
    <br/>
    </body>

    窗口

    ①IsAlerPresent():是否弹出过提示框,出现返回true,否则else

    selenium.isAlertPresent()

    ②GetAlert():获得提示文本框内容

    selenium.getAlert()

    ③IsConfirmationPresent():是否弹出过确认对话框,出现返回true,否则else

    selenium.isConfirmationPresent()

    ④GetConfirmationPresent():获得确认框文本内容

    selenium.getConfirmationPresent()

    ⑤ChooseOkOnNextConfirmation()和ChooseCancelOnNextConfirmation():确认框点击确定和取消,弹出对话框前就设定先弹出哪个

    selenium.ChooseOkOnNextConfirmation()

    selenium.click("xpath=");

    ⑥IsPromptPresent():是否弹出过选择,出现返回true,否则else

    selenium.isPromptPresent()

    ⑦GetPrompt():获得选择框文本内容

    selenium.getPrompt()

    ⑧AnswerOnNextPrompt(answer):当下一个输入框弹出时,输入参数中指定文本

      answer:要输入的文本

    selenium.answerOnNextPrompt("123456");

    //是否弹出提示框
    System.out.println("提示框弹出:"+selenium.isAlertPresent());
    //点击提示框
    selenium.click("//input[1]");
    //是否弹出提示框
    System.out.println("提示框弹出:"+selenium.isAlertPresent());
    //输出提示框内容
    System.out.println("提示内容:"+selenium.getAlert());
    System.out.println("***********************");

    //是否弹出确认框
    System.out.println("确认框弹出:"+selenium.isConfirmationPresent());
    //点击确认框
    selenium.click("//input[2]");
    //是否弹出提示框
    System.out.println("确认框弹出:"+selenium.isConfirmationPresent());
    //输出提示框内容
    System.out.println("确认内容:"+selenium.getConfirmation());
    System.out.println("***********************");

    //是否弹出选择框
    System.out.println("选择框弹出:"+selenium.isPromptPresent());
    selenium.chooseOkOnNextConfirmation();
    //点击确认框
    selenium.click("//input[3]");
    //是否弹出提示框
    selenium.chooseCancelOnNextConfirmation();
    //输出提示框内容
    selenium.click("//input[3]");
    System.out.println("选择框弹出:"+selenium.isPromptPresent());
    System.out.println("选择内容:"+selenium.getPrompt());

    System.out.println("***********************");
    selenium.answerOnNextPrompt("123456");

    (8)浏览器多窗口处理:弹出子窗口,多个窗口之间进行切换

    ①GetAllWindowIds()、GetAllWindowNames()、GetAllWindowTitles():获得所有窗口ID名称和标题将以字符串数组的形式返回

    点击弹出另一个窗口

    String[] ids = selenium.getAllWindowIds();
    String[] names = selenium.getAllWindowNames();//用name识别
    String[] tittle = selenium.getAllWindowTitles();

    ②WaitForPopUp(windowID,timeout):等待弹出窗口加载完毕

        windowID:窗口定位识别,最好用name

        timeout:超时时间,单位毫秒

    selenium.waitForPopUp(selenium.getAllWindowNames()[1],"15000");

    ③selectWindow(windowID)/selectPopUp(windowID):窗口定位识别,最好用name

    selenium.waitForPopUp(selenium.getAllWindowNames()[1],"15000");
    selenium.selectWindow(selenium.getAllWindowNames()[1]);

    ④OpentWindow(url,windowID)弹出窗口打开新的url

        url:打开的地址

        windowID:窗口定位识别

    selenium.openWindow("www.baidu.com",selenium.getAllWindowNames()[1]);

    (9)结束测试

    ①Close()/Stop()

    selenium.close();//浏览器
    selenium.stop();//selenium界面

    ②关闭selenium服务器

    selenium.shutDownSeleniumServer();
  • 相关阅读:
    进程的实践与练习2
    士兵队列训练问题
    大数相加
    Ignatius and the Princess II
    Parentheses Balance (括号平衡)---栈
    简单计算器
    C++全排列函数next_permutation()和prev_permutation()
    黑白图像
    9*9乘法表
    输入5 个数按从小到大的顺序输出
  • 原文地址:https://www.cnblogs.com/baoyu7yi/p/6992654.html
Copyright © 2011-2022 走看看