zoukankan      html  css  js  c++  java
  • java selenium webdriver第四讲 应用小结

    部分api

    1.访问网站

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

    或者

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

    2.操作浏览器窗口

    //声明一个point对象,两个150表示浏览器的位置相对于屏幕的左上角(0,0)的横坐标距离和纵坐标距离 
    Point point = new Point(150, 150);
    //声明dimension对象,两个500表示浏览器窗口的长度和宽度
     Dimension dimension = new Dimension(500, 500);
    //设定浏览器窗口的大小为长500 宽500
     driver.manage().window().setSize(dimension);
    //最大化浏览器
     driver.manage().window().maximize();

    3.浏览器输入值 和点击

     driver.navigate().to("http://www.baidu.com");
     driver.findElement(By.id("kw")).clear();
    driver.findElement(By.id("kw")).sendKeys("魔兽");
    driver.findElement(By.id("su")).click();

    4.操作多选的选择列表

    Select dropSelect = new Select(driver.findElement(By.name("fruit")));
    //判断页面是否进行多选
     Assert.assertTrue(dropSelect.isMultiple());
     //使用选择项索引选择第三个选项
     dropSelect.selectByIndex(3);
     //根据value值选择
     dropSelect.selectByValue("value");
      //根据选项文字选择
      dropSelect.selectByVisibleText("苹果");
      //取消所有选项的选中状态
     dropSelect.deselectAll();
     //取消第三个的选中状态
     dropSelect.deselectByIndex(3);
     //根据value值取消选择
     dropSelect.deselectByValue("value");
     //根据选项文字取消选择
     dropSelect.deselectByVisibleText("苹果");

    5.操作单选框

    WebElement radioOption = driver.findElement(By.xpath("//input[@value='orange']"));
              if(!radioOption.isSelected()){
                  radioOption.click();
              }
              List<WebElement> fruits = driver.findElements(By.name("fruit"));
              for(WebElement fruit : fruits){
                  if(fruit.getAttribute("value").equals("watermelon")){
                      if(!fruit.isSelected()){
                          fruit.click();
                          Assert.assertTrue(fruit.isSelected());
                          break;
                      }
                  }
              }

    6.复选框同上

    7.执行javascript脚本

    JavascriptExecutor js = (JavascriptExecutor) driver;
    //取消上传input隐藏
    js.executeScript("document.getElementById("file").style="display: block;"");
    JavascriptExecutor js = (JavascriptExecutor) driver;
    //返回搜索按钮上的文字
    String text = (String)js.executeScript("var button = document.getElementById("stb");return button.value");

    8.等待操作

    设定查找页面元素的最大等待时间,调用findElement方法的时候没有能立即找到某个元素

    ,则程序会每隔一段时间后不断的尝试判断页面的DOM中是否出现被查找的元素,如果超过

    设定的等待时长依旧没有找到,则抛出NoSuchElementException

    隐形等待

    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

    @BeforeClass
     public static void init() {
         System.out.println("init...");
         System.setProperty("webdriver.chrome.driver","C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe");
         // 创建一个 ChromeDriver 的接口,用于连接 Chrome,
         //必须要有chromedriver.exe文件,selenium默认不能启动chrome
         // 创建一个 Chrome 的浏览器实例
         driver = new ChromeDriver();
         //最大化浏览器
         driver.manage().window().maximize();
         //设置全局的隐形等待
         driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
     }


    隐性等待的默认时长是0,一旦设置,这个隐试等待会在webdriver对象实例的整个生命周期起作用

    显示等待

    显示等待比隐式等待更节约测试脚本执行的时间,推荐使用显示等待判断页面元素是否存在

    使用ExpectedConditions类中自带的方法,可以进行显示等待的判断

    //页面元素是否在页面上可用和可被点击
    ExpectedConditions.elementToBeClickable(By locator);
    //页面元素是否处于被选中状态
    ExpectedConditions.elementToBeSelected(By locator);
    //页面元素在页面是否存在
    ExpectedConditions.presenceOfElementLocated(By locator);
    //是否包含特定的文本
    ExpectedConditions.textToBePresentInElement(locator, text)
    //页面元素值
    ExpectedConditions.textToBePresentInElementValue(locator, text);
    //标题
    ExpectedConditions.titleContains(title);
    // 等待元素可见且可被单击
     wait.until(ExpectedConditions.elementToBeClickable(By.id(id)));

    自定义的显示等待

    public static void sendKeysByXPath(WebDriver driver, String path, String key) {
            WebDriverWait wait = new WebDriverWait(driver, 10); // 最多等10秒
            WebElement element = wait.until(new ExpectedCondition<WebElement>() {
                @Override
                public WebElement apply(WebDriver d) {
                    return d.findElement(By.xpath(path));
                }
            });
            highLightElement(driver,element);
            element.clear();
            element.sendKeys(key);
        }

    注意一点:如果是该元素本来就在DOM中存在,但是此时是隐藏的,你是可以获得该元素的,但是缺无法操作

    做法就是线程等待或者等待元素可用,推荐第二种,线程等待的时间不定,不稳定

    public static void sendKeysByXPath(WebDriver driver, String path, String key) {
            WebDriverWait wait = new WebDriverWait(driver, 10); // 最多等10秒
            // 等待元素可见且可被单击
            wait.until(ExpectedConditions.elementToBeClickable(By.xpath(path)));
            WebElement element = wait.until(new ExpectedCondition<WebElement>() {
                @Override
                public WebElement apply(WebDriver d) {
                    return d.findElement(By.xpath(path));
                }
            });
            highLightElement(driver,element);
            element.clear();
            element.sendKeys(key);
        }

    9.操作javascript的Alert弹窗

    driver.findElement(By.xpath(path)).click();
    //切换到alert弹出框
    Alert alert = driver.switchTo().alert();
    AssertionUtil.assertEquals("这是个alert弹出框", alert.getText());
    //使用alert的accept方法,单击alert的确定按钮,关闭alert
    alert.accept();
    //如果alert未弹出,会抛出NoAlertPresentException异常
    //切换回去原来的窗体
    driver.switchTo().defaultContent();

    10.操作javascript的confirm弹窗

    confirm与alert类似,不同的是

    alert.accept();是确定

    alert.dismiss();是取消

    11.操作frame

    有时候我们在定位一个页面元素的时候发现一直定位不了,反复检查自己写的定位器没有任何问题,
    代码也没 有任何问题。这时你就要看一下这个页面元素是否在一个iframe中,这可能就是找不到的
    原因之一。如果你在 一个default content中查找一个在iframe中的元素,那肯定是找不到的。反之
    你在一个iframe中查找另一个 iframe元素或default content中的元素,那必然也定位不到

    //进入id="frame"或者name="frame"的frame中,
    driver.switchTo().frame("frame"); 

    12.操作模态框

    driver.switchTo().activeElement();

    13.父子框

    String newHandle = "";//获取当前窗口  父窗口
    String currentHandle = driver.getWindowHandle();//点击页面元素打开新窗口
    Set<String> handles = driver.getWindowHandles();//获取浏览器所有窗口
    Iterator<String> itWin = handles.iterator();while(itWin.hasNext()){
        String key = itWin.next();
        if(currentHandle.equals(key)){
            continue;
        }
        //得到新窗口
        newHandle = key;
    }//切换窗口
    WebDriver newDriver = driver.switchTo().window(newHandle);
    //业务操作
    //set中移除新窗口
    handles.remove(newHandle);
    //切回主窗口
    driver.switchTo().window(currentHandle);

    14.js单击

    有时候在点击页面的时候会存在该元素被其他元素覆盖的情况,比如,元素正被一个模态框挡住,此时模态框还没有消失,做法是等待

    模态框消失,再点击一次.或者使用js来触发点击

    /**
         * js执行点击事件 for:Element is not clickable at point. Other element would
         * receive the click
         * 
         * @param driver
         * @param selector
         */
        public static void clickElementByJS(WebDriver driver, By selector) {
            WebDriverWait wait = new WebDriverWait(driver, 10); // 最多等10秒
            WebElement element = wait.until(new ExpectedCondition<WebElement>() {
                @Override
                public WebElement apply(WebDriver d) {
                    return d.findElement(selector);
                }
            });
            ((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
        }
  • 相关阅读:
    hdu4578 (多标记线段树)
    hdu4757 (可持久化字典树+LCA)
    CF940F Machine Learning (带修改莫队)
    csps模拟测试7576一句话题解
    csps模拟测试74梦境,玩具,飘雪圣域题解
    csps模拟测试7273简单的操作小P的2048小P的单调数列小P的生成树
    csps模拟测试707172部分题解myc
    莫队算法学习
    csps模拟69chess,array,70木板,打扫卫生题解
    csps模拟68d,e,f题解
  • 原文地址:https://www.cnblogs.com/itliucheng/p/5578788.html
Copyright © 2011-2022 走看看