美文网首页
界面自动化

界面自动化

作者: 阿登20 | 来源:发表于2018-10-22 20:06 被阅读105次

    [界面自动化代码操作](http://192.168.3.13:8081/rendj/testdemo.html

    image.png
    []
    package com.guoyasoft.autoUI.firstDemo;
    
    import com.guoyasoft.autoUI.common.BaseUI;
    import java.util.List;
    import org.openqa.selenium.By;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.Keys;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.interactions.Action;
    import org.openqa.selenium.interactions.Actions;
    import org.openqa.selenium.support.ui.Select;
    import org.testng.annotations.Test;
    
    public class MyUITest3 extends BaseUI {
      /*@Test
       public void testNavigate(){
         //打开网页
         driver.get("http://www.baidu.com");
         sleep(1000);
         // 使用导航栏打开京东网页
         driver.navigate().to("http://www.jd.com");
         sleep(1000);
         //导航栏后退
         driver.navigate().back();
         sleep(1000);
         //导航栏前进
         driver.navigate().forward();
         sleep(1000);
         //导航栏刷新
         driver.navigate().refresh();
         sleep(1000);
       }
       */
      @Test
      public void testText(){
        driver.get("http://192.168.3.13:8081/rendj/testdemo.html");
        sleep(1000);
    //元素定位
        WebElement test = driver.findElement(By.id("edit"));
        //清空
        test.clear();
        //赋值
        test.sendKeys("我是谁我在那");
        //等待1秒
        sleep(1000);
    
      }
      @Test
      public void testFile(){
        driver.get("http://192.168.3.13:8081/rendj/testdemo.html");
        sleep(1000);
        WebElement file = driver.findElement(By.name("attach[]"));
        file.sendKeys("G:\\swagger.txt");
        sleep(1000);
      }
      @Test
      public void testRadio(){
        driver.get("http://192.168.3.13:8081/rendj/testdemo.html");
        sleep(2000);
        //定位元素 定位到了多个 findElment后面加个s
        List<WebElement> radios = driver.findElements(By.name("company"));
        //List容器 WebElement类型 变量 radios
        // findElements 定位元素 定位到了多个 前面用s
        for(int i=0;i<radios.size();i++){ // radios.size() 变量的长度
          radios.get(i).click();  //radios.get(i) 取下标
          sleep(2000);
    
        }
    
    
    
      }
      @Test
      public void testCheckBOX(){
        driver.get("http://192.168.3.13:8081/rendj/testdemo.html");
        sleep(2000);
        List<WebElement> checkboxs = driver.findElements(By.name("course"));
        for(int i=0;i<checkboxs.size();i++){
          checkboxs.get(i).click();
          sleep(2000);
    
        }
      }
      @Test
      public void testCheckBOX1(){
        driver.get("http://192.168.3.13:8081/rendj/testdemo.html");
        sleep(2000);
        List<WebElement> checkboxs = driver.findElements(By.name("course"));
        //for循环 声明 WebElement类 checkbox变量
        //每循环一次从容器checkboxs里面取值
        for (WebElement checkbox:checkboxs) {
          checkbox.click();// 点击
          sleep(2000);
        }
    
      }
      @Test
      //
      public void testRadio1(){
        driver.get("http://192.168.3.13:8081/rendj/testdemo.html");
        sleep(2000);
        List<WebElement> testRadios = driver.findElements(By.name("company"));
        for(int i=0;i<testRadios.size();i++){
          //testRadios.get(i) 用get取下标
          String value = testRadios.get(i).getAttribute("value");//getAttribute获取属性的值
          if (value.contains("AliBaBa")){
            testRadios.get(i).click();
            sleep(1000);
          }else if (value.contains("Baidu")){
            testRadios.get(i).click();
            sleep(2000);
    
          }
    
    
        }
      }
      @Test
      //if 结合 elseif 选判定前面 前面满足后面直接终止
      public void testIf(){
        int a =10;
        if (a<5){
          System.out.println("a = "+ a +" 小于5");
        }else if (a <10){
          System.out.println("a = "+ a +" 小于10");
        }else if (a <20){
          System.out.println("a = "+ a +" 小于20");
        }else if (a <100){
          System.out.println("a = "+ a +" 小于100");
        }
    
      }
      @Test
    //if单独独立 每个都执行
      public void testIf1(){
        int a =10;
        if (a<5){
          System.out.println("a = "+ a +" 小于5");
        }if (a <10){
          System.out.println("a = "+ a +" 小于10");
        }if (a <20){
          System.out.println("a = "+ a +" 小于20");
        }if (a <100){
          System.out.println("a = "+ a +" 小于100");
        }
      }
      @Test
    //时间控件web F12 console 里面编写
    //document.getElementsByName("startTime")[0].value="2018-10-22";
      public void testDate(){
        driver.get("http://192.168.3.13:8081/rendj/testdemo.html");
        sleep(2000);
        String js="document.getElementsByName(\"startTime\")[0].value=\"2018-10-22\"";
    //强制js类型转换
        //Executor 强制器
        JavascriptExecutor jsDriver = (JavascriptExecutor)driver;
        jsDriver.executeScript(js);
        sleep(2000);
      }
      @Test
    //promptbutton
      public void testPrompt(){
        driver.get("http://192.168.3.13:8081/rendj/testdemo.html");
        sleep(2000);
        WebElement prompt = driver.findElement(By.xpath("//input[@name=\"promptbutton\"]"));
        prompt.click();
        sleep(2000);
        //switchto/切换到 alert弹框 accept 确定  dismiss 取消
        driver.switchTo().alert().accept();
        sleep(2000);
    
      }
      @Test
    
      public void testTextArea(){
        driver.get("http://192.168.3.13:8081/rendj/testdemo.html");
        sleep(2000);
        //                                          通过标签定位
        WebElement textarea = driver.findElement(By.tagName("textarea"));
        textarea.clear();
        sleep(2000);
        textarea.sendKeys("我是谁,我来自哪里");
        sleep(2000);
      }
      @Test
    //下拉框
      public void testSelect(){
        driver.get("http://192.168.3.13:8081/rendj/testdemo.html");
        //先找select标签
        //再找option标签,并选择
        //把select标签封装成Select对象(封装了找select下面所有option的操作)
        //通过下标序号 value、展现文本
        sleep(2000);
        //通过id找
        WebElement selector = driver.findElement(By.id("Selector"));
        //Select S大写 选择selenium那个
        Select select =new Select(selector);
        sleep(2000);
        //通过下标选
        select.selectByIndex(1);
        sleep(2000);
        //通过value选
        select.selectByValue("banana");
        sleep(2000);
        //通过展示文本选
        select.selectByVisibleText("葡萄");
        sleep(2000);
    
      }
      @Test
    //超链接
      public void testA(){
        driver.get("http://192.168.3.13:8081/rendj/testdemo.html");
        sleep(2000);
        //  下面是精确点击使用linkText:按链接的文本精确匹配  linkText文本链接
        //WebElement a = driver.findElement(By.linkText("Copyright 2017 guoyasoft"));
        // 当前界面打开
        //a.click();
        //模糊点击 使用partialLinkText:按链接的文本模糊匹配
        WebElement a = driver.findElement(By.partialLinkText("guoyasoft"));
    //Actions选selenium这个
        Actions actions = new Actions(driver);
        //shift+点击:新窗口打开(新开一个浏览器)
        //actions.keyDown(Keys.SHIFT).click(a).keyUp(Keys.SHIFT).perform();
        //ctrl+点击:新窗口打开(新开一个浏览器)
        actions.keyDown(Keys.CONTROL).click(a).keyUp(Keys.CONTROL).perform();
        sleep(2000);
    
      }
    }
    
    
    
    

    引用一下小吴小吴老师的简书
    webdriver基础知识汇总

    一、打开chrome浏览器

    1. 安装chrome浏览器

    2. 下载控制chrome的驱动器

    chrome的版本和chromedriver的版本对应关系和下载地址
    https://blog.csdn.net/huilan_same/article/details/51896672

    存放路径:
    /工程名/src/main/resources/selenium/driver/chromedriver.exe

    3. 下载selenium的jar包

    pom.xml添加dependency

    <project>
        <dependencies>
            <!--selenium框架 -->
            <dependency>
                <groupId>org.seleniumhq.selenium</groupId>
                <artifactId>selenium-java</artifactId>
                <version>2.50.0</version>
            </dependency>
    
            <!--testNG测试框架 -->
            <dependency>
                <groupId>org.testng</groupId>
                <artifactId>testng</artifactId>
                <version>6.8</version>
            </dependency>
        </dependencies>
    </project>
    
    

    4. 新建java类,启动浏览器

    自写的类——>selenium——>chromedriver.exe——>chrome浏览器

            //此处src前面没有"/",说明是相对工程根目录的路径
            System.setProperty("webdriver.chrome.driver",
            "src/main/resources/selenium/driver/chromedriver.exe");
            WebDriver driver = new ChromeDriver();
    
    

    如果要窗口最大化,先设置参数,启动的时候传入

            //设置环境变量,指定chromedriver的路径
            System.setProperty("webdriver.chrome.driver",
                    "src/main/resources/selenium/driver_v236_63_65/chromedriver.exe");
    
            //设置浏览器的参数
            ChromeOptions options = new ChromeOptions();
            //最大化浏览器
            options.addArguments("--test-type", "--start-maximized");
            //指定浏览器位置
                //options.setBinary("C:/XXXXXXX/chrome.exe");
            //打开浏览器
            WebDriver driver = new ChromeDriver(options);
    
    

    常见报错原因:

    1. 浏览器和chromedriver版本不一致
    2. 防火墙导致无法访问chrome浏览器,关闭防火墙

    5. 关闭浏览器

            //先线程休眠3秒,便于观察,然后才关闭,不然启动就关闭像闪退
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //关闭浏览器,driver.close()是关闭当前窗口
            driver.quit();
    
    

    sleep()方法:

        public static void sleep(int millis) {
            try {
                Thread.sleep(millis);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
    

    二、地址栏和导航(前进、后退、刷新)

    1. get()方法打开

            driver.get("http://www.baidu.com");
    
    

    2. navigate().to()打开

    driver.navigate().to("http://www.dangdang.com");
    
    

    3. navigate导航

            // 1\. 先打开一个界面
            driver.navigate().to("http://www.baidu.com");
    
            //2\. to()方法再打开另一个界面
            driver.navigate().to("http://www.dangdang.com");
            sleep(2000);
            //3\. back()回退
            driver.navigate().back();
            sleep(2000);
            //4\. forward()前进
            driver.navigate().forward();
            sleep(2000);
            //5\. refresh()刷新
            driver.navigate().refresh();
    
    

    三、4种常见方式定位元素

    元素包含信息:

    1. 标签
    2. 属性
    3. 内容
    4. 位置

    1. 按id属性定位元素

            WebElement alertButton = driver.findElement(By.id("alertButtonId"));
    
    

    2. 按name属性定位元素

            WebElement alertButton = driver.findElement(By.name("alertButtonName"));
    
    

    3. 按class定位元素

            WebElement buttons = driver.findElements(By.className("alertButtonClass"));
    
    

    4. 使用xpath定位

    1)自动化处理的对象:标签(也称为元素)

    java html
    WebElement元素类 标签(如html、body、head、table、input、tr、alert弹出框等等)

    2) 标签

    1. 标签名
    2. 标签的属性
      • 为了定位标签的属性:id、name、class、type
      • 为了产生交互效果的属性:触发事件,可以指定触发后要执行的方法
    3. 要标识的数据:标签都是为了描述数据的

    2)xpath:选择html标签

    符号 用途 示例
    / 绝对路径 /html/body/table/tbody/tr/td/input
    // 相对路径 //body/table//input
    标签名 html的所有标签 //input
    [] 限定t条件 //input[@id='xxxid' and @type='button']
    数字 指定匹配到的第几个 //input[3]
    @属性名=属性值 通过属性限定条件
    函数() 通过函数限定条件
    and/or 连接多个条件
    WebElement alertButton = driver.findElement(
                By.xpath("//input[@id='alertButtonId' and @type='button']"));
    
    

    四、常见元素的基本操作

    0. 界面模板

    <html>
        <head>
            <title>导航栏</title>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
        </head>
    
        <body>
            xxxxxx替换元素代码xxxxxxx
        </body>
    
    </html>
    
    

    1. text文本框

    界面:
    <input type="text" name="edit" id="edit" value="" />
    
    
    自动化代码

    sendkeys()传要填的内容

            WebElement text=
            driver.findElement(By.xpath("//input[@type='text' and @id='edit']"));
            text.clear();
            text.sendKeys("傻不傻?傻!");
    
    

    2. file文件上传

    界面:
    <input type="file" name="attach[]" />
    
    
    自动化代码

    找到元素,sendkeys()传文件路径

            WebElement input=
            driver.findElement(By.xpath("//input[@type='file' and @name='attach[]']"));
            input.clear();
            input.sendKeys("C:/HtmlWeb/selenium/html_00_summary.html");
    
    

    3. radio单选框

    界面:
    <input type='radio' name="company" value='Baidu' /> <label>百度</label> <br/> 
    <input type='radio' name="company"  value="AliBaBa"/> <label>阿里巴巴</label><br/>
    <input type='radio' name="company" value='Tencent' checked /><label>腾讯</label><br/> 
    <input type='radio' name="company" value='Mi' /> <label>小米</label>
    
    
    自动化代码

    input元素,类型是raidio,name相同的多个radio类型的input组成选项,靠value进行区分

    选择具体某个选项,并选择:

            WebElement radio=
            driver.findElement(
                    By.xpath("//input[@type='radio' and @name='company' and @value='Mi']"));
            radio.click();
    
    

    所有选项都点一遍:

            List<WebElement> radios=
            driver.findElements(By.xpath("//input[@type='radio' and @name='company']"));
            for(int i=0;i<radios.size();i++){
                WebElement item=radios.get(i);
                sleep(1000);
            }
    
    

    4. checkbox多选框

    界面:
    <input type="checkbox" name="course" value="web" /><label>网络</label><br />
    <input type="checkbox" name="course" value="training" /><label>培训</label><br />
    <input type="checkbox" name="course" value="friend" /><label>朋友介绍</label><br />
    <input type="checkbox" name="course" value="other" /><label>其他方式</label>
    
    
    自动化代码

    input元素,类型是checkbox,name相同的多个checkbox类型的input组成选项,靠value进行区分

    选择具体某一个选项,并选择:

            WebElement checkbox=
            driver.findElement(
                    By.xpath("//input[@type='checkbox' and @name='course' and @value='web']"));
            checkbox.click();
    
    

    所有选项都勾选:

            List<WebElement> checkboxs=
            driver.findElements(
                    By.xpath("//input[@type='checkbox' and @name='course']"));
            for(int i=0;i<checkboxs.size();i++){
                WebElement item=checkboxs.get(i);
                item.click();
                sleep(1000);
            }
    
    

    5. 时间控件

    界面:
    <input type="date" name="startTime">
    
    
    自动化代码

    先写JavaScript代码,然后通过driver执行js
    js第一句是将只读属性去掉(若时间没设只读,则不需要)
    js第二句是给时间元素设置value属性,值为“2018-04-10”

    String js="document.getElementsByName('startTime')[0].removeAttribute('readOnly');document.getElementsByName('startTime')[0].setAttribute('value','2018-04-10');";
            JavascriptExecutor jsDriver = (JavascriptExecutor) driver;
            jsDriver.executeScript(js);
    
    

    6. button按钮

    界面:
    <input type="button" name="promptbutton"
           value="测试prompt对话框" onclick="confirm('确定提交吗?');" />
    
    
    自动化代码

    找到元素,click()点击

            WebElement button=
            driver.findElement(By.xpath("//input[@type='button' and @id='alertButtonId']"));
            button.click();
            Alert alert=driver.switchTo().alert();
            alert.accept();
    
    

    7. 文本域

    界面:

    多行多列的输入框

    <textarea rows="3" ></textarea>
    
    
    自动化代码
            WebElement textarea=driver.findElement(By.xpath("//textarea[@rows='3']"));
    textarea.clear();
    textarea.sendKeys(“内容”);
    
    

    8. img图片

    界面:

    可点击的图片,都是外面有一层<a>超链接,只是用图片替代了文本
    自动化测试的时候,要定位的是<a>超链接

    <a id='imgA'>
      <img src="xxxx">
    </a>
    
    
    自动化代码
    1. 定位<a>标签
    2. 点击
            WebElement img=driver.findElement(By.xpath("//a[@id='imgA']"));
            img.click();
    
    

    7. select选择框

    界面:

    select标签:定义一个下拉框
    option选项:定义一个选项,一个下拉框可以有很多个选项,即多个option
    option的3个属性:index(选项序号,默认自动加上的)、value选项值、visibleText展现文字

    <select id="Selector">
            <option value="apple" >苹果</option>
            <option value="peach" >桃子</option>
            <option value="banana" >香蕉</option>
            <option value="orange">桔子</option>
            <option value="grape" >葡萄</option>
            <option value="mango" >芒果</option>
    </select> 
    
    
    自动化代码
    1. 先找select标签
    2. 再找option标签,并选择
      • 把select标签封装成Select对象(封装了找select下面所有option的操作)
      • 通过value、展现文本、序号
            WebElement selectEle=driver.findElement(By.xpath("//select[@id='Selector']"));
            Select select=new Select(selectEle);
            select.selectByIndex(0);
            sleep(1000);
            select.selectByValue("banana");
            sleep(1000);
            select.selectByVisibleText("桔子");
            sleep(1000);
    
    

    8. a超链接

    界面:
    <a href="http://www.guoyasoft.com">Copyright 2017 guoyasoft</a>
    
    
    自动化代码

    定位超链接的3中方法:

    1. 使用xpath
    WebElement link=
            driver.findElement(By.xpath("//a[@href='http://www.guoyasoft.com']"));
    
    
    1. 使用linkText:按链接的文本精确匹配
    WebElement link=driver.findElement(By.linkText("Copyright 2017 guoyasoft"));
    
    
    1. 使用partialLinkText:按链接的文本模糊匹配
    WebElement baike=driver.findElement(By.partialLinkText("guoyasoft"));
    
    

    三种点击方式:1、 当前界面打开;2、新的标签页打开;新的窗口打开

    直接点击:当前界面打开

            WebElement link=
            driver.findElement(By.xpath("//a[@href='http://www.guoyasoft.com']"));
            link.click();
    
    

    ctrl+shift+点击:当前浏览器的新标签页打开

            Actions actions=new Actions(driver);
            actions.keyDown(Keys.SHIFT).keyDown(Keys.CONTROL).click(link).perform();
    
    

    shift+点击:新窗口打开(新开一个浏览器)

            Actions actions=new Actions(driver);
            actions.keyDown(Keys.SHIFT).click(link).perform();
    
    

    五、alert框切换

    界面:
                <tr>
                    <td>prompt对话框</td>
                    <td><input type="button" name="promptbutton"
                        value="测试prompt对话框" onclick="clickbutton();" /></td>
                </tr>
    
    

    javascript:

        function clickbutton() {
                var name = prompt("测试prompt对话框", "");
                if (name != null && name != "") {
                    //document.write(name);
                    alert(name);
                }
            }
    
    
    自动化代码
           WebElement clickOnPrompt = driver.findElement(By
                   .xpath("//td/input[@name='promptbutton']"));
           clickOnPrompt.click();
                   try {
               Thread.sleep(2000);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }   
           Alert prompt = driver.switchTo().alert();
                   try {
               Thread.sleep(2000);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }   
           prompt.sendKeys("I love Selenium");
           prompt.accept();
                   try {
               Thread.sleep(2000);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }   
           Alert afterAccept = driver.switchTo().alert();
           afterAccept.accept();
               try {
               Thread.sleep(2000);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }   
    
    

    六、窗口切换

    界面:
                <tr>
                    <td>超链接</td>
                    <td>
                        <div>
                            <a id="link_baidu" href="https://www.baidu.com">百度</a>
                        </div>
                        <div>
                            <a id="link_jd" href="https://www.JD.com">京东</a>
                        </div>
                        <div>
                            <a id="link_dangdang" href="http://www.dangdang.com/">当当</a>
                        </div>
                    </td>
                </tr>
    
    
    自动化代码

    核心代码:

    1. driver.getWindowHandle()获取当前窗口句柄
    2. driver.getWindowHandles()获取浏览器所有窗口句柄
    3. driver.switchTo().windows(目标句柄)
    4. 根据窗口的title判断选择的窗口是否正确(先切换控制,再查titile)
        public static void switchToWindow(String windowTitle, WebDriver dr) {
            // 将页面上所有的windowshandle放在入set集合当中
            String currentHandle = dr.getWindowHandle();
            Set<String> handles = dr.getWindowHandles();
            for (String s : handles) {
                dr.switchTo().window(s);
                // 判断title是否和handles当前的窗口相同
                if (dr.getTitle().contains(windowTitle)) {
                    break;// 如果找到当前窗口就停止查找
                }
            }
        }
    
    

    实践测试:

    1. 打开测试界面
    2. 打开京东,切回原窗口
    3. 打开百度,切回原窗口
    4. 打开当当,切回原窗口
        private void testWindow(WebDriver driver, TestSelenium3 test) {
            /*
             * 第1步:打开测试界面
             */
            driver.get("http://127.0.0.1:8081/HtmlWeb/selenium/html_00_summary.html");
            Actions actions = new Actions(driver);
    
            /*
             * 第2步:点击京东,再切换回原界面
             */
            WebElement jd = driver.findElement(By.xpath("//a[@id='link_jd']"));
            //按顺序点,按顺序放
            actions.keyDown(Keys.SHIFT).keyDown(Keys.CONTROL).click(jd)
                    .keyUp(Keys.SHIFT).keyUp(Keys.CONTROL).perform();
            test.mySleep(1000);
    
            //窗口切换到京东,进行操作,此处不做任何操作
            switchToWindow("京东", driver);
            test.mySleep(1000);
    
            //切换回原窗口
            switchToWindow("selenium", driver);
            test.mySleep(1000);
    
            /*
             * 第3步:点击百度,再切换回原界面
             */
            WebElement baidu = driver
                    .findElement(By.xpath("//a[@id='link_baidu']"));
            actions.keyDown(Keys.SHIFT).keyDown(Keys.CONTROL).click(baidu)
                    .keyUp(Keys.SHIFT).keyUp(Keys.CONTROL).perform();
            actions.click();
            test.mySleep(1000);
    
            switchToWindow("百度一下,你就知道", driver);
            test.mySleep(1000);
    
            switchToWindow("selenium", driver);
            test.mySleep(1000);
            /*
             * 第4步:点击当当,再切换回原界面
             */
            WebElement dangdang = driver.findElement(By
                    .xpath("//a[@id='link_dangdang']"));
            actions.keyDown(Keys.SHIFT).keyDown(Keys.CONTROL).click(dangdang)
                    .keyUp(Keys.SHIFT).keyUp(Keys.CONTROL).perform();
            actions.click();
            test.mySleep(1000);
            switchToWindow("当当", driver);
            test.mySleep(1000);
    
            switchToWindow("selenium", driver);
            test.mySleep(1000);
    
        }
    
    

    七、切换界面框架frame

    1. 界面代码

    1.1 main.html

    <html>
    <head>
    <title>iframe测试界面</title>
    </head>
    <frameset rows="15%,75%,*" frameborder="1" framespacing="10">
        <frame src="top.html"></frame>
        <frameset cols="20%,*">
            <frame src="left.html"></frame>
            <frame src="right.html" name="content"></frame>
        </frameset>
        <frame src="button.html"></frame>
    </frameset>
    </html>
    
    

    1.2 top.html

    <html>
    <body>
     <!--图片放到webapp/images下面-->
        <img src="../../images/top.png" width="90%" height="80%">
    </body>
    </html>
    
    

    [图片上传失败...(image-59c914-1540209676324)]

    1.3 left.html

    <html>
    
    <body>
        <ul>
            <li><a href="http://www.baidu.com" target="content">百度</a></li>
            <li><a href="http://www.jd.com" target="content">京东</a></li>
            <li><a href="http://www.taobao.com" target="content">淘宝</a></li>
            <li><a href="http://www.dangdang.com" target="content">当当</a></li>
            <li><a href="http://www.youku.com" target="content">优酷</a></li>
        </ul>
    </body>
    </html>
    
    

    1.4 right.html

    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>管理页面</title>
    </head>
    <body>该界面用于展示菜单内容
    </body>
    </html>
    
    

    1.5 button.html

    <html>
    
    <body>
    
        <img src="../../images/button.png" width="90%" height="75%" >
    </body>
    </html>
    
    

    [图片上传失败...(image-6b3062-1540209676324)]

    2. frame测试代码

    第1步:打开测试界面

    driver.get("http://127.0.0.1:8081/HtmlWeb/selenium/iframe2/main.html");
    
    

    第2步:找到左边导航frame框

    WebElement leftFrame=driver.findElement(By.xpath("//frame[@src='left.html']"));
    
    

    第3步:切换控制权到frame窗口

    driver.switchTo().frame(leftFrame);
    
    

    第4步:测试frame框里的内容

    WebElement baidu=driver.findElement(By.xpath("//a[@href='http://www.baidu.com']"));
                baidu.click();
                test.mySleep(2000);
    
    

    第5步:将控制窗口切换回原主窗口

    driver.switchTo().defaultContent();
    
    

    第6步:定位右窗口,即点击连接后打开的内容

    WebElement rightFrame=driver.findElement(By.xpath("//frame[@src='right.html']"));
    
    

    第7步:切换到右边的frame窗口

    driver.switchTo().frame(rightFrame);
    
    

    第8步:测试右窗口

                test.mySleep(3000);
                WebElement input=driver.findElement(By.xpath("//input[@id='kw']"));
                input.clear();
                input.sendKeys("果芽软件");
    
                WebElement submit=driver.findElement(By.id("su"));
                submit.click();
                test.mySleep(2000);
    
                //定位超链接元素的专用方法(精确和模糊两种,类似id和name选择器)
                WebElement baike=driver.findElement(By.linkText("上海果芽软件科技有限公司_百度百科"));
                //WebElement baike=driver.findElement(By.partialLinkText("果芽软件"));
    
                baike.click();
                test.mySleep(2000);
    
    

    八、设置界面加载和元素定位等待时间

    1. 线程休眠

            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
    
    

    2. 隐式等待

            //设置界面加载等待时间,全局设置,作用于driver,对所有后续界面加载都有效
            driver.manage().timeouts().pageLoadTimeout(3000, TimeUnit.MILLISECONDS);
            driver.get("http://www.baidu.com");
            //设置元素定位超时等待时间,全局设置,作用于driver,对所有后续元素定位都有效
            driver.manage().timeouts().implicitlyWait(3000, TimeUnit.MILLISECONDS);
            WebElement element=driver.findElement(By.xpath("kw"));
    
    

    3. 显示等待

            WebDriverWait wait=new WebDriverWait(driver, 2);
            wait.until(new ExpectedCondition<Boolean>() {
                public Boolean apply(WebDriver d) {
                    boolean loadcomplete = d.findElement(By.xpath("")).isDisplayed();
                    return loadcomplete;
                    }
                    });
    
    

    常见错误

    image.png

    相关文章

      网友评论

          本文标题:界面自动化

          本文链接:https://www.haomeiwen.com/subject/lwcnzftx.html