Selenium(2)-----常用对象

思维导图

Selenium框架的目的是模拟人与浏览器的交互,其中的中介就是Selenium的对象.通过Selenium特有的对象,调用专门的方法来实现模拟浏览器的动作

一.WebDriver

WebDriver是Selenium框架中最基础的对象,它最主要的功能就是定位元素,还可以模拟鼠标点击,浏览器的前进,后退等操作

1.1元素定位

在上一篇文章中已经说过如何创建WebDriver对象,这篇文章中就不在连篇累牍了.

WebDriver可以通过网页上元素的各种标签进行定位,比如id,name,class,xpath等等.

@Test
    public void firstCase(){
        //进入百度首页
        webDriver.get("https://www.baidu.com/");

        //使用各种方式进行定位百度的搜索框
        WebElement searchText;//网页元素的抽象对象
        //使用id定位
        searchText = webDriver.findElement(By.id("kw"));
        //使用xpath定位
        searchText = webDriver.findElement(By.xpath("//*[@id=\"kw\"]"));
        //使用name定位
        searchText = webDriver.findElement(By.name("wd"));
        //使用class定位
        searchText = webDriver.findElement(By.className("s_ipt"));
    }

1.2等待

我们在使用Selenium框架的时候,经常性的会让代码停止等待一下,这是因为代码执行的太快,而浏览器打开页面,转到页面等操作执行的比较慢,导致页面还没加载完时,就使用WebDriver进行定位或者其他操作,这就会爆出异常.反之,如果让代码等待浏览器加载完成,变可以顺利的执行自动化测试.

    @Test
    public void testWait() throws Exception{
        //强制性等待
        Thread.sleep(1000);

        //隐式等待,设定最大的等待时间,对所有的元素定位起作用,即所有的元素定位都会进行隐式的等待.
        // 如果元素在等待时间内加载完成,则执行下一步,如果超过等待时间元素还未加载完成,会强制代码继续执行
        webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.MICROSECONDS);

        //显示等待,可以根据具体条件进行等待,非常灵活
        //5是最长的等待时间,超过最长等待时间会爆出异常
        WebDriverWait webDriverWait = new WebDriverWait(webDriver,5);

        //标题是不是“百度一下,你就知道”
        new WebDriverWait(webDriver,5).until(ExpectedConditions.titleIs("百度一下,你就知道"));
        //标题是不是包含“百度一下”
        new WebDriverWait(webDriver,5).until(ExpectedConditions.titleContains("百度一下"));
        //判断该元素是否被加载在DOM中,并不代表该元素一定可见        
        new WebDriverWait(webDriver,5).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='kw']")));
        //判断元素(定位后)是否可见
        new WebDriverWait(webDriver,5).until(ExpectedConditions.visibilityOf(webDriver.findElement(By.xpath("//*[@id='kw']"))));
        //判断元素是否可见(非隐藏,并且元素的宽和高都不等于0)
        new WebDriverWait(webDriver,5).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='kw']")));
        //只要存在一个就是true
        ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//*[@id='kw']"));
        //元素中的text是否包含语气的字符串
        ExpectedConditions.textToBePresentInElementLocated(By.xpath("//*[@id='kw']"), "百度一下");
        //元素的value属性中是否包含预期的字符串
        ExpectedConditions.textToBePresentInElementValue(By.xpath("//*[@id='kw']"), "***");
        //判断该表单是否可以切过去,可以就切过去并返回true,否则放回false
        ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("**"));
        //判断某个元素是否不存在于DOM或不可见
        ExpectedConditions.invisibilityOfElementLocated(By.xpath("//*[@id='kw']"));
        //判断元素是否可以点击
        ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='kw']"));
        //等到一个元素从DOM中移除
        ExpectedConditions.stalenessOf(webDriver.findElement(By.xpath("//*[@id='kw']")));
        //判断某个元素是否被选中,一般用在下拉列表
        ExpectedConditions.elementToBeSelected(By.xpath("//*[@id='kw']"));
        //判断某个元素的选中状态是否符合预期
        ExpectedConditions.elementSelectionStateToBe(By.xpath("//*[@id='kw']"), true);
        //判断某个元素(已定位)的选中状态是否符合预期
        ExpectedConditions.elementSelectionStateToBe(webDriver.findElement(By.xpath("//*[@id='kw']")), false);
        //判断页面中是否存在alert
        new WebDriverWait(webDriver,5).until(ExpectedConditions.alertIsPresent());
        //--------------------自定义判断条件-----------------------------
        WebDriverWait wait = new WebDriverWait(webDriver, 3);
        wait.until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver driver) {
                return !driver.findElement(By.xpath("//*[@id='kw']")).getAttribute("class").contains("x-form-invalid-field");
            }
        });
        
    }

 1.3.浏览器操作

浏览器操作指浏览器本身常用的操作,比如前进,后退,刷新,跳转页面等等

    @Test
    public void testBrowser() throws Exception{
        //访问网页
        webDriver.get("https://www.baidu.com/");

        //定位搜索元素并输入搜索字符串
        WebElement webElement = webDriver.findElement(By.id("kw"));
        webElement.sendKeys("学习");//输入字符
        webElement.click();//模拟点击操作
        Thread.sleep(500);

        //模拟后退
        webDriver.navigate().back();
        Thread.sleep(500);
        //模拟刷新
        webDriver.navigate().refresh();
        Thread.sleep(500);
        //模拟前进
        webDriver.navigate().forward();
        Thread.sleep(500);

        //另一种页面访问方式
        webDriver.navigate().to("https://www.baidu.com/");
        Thread.sleep(500);
        //得到标题和URL
        System.out.println(webDriver.getTitle());
        System.out.println(webDriver.getCurrentUrl());
    }

2.WebElement

WebElement是网页元素的抽象对象,通过WebElement可以模拟点击,输入等操作,下方的代码并没有例举所有的方法.

    @Test
    public void testWebElement(){
        WebElement webElement = webDriver.findElement(By.id("kw"));
        //点击操作
        webElement.click();
        //输入操作
        webElement.sendKeys("输入字符串");
        //清除操作
        webElement.clear();
        //是否可见
        webElement.isDisplayed();
        //通过元素本身寻找元素,速度更快,但是必须包含要搜索的子元素
        webElement.findElement(By.id("***"));
    }

3.Select

Select对象是下拉菜单元素的抽象对象,可用通过Select对象选择或取消下拉菜单的值.

    @Test
    public void testSelect(){
        Select select = new Select(webDriver.findElement(By.id("**")));
        //根据下拉菜单的文本选择
        select.selectByVisibleText("下拉菜单的文本");
        //根据下拉菜单的值选择
        select.selectByValue("下拉菜单的值");
        //也可以通过同样的方式取消选择
        select.deselectByValue("下拉菜单的值");
    }

4.Actions

Actions对象的主要功能就是模拟鼠标和键盘一些常用的动作,比如移动,拖动,输入键盘值的操作.下方代码只是举了两个简单的例子,更多的操作需要读者自己去探索,其实我自己掌握的也很差哈.总之,如果需要模拟鼠标和键盘的操作,使用Actions类就可以达到我们的目的.

    @Test
    public void testActions(){
        //通过webDriver创建Actions,使用Actions对象确认操作后需要使用他的perform()方法执行
        Actions actions = new Actions(webDriver);
        //模拟键盘操作-输入下方向键
        actions.sendKeys(Keys.ARROW_DOWN).perform();
        //模拟鼠标操作-拖动元素-将一个元素拖动到另一个元素
        actions.dragAndDrop(webDriver.findElement(By.id("**")),webDriver.findElement(By.id("**"))).perform();
    }

猜你喜欢

转载自blog.csdn.net/zh328271057/article/details/82355017